Add new Match application to XACML
[policy/xacml-pdp.git] / applications / match / src / test / java / org / onap / policy / xacml / pdp / application / match / MatchPdpApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.match;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
28
29 import com.att.research.xacml.api.Response;
30 import java.io.File;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
35 import java.util.Map;
36 import java.util.Map.Entry;
37 import java.util.Properties;
38 import java.util.ServiceLoader;
39 import org.apache.commons.lang3.tuple.Pair;
40 import org.junit.BeforeClass;
41 import org.junit.ClassRule;
42 import org.junit.FixMethodOrder;
43 import org.junit.Test;
44 import org.junit.rules.TemporaryFolder;
45 import org.junit.runners.MethodSorters;
46 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.common.utils.resources.TextFileUtils;
51 import org.onap.policy.models.decisions.concepts.DecisionRequest;
52 import org.onap.policy.models.decisions.concepts.DecisionResponse;
53 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
54 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
55 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
56 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
57 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 public class MatchPdpApplicationTest {
63     private static final Logger LOGGER = LoggerFactory.getLogger(MatchPdpApplicationTest.class);
64     private static Properties properties = new Properties();
65     private static File propertiesFile;
66     private static XacmlApplicationServiceProvider service;
67     private static StandardCoder gson = new StandardCoder();
68     private static DecisionRequest baseRequest;
69     private static RestServerParameters clientParams;
70
71     @ClassRule
72     public static final TemporaryFolder policyFolder = new TemporaryFolder();
73
74     /**
75      * Copies the xacml.properties and policies files into
76      * temporary folder and loads the service provider saving
77      * instance of provider off for other tests to use.
78      */
79     @BeforeClass
80     public static void setUp() throws Exception {
81         clientParams = mock(RestServerParameters.class);
82         when(clientParams.getHost()).thenReturn("localhost");
83         when(clientParams.getPort()).thenReturn(6969);
84         //
85         // Load Single Decision Request
86         //
87         baseRequest = gson.decode(
88                 TextFileUtils
89                     .getTextFileAsString(
90                             "src/test/resources/decision.match.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         // Copy the test policy types into data area
100         //
101         String policy = "onap.policies.match.Test";
102         String policyType = ResourceUtils.getResourceAsString("src/test/resources/" + policy + ".yaml");
103         LOGGER.info("Copying {}", policyType);
104         Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
105                 policyType.getBytes());
106         //
107         // Load service
108         //
109         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
110                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
111         //
112         // Iterate through Xacml application services and find
113         // the optimization service. Save it for use throughout
114         // all the Junit tests.
115         //
116         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
117         for (XacmlApplicationServiceProvider application : applicationLoader) {
118             //
119             // Is it our service?
120             //
121             if (application instanceof MatchPdpApplication) {
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(), clientParams);
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("match");
153         //
154         // Ensure it has the supported policy types and
155         // can support the correct policy types.
156         //
157         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
158                 "onap.policies.match.Test", "1.0.0"))).isTrue();
159         assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
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
176     @Test
177     public void test03Match() throws CoderException, FileNotFoundException, IOException,
178         XacmlApplicationException {
179         //
180         // Now load all the test match policies
181         //
182         TestUtils.loadPolicies("src/test/resources/test-match-policies.yaml", service);
183         //
184         // Ask for a decision
185         //
186         DecisionResponse response = makeDecision();
187         //
188         // There is no default policy
189         //
190         assertThat(response).isNotNull();
191         assertThat(response.getPolicies()).isEmpty();
192         //
193         // Ask for foo
194         //
195         baseRequest.getResource().put("matchable", "foo");
196         //
197         // Get the decision
198         //
199         response = makeDecision();
200         assertThat(response).isNotNull();
201         assertThat(response.getPolicies()).hasSize(1);
202         //
203         // Validate it
204         //
205         validateDecision(response, baseRequest, "value1");
206         //
207         // Ask for bar
208         //
209         baseRequest.getResource().put("matchable", "bar");
210         //
211         // Get the decision
212         //
213         response = makeDecision();
214         assertThat(response).isNotNull();
215         assertThat(response.getPolicies()).hasSize(1);
216         //
217         // Validate it
218         //
219         validateDecision(response, baseRequest, "value2");
220         //
221         // Ask for hello (should return nothing)
222         //
223         baseRequest.getResource().put("matchable", "hello");
224         //
225         // Get the decision
226         //
227         response = makeDecision();
228         assertThat(response).isNotNull();
229         assertThat(response.getPolicies()).isEmpty();
230     }
231
232     private DecisionResponse makeDecision() {
233         Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
234         LOGGER.info("Request Resources {}", baseRequest.getResource());
235         LOGGER.info("Decision {}", decision.getKey());
236         for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
237             LOGGER.info("Policy {}", entrySet.getKey());
238         }
239         return decision.getKey();
240     }
241
242     @SuppressWarnings("unchecked")
243     private void validateDecision(DecisionResponse decision, DecisionRequest request, String value) {
244         for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
245             LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
246             assertThat(entrySet.getValue()).isInstanceOf(Map.class);
247             Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
248             assertThat(policyContents).containsKey("properties");
249             assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
250             Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
251
252             assertThat(policyProperties.get("nonmatchable").toString()).hasToString(value);
253         }
254     }
255 }