Merge "Include returned attributes in Decision"
[policy/xacml-pdp.git] / applications / naming / src / test / java / org / onap / policy / xacml / pdp / application / naming / NamingPdpApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.xacml.pdp.application.naming;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import com.att.research.xacml.api.Response;
29 import java.io.File;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.nio.file.Files;
33 import java.nio.file.Paths;
34 import java.util.Arrays;
35 import java.util.Collection;
36 import java.util.Iterator;
37 import java.util.Map;
38 import java.util.Map.Entry;
39 import java.util.Properties;
40 import java.util.ServiceLoader;
41 import org.apache.commons.lang3.tuple.Pair;
42 import org.assertj.core.api.Condition;
43 import org.junit.BeforeClass;
44 import org.junit.ClassRule;
45 import org.junit.FixMethodOrder;
46 import org.junit.Test;
47 import org.junit.rules.TemporaryFolder;
48 import org.junit.runners.MethodSorters;
49 import org.onap.policy.common.utils.coder.CoderException;
50 import org.onap.policy.common.utils.coder.StandardCoder;
51 import org.onap.policy.common.utils.resources.ResourceUtils;
52 import org.onap.policy.common.utils.resources.TextFileUtils;
53 import org.onap.policy.models.decisions.concepts.DecisionRequest;
54 import org.onap.policy.models.decisions.concepts.DecisionResponse;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
57 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
58 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
59 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
64 public class NamingPdpApplicationTest {
65     private static final Logger LOGGER = LoggerFactory.getLogger(NamingPdpApplicationTest.class);
66     private static Properties properties = new Properties();
67     private static File propertiesFile;
68     private static XacmlApplicationServiceProvider service;
69     private static StandardCoder gson = new StandardCoder();
70     private static DecisionRequest baseRequest;
71
72     @ClassRule
73     public static final TemporaryFolder policyFolder = new TemporaryFolder();
74
75     /**
76      * Copies the xacml.properties and policies files into
77      * temporary folder and loads the service provider saving
78      * instance of provider off for other tests to use.
79      */
80     @BeforeClass
81     public static void setUp() throws Exception {
82         //
83         // Load Single Decision Request
84         //
85         baseRequest = gson.decode(
86                 TextFileUtils
87                     .getTextFileAsString(
88                             "src/test/resources/decision.naming.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         // Copy the test policy types into data area
98         //
99         String policy = "onap.policies.Naming";
100         String policyType = ResourceUtils.getResourceAsString("policytypes/" + policy + ".yaml");
101         LOGGER.info("Copying {}", policyType);
102         Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
103                 policyType.getBytes());
104         //
105         // Load service
106         //
107         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
108                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
109         //
110         // Iterate through Xacml application services and find
111         // the optimization service. Save it for use throughout
112         // all the Junit tests.
113         //
114         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
115         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
116         while (iterator.hasNext()) {
117             XacmlApplicationServiceProvider application = iterator.next();
118             //
119             // Is it our service?
120             //
121             if (application instanceof NamingPdpApplication) {
122                 //
123                 // Should be the first and only one
124                 //
125                 assertThat(service).isNull();
126                 service = application;
127             }
128             strDump.append(application.applicationName());
129             strDump.append(" supports ");
130             strDump.append(application.supportedPolicyTypes());
131             strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
132         }
133         LOGGER.debug("{}", strDump);
134         assertThat(service).isNotNull();
135         //
136         // Tell it to initialize based on the properties file
137         // we just built for it.
138         //
139         service.initialize(propertiesFile.toPath().getParent(), null);
140     }
141
142     @Test
143     public void test01Basics() {
144         //
145         // Make sure there's an application name
146         //
147         assertThat(service.applicationName()).isNotEmpty();
148         //
149         // Decisions
150         //
151         assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
152         assertThat(service.actionDecisionsSupported()).contains("naming");
153         //
154         // Ensure it has the supported policy types and
155         // can support the correct policy types.
156         //
157         assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
158                 "onap.policies.Naming", "1.0.0"))).isTrue();
159         assertThat(service.canSupportPolicyType(new ToscaConceptIdentifier(
160                 "onap.foobar", "1.0.0"))).isFalse();
161     }
162
163     @Test
164     public void test02NoPolicies() throws CoderException {
165         //
166         // Ask for a decision when there are no policies loaded
167         //
168         LOGGER.info("Request {}", gson.encode(baseRequest));
169         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
170         LOGGER.info("Decision {}", decision.getKey());
171
172         assertThat(decision.getKey()).isNotNull();
173         assertThat(decision.getKey().getPolicies()).isEmpty();
174         //
175         // Naming applications should not have this information returned
176         //
177         assertThat(decision.getKey().getAdvice()).isNull();
178         assertThat(decision.getKey().getObligations()).isNull();
179         assertThat(decision.getKey().getAttributes()).isNull();
180     }
181
182     @Test
183     public void test03Naming() throws CoderException, FileNotFoundException, IOException,
184         XacmlApplicationException {
185         //
186         // Now load all the optimization policies
187         //
188         TestUtils.loadPolicies("policies/sdnc.policy.naming.input.tosca.yaml", service);
189         //
190         // Ask for a decision for available default policies
191         //
192         DecisionResponse response = makeDecision();
193         //
194         // There is no default policy
195         //
196         assertThat(response).isNotNull();
197         assertThat(response.getPolicies()).isEmpty();
198         //
199         // Ask for VNF
200         //
201         baseRequest.getResource().put("policy-type", Arrays.asList("onap.policies.Naming"));
202         //
203         // Ask for a decision for VNF default policies
204         //
205         response = makeDecision();
206         assertThat(response).isNotNull();
207         assertThat(response.getPolicies()).hasSize(1);
208         //
209         // Naming applications should not have this information returned
210         //
211         assertThat(response.getAdvice()).isNull();
212         assertThat(response.getObligations()).isNull();
213         assertThat(response.getAttributes()).isNull();
214         //
215         // Validate it
216         //
217         validateDecision(response, baseRequest);
218     }
219
220     private DecisionResponse makeDecision() {
221         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
222         LOGGER.info("Request Resources {}", baseRequest.getResource());
223         LOGGER.info("Decision {}", decision.getKey());
224         for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
225             LOGGER.info("Policy {}", entrySet.getKey());
226         }
227         return decision.getKey();
228     }
229
230     @SuppressWarnings("unchecked")
231     private void validateDecision(DecisionResponse decision, DecisionRequest request) {
232         for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
233             LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
234             assertThat(entrySet.getValue()).isInstanceOf(Map.class);
235             Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
236             assertThat(policyContents).containsKey("properties");
237             assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
238             Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
239
240             validateMatchable((Collection<String>) request.getResource().get("nfRole"),
241                     (Collection<String>) policyProperties.get("nfRole"));
242
243             validateMatchable((Collection<String>) request.getResource().get("naming-type"),
244                     (Collection<String>) policyProperties.get("naming-type"));
245
246             validateMatchable((Collection<String>) request.getResource().get("property-name"),
247                     (Collection<String>) policyProperties.get("property-name"));
248         }
249     }
250
251     private void validateMatchable(Collection<String> requestList, Collection<String> policyProperties) {
252         LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList);
253         //
254         // Null or empty implies '*' - that is any value is acceptable
255         // for this policy.
256         //
257         if (policyProperties == null || policyProperties.isEmpty()) {
258             return;
259         }
260         Condition<String> condition = new Condition<>(
261                 requestList::contains,
262                 "Request list is contained");
263         assertThat(policyProperties).haveAtLeast(1, condition);
264
265     }
266 }