Search in sources :

Example 1 with ParamParsers

use of ninja.params.ParamParsers 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 ParamParsers

use of ninja.params.ParamParsers in project ninja by ninjaframework.

the class FileItemProviderImpl2 method init.

public void init(final Class<? extends FileItemProvider> fileItemProviderClass) throws IOException, ServletException {
    //default setup for httpServlet request.
    //According to servlet spec the following will be returned:
    when(httpServletRequest.getContextPath()).thenReturn("");
    when(httpServletRequest.getRequestURI()).thenReturn("/");
    when(httpServletRequest.getContentType()).thenReturn("multipart/form-data");
    when(httpServletRequest.getMethod()).thenReturn("POST");
    NinjaPropertiesImpl properties = new NinjaPropertiesImpl(NinjaMode.test);
    this.ninjaProperties = properties;
    final FileItemIterator fileItemIterator = makeFileItemsIterator();
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(NinjaProperties.class).toInstance(ninjaProperties);
            bind(FileItemProvider.class).to(fileItemProviderClass);
        }
    });
    context = new NinjaServletContext(bodyParserEngineManager, flashCookie, ninjaProperties, resultHandler, sessionCookie, validation, injector, new ParamParsers(new HashSet<ParamParser>())) {

        public FileItemIterator getFileItemIterator() {
            return fileItemIterator;
        }

        ;
    };
}
Also used : NinjaPropertiesImpl(ninja.utils.NinjaPropertiesImpl) ParamParser(ninja.params.ParamParser) ParamParsers(ninja.params.ParamParsers) Injector(com.google.inject.Injector) FileItemIterator(org.apache.commons.fileupload.FileItemIterator) AbstractModule(com.google.inject.AbstractModule)

Example 3 with ParamParsers

use of ninja.params.ParamParsers 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)

Aggregations

ParamParsers (ninja.params.ParamParsers)3 Injector (com.google.inject.Injector)2 NinjaBaseDirectoryResolver (ninja.utils.NinjaBaseDirectoryResolver)2 Before (org.junit.Before)2 AbstractModule (com.google.inject.AbstractModule)1 Provider (com.google.inject.Provider)1 ControllerMethod0 (ninja.ControllerMethods.ControllerMethod0)1 ParamParser (ninja.params.ParamParser)1 MethodReference (ninja.utils.MethodReference)1 NinjaProperties (ninja.utils.NinjaProperties)1 NinjaPropertiesImpl (ninja.utils.NinjaPropertiesImpl)1 FileItemIterator (org.apache.commons.fileupload.FileItemIterator)1