Started with test decision JSON objects.
[policy/xacml-pdp.git] / applications / guard / src / test / java / org / onap / policy / xacml / pdp / application / guard / GuardPdpApplicationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.guard;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatCode;
27
28 import com.att.research.xacml.util.XACMLProperties;
29 import com.google.common.io.Files;
30 import com.google.gson.Gson;
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Iterator;
40 import java.util.Properties;
41 import java.util.ServiceLoader;
42
43 import org.junit.Before;
44 import org.junit.ClassRule;
45 import org.junit.Test;
46 import org.junit.rules.TemporaryFolder;
47 import org.onap.policy.common.utils.resources.TextFileUtils;
48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
49 import org.onap.policy.models.decisions.serialization.DecisionRequestMessageBodyHandler;
50 import org.onap.policy.models.decisions.serialization.DecisionResponseMessageBodyHandler;
51 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class GuardPdpApplicationTest {
56
57     private static final Logger LOGGER = LoggerFactory.getLogger(GuardPdpApplicationTest.class);
58     private static Properties properties = new Properties();
59     private static File propertiesFile;
60     private static XacmlApplicationServiceProvider service;
61     private static DecisionRequest requestSinglePolicy;
62
63     private static Gson gsonDecisionRequest;
64     private static Gson gsonDecisionResponse;
65
66     @ClassRule
67     public static final TemporaryFolder policyFolder = new TemporaryFolder();
68
69     @Before
70     public void setUp() throws Exception {
71
72     }
73
74     @Test
75     public void testBasics() {
76         assertThatCode(() -> {
77             //
78             // Create our Gson builder
79             //
80             gsonDecisionRequest = new DecisionRequestMessageBodyHandler().getGson();
81             gsonDecisionResponse = new DecisionResponseMessageBodyHandler().getGson();
82             //
83             // Load Single Decision Request
84             //
85             requestSinglePolicy = gsonDecisionRequest.fromJson(
86                     TextFileUtils
87                         .getTextFileAsString("../../main/src/test/resources/decisions/decision.single.input.json"),
88                         DecisionRequest.class);
89             //
90             // Copy all the properties and root policies to the temporary folder
91             //
92             try (InputStream is = new FileInputStream("src/test/resources/xacml.properties")) {
93                 //
94                 // Load it in
95                 //
96                 properties.load(is);
97                 propertiesFile = policyFolder.newFile("xacml.properties");
98                 //
99                 // Copy the root policies
100                 //
101                 for (String root : XACMLProperties.getRootPolicyIDs(properties)) {
102                     //
103                     // Get a file
104                     //
105                     Path rootPath = Paths.get(properties.getProperty(root + ".file"));
106                     LOGGER.debug("Root file {} {}", rootPath, rootPath.getFileName());
107                     //
108                     // Construct new file name
109                     //
110                     File newRootPath = policyFolder.newFile(rootPath.getFileName().toString());
111                     //
112                     // Copy it
113                     //
114                     Files.copy(rootPath.toFile(), newRootPath);
115                     assertThat(newRootPath).exists();
116                     //
117                     // Point to where the new policy is in the temp dir
118                     //
119                     properties.setProperty(root + ".file", newRootPath.getAbsolutePath());
120                 }
121                 try (OutputStream os = new FileOutputStream(propertiesFile.getAbsolutePath())) {
122                     properties.store(os, "");
123                     assertThat(propertiesFile).exists();
124                 }
125             }
126             //
127             // Load service
128             //
129             ServiceLoader<XacmlApplicationServiceProvider> applicationLoader =
130                     ServiceLoader.load(XacmlApplicationServiceProvider.class);
131             //
132             // Iterate through them - I could store the object as
133             // XacmlApplicationServiceProvider pointer.
134             //
135             // Try this later.
136             //
137             StringBuilder strDump = new StringBuilder("Loaded applications:" + System.lineSeparator());
138             Iterator<XacmlApplicationServiceProvider> iterator = applicationLoader.iterator();
139             while (iterator.hasNext()) {
140                 XacmlApplicationServiceProvider application = iterator.next();
141                 //
142                 // Is it our service?
143                 //
144                 if (application instanceof GuardPdpApplication) {
145                     //
146                     // Should be the first and only one
147                     //
148                     assertThat(service).isNull();
149                     service = application;
150                 }
151                 strDump.append(application.applicationName());
152                 strDump.append(" supports ");
153                 strDump.append(application.supportedPolicyTypes());
154                 strDump.append(System.lineSeparator());
155             }
156             LOGGER.debug("{}", strDump);
157             //
158             // Tell it to initialize based on the properties file
159             // we just built for it.
160             //
161             service.initialize(propertiesFile.toPath().getParent());
162             //
163             // Make sure there's an application name
164             //
165             assertThat(service.applicationName()).isNotEmpty();
166             //
167             // Decisions
168             //
169             assertThat(service.actionDecisionsSupported().size()).isEqualTo(1);
170             assertThat(service.actionDecisionsSupported()).contains("guard");
171             //
172             // Ensure it has the supported policy types and
173             // can support the correct policy types.
174             //
175             assertThat(service.supportedPolicyTypes()).isNotEmpty();
176             assertThat(service.supportedPolicyTypes().size()).isEqualTo(2);
177             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.0"))
178                 .isTrue();
179             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.FrequencyLimiter", "1.0.1"))
180                 .isFalse();
181             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.MinMax", "1.0.0")).isTrue();
182             assertThat(service.canSupportPolicyType("onap.policies.controlloop.guard.MinMax", "1.0.1")).isFalse();
183             assertThat(service.canSupportPolicyType("onap.foo", "1.0.1")).isFalse();
184             //
185             // Ensure it supports decisions
186             //
187             assertThat(service.actionDecisionsSupported()).contains("guard");
188         }).doesNotThrowAnyException();
189     }
190 }