Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating all Play Framework routes in Test

Is there any way to Iterate all described services in routes file? URL and HTTP methods are needed.

I need this feature for running some integration test.

I am using Play for Java.

like image 562
Mohsen Kashi Avatar asked May 05 '15 06:05

Mohsen Kashi


1 Answers

Not easily. I managed to hack my way through it a while ago(no scala know-how). I'll post that code maybe it can be of use.

public static List<String[]> parseRoutes() {
    scala.Option<play.core.Router.Routes> option = Play.application().getWrappedApplication().routes();
    if (option.isDefined()) {
        play.core.Router.Routes routes = option.get();
        scala.collection.Seq<scala.Tuple3<String, String, String>> doc = routes.documentation();
        scala.collection.Iterator<scala.Tuple3<String, String, String>> it = doc.iterator();

        List<String[]> listOfRoutes = new ArrayList<String[]>();

        while(it.hasNext()) {
            scala.Tuple3<String, String, String> tuple = it.next();
            //tuple._1() is the method and tuple._2() the url... tuple._3() is the controller name
            String[] route = {tuple._1(), tuple._2()};
            listOfRoutes.add(route);
            Logger.debug("route -> " + Arrays.toString(route));  
        }
        return listOfRoutes;
    }
    return null;
}

Don't worry about the .iterator() showing a The method iterator() is ambiguous for the type Seq<Tuple3<String,String,String>>. It compiles just fine in play.

like image 187
sebster Avatar answered Oct 04 '22 09:10

sebster