4fda0983f5aebb14d9233448801b7a0728579b7f
[policy/xacml-pdp.git] / tutorials / tutorial-xacml-application / src / test / java / org / onap / policy / tutorial / tutorial / TutorialApplicationTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.tutorial.tutorial;
20
21 import static org.junit.Assert.assertEquals;
22
23 import com.att.research.xacml.api.Response;
24 import java.io.File;
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.utils.coder.CoderException;
34 import org.onap.policy.common.utils.coder.StandardCoder;
35 import org.onap.policy.common.utils.resources.TextFileUtils;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.models.decisions.concepts.DecisionResponse;
38 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
39 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
40 import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
41 import org.onap.policy.pdp.xacml.xacmltest.TestUtils;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class TutorialApplicationTest {
46     private static final Logger LOGGER = LoggerFactory.getLogger(TutorialApplicationTest.class);
47     private static Properties properties = new Properties();
48     private static File propertiesFile;
49     private static XacmlApplicationServiceProvider service;
50     private static StandardCoder gson = new StandardCoder();
51
52     @ClassRule
53     public static final TemporaryFolder policyFolder = new TemporaryFolder();
54
55     /**
56      * setup the tests.
57      *
58      * @throws Exception Should not have exceptions thrown.
59      */
60     @BeforeClass
61     public static void setup() throws Exception {
62         //
63         // Setup our temporary folder
64         //
65         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
66         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
67                 properties, myCreator);
68         //
69         // Load XacmlApplicationServiceProvider service
70         //
71         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
72                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
73         //
74         // Look for our class instance and save it
75         //
76         for (XacmlApplicationServiceProvider application : applicationLoader) {
77             //
78             // Is it our service?
79             //
80             if (application instanceof TutorialApplication) {
81                 service = application;
82             }
83         }
84         //
85         // Tell the application to initialize based on the properties file
86         // we just built for it.
87         //
88         service.initialize(propertiesFile.toPath().getParent(), null);
89     }
90
91     @Test
92     public void test() throws CoderException, XacmlApplicationException, IOException {
93         //
94         // Now load the tutorial policies.
95         //
96         TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
97         //
98         // Load a Decision request
99         //
100         DecisionRequest decisionRequest = gson.decode(
101                 TextFileUtils
102                 .getTextFileAsString("src/test/resources/tutorial-decision-request.json"),
103                 DecisionRequest.class);
104         //
105         // Test a decision - should start with a permit
106         //
107         Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
108         LOGGER.info(decision.getLeft().toString());
109         assertEquals("Permit", decision.getLeft().getStatus());
110         //
111         // This should be a deny
112         //
113         decisionRequest.getResource().put("user", "audit");
114         decision = service.makeDecision(decisionRequest, null);
115         LOGGER.info(decision.getLeft().toString());
116         assertEquals("Deny", decision.getLeft().getStatus());
117     }
118
119 }