* ============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.
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;
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;
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;
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
//
// 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
//
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
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
//
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();
+ }
}