04c9d20b88731f46c08c92db7a566d178b088265
[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
29 import java.io.File;
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;
35
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;
56
57 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
58 public class MonitoringPdpApplicationTest {
59
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;
65
66     private static StandardCoder gson = new StandardCoder();
67
68     @ClassRule
69     public static final TemporaryFolder policyFolder = new TemporaryFolder();
70
71     /**
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.
75      */
76     @BeforeClass
77     public static void setup() throws Exception {
78         //
79         // Load Single Decision Request
80         //
81         requestSinglePolicy = gson.decode(
82                 TextFileUtils
83                     .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
84                     DecisionRequest.class);
85         //
86         // Setup our temporary folder
87         //
88         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
89         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
90                 properties, myCreator);
91         //
92         // Load XacmlApplicationServiceProvider service
93         //
94         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
95                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
96         //
97         // Look for our class instance and save it
98         //
99         StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
100         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
101         while (iterator.hasNext()) {
102             XacmlApplicationServiceProvider application = iterator.next();
103             //
104             // Is it our service?
105             //
106             if (application instanceof MonitoringPdpApplication) {
107                 //
108                 // Should be the first and only one
109                 //
110                 assertThat(service).isNull();
111                 service = application;
112             }
113             strDump.append(application.applicationName());
114             strDump.append(" supports ");
115             strDump.append(application.supportedPolicyTypes());
116             strDump.append(System.lineSeparator());
117         }
118         LOGGER.debug("{}", strDump);
119         //
120         // Tell it to initialize based on the properties file
121         // we just built for it.
122         //
123         service.initialize(propertiesFile.toPath().getParent());
124     }
125
126     @Test
127     public void test1Basics() {
128         //
129         // Make sure there's an application name
130         //
131         assertThat(service.applicationName()).isNotEmpty();
132         //
133         // Ensure it has the supported policy types and
134         // can support the correct policy types.
135         //
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();
141         //
142         // Ensure it supports decisions
143         //
144         assertThat(service.actionDecisionsSupported()).contains("configure");
145     }
146
147     @Test
148     public void test2NoPolicies() {
149         //
150         // Ask for a decision
151         //
152         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
153         LOGGER.info("Decision {}", decision);
154
155         assertThat(decision.getKey()).isNotNull();
156         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
157     }
158
159     @Test
160     public void test3AddvDnsPolicy() throws IOException, CoderException, XacmlApplicationException {
161         //
162         // Now load the vDNS Policy - make sure
163         // the pdp can support it and have it load
164         // into the PDP.
165         //
166         //
167         // Now load the optimization policies
168         //
169         final List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/vDNS.policy.input.yaml",
170                 service);
171         //
172         // Ask for a decision
173         //
174         Pair<DecisionResponse, Response> decision = service.makeDecision(requestSinglePolicy);
175         LOGGER.info("Decision {}", decision);
176
177         assertThat(decision.getKey()).isNotNull();
178         assertThat(decision.getKey().getPolicies().size()).isEqualTo(1);
179         //
180         // Dump it out as Json
181         //
182         LOGGER.info(gson.encode(decision.getKey()));
183         LOGGER.info("Now testing unloading of policy");
184         //
185         // Now unload it
186         //
187         for (ToscaPolicy policy : loadedPolicies) {
188             assertThat(service.unloadPolicy(policy)).isTrue();
189         }
190         //
191         // Ask for a decision
192         //
193         decision = service.makeDecision(requestSinglePolicy);
194         LOGGER.info("Decision {}", decision.getKey());
195
196         assertThat(decision.getKey()).isNotNull();
197         assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
198     }
199
200 }