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;
28 import java.io.IOException;
29 import java.util.Iterator;
30 import java.util.Properties;
31 import java.util.ServiceLoader;
33 import org.junit.BeforeClass;
34 import org.junit.ClassRule;
35 import org.junit.FixMethodOrder;
36 import org.junit.Test;
37 import org.junit.rules.TemporaryFolder;
38 import org.junit.runners.MethodSorters;
39 import org.onap.policy.common.utils.coder.CoderException;
40 import org.onap.policy.common.utils.coder.StandardCoder;
41 import org.onap.policy.common.utils.resources.TextFileUtils;
42 import org.onap.policy.models.decisions.concepts.DecisionRequest;
43 import org.onap.policy.models.decisions.concepts.DecisionResponse;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
45 import org.onap.policy.pdp.xacml.application.common.TestUtils;
46 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
47 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
48 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
53 public class MonitoringPdpApplicationTest {
55 private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
56 private static Properties properties = new Properties();
57 private static File propertiesFile;
58 private static XacmlApplicationServiceProvider service;
59 private static DecisionRequest requestSinglePolicy;
61 private static StandardCoder gson = new StandardCoder();
64 public static final TemporaryFolder policyFolder = new TemporaryFolder();
67 * Copies the xacml.properties and policies files into
68 * temporary folder and loads the service provider saving
69 * instance of provider off for other tests to use.
72 public static void setup() throws Exception {
74 // Load Single Decision Request
76 requestSinglePolicy = gson.decode(
78 .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
79 DecisionRequest.class);
81 // Setup our temporary folder
83 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
84 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
85 properties, myCreator);
87 // Load XacmlApplicationServiceProvider service
89 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
90 ServiceLoader.load(XacmlApplicationServiceProvider.class);
92 // Look for our class instance and save it
94 StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
95 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
96 while (iterator.hasNext()) {
97 XacmlApplicationServiceProvider application = iterator.next();
101 if (application instanceof MonitoringPdpApplication) {
103 // Should be the first and only one
105 assertThat(service).isNull();
106 service = application;
108 strDump.append(application.applicationName());
109 strDump.append(" supports ");
110 strDump.append(application.supportedPolicyTypes());
111 strDump.append(System.lineSeparator());
113 LOGGER.debug("{}", strDump);
115 // Tell it to initialize based on the properties file
116 // we just built for it.
118 service.initialize(propertiesFile.toPath().getParent());
122 public void test1Basics() {
124 // Make sure there's an application name
126 assertThat(service.applicationName()).isNotEmpty();
128 // Ensure it has the supported policy types and
129 // can support the correct policy types.
131 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.0.0"))).isTrue();
132 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.5.0"))).isTrue();
133 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
134 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
135 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.foobar", "1.0.0"))).isFalse();
137 // Ensure it supports decisions
139 assertThat(service.actionDecisionsSupported()).contains("configure");
143 public void test2NoPolicies() {
145 // Ask for a decision
147 DecisionResponse response = service.makeDecision(requestSinglePolicy);
148 LOGGER.info("Decision {}", response);
150 assertThat(response).isNotNull();
151 assertThat(response.getPolicies().size()).isEqualTo(0);
155 public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
157 // Now load the vDNS Policy - make sure
158 // the pdp can support it and have it load
162 // Now load the optimization policies
164 TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml", service);
166 // Ask for a decision
168 DecisionResponse response = service.makeDecision(requestSinglePolicy);
169 LOGGER.info("Decision {}", response);
171 assertThat(response).isNotNull();
172 assertThat(response.getPolicies().size()).isEqualTo(1);
174 // Dump it out as Json
176 LOGGER.info(gson.encode(response));
180 public void test4BadPolicies() {
183 * THESE TEST SHOULD BE MOVED INTO THE API PROJECT
186 // No need for service, just test some of the methods
189 MonitoringPdpApplication onapPdpEngine = new MonitoringPdpApplication();
191 assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
192 try (InputStream is =
193 new FileInputStream("src/test/resources/test.monitoring.policy.missingmetadata.yaml")) {
194 onapPdpEngine.convertPolicies(is);
196 }).withMessageContaining("missing metadata section");
198 assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
199 try (InputStream is =
200 new FileInputStream("src/test/resources/test.monitoring.policy.missingtype.yaml")) {
201 onapPdpEngine.convertPolicies(is);
203 }).withMessageContaining("missing type value");
205 assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
206 try (InputStream is =
207 new FileInputStream("src/test/resources/test.monitoring.policy.missingversion.yaml")) {
208 onapPdpEngine.convertPolicies(is);
210 }).withMessageContaining("missing version value");
212 assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
213 try (InputStream is =
214 new FileInputStream("src/test/resources/test.monitoring.policy.badmetadata.1.yaml")) {
215 onapPdpEngine.convertPolicies(is);
217 }).withMessageContaining("missing metadata policy-version");
219 assertThatExceptionOfType(ToscaPolicyConversionException.class).isThrownBy(() -> {
220 try (InputStream is =
221 new FileInputStream("src/test/resources/test.monitoring.policy.badmetadata.2.yaml")) {
222 onapPdpEngine.convertPolicies(is);
224 }).withMessageContaining("missing metadata policy-id");