2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.nio.file.StandardCopyOption;
34 import org.junit.BeforeClass;
35 import org.junit.ClassRule;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardYamlCoder;
41 import org.onap.policy.common.utils.resources.ResourceUtils;
42 import org.onap.policy.models.decisions.concepts.DecisionRequest;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
46 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
47 import org.onap.policy.xacml.pdp.application.guard.GuardPdpApplication;
48 import org.onap.policy.xacml.pdp.application.nativ.NativePdpApplication;
49 import org.onap.policy.xacml.pdp.application.optimization.OptimizationPdpApplication;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class XacmlPdpApplicationManagerTest {
54 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpApplicationManagerTest.class);
55 private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
56 private static final BusTopicParams params = new BusTopicParams();
57 private static Path appsDirectory;
58 private static ToscaServiceTemplate completedJtst;
61 public static final TemporaryFolder appsFolder = new TemporaryFolder();
64 * setupTestEnvironment.
66 * @throws Exception Exception if anything is missing
69 public static void setupTestEnvironment() throws Exception {
71 // No need to do more than this
73 params.setClientName("policyApiParameters");
75 // Load an example policy
77 String policyYaml = ResourceUtils
78 .getResourceAsString("../applications/monitoring/src/test/resources/vDNS.policy.input.yaml");
80 // Serialize it into a class
82 ToscaServiceTemplate serviceTemplate;
84 serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
85 } catch (CoderException e) {
86 throw new XacmlApplicationException("Failed to decode policy from resource file", e);
89 // Make sure all the fields are setup properly
91 JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
92 jtst.fromAuthorative(serviceTemplate);
93 completedJtst = jtst.toAuthorative();
95 // We need at least 1 policies
97 assertThat(completedJtst).isNotNull();
98 assertThat(completedJtst.getToscaTopologyTemplate().getPolicies().size()).isPositive();
100 // Copy test directory over of the application directories
102 Path src = Paths.get("src/test/resources/apps");
103 File apps = appsFolder.newFolder("apps");
104 Files.walk(src).forEach(source -> {
105 copy(source, apps.toPath().resolve(src.relativize(source)));
107 appsDirectory = apps.toPath();
111 public void testXacmlPdpApplicationManagerBadPath() throws Exception {
113 // Make up a non existent directory to initialize from
115 Path nonExistentPath = Paths.get(appsFolder.getRoot().getAbsolutePath(), "nonexistent");
117 // Create our app manager
119 XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(nonExistentPath, params);
121 // Still creates the manager, but the apps were not able to initialize
123 assertThat(manager).isNotNull();
124 assertThat(manager.findNativeApplication()).isNull();
126 // Now create the directory
128 Files.createDirectory(nonExistentPath);
129 manager = new XacmlPdpApplicationManager(nonExistentPath, params);
131 // Now it should have initialized the apps
133 assertThat(manager).isNotNull();
134 assertThat(manager.findNativeApplication()).isNull();
138 public void testXacmlPdpApplicationManagerSimple() {
139 XacmlPdpApplicationManager manager = new XacmlPdpApplicationManager(appsDirectory, params);
141 // Test the basics from the startup
143 assertThat(manager).isNotNull();
144 assertThat(manager.getPolicyCount()).isZero();
145 assertThat(manager.getPolicyTypeCount()).isEqualTo(18);
146 assertThat(manager.getToscaPolicies()).isEmpty();
147 assertThat(manager.getToscaPolicyIdentifiers()).isEmpty();
148 assertThat(manager.getToscaPolicyTypeIdents()).hasSize(18);
150 assertThat(manager.findNativeApplication()).isInstanceOf(NativePdpApplication.class);
152 DecisionRequest request = new DecisionRequest();
153 request.setAction("optimize");
154 assertThat(manager.findApplication(request)).isInstanceOf(OptimizationPdpApplication.class);
155 request.setAction("guard");
156 assertThat(manager.findApplication(request)).isInstanceOf(GuardPdpApplication.class);
158 // Try to unload a policy that isn't loaded
160 ToscaPolicy policy = null;
161 for (Map<String, ToscaPolicy> map : completedJtst.getToscaTopologyTemplate().getPolicies()) {
162 policy = map.get("onap.scaleout.tca");
164 assertThat(policy).isNotNull();
166 // Without this being set, it throws NonNull Exception
168 policy.setTypeVersion("1.0.0");
170 // Try loading and unloading
172 final ToscaPolicy policyFinal = policy;
173 assertThatCode(() -> {
174 manager.removeUndeployedPolicy(policyFinal);
175 assertThat(manager.getPolicyCount()).isZero();
176 manager.loadDeployedPolicy(policyFinal);
177 assertThat(manager.getPolicyCount()).isEqualTo(1);
178 manager.removeUndeployedPolicy(policyFinal);
179 assertThat(manager.getPolicyCount()).isZero();
180 }).doesNotThrowAnyException();
182 // try loading something unsupported
184 assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() -> {
185 ToscaPolicy unsupportedPolicy = new ToscaPolicy();
186 unsupportedPolicy.setType("I.am.not.supported");
187 unsupportedPolicy.setTypeVersion("5.5.5");
188 manager.loadDeployedPolicy(unsupportedPolicy);
192 private static void copy(Path source, Path dest) {
194 Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
195 } catch (IOException e) {
196 LOGGER.error("Failed to copy {} to {}", source, dest);