Changes for Checkstyle 8.32
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / XacmlPolicyUtilsTest.java
index fe0f675..fbfde1a 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,10 +24,10 @@ package org.onap.policy.pdp.xacml.application.common;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
 
 import com.att.research.xacml.api.XACML3;
 import com.att.research.xacml.util.XACMLPolicyWriter;
-
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -37,8 +37,9 @@ import java.io.OutputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.List;
 import java.util.Properties;
-
+import java.util.stream.Collectors;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
@@ -47,7 +48,6 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
-
 import org.junit.BeforeClass;
 import org.junit.ClassRule;
 import org.junit.Test;
@@ -109,11 +109,21 @@ public class XacmlPolicyUtilsTest {
                 properties.load(is);
             }
             //
+            // Change "/" to file separator in file names
+            //
+            if (!"/".equals(File.separator)) {
+                List<String> fileProps = properties.keySet().stream().map(Object::toString)
+                                .filter(key -> key.endsWith(".file")).collect(Collectors.toList());
+                for (String fileProp : fileProps) {
+                    properties.setProperty(fileProp, properties.getProperty(fileProp).replace("/", File.separator));
+                }
+            }
+            //
             // Save root policy
             //
-            File rootFile = policyFolder.newFile("root.xml");
-            LOGGER.info("Creating Root Policy {}", rootFile.getAbsolutePath());
-            rootPath = XACMLPolicyWriter.writePolicyFile(rootFile.toPath(), rootPolicy);
+            Path rootFile = XacmlPolicyUtils.constructUniquePolicyFilename(rootPolicy, policyFolder.getRoot().toPath());
+            LOGGER.info("Creating Root Policy {}", rootFile.toAbsolutePath());
+            rootPath = XacmlPolicyUtils.writePolicyFile(rootFile, rootPolicy);
             //
             // Create policies - Policies 1 and 2 will become references in the
             // root policy. While Policies 3 and 4 will become references in the
@@ -190,9 +200,21 @@ public class XacmlPolicyUtilsTest {
         //
         // Save it to disk
         //
-        File file = policyFolder.newFile(policy.getPolicyId() + ".xml");
-        LOGGER.info("Creating Policy {}", file.getAbsolutePath());
-        return XACMLPolicyWriter.writePolicyFile(file.toPath(), policy);
+        Path policyFile = XacmlPolicyUtils.constructUniquePolicyFilename(policy, policyFolder.getRoot().toPath());
+        LOGGER.info("Creating Policy {}", policyFile.toAbsolutePath());
+        return XacmlPolicyUtils.writePolicyFile(policyFile, policy);
+    }
+
+    @Test
+    public void testUncommonConditions() throws IOException {
+        File fileTemp = policyFolder.newFile();
+        assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() ->
+            XacmlPolicyUtils.writePolicyFile(fileTemp.toPath(), new String("not a policy"))
+        );
+        assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() ->
+            XacmlPolicyUtils.constructUniquePolicyFilename(new String("not a policy"),
+                    policyFolder.getRoot().toPath())
+        );
     }
 
     @Test
@@ -211,7 +233,7 @@ public class XacmlPolicyUtilsTest {
             //
             try (OutputStream os = new ByteArrayOutputStream()) {
                 XACMLPolicyWriter.writePolicyFile(os, rootPolicy);
-                LOGGER.debug("New Root Policy:{}{}", System.lineSeparator(), os.toString());
+                LOGGER.debug("New Root Policy:{}{}", XacmlPolicyUtils.LINE_SEPARATOR, os);
             }
             //
             // Just update root and PolicySet
@@ -219,13 +241,13 @@ public class XacmlPolicyUtilsTest {
             XacmlPolicyUtils.addPolicySetsToXacmlRootPolicy(rootPolicy, policySet5);
             try (OutputStream os = new ByteArrayOutputStream()) {
                 XACMLPolicyWriter.writePolicyFile(os, rootPolicy);
-                LOGGER.debug("New Root Policy:{}{}", System.lineSeparator(), os.toString());
+                LOGGER.debug("New Root Policy:{}{}", XacmlPolicyUtils.LINE_SEPARATOR, os);
             }
         }).doesNotThrowAnyException();
     }
 
     @Test
-    public void testRemovingProperties() {
+    public void testRemovingReferencedProperties() {
         //
         // Dump what we are starting with
         //
@@ -261,4 +283,59 @@ public class XacmlPolicyUtilsTest {
         XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER);
         assertThat(properties.getProperty("refstart4.file")).isNullOrEmpty();
     }
+
+    @Test
+    public void testRemovingRootProperties() {
+        //
+        // Dump what we are starting with
+        //
+        XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER);
+        //
+        // Remove root policies
+        //
+        Path ref = Paths.get("src/test/resources/root.xml");
+        XacmlPolicyUtils.removeRootPolicy(properties, ref);
+        XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER);
+        assertThat(properties.getProperty("root.file")).isNullOrEmpty();
+
+        //
+        // Test one that isn't in there
+        //
+        ref = Paths.get("src/test/resources/NotThere.xml");
+        XacmlPolicyUtils.removeRootPolicy(properties, ref);
+        XacmlPolicyUtils.debugDumpPolicyProperties(properties, LOGGER);
+        assertThat(properties.getProperty("refstart3.file")).isNotBlank();
+    }
+
+    @Test
+    public void testCopyingProperties() throws Exception {
+        //
+        // Copy to this folder
+        //
+        File copyFolder = policyFolder.newFolder("copy");
+        assertThat(copyFolder.exists()).isTrue();
+        //
+        // Mock up a properties object
+        //
+        Properties mockProperties = new Properties();
+        XacmlPolicyUtils.addRootPolicy(mockProperties, rootPath);
+        XacmlPolicyUtils.addReferencedPolicy(mockProperties, path1);
+        //
+        // Write the properties out to a file
+        //
+        Path fileProperties = XacmlPolicyUtils.getPropertiesPath(policyFolder.getRoot().toPath());
+        XacmlPolicyUtils.storeXacmlProperties(mockProperties, fileProperties);
+        //
+        // Now we can test the copy method
+        //
+        XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile("copy/" + filename);
+        File propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents(
+                fileProperties.toAbsolutePath().toString(), mockProperties, myCreator);
+
+        assertThat(propertiesFile.canRead()).isTrue();
+        assertThat(Path.of(copyFolder.getAbsolutePath(),
+                rootPath.getFileName().toString()).toFile().canRead()).isTrue();
+        assertThat(Path.of(copyFolder.getAbsolutePath(),
+                path1.getFileName().toString()).toFile().canRead()).isTrue();
+    }
 }