StateRest { @PUT @Path("{stateId}") @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Updates State as long as it belongs to the User making the request") public Response updateState(@Context ContainerRequestContext context, @PathParam("stateId") String stateId, String newStateJson) { String username = RestUtils.getActiveUsername(context); try { if (!stateManager.stateExistsForUser(factory.createIRI(stateId), username)) { throw ErrorUtils.sendError("Not allowed", Response.Status.UNAUTHORIZED); } Model newState = transformer.mobiModel(Rio.parse(IOUtils.toInputStream(newStateJson), "", RDFFormat.JSONLD)); if (newState.isEmpty()) { throw ErrorUtils.sendError("Empty state model", Response.Status.BAD_REQUEST); } stateManager.updateState(factory.createIRI(stateId), newState); } catch (IOException ex) { throw ErrorUtils.sendError(ex, "Invalid JSON-LD", Response.Status.BAD_REQUEST); } catch (IllegalArgumentException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.NOT_FOUND); } catch (MobiException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR); } return Response.ok().build(); }  @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 updateStateTest() { Model state = mf.createModel(); state.add(vf.createIRI("http: Response response = target().path("states/" + encode(stateId.stringValue())) .request().put(Entity.json(modelToJsonld(state))); assertEquals(response.getStatus(), 200); verify(stateManager).stateExistsForUser(eq(stateId), anyString()); verify(stateManager).updateState(eq(stateId), eq(state)); }
@Test public void updateStateThatDoesNotExistTest() { Model state = mf.createModel(); state.add(vf.createIRI("http: doThrow(new IllegalArgumentException()).when(stateManager).updateState(any(Resource.class), any(Model.class)); Response response = target().path("states/" + encode(stateId.stringValue())) .request().put(Entity.json(modelToJsonld(state))); assertEquals(response.getStatus(), 404); }
@Test public void updateStateExceptionThrownTest() { Model state = mf.createModel(); state.add(vf.createIRI("http: doThrow(new MobiException()).when(stateManager).updateState(any(Resource.class), any(Model.class)); Response response = target().path("states/" + encode(stateId.stringValue())) .request().put(Entity.json(modelToJsonld(state))); assertEquals(response.getStatus(), 500); }