Search in sources :

Example 1 with ControllerMethod0

use of ninja.ControllerMethods.ControllerMethod0 in project ninja by ninjaframework.

the class RouterImplTest method before.

@Before
@SuppressWarnings("Convert2Lambda")
public void before() {
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    when(testControllerProvider.get()).thenReturn(new TestController());
    when(injector.getProvider(TestController.class)).thenReturn(testControllerProvider);
    when(injector.getInstance(ParamParsers.class)).thenReturn(new ParamParsers(Collections.emptySet()));
    Provider<RouteBuilderImpl> routeBuilderImplProvider = mock(Provider.class);
    when(routeBuilderImplProvider.get()).thenAnswer((invocation) -> new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver));
    router = new RouterImpl(injector, ninjaProperties, routeBuilderImplProvider);
    // add route:
    router.GET().route("/testroute").with(TestController.class, "index");
    router.GET().route("/user/{email}/{id: .*}").with(TestController.class, "user");
    router.GET().route("/u{userId: .*}/entries/{entryId: .*}").with(TestController.class, "entry");
    // second route to index should not break reverse routing matching the first
    router.GET().route("/testroute/another_url_by_index").with(TestController.class, "index");
    router.GET().route("/ref").with(new MethodReference(TestController.class, "ref"));
    // functional interface / lambda routing
    TestController testController1 = new TestController("Hi!");
    router.GET().route("/any_instance_method_ref").with(TestController::home);
    router.GET().route("/any_instance_method_ref_exception").with(TestController::exception);
    router.GET().route("/any_instance_method_ref2").with(ControllerMethods.of(TestController::home));
    router.GET().route("/specific_instance_method_ref").with(testController1::message);
    router.GET().route("/specific_instance_method_ref_annotations").with(testController1::status);
    router.GET().route("/anonymous_method_ref").with(() -> Results.status(202));
    Result staticResult = Results.status(208);
    router.GET().route("/anonymous_method_ref_captured").with(() -> staticResult);
    router.GET().route("/anonymous_method_ref_context").with((Context context) -> Results.status(context.getParameterAsInteger("status")));
    router.GET().route("/anonymous_class").with(new ControllerMethod0() {

        @Override
        public Result apply() {
            return Results.status(203);
        }
    });
    router.GET().route("/anonymous_class_annotations").with(new ControllerMethod1<Integer>() {

        @Override
        public Result apply(@Param("status") Integer status) {
            return Results.status(status);
        }
    });
    router.compileRoutes();
}
Also used : ParamParsers(ninja.params.ParamParsers) NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) ControllerMethod0(ninja.ControllerMethods.ControllerMethod0) MethodReference(ninja.utils.MethodReference) Before(org.junit.Before)

Aggregations

ControllerMethod0 (ninja.ControllerMethods.ControllerMethod0)1 ParamParsers (ninja.params.ParamParsers)1 MethodReference (ninja.utils.MethodReference)1 NinjaBaseDirectoryResolver (ninja.utils.NinjaBaseDirectoryResolver)1 Before (org.junit.Before)1