use of core.framework.web.Response 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.Response 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.Response in project core-ng-project by neowu.
the class InvocationImplTest method process.
@Test
void process() throws Exception {
Stack stack = new Stack();
TestController controller = new TestController(stack, 3);
Interceptors interceptors = new Interceptors();
interceptors.add(new TestInterceptor(stack, 0));
interceptors.add(new TestInterceptor(stack, 1));
interceptors.add(new TestInterceptor(stack, 2));
InvocationImpl invocation = new InvocationImpl(new ControllerHolder(controller, null, null, null, false), interceptors, mock(Request.class), new WebContextImpl());
Response response = invocation.proceed();
assertEquals(HTTPStatus.NO_CONTENT, response.status());
assertTrue(controller.executed);
for (Interceptor interceptor : interceptors.interceptors) {
assertTrue(((TestInterceptor) interceptor).executed);
}
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class InvocationImplTest method skipInterceptor.
@Test
void skipInterceptor() throws Exception {
Stack stack = new Stack();
TestController controller = new TestController(stack, 0);
Interceptors interceptors = new Interceptors();
interceptors.add(new TestInterceptor(stack, 0));
InvocationImpl invocation = new InvocationImpl(new ControllerHolder(controller, null, null, null, true), interceptors, mock(Request.class), new WebContextImpl());
Response response = invocation.proceed();
assertEquals(HTTPStatus.NO_CONTENT, response.status());
assertTrue(controller.executed);
for (Interceptor interceptor : interceptors.interceptors) {
assertFalse(((TestInterceptor) interceptor).executed);
}
}
use of core.framework.web.Response in project core-ng-project by neowu.
the class WebServiceControllerBuilder method buildMethod.
private String buildMethod() {
CodeBuilder builder = new CodeBuilder();
builder.append("public {} execute({} request) throws Exception {\n", type(Response.class), type(Request.class));
List<String> params = Lists.newArrayList();
Annotation[][] annotations = method.getParameterAnnotations();
Type[] paramTypes = method.getGenericParameterTypes();
for (int i = 0; i < annotations.length; i++) {
Type paramType = paramTypes[i];
String paramTypeLiteral = type(paramType);
PathParam pathParam = Params.annotation(annotations[i], PathParam.class);
if (pathParam != null) {
params.add(pathParam.value());
builder.indent(1).append("{} {} = ({}) request.pathParam(\"{}\", {});\n", paramTypeLiteral, pathParam.value(), paramTypeLiteral, pathParam.value(), variable(paramType));
} else {
params.add("bean");
builder.indent(1).append("{} bean = ({}) request.bean({});\n", paramTypeLiteral, paramTypeLiteral, variable(paramType));
}
}
if (void.class == method.getReturnType()) {
builder.indent(1).append("delegate.{}(", method.getName());
} else {
builder.indent(1).append("{} response = delegate.{}(", type(method.getReturnType()), method.getName());
}
builder.appendCommaSeparatedValues(params).append(");\n");
if (void.class.equals(method.getReturnType())) {
builder.indent(1).append("return {}.empty().status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
} else {
builder.indent(1).append("return {}.bean(response).status({});\n", Response.class.getCanonicalName(), variable(responseStatus));
}
builder.append("}");
return builder.build();
}
Aggregations