2 * ============LICENSE_START=======================================================
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
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.naming;
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;
35 import java.util.Collection;
36 import java.util.Iterator;
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.Test;
46 import org.junit.rules.TemporaryFolder;
47 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.common.utils.resources.TextFileUtils;
52 import org.onap.policy.models.decisions.concepts.DecisionRequest;
53 import org.onap.policy.models.decisions.concepts.DecisionResponse;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
55 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
56 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
57 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
58 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
62 public class NamingPdpApplicationTest {
63 private static final Logger LOGGER = LoggerFactory.getLogger(NamingPdpApplicationTest.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.naming.input.json"),
91 DecisionRequest.class);
93 // Setup our temporary folder
95 // TODO use lambda policyFolder::newFile
96 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
97 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
98 properties, myCreator);
100 // Copy the test policy types into data area
102 String policy = "onap.policies.Naming";
103 String policyType = ResourceUtils.getResourceAsString("policytypes/" + policy + ".yaml");
104 LOGGER.info("Copying {}", policyType);
105 // TODO investigate UTF-8
106 Files.write(Paths.get(policyFolder.getRoot().getAbsolutePath(), policy + "-1.0.0.yaml"),
107 policyType.getBytes());
111 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
112 ServiceLoader.load(XacmlApplicationServiceProvider.class);
114 // Iterate through Xacml application services and find
115 // the optimization service. Save it for use throughout
116 // all the Junit tests.
118 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
119 Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
120 while (iterator.hasNext()) {
121 XacmlApplicationServiceProvider application = iterator.next();
123 // Is it our service?
125 if (application instanceof NamingPdpApplication) {
127 // Should be the first and only one
129 assertThat(service).isNull();
130 service = application;
132 strDump.append(application.applicationName());
133 strDump.append(" supports ");
134 strDump.append(application.supportedPolicyTypes());
135 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
137 LOGGER.debug("{}", strDump);
138 assertThat(service).isNotNull();
140 // Tell it to initialize based on the properties file
141 // we just built for it.
143 service.initialize(propertiesFile.toPath().getParent(), clientParams);
147 public void test01Basics() {
149 // Make sure there's an application name
151 assertThat(service.applicationName()).isNotEmpty();
155 assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
156 assertThat(service.actionDecisionsSupported()).contains("naming");
158 // Ensure it has the supported policy types and
159 // can support the correct policy types.
161 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
162 "onap.policies.Naming", "1.0.0"))).isTrue();
163 assertThat(service.canSupportPolicyType(new ToscaPolicyTypeIdentifier(
164 "onap.foobar", "1.0.0"))).isFalse();
168 public void test02NoPolicies() throws CoderException {
170 // Ask for a decision when there are no policies loaded
172 LOGGER.info("Request {}", gson.encode(baseRequest));
173 Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
174 LOGGER.info("Decision {}", decision.getKey());
176 assertThat(decision.getKey()).isNotNull();
177 assertThat(decision.getKey().getPolicies().size()).isEqualTo(0);
181 public void test03Naming() throws CoderException, FileNotFoundException, IOException,
182 XacmlApplicationException {
184 // Now load all the optimization policies
186 TestUtils.loadPolicies("policies/sdnc.policy.naming.input.tosca.yaml", service);
188 // Ask for a decision for available default policies
190 DecisionResponse response = makeDecision();
192 assertThat(response).isNotNull();
193 assertThat(response.getPolicies().size()).isEqualTo(1);
197 validateDecision(response, baseRequest);
200 private DecisionResponse makeDecision() {
201 Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
202 LOGGER.info("Request Resources {}", baseRequest.getResource());
203 LOGGER.info("Decision {}", decision.getKey());
204 for (Entry<String, Object> entrySet : decision.getKey().getPolicies().entrySet()) {
205 LOGGER.info("Policy {}", entrySet.getKey());
207 return decision.getKey();
210 @SuppressWarnings("unchecked")
211 private void validateDecision(DecisionResponse decision, DecisionRequest request) {
212 for (Entry<String, Object> entrySet : decision.getPolicies().entrySet()) {
213 LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
214 assertThat(entrySet.getValue()).isInstanceOf(Map.class);
215 Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
216 assertThat(policyContents.containsKey("properties")).isTrue();
217 assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
218 Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
220 validateMatchable((Collection<String>) request.getResource().get("nfRole"),
221 (Collection<String>) policyProperties.get("nfRole"));
223 validateMatchable((Collection<String>) request.getResource().get("naming-type"),
224 (Collection<String>) policyProperties.get("naming-type"));
226 validateMatchable((Collection<String>) request.getResource().get("property-name"),
227 (Collection<String>) policyProperties.get("property-name"));
231 private void validateMatchable(Collection<String> requestList, Collection<String> policyProperties) {
232 LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList);
234 // Null or empty implies '*' - that is any value is acceptable
237 if (policyProperties == null || policyProperties.isEmpty()) {
240 Condition<String> condition = new Condition<>(
241 requestList::contains,
242 "Request list is contained");
243 assertThat(policyProperties).haveAtLeast(1, condition);