2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.xacml.pdp.application.match;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import com.att.research.xacml.api.Response;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.nio.file.Files;
34 import java.nio.file.Paths;
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;
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;
72 public static final TemporaryFolder policyFolder = new TemporaryFolder();
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.
80 public static void setUp() throws Exception {
81 clientParams = mock(RestServerParameters.class);
82 when(clientParams.getHost()).thenReturn("localhost");
83 when(clientParams.getPort()).thenReturn(6969);
85 // Load Single Decision Request
87 baseRequest = gson.decode(
90 "src/test/resources/decision.match.input.json"),
91 DecisionRequest.class);
93 // Setup our temporary folder
95 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
96 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
97 properties, myCreator);
99 // Copy the test policy types into data area
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());
109 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
110 ServiceLoader.load(XacmlApplicationServiceProvider.class);
112 // Iterate through Xacml application services and find
113 // the optimization service. Save it for use throughout
114 // all the Junit tests.
116 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
117 for (XacmlApplicationServiceProvider application : applicationLoader) {
119 // Is it our service?
121 if (application instanceof MatchPdpApplication) {
123 // Should be the first and only one
125 assertThat(service).isNull();
126 service = application;
128 strDump.append(application.applicationName());
129 strDump.append(" supports ");
130 strDump.append(application.supportedPolicyTypes());
131 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
133 LOGGER.debug("{}", strDump);
134 assertThat(service).isNotNull();
136 // Tell it to initialize based on the properties file
137 // we just built for it.
139 service.initialize(propertiesFile.toPath().getParent(), clientParams);
143 public void test01Basics() {
145 // Make sure there's an application name
147 assertThat(service.applicationName()).isNotEmpty();
151 assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
152 assertThat(service.actionDecisionsSupported()).contains("match");
154 // Ensure it has the supported policy types and
155 // can support the correct policy types.
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();
164 public void test02NoPolicies() throws CoderException {
166 // Ask for a decision when there are no policies loaded
168 LOGGER.info("Request {}", gson.encode(baseRequest));
169 Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
170 LOGGER.info("Decision {}", decision.getKey());
172 assertThat(decision.getKey()).isNotNull();
173 assertThat(decision.getKey().getPolicies()).isEmpty();
177 public void test03Match() throws CoderException, FileNotFoundException, IOException,
178 XacmlApplicationException {
180 // Now load all the test match policies
182 TestUtils.loadPolicies("src/test/resources/test-match-policies.yaml", service);
184 // Ask for a decision
186 DecisionResponse response = makeDecision();
188 // There is no default policy
190 assertThat(response).isNotNull();
191 assertThat(response.getPolicies()).isEmpty();
195 baseRequest.getResource().put("matchable", "foo");
199 response = makeDecision();
200 assertThat(response).isNotNull();
201 assertThat(response.getPolicies()).hasSize(1);
205 validateDecision(response, baseRequest, "value1");
209 baseRequest.getResource().put("matchable", "bar");
213 response = makeDecision();
214 assertThat(response).isNotNull();
215 assertThat(response.getPolicies()).hasSize(1);
219 validateDecision(response, baseRequest, "value2");
221 // Ask for hello (should return nothing)
223 baseRequest.getResource().put("matchable", "hello");
227 response = makeDecision();
228 assertThat(response).isNotNull();
229 assertThat(response.getPolicies()).isEmpty();
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());
239 return decision.getKey();
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");
252 assertThat(policyProperties.get("nonmatchable").toString()).hasToString(value);