AsyncResource { @GET @Path("/{action}/{id}") public Response getResponse(@PathParam("action") final String action, @NotNull @PathParam("id") final String id) { return Response.ok(getData(new AsyncDataToken(action, id))).build(); }  @GET @Path("/{action}/{id}") Response getResponse(@PathParam("action") final String action, @NotNull @PathParam("id") final String id); @POST Response getResponsePost(final AsyncDataToken dataToken);  }
@Test public void testGetResponse() throws Exception { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device", "version")); Map<String, Object> expectedResponse = new LinkedHashMap<String, Object>(); final Map<String, Object> nexusResponse = new LinkedHashMap<String, Object>(){{ put("1", 2); put("2", 2); put("3", 1); }}; final Map<String, Object> galaxyResponse = new LinkedHashMap<String, Object>(){{ put("2", 1); put("3", 1); }}; expectedResponse.put("android", new LinkedHashMap<String, Object>() {{ put("nexus", nexusResponse); put("galaxy", galaxyResponse); }}); final Map<String, Object> nexusResponse2 = new LinkedHashMap<String, Object>(){{ put("2", 1);}}; final Map<String, Object> iPadResponse = new LinkedHashMap<String, Object>(){{ put("2", 2); }}; final Map<String, Object> iPhoneResponse = new LinkedHashMap<String, Object>(){{ put("1", 1); }}; expectedResponse.put("ios", new LinkedHashMap<String, Object>() {{ put("nexus", nexusResponse2); put("ipad", iPadResponse); put("iphone", iPhoneResponse); }}); AsyncDataToken dataToken = queryExecutor.executeAsync(groupRequest); Thread.sleep(1000); WebResource webResource = client().resource("/v1/async/" + dataToken.getAction() + "/" + dataToken.getKey()); GroupResponse groupResponse = webResource.type(MediaType.APPLICATION_JSON_TYPE).get(GroupResponse.class); assertEquals(expectedResponse, groupResponse.getResult()); }
@Test public void testGetResponseInvalidAction() throws Exception { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device", "version")); AsyncDataToken dataToken = queryExecutor.executeAsync(groupRequest); Thread.sleep(1000); try { client().resource(String.format("/v1/async/distinct/%s", dataToken.getKey())).type(MediaType.APPLICATION_JSON_TYPE).get(GroupResponse.class); } catch (UniformInterfaceException ex) { assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.getResponse().getStatus()); } }
@Test public void testGetResponseInvalidKey() throws Exception { GroupRequest groupRequest = new GroupRequest(); groupRequest.setTable(TestUtils.TEST_TABLE_NAME); groupRequest.setNesting(Arrays.asList("os", "device", "version")); AsyncDataToken dataToken = queryExecutor.executeAsync(groupRequest); Thread.sleep(1000); try { client().resource(String.format("/v1/async/%s/dummy", dataToken.getAction())).type(MediaType.APPLICATION_JSON_TYPE).get(GroupResponse.class); } catch (UniformInterfaceException ex) { assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.getResponse().getStatus()); } }
@Test public void testGetResponsePostInvalidAction() throws Exception { AsyncDataToken dataToken = new AsyncDataToken(null, UUID.randomUUID().toString()); try { client().resource("/v1/async") .type(MediaType.APPLICATION_JSON_TYPE) .post(GroupResponse.class, dataToken); } catch (UniformInterfaceException ex) { assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), ex.getResponse().getStatus()); } }