Search in sources :

Example 26 with Controller

use of com.tvd12.ezyhttp.server.core.annotation.Controller in project ezyhttp by youngmonkeys.

the class RequestHandlerImplementerTest method implementOneFailed.

@Test
public void implementOneFailed() throws Exception {
    // given
    ControllerProxy controller = new ControllerProxy(new Controller());
    RequestHandlerMethod handlerMethod = new RequestHandlerMethod("/", new EzyMethod(Controller.class.getDeclaredMethod("doGet")));
    RequestHandlerImplementer sut = new RequestHandlerImplementer(controller, handlerMethod);
    // when
    Throwable e = Asserts.assertThrows(sut::implement);
    // then
    Asserts.assertEquals(0, handlerMethod.getParameterTypes().length);
    Asserts.assertThat(e).isEqualsType(IllegalStateException.class);
}
Also used : RequestHandlerMethod(com.tvd12.ezyhttp.server.core.reflect.RequestHandlerMethod) RequestHandlerImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlerImplementer) ControllerProxy(com.tvd12.ezyhttp.server.core.reflect.ControllerProxy) EzyMethod(com.tvd12.ezyfox.reflect.EzyMethod) Test(org.testng.annotations.Test)

Example 27 with Controller

use of com.tvd12.ezyhttp.server.core.annotation.Controller in project ezyhttp by youngmonkeys.

the class RequestHandlersImplementerTest method implementOneFailed.

@Test
public void implementOneFailed() {
    // given
    RequestHandlersImplementer sut = new RequestHandlersImplementer();
    Controller controller = new Controller();
    RequestHandlerManager manager = new RequestHandlerManager();
    // when
    Throwable e = Asserts.assertThrows(() -> manager.addHandlers(sut.implement(Collections.singletonList(controller))));
    // then
    Asserts.assertThat(e).isEqualsType(DuplicateURIMappingHandlerException.class);
}
Also used : RequestHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) Test(org.testng.annotations.Test)

Example 28 with Controller

use of com.tvd12.ezyhttp.server.core.annotation.Controller in project ezyhttp by youngmonkeys.

the class RequestHandlersImplementerTest method implementOneWithURIDecorator.

@Test
public void implementOneWithURIDecorator() {
    // given
    RequestHandlersImplementer sut = new RequestHandlersImplementer();
    Controller controller = new Controller();
    RequestHandlerManager manager = new RequestHandlerManager();
    manager.setAllowOverrideURI(true);
    RequestURIDecorator requestURIDecorator = mock(RequestURIDecorator.class);
    when(requestURIDecorator.decorate(any(), any())).thenReturn("hello-world");
    sut.setRequestURIDecorator(requestURIDecorator);
    // when
    manager.addHandlers(sut.implement(Collections.singletonList(controller)));
    // then
    RequestURI uri = new RequestURI(HttpMethod.GET, "/hello-world", false);
    Asserts.assertThat(manager.getHandlerListByURI().get(uri).size()).isEqualsTo(2);
}
Also used : RequestHandlersImplementer(com.tvd12.ezyhttp.server.core.asm.RequestHandlersImplementer) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) RequestURIDecorator(com.tvd12.ezyhttp.server.core.handler.RequestURIDecorator) Test(org.testng.annotations.Test)

Example 29 with Controller

use of com.tvd12.ezyhttp.server.core.annotation.Controller in project ezyhttp by youngmonkeys.

