4f2e8b22edf614759890d27eb1e8f768d1c9a4d8
[policy/api.git] / main / src / test / java / org / onap / policy / api / main / rest / utils / CommonTestRestController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2022 Nordix Foundation. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.api.main.rest.utils;
24
25 import static org.junit.Assert.assertTrue;
26
27 import java.security.SecureRandom;
28 import javax.net.ssl.SSLContext;
29 import javax.net.ssl.TrustManager;
30 import javax.ws.rs.client.Client;
31 import javax.ws.rs.client.ClientBuilder;
32 import javax.ws.rs.client.Entity;
33 import javax.ws.rs.client.Invocation;
34 import javax.ws.rs.client.WebTarget;
35 import javax.ws.rs.core.Response;
36 import org.glassfish.jersey.client.ClientProperties;
37 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
38 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
39 import org.onap.policy.common.gson.GsonMessageBodyHandler;
40 import org.onap.policy.common.utils.coder.CoderException;
41 import org.onap.policy.common.utils.coder.StandardCoder;
42 import org.onap.policy.common.utils.coder.StandardYamlCoder;
43 import org.onap.policy.common.utils.network.NetworkUtil;
44 import org.onap.policy.common.utils.resources.ResourceUtils;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
46
47 /**
48  * Util class to perform REST unit tests.
49  *
50  */
51 public class CommonTestRestController {
52
53     protected static final String APP_JSON = "application/json";
54     protected static final String APP_YAML = "application/yaml";
55
56     protected static final StandardCoder standardCoder = new StandardCoder();
57     protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
58
59     protected static final String HTTPS_PREFIX = "https://localhost:";
60     protected static final String CONTEXT_PATH = "/policy/api/v1/";
61
62     protected void testSwagger(final int apiPort) throws Exception {
63         final Invocation.Builder invocationBuilder = sendHttpsRequest("/", "v3/api-docs", APP_JSON, apiPort);
64         final String resp = invocationBuilder.get(String.class);
65         assertTrue((resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\""));
66     }
67
68     protected Response createResource(String endpoint, String resourceName, int apiPort)
69         throws Exception {
70
71         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
72         String mediaType = getMediaType(resourceName);
73         mediaType = mediaType == null ?  APP_JSON : mediaType;
74
75         final Invocation.Builder invocationBuilder;
76         invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
77         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
78         return invocationBuilder.post(entity);
79     }
80
81     protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {
82
83         final Invocation.Builder invocationBuilder;
84         invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
85         return invocationBuilder.get();
86     }
87
88     protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {
89
90         final Invocation.Builder invocationBuilder;
91         invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
92         return invocationBuilder.delete();
93     }
94
95     protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
96         throws Exception {
97
98         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
99
100         final Invocation.Builder invocationBuilder;
101         invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
102         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
103         return invocationBuilder.put(entity);
104     }
105
106     protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
107         return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
108     }
109
110     protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
111         return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
112     }
113
114     protected Invocation.Builder sendHttpsRequest(
115             final String context, final String endpoint, String mediaType, int apiPort) throws Exception {
116
117         final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
118
119         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
120         sc.init(null, noopTrustManager, new SecureRandom());
121         final ClientBuilder clientBuilder =
122             ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
123         final Client client = clientBuilder.build();
124         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
125         client.register(feature);
126
127         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
128         if (APP_JSON.equalsIgnoreCase(mediaType)) {
129             client.register(GsonMessageBodyHandler.class);
130         } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
131             client.register(YamlMessageBodyHandler.class);
132         }
133
134         final WebTarget webTarget = client.target(HTTPS_PREFIX + apiPort + context + endpoint);
135
136         final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
137
138         if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
139             throw new IllegalStateException("cannot connect to port " + apiPort);
140         }
141         return invocationBuilder;
142     }
143
144     private ToscaServiceTemplate getRawServiceTemplate(String resourceName) throws CoderException {
145         ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
146         if (APP_JSON.equals(getMediaType(resourceName))) {
147             rawServiceTemplate = decodeJson(resourceName);
148         } else if (APP_YAML.equals(getMediaType(resourceName))) {
149             rawServiceTemplate = decodeYaml(resourceName);
150         }
151         return  rawServiceTemplate;
152     }
153
154     private String getMediaType(String resourceName) {
155         if (resourceName.endsWith(".json")) {
156             return APP_JSON;
157         } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
158             return APP_YAML;
159         }
160         return null;
161     }
162
163 }