PolicyRest { @GET @Produces(MediaType.APPLICATION_JSON) @Path("{policyId}") @RolesAllowed("user") @ApiOperation("Retrieves a specific security policy by its ID.") @ResourceId(type = ValueType.PATH, value = "policyId") public Response retrievePolicy(@PathParam("policyId") String policyId) { try { Optional<XACMLPolicy> policy = policyManager.getPolicy(vf.createIRI(policyId)); if (!policy.isPresent()) { throw ErrorUtils.sendError("Policy could not be found", Response.Status.BAD_REQUEST); } return Response.ok(policyToJson(policy.get())).build(); } catch (IllegalStateException | MobiException ex) { throw ErrorUtils.sendError(ex, "Policy could not be retrieved", Response.Status.INTERNAL_SERVER_ERROR); } }  @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves security policies matching the provided filters.") Response getPolicies(@QueryParam("relatedSubject") String relatedSubject,
                         @QueryParam("relatedResource") String relatedResource,
                         @QueryParam("relatedAction") String relatedAction); @POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("admin") @ApiOperation("Creates a new security policy using the provided JSON body.") Response createPolicy(String policyJson); @GET @Produces(MediaType.APPLICATION_JSON) @Path("{policyId}") @RolesAllowed("user") @ApiOperation("Retrieves a specific security policy by its ID.") @ResourceId(type = ValueType.PATH, value = "policyId") Response retrievePolicy(@PathParam("policyId") String policyId); @PUT @Path("{policyId}") @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("admin") @ApiOperation("Updates an existing security policy using the provided JSON body.") @ResourceId(type = ValueType.PATH, value = "policyId") Response updatePolicy(@PathParam("policyId") String policyId, String policyJson);  }
@Test public void retrievePolicyTest() { Response response = target().path("policies/" + encode(policyId.stringValue())).request().get(); assertEquals(response.getStatus(), 200); verify(policyManager).getPolicy(policyId); try { JSONObject result = JSONObject.fromObject(response.readEntity(String.class)); String id = result.optString("PolicyId"); assertNotNull(id); assertEquals(id, policyId.stringValue()); } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }