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);
}
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);
}
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);
}
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"));
}
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}");
}
Aggregations