PolicyAndFolderLoader implements EntityLoader { @Override public void load(final Bundle bundle, final File rootDir) { final File policyRootDir = FolderLoaderUtils.getPolicyRootDir(rootDir); if (policyRootDir == null) return; final Map<String, Policy> policies = new HashMap<>(); loadPolicies(policyRootDir, policyRootDir, null, policies, bundle); loadPoliciesMetadata(rootDir, policies, bundle); bundle.putAllPolicies(policies); } @Inject  PolicyAndFolderLoader(PolicyConverterRegistry policyConverterRegistry, FileUtils fileUtils,
                          IdGenerator idGenerator, JsonFileUtils jsonFileUtils); @Override void load(final Bundle bundle, final File rootDir); @Override void load(Bundle bundle, String name, String value); @Override Object loadSingle(String name, File entitiesFile); @Override Map<String, Object> load(File entitiesFile); @Override String getEntityType();  }
@Test @ExtendWith(TemporaryFolderExtension.class) void testLoad(TemporaryFolder temporaryFolder) throws IOException { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, fileUtils, new IdGenerator(), JsonFileUtils.INSTANCE); Mockito.when(fileUtils.getFileAsString(Mockito.any(File.class))).thenReturn("policy-content"); File policyFolder = temporaryFolder.createDirectory("policy"); File a = new File(policyFolder, "a"); Assert.assertTrue(a.mkdir()); File b = new File(a, "b"); Assert.assertTrue(b.mkdir()); File c = new File(b, "c"); Assert.assertTrue(c.mkdir()); File policy = new File(c, "policy.xml"); Files.touch(policy); File configFolder = temporaryFolder.createDirectory("config"); File policyDep = new File(configFolder, "policies.yml"); Map<String, PolicyMetadata> policyMetadataMap = new HashMap<>(); PolicyMetadata policyMetadata = new PolicyMetadata(); policyMetadata.setPath("a/b/c"); policyMetadata.setName("policy"); policyMetadata.setTag("tag"); policyMetadata.setType("include"); Set<Dependency> dependencySet = new HashSet<>(); dependencySet.add(new Dependency("jdbc", EntityTypes.JDBC_CONNECTION)); policyMetadata.setUsedEntities(dependencySet); policyMetadataMap.put(policyMetadata.getFullPath(), policyMetadata); JsonFileUtils.INSTANCE.createFile(policyMetadataMap, policyDep.toPath()); Bundle bundle = new Bundle(); policyAndFolderLoader.load(bundle, temporaryFolder.getRoot()); Assert.assertNotNull(bundle.getFolders().get("")); Assert.assertNotNull(bundle.getFolders().get("a/")); Assert.assertNotNull(bundle.getFolders().get("a/b/")); Folder parentFolder = bundle.getFolders().get("a/b/c/"); Assert.assertNotNull(parentFolder); Policy loadedPolicy = bundle.getPolicies().get("a/b/c/policy"); Assert.assertNotNull(loadedPolicy); Assert.assertEquals(parentFolder, loadedPolicy.getParentFolder()); Map<Dependency, List<Dependency>> dependencyListMap = bundle.getDependencyMap(); Assert.assertNotNull(dependencyListMap); List<Dependency> dependencyList = dependencyListMap.get(new Dependency("policy", EntityTypes.POLICY_TYPE)); Assert.assertNotNull(dependencyList); Assert.assertEquals(dependencyList.size(), 1); }
@Test @ExtendWith(TemporaryFolderExtension.class) void testLoadNoPolicyFolder(TemporaryFolder temporaryFolder) { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, FileUtils.INSTANCE, new IdGenerator(), JsonFileUtils.INSTANCE); Bundle bundle = new Bundle(); policyAndFolderLoader.load(bundle, temporaryFolder.getRoot()); Assert.assertTrue(bundle.getFolders().isEmpty()); }
@Test @ExtendWith(TemporaryFolderExtension.class) void testLoadPolicyFolderIsFile(TemporaryFolder temporaryFolder) throws IOException { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, FileUtils.INSTANCE, new IdGenerator(), JsonFileUtils.INSTANCE); temporaryFolder.createFile("policy"); Bundle bundle = new Bundle(); assertThrows(ConfigLoadException.class, () -> policyAndFolderLoader.load(bundle, temporaryFolder.getRoot())); }
@Test @ExtendWith(TemporaryFolderExtension.class) void testLoadPolicyUnknownType(TemporaryFolder temporaryFolder) throws IOException { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, FileUtils.INSTANCE, new IdGenerator(), JsonFileUtils.INSTANCE); File policyFolder = temporaryFolder.createDirectory("policy"); File policy = new File(policyFolder, "policy.blah"); Files.touch(policy); File configFolder = temporaryFolder.createDirectory("config"); File policyDep = new File(configFolder, "policies.yml"); Map<String, PolicyMetadata> policyMetadataMap = new HashMap<>(); JsonFileUtils.INSTANCE.createFile(policyMetadataMap, policyDep.toPath()); Bundle bundle = new Bundle(); policyAndFolderLoader.load(bundle, temporaryFolder.getRoot()); Policy loadedPolicy = bundle.getPolicies().get("policy"); Assert.assertNull(loadedPolicy); }
@Test @ExtendWith(TemporaryFolderExtension.class) void testLoadPolicySameNameDifferentType(TemporaryFolder temporaryFolder) throws IOException { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, FileUtils.INSTANCE, new IdGenerator(), JsonFileUtils.INSTANCE); File policyFolder = temporaryFolder.createDirectory("policy"); File policy = new File(policyFolder, "policy.xml"); Files.touch(policy); policy = new File(policyFolder, "policy.assertion.js"); Files.touch(policy); Bundle bundle = new Bundle(); assertThrows(ConfigLoadException.class, () -> policyAndFolderLoader.load(bundle, temporaryFolder.getRoot())); }
@Test void loadFromEnvironment() { PolicyAndFolderLoader policyAndFolderLoader = new PolicyAndFolderLoader(policyConverterRegistry, FileUtils.INSTANCE, new IdGenerator(), JsonFileUtils.INSTANCE); assertThrows(ConfigLoadException.class, () -> policyAndFolderLoader.load(new Bundle(), "policy", "policy")); }