StateRest { @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves State for the User making the request based on filter criteria") public Response getStates(@Context ContainerRequestContext context, @QueryParam("application") String applicationId, @QueryParam("subjects") List<String> subjectIds) { String username = RestUtils.getActiveUsername(context); Set<Resource> subjects = subjectIds.stream() .map(factory::createIRI) .collect(Collectors.toSet()); try { Map<Resource, Model> results = stateManager.getStates(username, applicationId, subjects); JSONArray array = new JSONArray(); results.keySet().forEach(resource -> { JSONObject state = new JSONObject(); state.put("id", resource.stringValue()); state.put("model", convertModel(results.get(resource))); array.add(state); }); return Response.ok(array).build(); } catch (MobiException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR); } }  @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves State for the User making the request based on filter criteria") Response getStates(@Context ContainerRequestContext context,
                       @QueryParam("application") String applicationId,
                       @QueryParam("subjects") List<String> subjectIds); @POST @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Creates a new State for the User making the request") Response createState(@Context ContainerRequestContext context,
                         @QueryParam("application") String applicationId,
                         String stateJson); @GET @Path("{stateId}") @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves State by ID as long it belongs to the User making the request") Response getState(@Context ContainerRequestContext context, @PathParam("stateId") String stateId); @PUT @Path("{stateId}") @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Updates State as long as it belongs to the User making the request") Response updateState(@Context ContainerRequestContext context,
                         @PathParam("stateId") String stateId,
                         String newStateJson); @DELETE @Path("{stateId}") @RolesAllowed("user") @ApiOperation("Deletes State as long as it belongs to the User making the request") Response deleteState(@Context ContainerRequestContext context, @PathParam("stateId") String stateId);  }
@Test public void getStatesWithoutFiltersTest() { Response response = target().path("states").request().get(); assertEquals(response.getStatus(), 200); verify(stateManager).getStates(anyString(), anyString(), anySetOf(Resource.class)); try { String str = response.readEntity(String.class); JSONArray arr = JSONArray.fromObject(str); assertEquals(results.size(), arr.size()); for (int i = 0; i < arr.size(); i++) { JSONObject object = arr.optJSONObject(i); assertNotNull(object); assertTrue(results.keySet().contains(vf.createIRI(object.get("id").toString()))); } } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }
@Test public void getStatesWithFiltersTest() { Set<Resource> subjects = IntStream.range(0, 2) .mapToObj(i -> "http: .map(vf::createIRI) .collect(Collectors.toSet()); WebTarget webTarget = target().path("states").queryParam("application", "app"); for (Resource subject : subjects) { webTarget = webTarget.queryParam("subjects", subject.stringValue()); } Response response = webTarget.request().get(); assertEquals(response.getStatus(), 200); verify(stateManager).getStates(anyString(), eq("app"), eq(subjects)); try { String str = response.readEntity(String.class); JSONArray arr = JSONArray.fromObject(str); assertEquals(results.size(), arr.size()); for (int i = 0; i < arr.size(); i++) { JSONObject object = arr.optJSONObject(i); assertNotNull(object); assertTrue(results.keySet().contains(vf.createIRI(object.get("id").toString()))); } } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }