aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/api/main/rest/utils/CommonTestRestController.java
blob: 947ed6a50d93ff89eda84c8304d21d5abb8445bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy API
 * ================================================================================
 * Copyright (C) 2022 Nordix Foundation. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.api.main.rest.utils;

import static org.junit.Assert.assertTrue;

import java.security.SecureRandom;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;

/**
 * Util class to perform REST unit tests.
 *
 */
public class CommonTestRestController {

    protected static final String APP_JSON = "application/json";
    protected static final String APP_YAML = "application/yaml";

    protected static final StandardCoder standardCoder = new StandardCoder();
    protected static StandardYamlCoder standardYamlCoder = new StandardYamlCoder();

    protected static final String HTTPS_PREFIX = "https://localhost:";
    protected static final String CONTEXT_PATH = "/policy/api/v1/";

    protected void testSwagger(final int apiPort) throws Exception {
        final Invocation.Builder invocationBuilder = sendHttpsRequest(CONTEXT_PATH, "v3/api-docs", APP_JSON, apiPort);
        final String resp = invocationBuilder.get(String.class);
        assertTrue((resp).contains("{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Policy Framework Lifecycle API\""));
    }

    protected Response createResource(String endpoint, String resourceName, int apiPort)
        throws Exception {

        ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);
        String mediaType = getMediaType(resourceName);
        mediaType = mediaType == null ?  APP_JSON : mediaType;

        final Invocation.Builder invocationBuilder;
        invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
        Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
        return invocationBuilder.post(entity);
    }

    protected Response readResource(String endpoint, String mediaType, int apiPort) throws Exception {

        final Invocation.Builder invocationBuilder;
        invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
        return invocationBuilder.get();
    }

    protected Response deleteResource(String endpoint, String mediaType, int apiPort) throws Exception {

        final Invocation.Builder invocationBuilder;
        invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
        return invocationBuilder.delete();
    }

    protected Response updateResource(String endpoint, String resourceName, String mediaType, int apiPort)
        throws Exception {

        ToscaServiceTemplate rawServiceTemplate = getRawServiceTemplate(resourceName);

        final Invocation.Builder invocationBuilder;
        invocationBuilder = sendHttpsRequest(CONTEXT_PATH, endpoint, mediaType, apiPort);
        Entity<ToscaServiceTemplate> entity = Entity.entity(rawServiceTemplate, mediaType);
        return invocationBuilder.put(entity);
    }

    protected ToscaServiceTemplate decodeJson(String resourceName) throws CoderException {
        return standardCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
    }

    protected ToscaServiceTemplate decodeYaml(String resourceName) throws CoderException {
        return standardYamlCoder.decode(ResourceUtils.getResourceAsString(resourceName), ToscaServiceTemplate.class);
    }

    protected Invocation.Builder sendHttpsRequest(
            final String context, final String endpoint, String mediaType, int apiPort) throws Exception {

        final TrustManager[] noopTrustManager = NetworkUtil.getAlwaysTrustingManager();

        final SSLContext sc = SSLContext.getInstance("TLSv1.2");
        sc.init(null, noopTrustManager, new SecureRandom());
        final ClientBuilder clientBuilder =
            ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true);
        final Client client = clientBuilder.build();
        final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("policyadmin", "zb!XztG34");
        client.register(feature);

        client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true");
        if (APP_JSON.equalsIgnoreCase(mediaType)) {
            client.register(GsonMessageBodyHandler.class);
        } else if (APP_YAML.equalsIgnoreCase(mediaType)) {
            client.register(YamlMessageBodyHandler.class);
        }

        final WebTarget webTarget = client.target(HTTPS_PREFIX + apiPort + context + endpoint);

        final Invocation.Builder invocationBuilder = webTarget.request(mediaType);

        if (!NetworkUtil.isTcpPortOpen("localhost", apiPort, 60, 1000L)) {
            throw new IllegalStateException("cannot connect to port " + apiPort);
        }
        return invocationBuilder;
    }

    private ToscaServiceTemplate getRawServiceTemplate(String resourceName) throws CoderException {
        ToscaServiceTemplate rawServiceTemplate = new ToscaServiceTemplate();
        if (APP_JSON.equals(getMediaType(resourceName))) {
            rawServiceTemplate = decodeJson(resourceName);
        } else if (APP_YAML.equals(getMediaType(resourceName))) {
            rawServiceTemplate = decodeYaml(resourceName);
        }
        return  rawServiceTemplate;
    }

    private String getMediaType(String resourceName) {
        if (resourceName.endsWith(".json")) {
            return APP_JSON;
        } else if (resourceName.endsWith(".yaml") || resourceName.endsWith(".yml")) {
            return APP_YAML;
        }
        return null;
    }

}