summaryrefslogtreecommitdiffstats
path: root/main/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/test/java')
-rw-r--r--main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java200
1 files changed, 200 insertions, 0 deletions
diff --git a/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java
new file mode 100644
index 00000000..243c9eab
--- /dev/null
+++ b/main/src/test/java/org/onap/policy/pdpx/main/rest/XacmlPdpApplicationManagerTest.java
@@ -0,0 +1,200 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdpx.main.rest;
+
+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 java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.Map;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.decisions.concepts.DecisionRequest;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
+import org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication;
+import org.onap.policy.xacml.pdp.application.nativ.NativePdpApplication;
+import org.onap.policy.xacml.pdp.application.optimization.OptimizationPdpApplication;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XacmlPdpApplicationManagerTest {
+ private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManagerTest.class);
+ private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
+ private static final RestServerParameters params = new RestServerParameters();
+ private static Path appsDirectory;
+ private static ToscaServiceTemplate completedJtst;
+
+ @ClassRule
+ public static final TemporaryFolder appsFolder = new TemporaryFolder();
+
+ /**
+ * setupTestEnvironment.
+ *
+ * @throws Exception Exception if anything is missing
+ */
+ @BeforeClass
+ public static void setupTestEnvironment() throws Exception {
+ //
+ // No need to do more than this
+ //
+ params.setName("policyApiParameters");
+ //
+ // Load an example policy
+ //
+ String policyYaml = ResourceUtils
+ .getResourceAsString("../applications/monitoring/src/test/resources/vDNS.policy.input.yaml");
+ //
+ // Serialize it into a class
+ //
+ ToscaServiceTemplate serviceTemplate;
+ try {
+ serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
+ } catch (CoderException e) {
+ throw new XacmlApplicationException("Failed to decode policy from resource file", e);
+ }
+ //
+ // Make sure all the fields are setup properly
+ //
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ jtst.fromAuthorative(serviceTemplate);
+ completedJtst = jtst.toAuthorative();
+ //
+ // We need at least 1 policies
+ //
+ assertThat(completedJtst).isNotNull();
+ assertThat(completedJtst.getToscaTopologyTemplate().getPolicies().size()).isGreaterThan(0);
+ //
+ // Copy test directory over of the application directories
+ //
+ Path src = Paths.get("src/test/resources/apps");
+ File apps = appsFolder.newFolder("apps");
+ Files.walk(src).forEach(source -> {
+ copy(source, apps.toPath().resolve(src.relativize(source)));
+ });
+ appsDirectory = apps.toPath();
+ }
+
+ @Test
+ public void testXacmlPdpApplicationManagerBadPath() throws Exception {
+ //
+ // Make up a non existent directory to initialize from
+ //
+ Path nonExistentPath = Paths.get(appsFolder.getRoot().getAbsolutePath(), "nonexistent");
+ //
+ // Create our app manager
+ //
+ XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(nonExistentPath, params);
+ //
+ // Still creates the manager, but the apps were not able to initialize
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.findNativeApplication()).isNull();
+ //
+ // Now create the directory
+ //
+ Files.createDirectory(nonExistentPath);
+ manager = new XacmlPdpApplicationManager(nonExistentPath, params);
+ //
+ // Now it should have initialized the apps
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.findNativeApplication()).isNull();
+ }
+
+ @Test
+ public void testXacmlPdpApplicationManagerSimple() {
+ XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(appsDirectory, params);
+ //
+ // Test the basics from the startup
+ //
+ assertThat(manager).isNotNull();
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ assertThat(manager.getPolicyTypeCount()).isEqualTo(19);
+ assertThat(manager.getToscaPolicies()).isEmpty();
+ assertThat(manager.getToscaPolicyIdentifiers()).isEmpty();
+ assertThat(manager.getToscaPolicyTypeIdents()).hasSize(19);
+
+ assertThat(manager.findNativeApplication()).isInstanceOf(NativePdpApplication.class);
+
+ DecisionRequest request = new DecisionRequest();
+ request.setAction("optimize");
+ assertThat(manager.findApplication(request)).isInstanceOf(OptimizationPdpApplication.class);
+ request.setAction("guard");
+ assertThat(manager.findApplication(request)).isInstanceOf(GuardPdpApplication.class);
+ //
+ // Try to unload a policy that isn't loaded
+ //
+ ToscaPolicy policy = null;
+ for (Map<String, ToscaPolicy> map : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+ policy = map.get("onap.scaleout.tca");
+ }
+ assertThat(policy).isNotNull();
+ //
+ // Without this being set, it throws NonNull Exception
+ //
+ policy.setTypeVersion("1.0.0");
+ //
+ // Try loading and unloading
+ //
+ final ToscaPolicy policyFinal = policy;
+ assertThatCode(() -> {
+ manager.removeUndeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ manager.loadDeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(1);
+ manager.removeUndeployedPolicy(policyFinal);
+ assertThat(manager.getPolicyCount()).isEqualTo(0);
+ }).doesNotThrowAnyException();
+ //
+ // try loading something unsupported
+ //
+ assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> {
+ ToscaPolicy unsupportedPolicy = new ToscaPolicy();
+ unsupportedPolicy.setType("I.am.not.supported");
+ unsupportedPolicy.setTypeVersion("5.5.5");
+ manager.loadDeployedPolicy(unsupportedPolicy);
+ });
+ }
+
+ private static void copy(Path source, Path dest) {
+ try {
+ Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
+ } catch (IOException e) {
+ LOGGER.error("Failed to copy {} to {}", source, dest);
+ }
+ }
+
+}