PolicyRest { @GET @Produces(MediaType.APPLICATION_JSON) @RolesAllowed("user") @ApiOperation("Retrieves security policies matching the provided filters.") public Response getPolicies(@QueryParam("relatedSubject") String relatedSubject, @QueryParam("relatedResource") String relatedResource, @QueryParam("relatedAction") String relatedAction) { PolicyQueryParams.Builder params = new PolicyQueryParams.Builder(); if (StringUtils.isNotEmpty(relatedResource)) { params.addResourceIRI(vf.createIRI(relatedResource)); } if (StringUtils.isNotEmpty(relatedSubject)) { params.addSubjectIRI(vf.createIRI(relatedSubject)); } if (StringUtils.isNotEmpty(relatedAction)) { params.addActionIRI(vf.createIRI(relatedAction)); } try { return Response.ok(policyManager.getPolicies(params.build()).stream() .map(this::policyToJson) .collect(JSONArray::new, JSONArray::add, JSONArray::add)).build(); } catch (Exception ex) { throw ErrorUtils.sendError(ex, "Error retrieving policies", 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 getPoliciesTest() { Response response = target().path("policies").request().get(); assertEquals(response.getStatus(), 200); verify(policyManager).getPolicies(any(PolicyQueryParams.class)); try { JSONArray result = JSONArray.fromObject(response.readEntity(String.class)); assertEquals(result.size(), 1); JSONObject policyObj = result.optJSONObject(0); assertNotNull(policyObj); String id = policyObj.optString("PolicyId"); assertNotNull(id); assertEquals(id, policyId.stringValue()); } catch (Exception e) { fail("Expected no exception, but got: " + e.getMessage()); } }
@Test public void getPoliciesErrorTest() { doThrow(new IllegalStateException()).when(policyManager).getPolicies(any(PolicyQueryParams.class)); Response response = target().path("policies").request().get(); assertEquals(response.getStatus(), 500); verify(policyManager).getPolicies(any(PolicyQueryParams.class)); }