PolicyEntityBuilder implements EntityBuilder { @VisibleForTesting static void maybeAddPolicy(Bundle bundle, Policy policy, List<Policy> orderedPolicies, Set<Policy> seenPolicies) { if (orderedPolicies.contains(policy) || bundle.getServices().get(FilenameUtils.removeExtension(policy.getPath())) != null) { return; } if (seenPolicies.contains(policy)) { throw new EntityBuilderException("Detected Policy Include cycle containing policies: " + seenPolicies.stream().map(Policy::getPath).collect(Collectors.joining(","))); } seenPolicies.add(policy); policy.getDependencies().forEach(dependency -> maybeAddPolicy(bundle, dependency, orderedPolicies, seenPolicies)); seenPolicies.remove(policy); orderedPolicies.add(policy); } @Inject  PolicyEntityBuilder(DocumentTools documentTools, IdGenerator idGenerator, PolicyXMLBuilder policyXMLBuilder); List<Entity> buildEntities(Map<String, ?> policyMap, AnnotatedBundle annotatedBundle, Bundle bundle, BundleType bundleType, Document document); List<Entity> build(Bundle bundle, BundleType bundleType, Document document); @NotNull @Override Integer getOrder(); static void resolvePossibleMissingEncapsulatedAssertionDependencies(Bundle bundle, Element encapsulatedAssertionElement); static final String POLICY; static final String ZERO_GUID; }
@Test void maybeAddPolicy() { Policy policy1 = new Policy(); policy1.setName("policy1"); policy1.setPath("policy1"); Policy policy2 = new Policy(); policy2.setName("policy2"); policy2.setPath("policy2"); policy1.getDependencies().add(policy2); ArrayList<Policy> orderedPolicies = new ArrayList<>(); HashSet<Policy> seenPolicies = new HashSet<>(); PolicyEntityBuilder.maybeAddPolicy(bundle, policy1, orderedPolicies, seenPolicies); assertEquals(2, orderedPolicies.size()); assertEquals(policy1, orderedPolicies.get(1)); assertEquals(policy2, orderedPolicies.get(0)); PolicyEntityBuilder.maybeAddPolicy(bundle, policy2, orderedPolicies, seenPolicies); assertEquals(2, orderedPolicies.size()); assertEquals(policy1, orderedPolicies.get(1)); assertEquals(policy2, orderedPolicies.get(0)); policy2.getDependencies().add(policy1); assertThrows(EntityBuilderException.class, () -> PolicyEntityBuilder.maybeAddPolicy(bundle, policy2, new ArrayList<>(), new HashSet<>())); }