Search in sources :

Example 6 with Response

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());
}
Also used : Response(core.framework.web.Response) Controller(core.framework.web.Controller) Test(org.junit.jupiter.api.Test)

Example 7 with Response

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());
}
Also used : Response(core.framework.web.Response) Controller(core.framework.web.Controller) Test(org.junit.jupiter.api.Test)

Example 8 with Response

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);
    }
}
Also used : Response(core.framework.web.Response) Request(core.framework.web.Request) Interceptor(core.framework.web.Interceptor) Test(org.junit.jupiter.api.Test)

Example 9 with Response

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);
    }
}
Also used : Response(core.framework.web.Response) Request(core.framework.web.Request) Interceptor(core.framework.web.Interceptor) Test(org.junit.jupiter.api.Test)

Example 10 with Response

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();
}
Also used : Response(core.framework.web.Response) Type(java.lang.reflect.Type) Request(core.framework.web.Request) PathParam(core.framework.api.web.service.PathParam) CodeBuilder(core.framework.impl.asm.CodeBuilder)

Aggregations

Response (core.framework.web.Response)13 Test (org.junit.jupiter.api.Test)6 Controller (core.framework.web.Controller)4 Request (core.framework.web.Request)4 Interceptor (core.framework.web.Interceptor)2 Protected (app.web.interceptor.Protected)1 PathParam (core.framework.api.web.service.PathParam)1 ContentType (core.framework.http.ContentType)1 CodeBuilder (core.framework.impl.asm.CodeBuilder)1 ActionLog (core.framework.impl.log.ActionLog)1 RequestImpl (core.framework.impl.web.request.RequestImpl)1 ResponseImpl (core.framework.impl.web.response.ResponseImpl)1 ErrorResponse (core.framework.impl.web.service.ErrorResponse)1 NotFoundException (core.framework.web.exception.NotFoundException)1 HeaderMap (io.undertow.util.HeaderMap)1 HttpString (io.undertow.util.HttpString)1 Type (java.lang.reflect.Type)1 Path (java.nio.file.Path)1 Optional (java.util.Optional)1