PolicyBackedServiceLinker implements EntityLinker<PolicyBackedService> { @Override public void link(PolicyBackedService pbs, Bundle bundle, Bundle targetBundle) { for (PolicyBackedServiceOperation operation : pbs.getOperations()) { String policyId = operation.getPolicy(); Policy policy = bundle.getPolicies().values().stream().filter(p -> policyId.equals(p.getId())).findFirst().orElse(null); if (policy == null) { throw new LinkerException("Could not find policy for Policy Backed Service. Policy ID: " + policyId); } operation.setPolicy(getPolicyPath(policy, bundle, pbs)); } }  @Override Class<PolicyBackedService> getEntityClass(); @Override void link(PolicyBackedService pbs, Bundle bundle, Bundle targetBundle);  }
@Test void link() { PolicyBackedServiceLinker policyBackedServiceLinker = new PolicyBackedServiceLinker(); Map<String, String> operations = new HashMap<>(); operations.put("operation1", "1"); operations.put("operation2", "2"); PolicyBackedService myPolicyBackedService = createPolicyBackedService("myEncass", "1", "PolicyBackedService", operations); Bundle bundle = new Bundle(); bundle.addEntity(myPolicyBackedService); Bundle fullBundle = new Bundle(); fullBundle.addEntity(myPolicyBackedService); fullBundle.addEntity(createPolicy("operation1Policy", "1", "1", "1", null, "")); fullBundle.addEntity(createPolicy("operation2Policy", "2", "2", "1", null, "")); fullBundle.addEntity(createFolder("myFolder", "1", null)); FolderTree folderTree = new FolderTree(fullBundle.getEntities(Folder.class).values()); fullBundle.setFolderTree(folderTree); policyBackedServiceLinker.link(bundle, fullBundle); assertEquals("operation1Policy", bundle.getEntities(PolicyBackedService.class).get("1").getOperations().stream().collect(Collectors.toMap(PolicyBackedServiceOperation::getOperationName, PolicyBackedServiceOperation::getPolicy)).get("operation1")); assertEquals("operation2Policy", bundle.getEntities(PolicyBackedService.class).get("1").getOperations().stream().collect(Collectors.toMap(PolicyBackedServiceOperation::getOperationName, PolicyBackedServiceOperation::getPolicy)).get("operation2")); }
@Test void linkMissingPolicy() { PolicyBackedServiceLinker policyBackedServiceLinker = new PolicyBackedServiceLinker(); Map<String, String> operations = new HashMap<>(); operations.put("operation1", "1"); operations.put("operation2", "2"); PolicyBackedService myPolicyBackedService = createPolicyBackedService("myEncass", "1", "PolicyBackedService", operations); Bundle bundle = new Bundle(); bundle.addEntity(myPolicyBackedService); Bundle fullBundle = new Bundle(); fullBundle.addEntity(myPolicyBackedService); fullBundle.addEntity(createPolicy("operation1Policy", "1", "1", "1", null, "")); fullBundle.addEntity(createPolicy("operation3Policy", "3", "3", "1", null, "")); fullBundle.addEntity(createFolder("myFolder", "1", null)); FolderTree folderTree = new FolderTree(fullBundle.getEntities(Folder.class).values()); fullBundle.setFolderTree(folderTree); assertThrows(LinkerException.class, () -> policyBackedServiceLinker.link(bundle, fullBundle)); }
@Test void linkFolderIsMissing() { PolicyBackedServiceLinker policyBackedServiceLinker = new PolicyBackedServiceLinker(); Map<String, String> operations = new HashMap<>(); operations.put("operation1", "1"); operations.put("operation2", "2"); PolicyBackedService myPolicyBackedService = createPolicyBackedService("myEncass", "1", "PolicyBackedService", operations); Bundle bundle = new Bundle(); bundle.addEntity(myPolicyBackedService); Bundle fullBundle = new Bundle(); fullBundle.addEntity(myPolicyBackedService); fullBundle.addEntity(createPolicy("operation1Policy", "1", "1", "1", null, "")); fullBundle.addEntity(createPolicy("operation2Policy", "2", "2", "1", null, "")); fullBundle.addEntity(createFolder("myFolder", "2", null)); FolderTree folderTree = new FolderTree(fullBundle.getEntities(Folder.class).values()); fullBundle.setFolderTree(folderTree); assertThrows(LinkerException.class, () -> policyBackedServiceLinker.link(bundle, fullBundle)); }