WebService { @POST @Path("/request") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public WebResponse process(WebRequest req) { WebResponse webResp; Request request = new Request(req.getText()); Response response = app.process(request); if (response.isSuccess()) { if (response.hasPayload()) { webResp = new WebResponseWithPayload(response); } else { webResp = new WebResponse(response); } } else { webResp = new WebResponseWithError(response); } return webResp; }  @POST @Path("/request") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) WebResponse process(WebRequest req); @GET @Path("/agents") @Produces(MediaType.APPLICATION_JSON) Description[] getAgents(); @GET @Path("/ping") @Produces(MediaType.APPLICATION_JSON) String ping();  }
@Test public void testRequest() { Response resp = new Response("this is a test"); when(app.process(any(Request.class))).thenReturn(resp); WebRequest wr = new WebRequest("why?"); Entity<WebRequest> body = Entity.entity(wr, MediaType.APPLICATION_JSON_TYPE); javax.ws.rs.core.Response response = target("/request").request(MediaType.APPLICATION_JSON_TYPE).post(body); assertEquals(200, response.getStatus()); String expected = "{\"type\":\"text\",\"text\":\"this is a test\"}"; assertEquals(expected, response.readEntity(String.class)); }
@Test public void testRequestWithError() { Response resp = new Response(new ResponseStatus(ResponseStatus.Code.AGENT_DID_NOT_KNOW_ANSWER, "Huh?")); when(app.process(any(Request.class))).thenReturn(resp); WebRequest wr = new WebRequest("why?"); Entity<WebRequest> body = Entity.entity(wr, MediaType.APPLICATION_JSON_TYPE); javax.ws.rs.core.Response response = target("/request").request(MediaType.APPLICATION_JSON_TYPE).post(body); assertEquals(200, response.getStatus()); String expected = "{\"type\":\"error\",\"text\":null,\"error\":{\"code\":201,\"message\":\"Huh?\"}}"; assertEquals(expected, response.readEntity(String.class)); }
@Test public void testRequestWithPayload() { Response resp = new Response(Response.Type.IMAGE_EMBED, "Here is your image", "base64_image"); when(app.process(any(Request.class))).thenReturn(resp); WebRequest wr = new WebRequest("show me a flamingo"); Entity<WebRequest> body = Entity.entity(wr, MediaType.APPLICATION_JSON_TYPE); javax.ws.rs.core.Response response = target("/request").request(MediaType.APPLICATION_JSON_TYPE).post(body); assertEquals(200, response.getStatus()); String expected = "{\"type\":\"image_embed\",\"text\":\"Here is your image\",\"payload\":\"base64_image\"}"; assertEquals(expected, response.readEntity(String.class)); }