b25c2a3114dc8d8282830a45934175994b6830d6
[policy/xacml-pdp.git] /
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.nativ;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26
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;
32 import java.io.File;
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;
46
47 public class NativePdpApplicationTest {
48
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;
56
57     @ClassRule
58     public static final TemporaryFolder policyFolder = new TemporaryFolder();
59
60     /**
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.
64      */
65     @BeforeClass
66     public static void setup() throws Exception {
67         LOGGER.info("Setting up class");
68         //
69         // Setup our temporary folder
70         //
71         XacmlPolicyUtils.FileCreator myCreator = (filename) -> policyFolder.newFile(filename);
72         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
73                 properties, myCreator);
74         //
75         // Load service
76         //
77         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
78                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
79         //
80         // Find the native application and save for use in all the tests
81         //
82         StringBuilder strDump = new StringBuilder("Loaded applications:" + XacmlPolicyUtils.LINE_SEPARATOR);
83         for (XacmlApplicationServiceProvider application : applicationLoader) {
84             //
85             // Is it our service?
86             //
87             if (application instanceof NativePdpApplication) {
88                 //
89                 // Should be the first and only one
90                 //
91                 assertThat(service).isNull();
92                 service = (NativePdpApplication) application;
93             }
94             strDump.append(application.applicationName());
95             strDump.append(" supports ");
96             strDump.append(application.supportedPolicyTypes());
97             strDump.append(XacmlPolicyUtils.LINE_SEPARATOR);
98         }
99         LOGGER.info("{}", strDump);
100         //
101         // Tell it to initialize based on the properties file
102         // we just built for it.
103         //
104         service.initialize(propertiesFile.toPath().getParent(), clientParams);
105         //
106         // Load XACML Request
107         //
108         request = DOMRequest.load(
109                 TextFileUtils.getTextFileAsString(
110                         "src/test/resources/requests/native.policy.request.xml"));
111     }
112
113     @Test
114     public void testNativePolicy() throws Exception {
115
116         LOGGER.info("*********** Running native policy test *************");
117         //
118         // Now load the TOSCA compliant native policy - make sure
119         // the pdp can support it and have it load into the PDP.
120         //
121         TestUtils.loadPolicies("src/test/resources/policies/native.policy.yaml", service);
122         //
123         // Send the request and verify decision result
124         //
125         requestAndCheckDecision(request, PERMIT);
126     }
127
128     /**
129      * Request a decision and check that it matches expectation.
130      *
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
134      *
135      **/
136     private void requestAndCheckDecision(Request request, String expected) throws Exception {
137         //
138         // Ask for a decision
139         //
140         Response decision = service.makeNativeDecision(request);
141         //
142         // Check decision
143         //
144         checkDecision(expected, decision);
145     }
146
147     /**
148      * Check that decision matches expectation.
149      *
150      * @param expected from the response
151      * @param response received
152      * @throws Exception on errors checking the decision
153      *
154      **/
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));
162     }
163 }