RecordPermissionsRest { @PUT @Path("{recordId}") @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Updates an existing record security policy using the provided JSON body.") @ResourceId(type = ValueType.PATH, value = "recordId") public Response updateRecordPolicy(@PathParam("recordId") String recordId, String policyJson) { try (RepositoryConnection conn = repo.getConnection()) { Optional<String> recordPolicyIdOpt = getRelatedResourceId(recordId, conn); String recordPolicyId = recordPolicyIdOpt.orElseThrow(() -> ErrorUtils.sendError("Policy for record " + recordId + " does not exist in repository", Response.Status.BAD_REQUEST)); IRI recordPolicyIRI = vf.createIRI(recordPolicyId); Optional<XACMLPolicy> recordPolicy = policyManager.getPolicy(recordPolicyIRI); if (!recordPolicy.isPresent()) { throw ErrorUtils.sendError("Record policy to update could not be found", Response.Status.BAD_REQUEST); } XACMLPolicy updatedRecordPolicy = recordJsonToPolicy(policyJson, recordPolicy.get().getJaxbPolicy()); if (!updatedRecordPolicy.getId().equals(recordPolicyIRI)) { throw ErrorUtils.sendError("Policy Id does not match provided record policy", Response.Status.BAD_REQUEST); } Optional<String> policyPolicyIdOpt = getRelatedResourceId(recordPolicyId, conn); String policyPolicyId = policyPolicyIdOpt.orElseThrow(() -> ErrorUtils.sendError("Policy for record " + "policy " + recordId + " does not exist in repository", Response.Status.BAD_REQUEST)); if (StringUtils.isEmpty(policyPolicyId)) { throw ErrorUtils.sendError("Policy for policy " + recordPolicyId + "does not exist in repository", Response.Status.BAD_REQUEST); } IRI policyPolicyIRI = vf.createIRI(policyPolicyId); Optional<XACMLPolicy> policyPolicy = policyManager.getPolicy(policyPolicyIRI); if (!policyPolicy.isPresent()) { throw ErrorUtils.sendError("Policy policy to update could not be found", Response.Status.BAD_REQUEST); } XACMLPolicy updatedPolicyPolicy = updatePolicyPolicy(policyJson, policyPolicy.get().getJaxbPolicy()); if (!updatedPolicyPolicy.getId().equals(vf.createIRI(policyPolicyId))) { throw ErrorUtils.sendError("Policy policy Id does not match provided policy", Response.Status.BAD_REQUEST); } policyManager.updatePolicy(updatedRecordPolicy); policyManager.updatePolicy(updatedPolicyPolicy); return Response.ok().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 updated", Response.Status.INTERNAL_SERVER_ERROR); } }  @Reference(target = "(id=system)") void setRepo(Repository repo); @GET @Produces(MediaType.APPLICATION_JSON) @Path("{recordId}") @ApiOperation("Retrieves a specific record security policy by its ID.") @ActionId(Update.TYPE) @ResourceId(type = ValueType.PATH, value = "recordId") Response retrieveRecordPolicy(@PathParam("recordId") String recordId); @PUT @Path("{recordId}") @Consumes(MediaType.APPLICATION_JSON) @ApiOperation("Updates an existing record security policy using the provided JSON body.") @ResourceId(type = ValueType.PATH, value = "recordId") Response updateRecordPolicy(@PathParam("recordId") String recordId, String policyJson);  }
@Test public void updateRecordPolicyTest() { when(policyManager.createPolicy(any(PolicyType.class))).thenAnswer(invocation -> { PolicyType pt = invocation.getArgumentAt(0, PolicyType.class); if (pt.getPolicyId().equals(recordPolicyIRI.stringValue())) { return recordPolicy; } else { return policyPolicy; } }); Response response = target().path("record-permissions/" + encode(recordIRI.stringValue())).request().put(Entity.json(recordJson)); assertEquals(response.getStatus(), 200); verify(policyManager).getPolicy(recordPolicyIRI); verify(policyManager).getPolicy(policyPolicyIRI); verify(policyManager, times(2)).createPolicy(any(PolicyType.class)); verify(policyManager, times(2)).updatePolicy(any(XACMLPolicy.class)); }