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;
30 import java.io.IOException;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Properties;
34 import java.util.ServiceLoader;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.FixMethodOrder;
40 import org.junit.Test;
41 import org.junit.rules.TemporaryFolder;
42 import org.junit.runners.MethodSorters;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.resources.TextFileUtils;
46 import org.onap.policy.models.decisions.concepts.DecisionRequest;
47 import org.onap.policy.models.decisions.concepts.DecisionResponse;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
50 import org.onap.policy.pdp.xacml.application.common.TestUtils;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
52 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
53 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 public class MonitoringPdpApplicationTest {
60 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
61 private static Properties properties = new Properties();
62 private static File propertiesFile;
63 private static XacmlApplicationServiceProvider service;
64 private static DecisionRequest requestSinglePolicy;
66 private static StandardCoder gson = new StandardCoder();
69 public static final TemporaryFolder policyFolder = new TemporaryFolder();
72 * Copies the xacml.properties and policies files into
73 * temporary folder and loads the service provider saving
74 * instance of provider off for other tests to use.
77 public static void setup() throws Exception {
79 // Load Single Decision Request
81 requestSinglePolicy = gson.decode(
83 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
84 DecisionRequest.class);
86 // Setup our temporary folder
88 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
89 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
90 properties, myCreator);
92 // Load XacmlApplicationServiceProvider service
94 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
95 ServiceLoader.load(XacmlApplicationServiceProvider.class);
97 // Look for our class instance and save it
99 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
100 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
101 while (iterator.hasNext()) {
102 XacmlApplicationServiceProvider application = iterator.next();
104 // Is it our service?
106 if (application instanceof MonitoringPdpApplication) {
108 // Should be the first and only one
110 assertThat(service).isNull();
111 service = application;
113 strDump.append(application.applicationName());
114 strDump.append(" supports ");
115 strDump.append(application.supportedPolicyTypes());
116 strDump.append(System.lineSeparator());
118 LOGGER.debug("{}", strDump);
120 // Tell it to initialize based on the properties file
121 // we just built for it.
123 service.initialize(propertiesFile.toPath().getParent());
127 public void test1Basics() {
129 // Make sure there's an application name
131 assertThat(service.applicationName()).isNotEmpty();
133 // Ensure it has the supported policy types and
134 // can support the correct policy types.
136 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.0.0"))).isTrue();
137 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.5.0"))).isTrue();
138 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
139 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
140 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.foobar", "1.0.0"))).isFalse();
142 // Ensure it supports decisions
144 assertThat(service.actionDecisionsSupported()).contains("configure");
148 public void test2NoPolicies() {
150 // Ask for a decision
152 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
153 LOGGER.info("Decision {}", decision);
155 assertThat(decision.getKey()).isNotNull();
156 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
160 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
162 // Now load the vDNS Policy - make sure
163 // the pdp can support it and have it load
167 // Now load the optimization policies
169 final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
172 // Ask for a decision
174 Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
175 LOGGER.info("Decision {}", decision);
177 assertThat(decision.getKey()).isNotNull();
178 assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
180 // Dump it out as Json
182 LOGGER.info(gson.encode(decision.getKey()));
183 LOGGER.info("Now testing unloading of policy");
187 for (ToscaPolicy policy : loadedPolicies) {
188 assertThat(service.unloadPolicy(policy)).isTrue();
191 // Ask for a decision
193 decision = service.makeDecision(requestSinglePolicy);
194 LOGGER.info("Decision {}", decision.getKey());
196 assertThat(decision.getKey()).isNotNull();
197 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);