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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
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;
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;
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;
66 public class TestDecision {
68 private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
70 private static int port;
71 private static Main main;
72 private static HttpClient client;
75 public static final TemporaryFolder appsFolder = new TemporaryFolder();
78 * BeforeClass setup environment.
79 * @throws IOException Cannot create temp apps folder
80 * @throws Exception exception if service does not start
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");
87 port = NetworkUtil.allocPort();
90 // Copy test directory over of the application directories
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)));
98 // Get the parameters file correct.
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());
111 main = startXacmlPdpService(fileParams);
113 // Make sure it is running
115 if (!NetworkUtil.isTcpPortOpen("localhost", port, 20, 1000L)) {
116 throw new IllegalStateException("Cannot connect to port " + port);
121 client = getNoAuthHttpClient();
125 public static void after() throws PolicyXacmlPdpException {
126 stopXacmlPdpService(main);
130 public void testDecision_UnsupportedAction() throws Exception {
132 LOGGER.info("Running test testDecision_UnsupportedAction");
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);
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");
151 public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException,
152 ClassNotFoundException {
154 LOGGER.info("Running test testDecision_Guard");
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);
166 DecisionResponse response = getDecision(request);
167 LOGGER.info("Response {}", response);
168 assertThat(response.getStatus()).isEqualTo("Permit");
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);
177 private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
181 private DecisionResponse getDecision(DecisionRequest request) {
183 Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
184 Response response = client.post("", entityRequest, Collections.emptyMap());
186 assertEquals(200, response.getStatus());
188 return HttpClient.getBody(response, DecisionResponse.class);
191 private ErrorResponse getErrorDecision(DecisionRequest request) {
193 Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
194 Response response = client.post("", entityRequest, Collections.emptyMap());
196 assertEquals(400, response.getStatus());
198 return HttpClient.getBody(response, ErrorResponse.class);
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());
211 private static void copy(Path source, Path dest) {
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);