65685236026a45766212fd455129455d2386d15b
[policy/parent.git] / docs / xacml / tutorial / app / src / test / java / org / onap / policy / tutorial / tutorial / TutorialApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 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 java.io.File;
22 import java.io.IOException;
23 import java.util.Iterator;
24 import java.util.Properties;
25 import java.util.ServiceLoader;
26
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.junit.BeforeClass;
29 import org.junit.ClassRule;
30 import org.junit.Test;
31 import org.junit.rules.TemporaryFolder;
32 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
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 import com.att.research.xacml.api.Response;
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     @BeforeClass
58     public static void setup() throws Exception {
59         //
60         // Setup our temporary folder
61         //
62         XacmlPolicyUtils.FileCreator myCreator = (String filename) -> policyFolder.newFile(filename);
63         propertiesFile = XacmlPolicyUtils.copyXacmlPropertiesContents("src/test/resources/xacml.properties",
64                 properties, myCreator);
65         //
66         // Load XacmlApplicationServiceProvider service
67         //
68         ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
69                 ServiceLoader.load(XacmlApplicationServiceProvider.class);
70         //
71         // Look for our class instance and save it
72         //
73         Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
74         while (iterator.hasNext()) {
75             XacmlApplicationServiceProvider application = iterator.next();
76             //
77             // Is it our service?
78             //
79             if (application instanceof TutorialApplication) {
80                 service = application;
81             }
82         }
83         //
84         // Tell the application to initialize based on the properties file
85         // we just built for it.
86         //
87         service.initialize(propertiesFile.toPath().getParent(), new RestServerParameters());
88     }
89
90     @Test
91     public void test() throws CoderException, XacmlApplicationException, IOException {
92         //
93         // Now load the tutorial policies.
94         //
95         TestUtils.loadPolicies("src/test/resources/tutorial-policies.yaml", service);
96         //
97         // Load a Decision request
98         //
99         DecisionRequest decisionRequest = gson.decode(
100                 TextFileUtils
101                 .getTextFileAsString("src/test/resources/tutorial-decision-request.json"),
102                 DecisionRequest.class);
103         //
104         // Test a decision
105         //
106         Pair<DecisionResponse, Response> decision = service.makeDecision(decisionRequest, null);
107         LOGGER.info(decision.getLeft().toString());
108     }
109
110 }