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;
33 import javax.ws.rs.client.Client;
34 import javax.ws.rs.client.ClientBuilder;
35 import javax.ws.rs.client.Entity;
36 import javax.ws.rs.client.Invocation;
37 import javax.ws.rs.client.WebTarget;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
42 import org.glassfish.jersey.client.ClientConfig;
43 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
44 import org.junit.AfterClass;
45 import org.junit.BeforeClass;
46 import org.junit.Test;
47 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
48 import org.onap.policy.common.endpoints.http.client.HttpClient;
49 import org.onap.policy.common.gson.GsonMessageBodyHandler;
50 import org.onap.policy.common.utils.network.NetworkUtil;
51 import org.onap.policy.models.decisions.concepts.DecisionRequest;
52 import org.onap.policy.models.decisions.concepts.DecisionResponse;
53 import org.onap.policy.models.errors.concepts.ErrorResponse;
54 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
55 import org.onap.policy.pdpx.main.startstop.Main;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class TestDecision {
61 private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
63 private static Main main;
66 * BeforeClass setup environment.
69 public static void beforeClass() {
70 System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
71 System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
72 main = startXacmlPdpService();
76 public static void after() throws PolicyXacmlPdpException {
77 stopXacmlPdpService(main);
81 public void testDecision_UnsupportedAction() throws KeyManagementException, NoSuchAlgorithmException,
82 ClassNotFoundException {
84 LOGGER.info("Running test testDecision_UnsupportedAction");
86 DecisionRequest request = new DecisionRequest();
87 request.setOnapName("DROOLS");
88 request.setAction("foo");
89 Map<String, Object> guard = new HashMap<String, Object>();
90 guard.put("actor", "foo");
91 guard.put("recipe", "bar");
92 guard.put("target", "somevnf");
93 guard.put("clname", "phoneyloop");
94 request.setResource(guard);
96 ErrorResponse response = getErrorDecision(request);
97 LOGGER.info("Response {}", response);
98 assertThat(response.getResponseCode()).isEqualTo(Status.BAD_REQUEST);
99 assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
103 public void testDecision_Guard() throws InterruptedException, IOException {
104 LOGGER.info("Running test testDecision_Guard");
106 DecisionRequest request = new DecisionRequest();
107 request.setOnapName("DROOLS");
108 request.setAction("guard");
109 Map<String, Object> guard = new HashMap<String, Object>();
110 guard.put("actor", "foo");
111 guard.put("recipe", "bar");
112 guard.put("target", "somevnf");
113 guard.put("clname", "phoneyloop");
114 request.setResource(guard);
116 DecisionResponse response = getDecision(request);
117 LOGGER.info("Response {}", response);
118 //assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
121 private static Main startXacmlPdpService() {
122 final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
123 return new Main(XacmlPdpConfigParameters);
126 private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
130 private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException {
131 final ClientConfig clientConfig = new ClientConfig();
133 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
134 clientConfig.register(feature);
136 final Client client = ClientBuilder.newClient(clientConfig);
137 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision");
139 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
141 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
142 throw new IllegalStateException("Cannot connect to port 6969");
145 return invocationBuilder.post(Entity.json(request), DecisionResponse.class);
148 private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException,
149 NoSuchAlgorithmException, ClassNotFoundException {
151 HttpClient client = getNoAuthHttpClient();
153 Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
154 Response response = client.post("", entityRequest, Collections.emptyMap());
156 assertEquals(400, response.getStatus());
158 return HttpClient.getBody(response, ErrorResponse.class);
161 private HttpClient getNoAuthHttpClient()
162 throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
163 return HttpClient.factory.build(BusTopicParams.builder()
164 .clientName("testDecisionClient")
165 .serializationProvider(GsonMessageBodyHandler.class.getName())
166 .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6969)
167 .basePath("policy/pdpx/v1/decision")
168 .userName("healthcheck").password("zb!XztG34").managed(true).build());