WebServiceControllerBuilder { public Controller build() { builder.addField("private final {} delegate;", type(serviceInterface)); builder.constructor(new Class<?>[]{serviceInterface}, "this.delegate = $1;"); boolean deprecated = method.isAnnotationPresent(Deprecated.class); builder.addMethod(buildMethod(deprecated)); return builder.build(service); } WebServiceControllerBuilder(Class<T> serviceInterface, T service, Method method); Controller build();  }
@Test void get() throws Exception { when(request.pathParam("id")).thenReturn("1"); var builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("get", Integer.class)); Controller controller = builder.build(); String sourceCode = builder.builder.sourceCode(); assertThat(sourceCode).isEqualTo(ClasspathResources.text("webservice-test/test-webservice-controller-get.java")); Response response = controller.execute(request); assertThat(response.status()).isEqualTo(HTTPStatus.OK); @SuppressWarnings("unchecked") var bean = (Optional<TestWebService.TestResponse>) ((BeanBody) ((ResponseImpl) response).body).bean; assertThat(bean.orElseThrow().intField).isEqualTo(2); }
@Test void create() throws Exception { var requestBean = new TestWebService.TestRequest(); requestBean.stringField = "value"; when(request.pathParam("id")).thenReturn("1"); when(request.bean(TestWebService.TestRequest.class)).thenReturn(requestBean); var builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("create", Integer.class, TestWebService.TestRequest.class)); Controller controller = builder.build(); assertThat(builder.builder.sourceCode()).isEqualTo(ClasspathResources.text("webservice-test/test-webservice-controller-create.java")); Response response = controller.execute(request); assertThat(response.status()).isEqualTo(HTTPStatus.CREATED); }
@Test void patch() throws Exception { var requestBean = new TestWebService.TestRequest(); requestBean.stringField = "value"; when(request.pathParam("id")).thenReturn("1"); when(request.bean(TestWebService.TestRequest.class)).thenReturn(requestBean); var builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("patch", Integer.class, TestWebService.TestRequest.class)); Controller controller = builder.build(); String sourceCode = builder.builder.sourceCode(); assertThat(sourceCode).isEqualTo(ClasspathResources.text("webservice-test/test-webservice-controller-patch.java")); Response response = controller.execute(request); assertThat(response.status()).isEqualTo(HTTPStatus.NO_CONTENT); }
@Test void getEnum() throws Exception { when(request.pathParam("id")).thenReturn("1"); when(request.pathParam("enum")).thenReturn("A1"); var builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("getEnum", Long.class, TestWebService.TestEnum.class)); Controller controller = builder.build(); String sourceCode = builder.builder.sourceCode(); assertThat(sourceCode).isEqualTo(ClasspathResources.text("webservice-test/test-webservice-controller-getEnum.java")); Response response = controller.execute(request); assertThat(response.status()).isEqualTo(HTTPStatus.OK); var bean = (TestWebService.TestResponse) ((BeanBody) ((ResponseImpl) response).body).bean; assertThat(bean).isNotNull(); }
@Test void deprecated() throws Exception { when(request.pathParam("id")).thenReturn("1"); var builder = new WebServiceControllerBuilder<>(TestWebService.class, serviceImpl, TestWebService.class.getDeclaredMethod("deprecated", Integer.class)); Controller controller = builder.build(); String sourceCode = builder.builder.sourceCode(); assertThat(sourceCode).isEqualTo(ClasspathResources.text("webservice-test/test-webservice-controller-deprecated.java")); Response response = controller.execute(request); assertThat(response.status()).isEqualTo(HTTPStatus.OK); @SuppressWarnings("unchecked") var bean = (Optional<TestWebService.TestResponse>) ((BeanBody) ((ResponseImpl) response).body).bean; assertThat(bean).isEmpty(); }