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.nativ;
25 import static org.assertj.core.api.Assertions.assertThat;
27 import com.att.research.xacml.api.Decision;
28 import com.att.research.xacml.api.Request;
29 import com.att.research.xacml.api.Response;
30 import com.att.research.xacml.std.dom.DOMRequest;
31 import com.att.research.xacml.std.dom.DOMResponse;
33 import java.util.Properties;
34 import java.util.ServiceLoader;
35 import org.junit.BeforeClass;
36 import org.junit.ClassRule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
40 import org.onap.policy.common.utils.resources.TextFileUtils;
41 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
42 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
43 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class NativePdpApplicationTest {
49 private static final Logger LOGGER = LoggerFactory.getLogger(NativePdpApplicationTest.class);
50 private static final String PERMIT = "Permit";
51 private static Properties properties = new Properties();
52 private static File propertiesFile;
53 private static RestServerParameters clientParams = new RestServerParameters();
54 private static NativePdpApplication service;
55 private static Request request;
58 public static final TemporaryFolder policyFolder = new TemporaryFolder();
61 * Copies the xacml.properties and policies files into
62 * temporary folder and loads the service provider saving
63 * instance of provider off for other tests to use.
66 public static void setup() throws Exception {
67 LOGGER.info("Setting up class");
69 // Setup our temporary folder
71 XacmlPolicyUtils.FileCreator myCreator = (filename) -> policyFolder.newFile(filename);
72 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
73 properties, myCreator);
77 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
78 ServiceLoader.load(XacmlApplicationServiceProvider.class);
80 // Find the native application and save for use in all the tests
82 StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
83 for (XacmlApplicationServiceProvider application : applicationLoader) {
87 if (application instanceof NativePdpApplication) {
89 // Should be the first and only one
91 assertThat(service).isNull();
92 service = (NativePdpApplication) application;
94 strDump.append(application.applicationName());
95 strDump.append(" supports ");
96 strDump.append(application.supportedPolicyTypes());
97 strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
99 LOGGER.info("{}", strDump);
101 // Tell it to initialize based on the properties file
102 // we just built for it.
104 service.initialize(propertiesFile.toPath().getParent(), clientParams);
106 // Load XACML Request
108 request = DOMRequest.load(
109 TextFileUtils.getTextFileAsString(
110 "src/test/resources/requests/native.policy.request.xml"));
114 public void testNativePolicy() throws Exception {
116 LOGGER.info("*********** Running native policy test *************");
118 // Now load the TOSCA compliant native policy - make sure
119 // the pdp can support it and have it load into the PDP.
121 TestUtils.loadPolicies("src/test/resources/policies/native.policy.yaml", service);
123 // Send the request and verify decision result
125 requestAndCheckDecision(request, PERMIT);
129 * Request a decision and check that it matches expectation.
131 * @param request to send to XACML PDP
132 * @param expected from the response
133 * @throws Exception on errors requesting a decision and checking the returned decision
136 private void requestAndCheckDecision(Request request, String expected) throws Exception {
138 // Ask for a decision
140 Response decision = service.makeNativeDecision(request);
144 checkDecision(expected, decision);
148 * Check that decision matches expectation.
150 * @param expected from the response
151 * @param response received
152 * @throws Exception on errors checking the decision
155 public void checkDecision(String expected, Response response) throws Exception {
156 LOGGER.info("Looking for {} Decision", expected);
157 assertThat(response).isNotNull();
158 Decision decision = response.getResults().iterator().next().getDecision();
159 assertThat(decision).isNotNull();
160 assertThat(decision.toString()).isEqualTo(expected);
161 LOGGER.info("Xacml response we received {}", DOMResponse.toString(response));