Allow xacml-pdp to use kafka
[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.Map;
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;
46
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();
52
53     @ClassRule
54     public static final TemporaryFolder policyFolder = new TemporaryFolder();
55
56     /**
57      * set up the tests.
58      *
59      * @throws Exception Should not have exceptions thrown.
60      */
61     @BeforeClass
62     public static void setup() throws Exception {
63         //
64         // Set up our temporary folder
65         //
66         XacmlPolicyUtils.FileCreator myCreator = policyFolder::newFile;
67         File propertiesFile = XacmlPolicyUtils
68             .copyXacmlPropertiesContents("src/test/resources/xacml.properties", properties, myCreator);
69         //
70         // Load XacmlApplicationServiceProvider service
71         //
72         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
73                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
74         //
75         // Look for our class instance and save it
76         //
77         for (XacmlApplicationServiceProvider application : applicationLoader) {
78             //
79             // Is it our service?
80             //
81             if (application instanceof TutorialApplication) {
82                 service = application;
83             }
84         }
85         //
86         // Tell the application to initialize based on the properties file
87         // we just built for it.
88         //
89         service.initialize(propertiesFile.toPath().getParent(), null);
90         //
91         // Now load the tutorial policies.
92         //
93         TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
94     }
95
96     @Test
97     public void testSingleDecision() throws CoderException, IOException {
98         //
99         // Load a Decision request
100         //
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));
105         //
106         // Test a decision - should start with a permit
107         //
108         Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
109         LOGGER.info("{}", gson.encode(decision.getLeft(), true));
110         assertEquals("Permit", decision.getLeft().getStatus());
111         //
112         // Check that there are attributes
113         //
114         assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
115                 .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
116         //
117         // This should be a "deny"
118         //
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());
124         //
125         // Check that there are attributes
126         //
127         assertThat(decision.getLeft().getAttributes()).isNotNull().hasSize(1)
128                 .containsKey(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE.stringValue());
129     }
130
131
132     @Test
133     public void testMultiDecision() throws CoderException, IOException {
134         //
135         // Load a Decision request
136         //
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));
141         //
142         // Test a decision - should start with a permit
143         //
144         Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
145         LOGGER.info("{}", gson.encode(decision.getLeft(), true));
146         assertEquals("multi", decision.getLeft().getStatus());
147         //
148         // Check that there no attributes for the overall response
149         //
150         assertThat(decision.getLeft().getAttributes()).isNull();
151         //
152         // Check that there are 7 decisions with attributes
153         //
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);
158     }
159
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);
167         switch (multiId) {
168             case "1", "2", "4":
169                 assertThat(permission.getStatus()).isEqualTo("Permit");
170                 return;
171             case "3", "5", "6", "7":
172                 assertThat(permission.getStatus()).isEqualTo("Deny");
173                 return;
174             default:
175                 //
176                 // Should not get here as we check the value range in line 168.
177                 // But CodeStyle wants a default.
178                 //
179                 break;
180         }
181     }
182 }