2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 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;
25 import java.io.IOException;
26 import java.util.List;
27 import org.junit.jupiter.api.AfterAll;
28 import org.junit.jupiter.api.BeforeAll;
29 import org.junit.jupiter.api.Test;
30 import org.junit.jupiter.api.extension.ExtendWith;
31 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
32 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
33 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
34 import org.onap.policy.clamp.acm.participant.http.utils.MockServerRest;
35 import org.onap.policy.common.utils.network.NetworkUtil;
36 import org.springframework.test.context.junit.jupiter.SpringExtension;
38 @ExtendWith(SpringExtension.class)
39 class AcHttpClientTest {
41 private static CommonTestData commonTestData;
43 private static int mockServerPort;
45 private static final String MOCK_URL = "http://localhost";
46 private static final String WRONG_URL = "http://wrong-url";
48 private static MockServerRest mockServer;
54 static void setUpMockServer() throws IOException, InterruptedException {
55 mockServerPort = NetworkUtil.allocPort();
56 mockServer = new MockServerRest(mockServerPort);
57 mockServer.validate();
58 commonTestData = new CommonTestData();
62 public static void stopServer() throws Exception {
68 void test_validRequest() {
69 // Add valid rest requests POST, GET
70 var configurationEntity = commonTestData.getConfigurationEntity();
72 var headers = commonTestData.getHeaders();
74 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
76 var client = new AcHttpClient();
77 var responseMap = client.run(configRequest);
78 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
80 var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
81 assertThat(restResponseMap.getKey()).isEqualTo(200);
85 void test_invalidRequest() {
86 // Add rest requests Invalid POST, Valid GET
87 var configurationEntity = commonTestData.getInvalidConfigurationEntity();
89 var headers = commonTestData.getHeaders();
91 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
93 var client = new AcHttpClient();
94 var responseMap = client.run(configRequest);
95 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
96 var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
97 assertThat(response.getKey()).isEqualTo(404);
101 void test_WrongUrl() {
102 // Add rest requests Invalid URL
103 var configurationEntity = commonTestData.getInvalidConfigurationEntity();
105 var headers = commonTestData.getHeaders();
107 new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
109 var client = new AcHttpClient();
110 var responseMap = client.run(configRequest);
111 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
112 var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
113 assertThat(response.getKey()).isEqualTo(404);