Integrate using Policy Type to find Matchable
[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.endpoints.parameters.RestServerParameters;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.common.utils.resources.TextFileUtils;
45 import org.onap.policy.models.decisions.concepts.DecisionRequest;
46 import org.onap.policy.models.decisions.concepts.DecisionResponse;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
49 import org.onap.policy.pdp.xacml.application.common.TestUtils;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
52 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
57 public class MonitoringPdpApplicationTest {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(MonitoringPdpApplicationTest.class);
60     private static Properties properties = new Properties();
61     private static File propertiesFile;
62     private static XacmlApplicationServiceProvider service;
63     private static DecisionRequest requestSinglePolicy;
64     private static DecisionRequest requestPolicyType;
65
66     private static StandardCoder gson = new StandardCoder();
67     private static RestServerParameters clientParams = new RestServerParameters();
68
69     @ClassRule
70     public static final TemporaryFolder policyFolder = new TemporaryFolder();
71
72     /**
73      * Copies the xacml.properties and policies files into
74      * temporary folder and loads the service provider saving
75      * instance of provider off for other tests to use.
76      */
77     @BeforeClass
78     public static void setup() throws Exception {
79         //
80         // Load Single Decision Request
81         //
82         requestSinglePolicy = gson.decode(
83                 TextFileUtils
84                     .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
85                     DecisionRequest.class);
86         // Load Single Decision Request
87         //
88         requestPolicyType = gson.decode(
89                 TextFileUtils
90                 .getTextFileAsString("../../main/src/test/resources/decisions/decision.policytype.input.json"),
91                 DecisionRequest.class);
92         //
93         // Setup our temporary folder
94         //
95         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
96         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
97                 properties, myCreator);
98         //
99         // Load XacmlApplicationServiceProvider service
100         //
101         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
102                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
103         //
104         // Look for our class instance and save it
105         //
106         StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
107         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
108         while (iterator.hasNext()) {
109             XacmlApplicationServiceProvider application = iterator.next();
110             //
111             // Is it our service?
112             //
113             if (application instanceof MonitoringPdpApplication) {
114                 //
115                 // Should be the first and only one
116                 //
117                 assertThat(service).isNull();
118                 service = application;
119             }
120             strDump.append(application.applicationName());
121             strDump.append(" supports ");
122             strDump.append(application.supportedPolicyTypes());
123             strDump.append(System.lineSeparator());
124         }
125         LOGGER.debug("{}", strDump);
126         //
127         // Tell it to initialize based on the properties file
128         // we just built for it.
129         //
130         service.initialize(propertiesFile.toPath().getParent(), clientParams);
131     }
132
133     @Test
134     public void test1Basics() {
135         //
136         // Make sure there's an application name
137         //
138         assertThat(service.applicationName()).isNotEmpty();
139         //
140         // Ensure it has the supported policy types and
141         // can support the correct policy types.
142         //
143         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.0.0"))).isTrue();
144         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.Monitoring", "1.5.0"))).isTrue();
145         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
146                 "onap.policies.monitoring.foobar", "1.0.1"))).isTrue();
147         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier("onap.foobar", "1.0.0"))).isFalse();
148         //
149         // Ensure it supports decisions
150         //
151         assertThat(service.actionDecisionsSupported()).contains("configure");
152     }
153
154     @Test
155     public void test2NoPolicies() {
156         //
157         // Ask for a decision
158         //
159         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
160         LOGGER.info("Decision {}", decision);
161
162         assertThat(decision.getKey()).isNotNull();
163         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
164     }
165
166     @Test
167     public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
168         //
169         // Now load the vDNS Policy - make sure
170         // the pdp can support it and have it load
171         // into the PDP.
172         //
173         //
174         // Now load the monitoring policies
175         //
176         final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
177                 service);
178         //
179         // Ask for a decision
180         //
181         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
182         LOGGER.info("Decision {}", decision);
183
184         assertThat(decision.getKey()).isNotNull();
185         assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
186         //
187         // Dump it out as Json
188         //
189         LOGGER.info(gson.encode(decision.getKey()));
190         //
191         // Ask for a decision based on policy-type
192         //
193         decision = service.makeDecision(requestPolicyType);
194         LOGGER.info("Decision {}", decision);
195
196         assertThat(decision.getKey()).isNotNull();
197         assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
198         //
199         // Dump it out as Json
200         //
201         LOGGER.info(gson.encode(decision.getKey()));
202         //
203         // Now unload it
204         //
205         LOGGER.info("Now testing unloading of policy");
206         for (ToscaPolicy policy : loadedPolicies) {
207             assertThat(service.unloadPolicy(policy)).isTrue();
208         }
209         //
210         // Ask for a decision
211         //
212         decision = service.makeDecision(requestSinglePolicy);
213         LOGGER.info("Decision {}", decision.getKey());
214
215         assertThat(decision.getKey()).isNotNull();
216         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
217     }
218
219 }