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 java.io.IOException;
27 import java.security.KeyManagementException;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.Collections;
30 import java.util.HashMap;
32 import javax.ws.rs.client.Client;
33 import javax.ws.rs.client.ClientBuilder;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.client.Invocation;
36 import javax.ws.rs.client.WebTarget;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.Response.Status;
40 import org.glassfish.jersey.client.ClientConfig;
41 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
46 import org.onap.policy.common.endpoints.http.client.HttpClient;
47 import org.onap.policy.common.gson.GsonMessageBodyHandler;
48 import org.onap.policy.common.utils.network.NetworkUtil;
49 import org.onap.policy.models.decisions.concepts.DecisionRequest;
50 import org.onap.policy.models.decisions.concepts.DecisionResponse;
51 import org.onap.policy.models.errors.concepts.ErrorResponse;
52 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
53 import org.onap.policy.pdpx.main.startstop.Main;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class TestDecision {
59 private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
61 private static Main main;
64 * BeforeClass setup environment.
67 public static void beforeClass() {
68 System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
69 System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
70 main = startXacmlPdpService();
74 public static void after() throws PolicyXacmlPdpException {
75 stopXacmlPdpService(main);
79 public void testDecision_UnsupportedAction() throws KeyManagementException, NoSuchAlgorithmException,
80 ClassNotFoundException {
82 LOGGER.info("Running test testDecision_UnsupportedAction");
84 DecisionRequest request = new DecisionRequest();
85 request.setOnapName("DROOLS");
86 request.setAction("foo");
87 Map<String, Object> guard = new HashMap<String, Object>();
88 guard.put("actor", "foo");
89 guard.put("recipe", "bar");
90 guard.put("target", "somevnf");
91 guard.put("clname", "phoneyloop");
92 request.setResource(guard);
94 ErrorResponse response = getErrorDecision(request);
95 LOGGER.info("Response {}", response);
96 assertThat(response.getResponseCode()).isEqualTo(Status.BAD_REQUEST);
97 assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
101 public void testDecision_Guard() throws InterruptedException, IOException {
102 LOGGER.info("Running test testDecision_Guard");
104 DecisionRequest request = new DecisionRequest();
105 request.setOnapName("DROOLS");
106 request.setAction("guard");
107 Map<String, Object> guard = new HashMap<String, Object>();
108 guard.put("actor", "foo");
109 guard.put("recipe", "bar");
110 guard.put("target", "somevnf");
111 guard.put("clname", "phoneyloop");
112 request.setResource(guard);
114 DecisionResponse response = getDecision(request);
115 LOGGER.info("Response {}", response);
116 //assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
119 private static Main startXacmlPdpService() {
120 final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p",
121 "parameters/topic.properties"};
122 return new Main(XacmlPdpConfigParameters);
125 private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
129 private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException {
130 final ClientConfig clientConfig = new ClientConfig();
132 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
133 clientConfig.register(feature);
135 final Client client = ClientBuilder.newClient(clientConfig);
136 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision");
138 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
140 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
141 throw new IllegalStateException("Cannot connect to port 6969");
144 return invocationBuilder.post(Entity.json(request), DecisionResponse.class);
147 private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException,
148 NoSuchAlgorithmException, ClassNotFoundException {
150 HttpClient client = getNoAuthHttpClient();
152 Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
153 Response response = client.post("", entityRequest, Collections.emptyMap());
155 assertEquals(400, response.getStatus());
157 return HttpClient.getBody(response, ErrorResponse.class);
160 private HttpClient getNoAuthHttpClient()
161 throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
162 return HttpClient.factory.build(BusTopicParams.builder()
163 .clientName("testDecisionClient")
164 .serializationProvider(GsonMessageBodyHandler.class.getName())
165 .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6969)
166 .basePath("policy/pdpx/v1/decision")
167 .userName("healthcheck").password("zb!XztG34").managed(true).build());