Add attribute return example into Tutorial
[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.assertj.core.api.Assertions.assertThat;
22 import static org.junit.Assert.assertEquals;
23
24 import com.att.research.xacml.api.Response;
25 import com.att.research.xacml.api.XACML3;
26 import java.io.File;
27 import java.io.IOException;
28 import java.util.Properties;
29 import java.util.ServiceLoader;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.junit.BeforeClass;
32 import org.junit.ClassRule;
33 import org.junit.Test;
34 import org.junit.rules.TemporaryFolder;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
37 import org.onap.policy.common.utils.resources.TextFileUtils;
38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
39 import org.onap.policy.models.decisions.concepts.DecisionResponse;
40 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
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 TutorialApplicationTest {
48     private static final Logger LOGGER = LoggerFactory.getLogger(TutorialApplicationTest.class);
49     private static Properties properties = new Properties();
50     private static File propertiesFile;
51     private static XacmlApplicationServiceProvider service;
52     private static StandardCoder gson = new StandardCoder();
53
54     @ClassRule
55     public static final TemporaryFolder policyFolder = new TemporaryFolder();
56
57     /**
58      * setup the tests.
59      *
60      * @throws Exception Should not have exceptions thrown.
61      */
62     @BeforeClass
63     public static void setup() throws Exception {
64         //
65         // Setup our temporary folder
66         //
67         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
68         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
69                 properties, myCreator);
70         //
71         // Load XacmlApplicationServiceProvider service
72         //
73         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
74                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
75         //
76         // Look for our class instance and save it
77         //
78         for (XacmlApplicationServiceProvider application : applicationLoader) {
79             //
80             // Is it our service?
81             //
82             if (application instanceof TutorialApplication) {
83                 service = application;
84             }
85         }
86         //
87         // Tell the application to initialize based on the properties file
88         // we just built for it.
89         //
90         service.initialize(propertiesFile.toPath().getParent(), null);
91     }
92
93     @Test
94     public void test() throws CoderException, XacmlApplicationException, IOException {
95         //
96         // Now load the tutorial policies.
97         //
98         TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
99         //
100         // Load a Decision request
101         //
102         DecisionRequest decisionRequest = gson.decode(
103                 TextFileUtils
104                 .getTextFileAsString("src/test/resources/tutorial-decision-request.json"),
105                 DecisionRequest.class);
106         LOGGER.info("{}", gson.encode(decisionRequest, true));
107         //
108         // Test a decision - should start with a permit
109         //
110         Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
111         LOGGER.info("{}", gson.encode(decision.getLeft(), true));
112         assertEquals("Permit", decision.getLeft().getStatus());
113         //
114         // Check that there are attributes
115         //
116         assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
117             .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
118         //
119         // This should be a deny
120         //
121         decisionRequest.getResource().put("user", "audit");
122         LOGGER.info("{}", gson.encode(decisionRequest, true));
123         decision = service.makeDecision(decisionRequest, null);
124         LOGGER.info("{}", gson.encode(decision.getLeft(), true));
125         assertEquals("Deny", decision.getLeft().getStatus());
126         //
127         // Check that there are attributes
128         //
129         assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
130             .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
131     }
132
133 }