Add REST Apis for Tosca Node template operations
[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 java.security.SecureRandom;
26 import javax.net.ssl.SSLContext;
27 import javax.net.ssl.TrustManager;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.Response;
34 import org.glassfish.jersey.client.ClientProperties;
35 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
37 import org.onap.policy.common.gson.GsonMessageBodyHandler;
38 import org.onap.policy.common.utils.coder.CoderException;
39 import org.onap.policy.common.utils.coder.StandardCoder;
40 import org.onap.policy.common.utils.coder.StandardYamlCoder;
41 import org.onap.policy.common.utils.network.NetworkUtil;
42 import org.onap.policy.common.utils.resources.ResourceUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
44
45 /**
46  * Util class to perform REST unit tests.
47  *
48  */
49 public class CommonTestRestController {
50
51     protected static final String APP_JSON = "application/json";
52     protected static final String APP_YAML = "application/yaml";
53
54     protected static final StandardCoder standardCoder = new StandardCoder();
55     protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();
56
57
58     protected Response createResource(String endpoint, String resourceName, int apiPort)
59         throws Exception {
60
61         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
62         String mediaType = getMediaType(resourceName);
63         mediaType = mediaType == null ?  APP_JSON : mediaType;
64
65         final Invocation.Builder invocationBuilder;
66         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
67         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
68         return invocationBuilder.post(entity);
69     }
70
71     protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {
72
73         final Invocation.Builder invocationBuilder;
74         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
75         return invocationBuilder.get();
76     }
77
78     protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {
79
80         final Invocation.Builder invocationBuilder;
81         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
82         return invocationBuilder.delete();
83     }
84
85     protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
86         throws Exception {
87
88         ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
89
90         final Invocation.Builder invocationBuilder;
91         invocationBuilder = sendHttpsRequest(endpoint, mediaType, apiPort);
92         Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
93         return invocationBuilder.put(entity);
94     }
95
96     protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
97         return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
98     }
99
100     protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
101         return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
102     }
103
104     protected Invocation.Builder sendHttpsRequest(final String endpoint, String mediaType, int apiPort)
105         throws Exception {
106
107         final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();
108
109         final SSLContext sc = SSLContext.getInstance("TLSv1.2");
110         sc.init(null, noopTrustManager, new SecureRandom());
111         final ClientBuilder clientBuilder =
112             ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
113         final Client client = clientBuilder.build();
114         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
115         client.register(feature);
116
117         client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
118         if (APP_JSON.equalsIgnoreCase(mediaType)) {
119             client.register(GsonMessageBodyHandler.class);
120         } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
121             client.register(YamlMessageBodyHandler.class);
122         }
123
124         final WebTarget webTarget = client.target("https://localhost:" + apiPort + "/policy/api/v1/" + endpoint);
125
126         final Invocation.Builder invocationBuilder = webTarget.request(mediaType);
127
128         if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
129             throw new IllegalStateException("cannot connect to port " + apiPort);
130         }
131         return invocationBuilder;
132     }
133
134     private ToscaServiceTemplate getRawServiceTemplate(String resourceName) throws CoderException {
135         ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
136         if (APP_JSON.equals(getMediaType(resourceName))) {
137             rawServiceTemplate = decodeJson(resourceName);
138         } else if (APP_YAML.equals(getMediaType(resourceName))) {
139             rawServiceTemplate = decodeYaml(resourceName);
140         }
141         return  rawServiceTemplate;
142     }
143
144     private String getMediaType(String resourceName) {
145         if (resourceName.endsWith(".json")) {
146             return APP_JSON;
147         } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
148             return APP_YAML;
149         }
150         return null;
151     }
152
153 }