@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);
    
    // test handling of missing tmp dir
    //
    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);
    
    // Test error handling for illegal arguments
    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);
}