use of core.framework.web.Controller in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method batch.
@Test
void batch() throws Exception {
TestWebService.TestRequest requestBean = new TestWebService.TestRequest();
requestBean.stringField = "value";
when(request.bean(Types.list(TestWebService.TestRequest.class))).thenReturn(Lists.newArrayList(requestBean));
Controller controller = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("batch", List.class)).build();
Response response = controller.execute(request);
assertEquals(HTTPStatus.OK, response.status());
}
use of core.framework.web.Controller in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method get.
@Test
void get() throws Exception {
when(request.pathParam("id", Integer.class)).thenReturn(1);
WebServiceControllerBuilder<TestWebService> builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("get", Integer.class));
Controller controller = builder.build();
String sourceCode = builder.builder.sourceCode();
assertEquals(ClasspathResources.text("webservice-test/test-webservice-controller-get.java"), sourceCode);
Response response = controller.execute(request);
assertEquals(HTTPStatus.OK, response.status());
@SuppressWarnings("unchecked") Optional<TestWebService.TestResponse> bean = (Optional<TestWebService.TestResponse>) ((BeanBody) ((ResponseImpl) response).body).bean;
assertEquals(2, (int) bean.get().intField);
}
use of core.framework.web.Controller in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method patch.
@Test
void patch() throws Exception {
TestWebService.TestRequest requestBean = new TestWebService.TestRequest();
requestBean.stringField = "value";
when(request.pathParam("id", Integer.class)).thenReturn(1);
when(request.bean(TestWebService.TestRequest.class)).thenReturn(requestBean);
WebServiceControllerBuilder<TestWebService> builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("patch", Integer.class, TestWebService.TestRequest.class));
Controller controller = builder.build();
String sourceCode = builder.builder.sourceCode();
assertEquals(ClasspathResources.text("webservice-test/test-webservice-controller-patch.java"), sourceCode);
Response response = controller.execute(request);
assertEquals(HTTPStatus.OK, response.status());
}
use of core.framework.web.Controller in project core-ng-project by neowu.
the class WebServiceControllerBuilderTest method create.
@Test
void create() throws Exception {
TestWebService.TestRequest requestBean = new TestWebService.TestRequest();
requestBean.stringField = "value";
when(request.pathParam("id", Integer.class)).thenReturn(1);
when(request.bean(TestWebService.TestRequest.class)).thenReturn(requestBean);
WebServiceControllerBuilder<TestWebService> builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("create", Integer.class, TestWebService.TestRequest.class));
Controller controller = builder.build();
String sourceCode = builder.builder.sourceCode();
assertEquals(ClasspathResources.text("webservice-test/test-webservice-controller-create.java"), sourceCode);
Response response = controller.execute(request);
assertEquals(HTTPStatus.CREATED, response.status());
}
use of core.framework.web.Controller in project core-ng-project by neowu.
the class APIConfig method service.
public <T> void service(Class<T> serviceInterface, T service) {
logger.info("create api service, interface={}", serviceInterface.getCanonicalName());
new WebServiceInterfaceValidator(serviceInterface, context.httpServer.handler.requestBeanMapper, context.httpServer.handler.responseBeanTypeValidator).validate();
new WebServiceImplValidator<>(serviceInterface, service).validate();
for (Method method : serviceInterface.getMethods()) {
HTTPMethod httpMethod = HTTPMethods.httpMethod(method);
String path = method.getDeclaredAnnotation(Path.class).value();
Controller controller = new WebServiceControllerBuilder<>(serviceInterface, service, method).build();
try {
Class<?>[] parameterTypes = method.getParameterTypes();
Class<?> serviceClass = service.getClass();
Method targetMethod = serviceClass.getMethod(method.getName(), parameterTypes);
String controllerInfo = serviceClass.getCanonicalName() + "." + targetMethod.getName();
String action = "api:" + ASCII.toLowerCase(httpMethod.name()) + ":" + path;
context.httpServer.handler.route.add(httpMethod, path, new ControllerHolder(controller, targetMethod, controllerInfo, action, false));
} catch (NoSuchMethodException e) {
throw new Error("failed to find impl method", e);
}
}
apiController().serviceInterfaces.add(serviceInterface);
}
Aggregations