PolicyUpdater { static void writePolicies(PolicyUpdaterConfiguration configuration, String domain, DomainSignedPolicyData domainSignedPolicyData) throws IOException { if (configuration == null) { throw new IllegalArgumentException("null configuration"); } String policyTmpDir = configuration.getPolicyFileTmpDir(); String policyDir = configuration.getPolicyFileDir(); if (policyTmpDir == null || policyDir == null || domain == null || domainSignedPolicyData == null) { throw new IllegalArgumentException("null parameters are not valid arguments"); } String pathToTempFile = policyTmpDir + File.separator + domain + TEMP_FILE_EXTENSION; String pathToPolicyFile = policyDir + File.separator + domain + POLICY_FILE_EXTENSION; verifyTmpDirSetup(configuration); LOG.info("Writing temp policy file: " + pathToTempFile); File file = new File(pathToTempFile); file.createNewFile(); Files.write(file.toPath(), JSON.bytes(domainSignedPolicyData)); Path sourceFile = Paths.get(pathToTempFile); Path destinationFile = Paths.get(pathToPolicyFile); try { LOG.info("Moving temp file : " + sourceFile + " to destination: " + destinationFile); Files.copy(sourceFile, destinationFile, StandardCopyOption.REPLACE_EXISTING); Files.deleteIfExists(sourceFile); } catch (IOException exc) { LOG.error("PolicyUpdater: Moving temp file failure. source: " + sourceFile + " : destination: " + destinationFile + " : exc: " + exc); } }  static void main(String[] args); static void postDomainMetrics(ZTSClient zts); static final String ZPE_METRIC_FILE_PATH; static final String ZPE_PROP_METRIC_FILE_PATH; }
@Test public void TestWritePolicies() throws Exception { Path path = Paths.get("./src/test/resources/sys.auth.pol"); DomainSignedPolicyData domainPolicySignedDataInput = JSON.fromBytes(Files.readAllBytes(path), DomainSignedPolicyData.class); PolicyUpdater.writePolicies(pupConfig, "sys.auth", domainPolicySignedDataInput); path = Paths.get(pupConfig.getRootDir() + TEST_POLICY_DIR + "/sys.auth.pol"); JSON.fromBytes(Files.readAllBytes(path), DomainSignedPolicyData.class); Path sysauthPath = Paths.get(pupConfig.getRootDir() + TEST_POLICY_TEMP_DIR + "/tmp/sys.auth"); Files.deleteIfExists(sysauthPath); sysauthPath = Paths.get(pupConfig.getRootDir() + TEST_POLICY_TEMP_DIR + "/tmp"); Files.deleteIfExists(sysauthPath); java.io.File polFile = path.toFile(); long flen = polFile.length(); long fmod = polFile.lastModified(); Thread.sleep(1000); PolicyUpdaterConfiguration config = new PolicyUpdaterConfiguration(); config.init(pathToAthenzConfigFile, pathToZPUConfigFile); config.setPolicyFileTmpDir(pupConfig.getRootDir() + TEST_POLICY_TEMP_DIR + "/tmp"); config.setPolicyFileDir(pupConfig.getRootDir() + TEST_POLICY_DIR); PolicyUpdater.writePolicies(config, "sys.auth", domainPolicySignedDataInput); long flen2 = polFile.length(); long fmod2 = polFile.lastModified(); Assert.assertTrue(flen == flen2); Assert.assertTrue(fmod < fmod2); boolean exceptionCaught = false; try { PolicyUpdater.writePolicies(null, "sys.auth", domainPolicySignedDataInput); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); exceptionCaught = false; try { config.setPolicyFileTmpDir(null); PolicyUpdater.writePolicies(config, "sys.auth", domainPolicySignedDataInput); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); exceptionCaught = false; try { config.setPolicyFileTmpDir(TEST_POLICY_TEMP_DIR); config.setPolicyFileDir(null); PolicyUpdater.writePolicies(config, "sys.auth", domainPolicySignedDataInput); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); try { config.setPolicyFileDir(TEST_POLICY_DIR); PolicyUpdater.writePolicies(config, null, domainPolicySignedDataInput); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); try { PolicyUpdater.writePolicies(config, "sys.auth", null); } catch (IllegalArgumentException ex) { exceptionCaught = true; } Assert.assertTrue(exceptionCaught); Files.delete(path); }