the class GraphQLConfigurationTest method test.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void test() throws NoSuchFieldException, IllegalAccessException {
    // given
    EzyBeanContextBuilder builder = new EzySimpleBeanContext.Builder();
    Set<String> packagesToScan = RandomUtil.randomSet(8, String.class);
    packagesToScan.add("com.tvd12.ezyhttp.server.graphql.test.config");
    for (String p : packagesToScan) {
        builder.scan(p);
    }
    EzyBeanContext context = builder.build();
    GraphQLConfiguration sut = new GraphQLConfiguration();
    EzySingletonFactory singletonFactory = context.getSingletonFactory();
    sut.setSingletonFactory(singletonFactory);
    sut.setObjectMapper(new ObjectMapper());
    sut.setGraphQLEnable(true);
    // when
    sut.config();
    GraphQLController controller = (GraphQLController) singletonFactory.getSingleton(GraphQLController.class);
    Field dataFetcherManagerField = GraphQLController.class.getDeclaredField("dataFetcherManager");
    dataFetcherManagerField.setAccessible(true);
    GraphQLDataFetcherManager dataFetcherManager = (GraphQLDataFetcherManager) dataFetcherManagerField.get(controller);
    Field dataFetchersField = GraphQLDataFetcherManager.class.getDeclaredField("dataFetchers");
    dataFetchersField.setAccessible(true);
    Map<String, GraphQLDataFetcher> dataFetchers = (Map<String, GraphQLDataFetcher>) dataFetchersField.get(dataFetcherManager);
    // then
    Asserts.assertNotNull(controller);
    Asserts.assertTrue(dataFetchers.containsKey("A"));
}
Also used : GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) EzyBeanContextBuilder(com.tvd12.ezyfox.bean.EzyBeanContextBuilder) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) EzySingletonFactory(com.tvd12.ezyfox.bean.EzySingletonFactory) Field(java.lang.reflect.Field) EzyBeanContextBuilder(com.tvd12.ezyfox.bean.EzyBeanContextBuilder) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) Map(java.util.Map) GraphQLConfiguration(com.tvd12.ezyhttp.server.graphql.GraphQLConfiguration) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Example 30 with Controller

use of com.tvd12.ezyhttp.server.core.annotation.Controller in project ezyhttp by youngmonkeys.

the class GraphQLControllerTest method testQueryWithNullVariableType.

@Test
public void testQueryWithNullVariableType() throws Exception {
    // given
    GraphQLSchemaParser schemaParser = new GraphQLSchemaParser();
    GraphQLDataFetcher fooDataFetcher = new GraphQLFooDataFetcher();
    GraphQLDataFetcherManager dataFetcherManager = GraphQLDataFetcherManager.builder().addDataFetcher(fooDataFetcher).build();
    ObjectMapper objectMapper = new ObjectMapper();
    GraphQLController controller = GraphQLController.builder().schemaParser(schemaParser).dataFetcherManager(dataFetcherManager).objectMapper(objectMapper).build();
    String fooQuery = "{foo}";
    // when
    Object fooResult1 = controller.doGet(fooQuery, "{\"value\": \"Bar\"}");
    Object fooResult2 = controller.doGet(fooQuery, null);
    // then
    Asserts.assertEquals(fooResult1.toString(), "{foo=Foo {value=Bar}}");
    Asserts.assertEquals(fooResult2.toString(), "{foo=Foo null}");
}
Also used : GraphQLSchemaParser(com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser) GraphQLDataFetcher(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher) GraphQLDataFetcherManager(com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager) GraphQLController(com.tvd12.ezyhttp.server.graphql.controller.GraphQLController) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)38 EzyArray (com.tvd12.ezyfox.entity.EzyArray)18 EzySimpleServer (com.tvd12.ezyfoxserver.EzySimpleServer)14 EzyZoneContext (com.tvd12.ezyfoxserver.context.EzyZoneContext)14 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)14 EzyResponseApi (com.tvd12.ezyfoxserver.api.EzyResponseApi)12 EzyServerContext (com.tvd12.ezyfoxserver.context.EzyServerContext)12 EzyLoginController (com.tvd12.ezyfoxserver.controller.EzyLoginController)12 EzySimpleLoginRequest (com.tvd12.ezyfoxserver.request.EzySimpleLoginRequest)12 EzySimpleServerContext (com.tvd12.ezyfoxserver.context.EzySimpleServerContext)11 BaseTest (com.tvd12.test.base.BaseTest)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 GraphQLController (com.tvd12.ezyhttp.server.graphql.controller.GraphQLController)6 GraphQLDataFetcherManager (com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcherManager)5 EzySimpleUserManagementSetting (com.tvd12.ezyfoxserver.setting.EzySimpleUserManagementSetting)4 EzyZoneSetting (com.tvd12.ezyfoxserver.setting.EzyZoneSetting)4 EzySessionManager (com.tvd12.ezyfoxserver.wrapper.EzySessionManager)4 GraphQLDataFetcher (com.tvd12.ezyhttp.server.graphql.GraphQLDataFetcher)4 GraphQLSchemaParser (com.tvd12.ezyhttp.server.graphql.GraphQLSchemaParser)4 EzySimplePlugin (com.tvd12.ezyfoxserver.EzySimplePlugin)3