2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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 String testMockUrl = "http://localhost";
58 private Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
60 private static ClientAndServer mockServer;
66 static void setUpMockServer() throws IOException {
67 mockServerPort = NetworkUtil.allocPort();
68 mockServer = ClientAndServer.startClientAndServer(mockServerPort);
69 commonTestData = new CommonTestData();
70 List<Parameter> queryParams = new ArrayList<>();
71 commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v)));
73 mockServer.when(request().withMethod("GET").withPath("/get")
74 .withHeader("Content-type", MediaType.APPLICATION_JSON)
75 .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams))
76 .respond(response().withBody("dummy body").withStatusCode(200)
77 .withHeader("Content-Type", MediaType.APPLICATION_JSON));
79 mockServer.when(request().withMethod("POST").withPath("/post")
80 .withHeader("Content-type", MediaType.APPLICATION_JSON)
81 .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)
82 .withBody("Test body"))
83 .respond(response().withStatusCode(200));
87 public static void stopServer() {
94 void test_validRequest() {
95 //Add valid rest requests POST, GET
96 ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity();
98 Map<String, String> headers = commonTestData.getHeaders();
99 ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
100 List.of(configurationEntity), 10);
102 AcHttpClient client = new AcHttpClient(configRequest, responseMap);
103 assertDoesNotThrow(client::run);
104 assertThat(responseMap).hasSize(2).containsKey(commonTestData
105 .restParamsWithGet().getRestRequestId());
107 Pair<Integer, String> restResponseMap = responseMap.get(commonTestData
108 .restParamsWithGet().getRestRequestId());
109 assertThat(restResponseMap.getKey()).isEqualTo(200);
113 void test_invalidRequest() {
114 //Add rest requests Invalid POST, Valid GET
115 ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity();
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);