Search in sources :

Example 1 with NinjaBaseDirectoryResolver

use of ninja.utils.NinjaBaseDirectoryResolver 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)

Example 2 with NinjaBaseDirectoryResolver

use of ninja.utils.NinjaBaseDirectoryResolver in project ninja by ninjaframework.

the class ReverseRouterTest method before.

@Before
@SuppressWarnings("Convert2Lambda")
public void before() {
    this.ninjaProperties = mock(NinjaProperties.class);
    this.injector = mock(Injector.class);
    this.testControllerProvider = mock(Provider.class);
    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);
    reverseRouter = new ReverseRouter(ninjaProperties, router);
    router.GET().route("/home").with(TestController::home);
    router.GET().route("/user/{email}/{id: .*}").with(TestController::user);
    router.GET().route("/u{userId: .*}/entries/{entryId: .*}").with(TestController::entry);
    // second route to index should not break reverse routing matching the first
    router.GET().route("/home/index").with(TestController::index);
    router.compileRoutes();
}
Also used : NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) ParamParsers(ninja.params.ParamParsers) Injector(com.google.inject.Injector) NinjaProperties(ninja.utils.NinjaProperties) Provider(com.google.inject.Provider) Before(org.junit.Before)

Example 3 with NinjaBaseDirectoryResolver

use of ninja.utils.NinjaBaseDirectoryResolver in project ninja by ninjaframework.

the class RouteBuilderImplTest method testGlobalFilters.

@Test
public void testGlobalFilters() throws Exception {
    // given
    // different setup that uses com.example packages and thus reads the Filters there
    Mockito.when(ninjaProperties.get(NinjaConstant.APPLICATION_MODULES_BASE_PACKAGE)).thenReturn("com.example");
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    this.routeBuilder = new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver);
    DummyFilter dummyFilter = new DummyFilter();
    Result expectedResult = Mockito.mock(Result.class);
    Context context = Mockito.mock(Context.class);
    Provider filterProvider = Mockito.mock(Provider.class);
    com.example.conf.Filters filters = new com.example.conf.Filters(DummyFilter.class);
    Mockito.when(injector.getInstance(com.example.conf.Filters.class)).thenReturn(filters);
    Mockito.when(injector.getProvider(DummyFilter.class)).thenReturn(filterProvider);
    Mockito.when(filterProvider.get()).thenReturn(dummyFilter);
    routeBuilder.GET().route("/").with(() -> expectedResult);
    Route route = routeBuilder.buildRoute(injector);
    FilterChain filterChain = route.getFilterChain();
    // when
    Result result = filterChain.next(context);
    // then
    Mockito.verify(injector).getInstance(com.example.conf.Filters.class);
    assertThat(dummyFilter.executed, Matchers.equalTo(1));
    assertThat(result, org.hamcrest.Matchers.equalTo(expectedResult));
}
Also used : Provider(com.google.inject.Provider) ApplicationFilters(ninja.application.ApplicationFilters) NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) Test(org.junit.Test)

Example 4 with NinjaBaseDirectoryResolver

use of ninja.utils.NinjaBaseDirectoryResolver in project ninja by ninjaframework.

the class RouteBuilderImplTest method testThatGlobalFiltersInRouteReplaceGlobalFiltersInConfFilters.

@Test
public void testThatGlobalFiltersInRouteReplaceGlobalFiltersInConfFilters() throws Exception {
    // DummyFilter is defined in conf.Filters, but .globalFilters(DummyFilter2.class) should
    // override that.
    // given
    // different setup that uses com.example packages and thus reads the Filters there
    Mockito.when(ninjaProperties.get(NinjaConstant.APPLICATION_MODULES_BASE_PACKAGE)).thenReturn("com.example");
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    this.routeBuilder = new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver);
    DummyFilter2 dummyFilter2 = new DummyFilter2();
    Result expectedResult = Mockito.mock(Result.class);
    Context context = Mockito.mock(Context.class);
    Provider filterProvider = Mockito.mock(Provider.class);
    Mockito.when(injector.getProvider(DummyFilter2.class)).thenReturn(filterProvider);
    Mockito.when(filterProvider.get()).thenReturn(dummyFilter2);
    routeBuilder.GET().route("/").globalFilters(DummyFilter2.class).with(() -> expectedResult);
    Route route = routeBuilder.buildRoute(injector);
    FilterChain filterChain = route.getFilterChain();
    // when
    Result result = filterChain.next(context);
    // then
    Mockito.verify(injector, Mockito.never()).getProvider(DummyFilter.class);
    Mockito.verify(injector).getProvider(DummyFilter2.class);
    assertThat(dummyFilter2.executed, Matchers.equalTo(1));
    assertThat(result, org.hamcrest.Matchers.equalTo(expectedResult));
}
Also used : NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) Provider(com.google.inject.Provider) Test(org.junit.Test)

Example 5 with NinjaBaseDirectoryResolver

use of ninja.utils.NinjaBaseDirectoryResolver in project ninja by ninjaframework.

the class RouteBuilderImplTest method before.

@Before
public void before() {
    this.ninjaBaseDirectoryResolver = new NinjaBaseDirectoryResolver(ninjaProperties);
    this.routeBuilder = new RouteBuilderImpl(ninjaProperties, ninjaBaseDirectoryResolver);
}
Also used : NinjaBaseDirectoryResolver(ninja.utils.NinjaBaseDirectoryResolver) Before(org.junit.Before)

Aggregations

NinjaBaseDirectoryResolver (ninja.utils.NinjaBaseDirectoryResolver)5 Provider (com.google.inject.Provider)3 Before (org.junit.Before)3 ParamParsers (ninja.params.ParamParsers)2 Test (org.junit.Test)2 Injector (com.google.inject.Injector)1 ControllerMethod0 (ninja.ControllerMethods.ControllerMethod0)1 ApplicationFilters (ninja.application.ApplicationFilters)1 MethodReference (ninja.utils.MethodReference)1 NinjaProperties (ninja.utils.NinjaProperties)1