fe307e4fe5ace8aeec9ef9ec4260f5df3a2fc73f
[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 java.io.IOException;
27 import java.security.KeyManagementException;
28 import java.security.NoSuchAlgorithmException;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.Map;
32
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;
41
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;
58
59 public class TestDecision {
60
61     private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
62
63     private static Main main;
64
65     /**
66      * BeforeClass setup environment.
67      */
68     @BeforeClass
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();
73     }
74
75     @AfterClass
76     public static void after() throws PolicyXacmlPdpException {
77         stopXacmlPdpService(main);
78     }
79
80     @Test
81     public void testDecision_UnsupportedAction() throws KeyManagementException, NoSuchAlgorithmException,
82         ClassNotFoundException {
83
84         LOGGER.info("Running test testDecision_UnsupportedAction");
85
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);
95
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");
100     }
101
102     @Test
103     public void testDecision_Guard() throws InterruptedException, IOException {
104         LOGGER.info("Running test testDecision_Guard");
105
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);
115
116         DecisionResponse response = getDecision(request);
117         LOGGER.info("Response {}", response);
118         //assertThat(response.getErrorMessage()).isEqualToIgnoringCase("No application for action foo");
119     }
120
121     private static Main startXacmlPdpService() {
122         final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
123         return new Main(XacmlPdpConfigParameters);
124     }
125
126     private static void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
127         main.shutdown();
128     }
129
130     private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException {
131         final ClientConfig clientConfig = new ClientConfig();
132
133         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
134         clientConfig.register(feature);
135
136         final Client client = ClientBuilder.newClient(clientConfig);
137         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision");
138
139         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
140
141         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
142             throw new IllegalStateException("Cannot connect to port 6969");
143         }
144
145         return invocationBuilder.post(Entity.json(request), DecisionResponse.class);
146     }
147
148     private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException,
149         NoSuchAlgorithmException, ClassNotFoundException {
150
151         HttpClient client = getNoAuthHttpClient();
152
153         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
154         Response response = client.post("", entityRequest, Collections.emptyMap());
155
156         assertEquals(400, response.getStatus());
157
158         return HttpClient.getBody(response, ErrorResponse.class);
159     }
160
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());
169     }
170
171
172 }