2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.monitoring;
25 import static org.assertj.core.api.Assertions.assertThat;
27 import com.att.research.xacml.api.Response;
29 import java.io.IOException;
30 import java.util.Iterator;
31 import java.util.List;
32 import java.util.Properties;
33 import java.util.ServiceLoader;
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.junit.BeforeClass;
36 import org.junit.ClassRule;
37 import org.junit.FixMethodOrder;
38 import org.junit.Test;
39 import org.junit.rules.TemporaryFolder;
40 import org.junit.runners.MethodSorters;
41 import org.onap.policy.common.utils.coder.CoderException;
42 import org.onap.policy.common.utils.coder.StandardCoder;
43 import org.onap.policy.common.utils.resources.TextFileUtils;
44 import org.onap.policy.models.decisions.concepts.DecisionRequest;
45 import org.onap.policy.models.decisions.concepts.DecisionResponse;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
48 import org.onap.policy.pdp.xacml.application.common.TestUtils;
49 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
51 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 public class MonitoringPdpApplicationTest {
58 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
59 private static Properties properties = new Properties();
60 private static File propertiesFile;
61 private static XacmlApplicationServiceProvider service;
62 private static DecisionRequest requestSinglePolicy;
63 private static DecisionRequest requestPolicyType;
65 private static StandardCoder gson = new StandardCoder();
68 public static final TemporaryFolder policyFolder = new TemporaryFolder();
71 * Copies the xacml.properties and policies files into
72 * temporary folder and loads the service provider saving
73 * instance of provider off for other tests to use.
76 public static void setup() throws Exception {
78 // Load Single Decision Request
80 requestSinglePolicy = gson.decode(
82 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
83 DecisionRequest.class);
84 // Load Single Decision Request
86 requestPolicyType = gson.decode(
88 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
89 DecisionRequest.class);
91 // Setup our temporary folder
93 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
94 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
95 properties, myCreator);
97 // Load XacmlApplicationServiceProvider service
99 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
100 ServiceLoader.load(XacmlApplicationServiceProvider.class);
102 // Look for our class instance and save it
104 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
105 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
106 while (iterator.hasNext()) {
107 XacmlApplicationServiceProvider application = iterator.next();
109 // Is it our service?
111 if (application instanceof MonitoringPdpApplication) {
113 // Should be the first and only one
115 assertThat(service).isNull();
116 service = application;
118 strDump.append(application.applicationName());
119 strDump.append(" supports ");
120 strDump.append(application.supportedPolicyTypes());
121 strDump.append(System.lineSeparator());
123 LOGGER.debug("{}", strDump);
125 // Tell it to initialize based on the properties file
126 // we just built for it.
128 service.initialize(propertiesFile.toPath().getParent());
132 public void test1Basics() {
134 // Make sure there's an application name
136 assertThat(service.applicationName()).isNotEmpty();
138 // Ensure it has the supported policy types and
139 // can support the correct policy types.
141 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.0.0"))).isTrue();
142 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.5.0"))).isTrue();
143 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
144 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
145 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.foobar", "1.0.0"))).isFalse();
147 // Ensure it supports decisions
149 assertThat(service.actionDecisionsSupported()).contains("configure");
153 public void test2NoPolicies() {
155 // Ask for a decision
157 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
158 LOGGER.info("Decision {}", decision);
160 assertThat(decision.getKey()).isNotNull();
161 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
165 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
167 // Now load the vDNS Policy - make sure
168 // the pdp can support it and have it load
172 // Now load the monitoring policies
174 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
177 // Ask for a decision
179 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
180 LOGGER.info("Decision {}", decision);
182 assertThat(decision.getKey()).isNotNull();
183 assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
185 // Dump it out as Json
187 LOGGER.info(gson.encode(decision.getKey()));
189 // Ask for a decision based on policy-type
191 decision = service.makeDecision(requestPolicyType);
192 LOGGER.info("Decision {}", decision);
194 assertThat(decision.getKey()).isNotNull();
195 assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
197 // Dump it out as Json
199 LOGGER.info(gson.encode(decision.getKey()));
203 LOGGER.info("Now testing unloading of policy");
204 for (ToscaPolicy policy : loadedPolicies) {
205 assertThat(service.unloadPolicy(policy)).isTrue();
208 // Ask for a decision
210 decision = service.makeDecision(requestSinglePolicy);
211 LOGGER.info("Decision {}", decision.getKey());
213 assertThat(decision.getKey()).isNotNull();
214 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);