5f7eb78cb0196ee29bf5692c116314d81c05551b
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestDecision.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.nio.file.StandardCopyOption;
35 import java.security.KeyManagementException;
36 import java.security.NoSuchAlgorithmException;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 import javax.ws.rs.client.Entity;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import javax.ws.rs.core.Response.Status;
45
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.ClassRule;
49 import org.junit.Test;
50 import org.junit.rules.TemporaryFolder;
51 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
52 import org.onap.policy.common.endpoints.http.client.HttpClient;
53 import org.onap.policy.common.gson.GsonMessageBodyHandler;
54 import org.onap.policy.common.utils.network.NetworkUtil;
55 import org.onap.policy.models.decisions.concepts.DecisionRequest;
56 import org.onap.policy.models.decisions.concepts.DecisionResponse;
57 import org.onap.policy.models.errors.concepts.ErrorResponse;
58 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
59 import org.onap.policy.pdpx.main.parameters.RestServerBuilder;
60 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
61 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
62 import org.onap.policy.pdpx.main.startstop.Main;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
65
66 public class TestDecision {
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
69
70     private static int port;
71     private static Main main;
72     private static HttpClient client;
73
74     @ClassRule
75     public static final TemporaryFolder appsFolder = new TemporaryFolder();
76
77     /**
78      * BeforeClass setup environment.
79      * @throws IOException Cannot create temp apps folder
80      * @throws Exception exception if service does not start
81      */
82     @BeforeClass
83     public static void beforeClass() throws Exception {
84         System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
85         System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
86
87         port = NetworkUtil.allocPort();
88
89         //
90         // Copy test directory over of the application directories
91         //
92         Path src = Paths.get("src/test/resources/apps");
93         File apps = appsFolder.newFolder("apps");
94         Files.walk(src).forEach(source -> {
95             copy(source, apps.toPath().resolve(src.relativize(source)));
96         });
97         //
98         // Get the parameters file correct.
99         //
100         RestServerParameters rest = new RestServerParameters(new RestServerBuilder()
101                 .setHost("0.0.0.0").setPort(port).setUserName("healthcheck").setPassword("zb!XztG34"));
102         XacmlPdpParameterGroup params = new XacmlPdpParameterGroup("XacmlPdpGroup", rest, apps.getAbsolutePath());
103         final Gson gson = new GsonBuilder().create();
104         File fileParams = appsFolder.newFile("params.json");
105         String jsonParams = gson.toJson(params);
106         LOGGER.info("Creating new params: {}", jsonParams);
107         Files.write(fileParams.toPath(), jsonParams.getBytes());
108         //
109         // Start the service
110         //
111         main = startXacmlPdpService(fileParams);
112         //
113         // Make sure it is running
114         //
115         if (!NetworkUtil.isTcpPortOpen("localhost", port, 20, 1000L)) {
116             throw new IllegalStateException("Cannot connect to port " + port);
117         }
118         //
119         // Create a client
120         //
121         client = getNoAuthHttpClient();
122     }
123
124     @AfterClass
125     public static void after() throws PolicyXacmlPdpException {
126         stopXacmlPdpService(main);
127     }
128
129     @Test
130     public void testDecision_UnsupportedAction() throws Exception {
131
132         LOGGER.info("Running test testDecision_UnsupportedAction");
133
134         DecisionRequest request = new DecisionRequest();
135         request.setOnapName("DROOLS");
136         request.setAction("foo");
137         Map<String, Object> guard = new HashMap<String, Object>();
138         guard.put("actor", "foo");
139         guard.put("recipe", "bar");
140         guard.put("target", "somevnf");
141         guard.put("clname", "phoneyloop");
142         request.setResource(guard);
143
144         ErrorResponse response = getErrorDecision(request);
145         LOGGER.info("Response {}", response);
146         assertThat(response.getResponseCode()).isEqualTo(Status.BAD_REQUEST);
147         assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
148     }
149
150     @Test
151     public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException,
152         ClassNotFoundException {
153
154         LOGGER.info("Running test testDecision_Guard");
155
156         DecisionRequest request = new DecisionRequest();
157         request.setOnapName("DROOLS");
158         request.setAction("guard");
159         Map<String, Object> guard = new HashMap<String, Object>();
160         guard.put("actor", "foo");
161         guard.put("recipe", "bar");
162         guard.put("target", "somevnf");
163         guard.put("clname", "phoneyloop");
164         request.setResource(guard);
165
166         DecisionResponse response = getDecision(request);
167         LOGGER.info("Response {}", response);
168         assertThat(response.getStatus()).isEqualTo("Permit");
169     }
170
171     private static Main startXacmlPdpService(File params) throws PolicyXacmlPdpException {
172         final String[] XacmlPdpConfigParameters = {"-c", params.getAbsolutePath(), "-p",
173             "parameters/topic.properties"};
174         return new Main(XacmlPdpConfigParameters);
175     }
176
177     private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
178         main.shutdown();
179     }
180
181     private DecisionResponse getDecision(DecisionRequest request) {
182
183         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
184         Response response = client.post("", entityRequest, Collections.emptyMap());
185
186         assertEquals(200, response.getStatus());
187
188         return HttpClient.getBody(response, DecisionResponse.class);
189     }
190
191     private ErrorResponse getErrorDecision(DecisionRequest request) {
192
193         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
194         Response response = client.post("", entityRequest, Collections.emptyMap());
195
196         assertEquals(400, response.getStatus());
197
198         return HttpClient.getBody(response, ErrorResponse.class);
199     }
200
201     private static HttpClient getNoAuthHttpClient()
202             throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
203         return HttpClient.factory.build(BusTopicParams.builder()
204                 .clientName("testDecisionClient")
205                 .serializationProvider(GsonMessageBodyHandler.class.getName())
206                 .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(port)
207                 .basePath("policy/pdpx/v1/decision")
208                 .userName("healthcheck").password("zb!XztG34").managed(true).build());
209     }
210
211     private static void copy(Path source, Path dest) {
212         try {
213             LOGGER.info("Copying {} to {}", source, dest);
214             Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
215         } catch (IOException e) {
216             LOGGER.error("Failed to copy {} to {}", source, dest);
217         }
218     }
219
220 }