2ad83030eeb35f0fa335da84187fdbf464fa9dca
[policy/xacml-pdp.git] / applications / monitoring / src / test / java / org / onap / policy / xacml / pdp / application / monitoring / MonitoringPdpApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.monitoring;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
27 import com.att.research.xacml.api.Response;
28 import java.io.File;
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;
54
55 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
56 public class MonitoringPdpApplicationTest {
57
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;
64
65     private static StandardCoder gson = new StandardCoder();
66
67     @ClassRule
68     public static final TemporaryFolder policyFolder = new TemporaryFolder();
69
70     /**
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.
74      */
75     @BeforeClass
76     public static void setup() throws Exception {
77         //
78         // Load Single Decision Request
79         //
80         requestSinglePolicy = gson.decode(
81                 TextFileUtils
82                     .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
83                     DecisionRequest.class);
84         // Load Single Decision Request
85         //
86         requestPolicyType = gson.decode(
87                 TextFileUtils
88                 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
89                 DecisionRequest.class);
90         //
91         // Setup our temporary folder
92         //
93         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
94         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
95                 properties, myCreator);
96         //
97         // Load XacmlApplicationServiceProvider service
98         //
99         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
100                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
101         //
102         // Look for our class instance and save it
103         //
104         StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
105         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
106         while (iterator.hasNext()) {
107             XacmlApplicationServiceProvider application = iterator.next();
108             //
109             // Is it our service?
110             //
111             if (application instanceof MonitoringPdpApplication) {
112                 //
113                 // Should be the first and only one
114                 //
115                 assertThat(service).isNull();
116                 service = application;
117             }
118             strDump.append(application.applicationName());
119             strDump.append(" supports ");
120             strDump.append(application.supportedPolicyTypes());
121             strDump.append(System.lineSeparator());
122         }
123         LOGGER.debug("{}", strDump);
124         //
125         // Tell it to initialize based on the properties file
126         // we just built for it.
127         //
128         service.initialize(propertiesFile.toPath().getParent());
129     }
130
131     @Test
132     public void test1Basics() {
133         //
134         // Make sure there's an application name
135         //
136         assertThat(service.applicationName()).isNotEmpty();
137         //
138         // Ensure it has the supported policy types and
139         // can support the correct policy types.
140         //
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();
146         //
147         // Ensure it supports decisions
148         //
149         assertThat(service.actionDecisionsSupported()).contains("configure");
150     }
151
152     @Test
153     public void test2NoPolicies() {
154         //
155         // Ask for a decision
156         //
157         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
158         LOGGER.info("Decision {}", decision);
159
160         assertThat(decision.getKey()).isNotNull();
161         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
162     }
163
164     @Test
165     public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
166         //
167         // Now load the vDNS Policy - make sure
168         // the pdp can support it and have it load
169         // into the PDP.
170         //
171         //
172         // Now load the monitoring policies
173         //
174         final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
175                 service);
176         //
177         // Ask for a decision
178         //
179         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
180         LOGGER.info("Decision {}", decision);
181
182         assertThat(decision.getKey()).isNotNull();
183         assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
184         //
185         // Dump it out as Json
186         //
187         LOGGER.info(gson.encode(decision.getKey()));
188         //
189         // Ask for a decision based on policy-type
190         //
191         decision = service.makeDecision(requestPolicyType);
192         LOGGER.info("Decision {}", decision);
193
194         assertThat(decision.getKey()).isNotNull();
195         assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
196         //
197         // Dump it out as Json
198         //
199         LOGGER.info(gson.encode(decision.getKey()));
200         //
201         // Now unload it
202         //
203         LOGGER.info("Now testing unloading of policy");
204         for (ToscaPolicy policy : loadedPolicies) {
205             assertThat(service.unloadPolicy(policy)).isTrue();
206         }
207         //
208         // Ask for a decision
209         //
210         decision = service.makeDecision(requestSinglePolicy);
211         LOGGER.info("Decision {}", decision.getKey());
212
213         assertThat(decision.getKey()).isNotNull();
214         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
215     }
216
217 }