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.junit.Assert.assertEquals;
23 import com.att.research.xacml.api.Response;
25 import java.io.IOException;
26 import java.util.Properties;
27 import java.util.ServiceLoader;
28 import org.apache.commons.lang3.tuple.Pair;
29 import org.junit.BeforeClass;
30 import org.junit.ClassRule;
31 import org.junit.Test;
32 import org.junit.rules.TemporaryFolder;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
34 import org.onap.policy.common.utils.coder.CoderException;
35 import org.onap.policy.common.utils.coder.StandardCoder;
36 import org.onap.policy.common.utils.resources.TextFileUtils;
37 import org.onap.policy.models.decisions.concepts.DecisionRequest;
38 import org.onap.policy.models.decisions.concepts.DecisionResponse;
39 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
40 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
41 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
42 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class TutorialApplicationTest {
47 private static final Logger LOGGER = LoggerFactory.getLogger(TutorialApplicationTest.class);
48 private static Properties properties = new Properties();
49 private static File propertiesFile;
50 private static XacmlApplicationServiceProvider service;
51 private static 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 // Setup our temporary folder
66 XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
67 propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
68 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(), new BusTopicParams());
93 public void test() throws CoderException, XacmlApplicationException, IOException {
95 // Now load the tutorial policies.
97 TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
99 // Load a Decision request
101 DecisionRequest decisionRequest = gson.decode(
103 .getTextFileAsString("src/test/resources/tutorial-decision-request.json"),
104 DecisionRequest.class);
106 // Test a decision - should start with a permit
108 Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
109 LOGGER.info(decision.getLeft().toString());
110 assertEquals("Permit", decision.getLeft().getStatus());
112 // This should be a deny
114 decisionRequest.getResource().put("user", "audit");
115 decision = service.makeDecision(decisionRequest, null);
116 LOGGER.info(decision.getLeft().toString());
117 assertEquals("Deny", decision.getLeft().getStatus());