PolicyRest { @POST @Produces(MediaType.TEXT_PLAIN) @Consumes(MediaType.APPLICATION_JSON) @RolesAllowed("admin") @ApiOperation("Creates a new security policy using the provided JSON body.") public Response createPolicy(String policyJson) { try { Resource policyId = policyManager.addPolicy(jsonToPolicy(policyJson)); return Response.status(201).entity(policyId.stringValue()).build(); } catch (IllegalArgumentException | PolicySyntaxException ex) { throw ErrorUtils.sendError(ex, ex.getMessage(), Response.Status.BAD_REQUEST); } catch (IllegalStateException | MobiException ex) { throw ErrorUtils.sendError(ex, "Policy could not be created", 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 createPolicyTest() { Response response = target().path("policies").request().post(Entity.json(json)); assertEquals(response.getStatus(), 201); assertEquals(response.readEntity(String.class), policyId.stringValue()); verify(policyManager).createPolicy(any(PolicyType.class)); verify(policyManager).addPolicy(policy); }
@Test public void createPolicyMissingIdTest() { Response response = target().path("policies").request().post(Entity.json("{}")); assertEquals(response.getStatus(), 400); verify(policyManager, times(0)).createPolicy(any(PolicyType.class)); verify(policyManager, times(0)).addPolicy(any(XACMLPolicy.class)); }
@Test public void createPolicyThatAlreadyExistsTest() { doThrow(new IllegalArgumentException()).when(policyManager).addPolicy(policy); Response response = target().path("policies").request().post(Entity.json("{\"PolicyId\": \"urn:test\"}")); assertEquals(response.getStatus(), 400); verify(policyManager).createPolicy(any(PolicyType.class)); verify(policyManager).addPolicy(any(XACMLPolicy.class)); }
@Test public void createPolicyBadXMLTest() { doThrow(new PolicySyntaxException()).when(policyManager).createPolicy(any(PolicyType.class)); Response response = target().path("policies").request().post(Entity.json("{\"PolicyId\": \"urn:test\"}")); assertEquals(response.getStatus(), 400); verify(policyManager).createPolicy(any(PolicyType.class)); verify(policyManager, times(0)).addPolicy(any(XACMLPolicy.class)); }
@Test public void createPolicyErrorTest() { doThrow(new IllegalStateException()).when(policyManager).addPolicy(policy); Response response = target().path("policies").request().post(Entity.json(json)); assertEquals(response.getStatus(), 500); verify(policyManager).createPolicy(any(PolicyType.class)); verify(policyManager).addPolicy(policy); }