2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2022 Nordix Foundation.
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.clamp.acm.participant.http.webclient;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25 import static org.mockserver.model.HttpRequest.request;
26 import static org.mockserver.model.HttpResponse.response;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
33 import javax.ws.rs.core.MediaType;
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockserver.integration.ClientAndServer;
40 import org.mockserver.model.Parameter;
41 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
42 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigurationEntity;
43 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
44 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
49 @ExtendWith(SpringExtension.class)
50 class AcHttpClientTest {
52 private static CommonTestData commonTestData;
54 private static int mockServerPort;
56 private final String testMockUrl = "http://localhost";
58 private static ClientAndServer mockServer;
64 static void setUpMockServer() throws IOException {
65 mockServerPort = NetworkUtil.allocPort();
66 mockServer = ClientAndServer.startClientAndServer(mockServerPort);
67 commonTestData = new CommonTestData();
68 List<Parameter> queryParams = new ArrayList<>();
69 commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v)));
71 mockServer.when(request().withMethod("GET").withPath("/get")
72 .withHeader("Content-type", MediaType.APPLICATION_JSON)
73 .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams))
74 .respond(response().withBody("dummy body").withStatusCode(200)
75 .withHeader("Content-Type", MediaType.APPLICATION_JSON));
77 mockServer.when(request().withMethod("POST").withPath("/post")
78 .withHeader("Content-type", MediaType.APPLICATION_JSON)
79 .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)
80 .withBody("Test body"))
81 .respond(response().withStatusCode(200));
85 public static void stopServer() {
92 void test_validRequest() {
93 //Add valid rest requests POST, GET
94 ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity();
95 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
97 Map<String, String> headers = commonTestData.getHeaders();
98 ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
99 List.of(configurationEntity), 10);
101 AcHttpClient client = new AcHttpClient(configRequest, responseMap);
102 assertDoesNotThrow(client::run);
103 assertThat(responseMap).hasSize(2).containsKey(commonTestData
104 .restParamsWithGet().getRestRequestId());
106 Pair<Integer, String> restResponseMap = responseMap.get(commonTestData
107 .restParamsWithGet().getRestRequestId());
108 assertThat(restResponseMap.getKey()).isEqualTo(200);
112 void test_invalidRequest() {
113 //Add rest requests Invalid POST, Valid GET
114 ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity();
115 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
117 Map<String, String> headers = commonTestData.getHeaders();
118 ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
119 List.of(configurationEntity), 10);
121 AcHttpClient client = new AcHttpClient(configRequest, responseMap);
122 assertDoesNotThrow(client::run);
123 assertThat(responseMap).hasSize(2).containsKey(commonTestData
124 .restParamsWithGet().getRestRequestId());
125 Pair<Integer, String> response = responseMap
126 .get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
127 assertThat(response.getKey()).isEqualTo(404);