2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.tutorial.tutorial;
21 import static org.assertj.core.api.Assertions.assertThat;
22 import static org.junit.Assert.assertEquals;
24 import com.att.research.xacml.api.Response;
25 import com.att.research.xacml.api.XACML3;
27 import java.io.IOException;
29 import java.util.Properties;
30 import java.util.ServiceLoader;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.junit.BeforeClass;
33 import org.junit.ClassRule;
34 import org.junit.Test;
35 import org.junit.rules.TemporaryFolder;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
38 import org.onap.policy.common.utils.resources.TextFileUtils;
39 import org.onap.policy.models.decisions.concepts.DecisionRequest;
40 import org.onap.policy.models.decisions.concepts.DecisionResponse;
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 TutorialApplicationTest {
48 private static final Logger LOGGER = LoggerFactory.getLogger(TutorialApplicationTest.class);
49 private static final Properties properties = new Properties();
50 private static XacmlApplicationServiceProvider service;
51 private static final StandardCoder gson = new StandardCoder();
54 public static final TemporaryFolder policyFolder = new TemporaryFolder();
59 * @throws Exception Should not have exceptions thrown.
62 public static void setup() throws Exception {
64 // Set up our temporary folder
66 XacmlPolicyUtils.FileCreator myCreator = policyFolder::newFile;
67 File propertiesFile = XacmlPolicyUtils
68 .copyXacmlPropertiesContents("src/test/resources/xacml.properties", properties, myCreator);
70 // Load XacmlApplicationServiceProvider service
72 ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
73 ServiceLoader.load(XacmlApplicationServiceProvider.class);
75 // Look for our class instance and save it
77 for (XacmlApplicationServiceProvider application : applicationLoader) {
81 if (application instanceof TutorialApplication) {
82 service = application;
86 // Tell the application to initialize based on the properties file
87 // we just built for it.
89 service.initialize(propertiesFile.toPath().getParent(), null);
91 // Now load the tutorial policies.
93 TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
97 public void testSingleDecision() throws CoderException, IOException {
99 // Load a Decision request
101 DecisionRequest decisionRequest =
102 gson.decode(TextFileUtils.getTextFileAsString("src/test/resources/tutorial-decision-request.json"),
103 DecisionRequest.class);
104 LOGGER.info("{}", gson.encode(decisionRequest, true));
106 // Test a decision - should start with a permit
108 Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
109 LOGGER.info("{}", gson.encode(decision.getLeft(), true));
110 assertEquals("Permit", decision.getLeft().getStatus());
112 // Check that there are attributes
114 assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
115 .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
117 // This should be a "deny"
119 decisionRequest.getResource().put("user", "audit");
120 LOGGER.info("{}", gson.encode(decisionRequest, true));
121 decision = service.makeDecision(decisionRequest, null);
122 LOGGER.info("{}", gson.encode(decision.getLeft(), true));
123 assertEquals("Deny", decision.getLeft().getStatus());
125 // Check that there are attributes
127 assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
128 .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
133 public void testMultiDecision() throws CoderException, IOException {
135 // Load a Decision request
137 DecisionRequest decisionRequest = gson.decode(
138 TextFileUtils.getTextFileAsString("src/test/resources/tutorial-decision-multi-request.json"),
139 DecisionRequest.class);
140 LOGGER.info("{}", gson.encode(decisionRequest, true));
142 // Test a decision - should start with a permit
144 Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
145 LOGGER.info("{}", gson.encode(decision.getLeft(), true));
146 assertEquals("multi", decision.getLeft().getStatus());
148 // Check that there no attributes for the overall response
150 assertThat(decision.getLeft().getAttributes()).isNull();
152 // Check that there are 7 decisions with attributes
154 assertThat(decision.getLeft()).isInstanceOf(TutorialResponse.class);
155 TutorialResponse tutorialResponse = (TutorialResponse) decision.getLeft();
156 assertThat(tutorialResponse.getPermissions()).hasSize(7);
157 tutorialResponse.getPermissions().forEach(this::checkPermission);
160 private void checkPermission(TutorialResponsePermission permission) {
161 assertThat(permission.getAttributes()).hasSize(1);
162 Object resourceAttributes = permission.getAttributes().get(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
163 assertThat(resourceAttributes).isNotNull().isInstanceOf(Map.class);
164 @SuppressWarnings("unchecked")
165 String multiId = ((Map<String, String>) resourceAttributes).get("urn:org:onap:tutorial-multi-id");
166 assertThat(Integer.parseInt(multiId)).isBetween(1, 7);
169 assertThat(permission.getStatus()).isEqualTo("Permit");
171 case "3", "5", "6", "7":
172 assertThat(permission.getStatus()).isEqualTo("Deny");
176 // Should not get here as we check the value range in line 168.
177 // But CodeStyle wants a default.