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;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.List;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.junit.jupiter.api.AfterAll;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
36 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
37 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
38 import org.onap.policy.clamp.acm.participant.http.utils.MockServerRest;
39 import org.onap.policy.common.utils.network.NetworkUtil;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.springframework.test.context.junit.jupiter.SpringExtension;
43 @ExtendWith(SpringExtension.class)
44 class AcHttpClientTest {
46 private static CommonTestData commonTestData;
48 private static int mockServerPort;
50 private static final String MOCK_URL = "http://localhost";
51 private static final String WRONG_URL = "http://wrong-url";
53 private static MockServerRest mockServer;
59 static void setUpMockServer() throws IOException, InterruptedException {
60 mockServerPort = NetworkUtil.allocPort();
61 mockServer = new MockServerRest(mockServerPort);
62 mockServer.validate();
63 commonTestData = new CommonTestData();
67 public static void stopServer() throws Exception {
73 void test_validRequest() {
74 // Add valid rest requests POST, GET
75 var configurationEntity = commonTestData.getConfigurationEntity();
76 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
78 var headers = commonTestData.getHeaders();
80 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
82 var client = new AcHttpClient();
83 assertDoesNotThrow(() -> client.run(configRequest, responseMap));
84 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
86 var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
87 assertThat(restResponseMap.getKey()).isEqualTo(200);
91 void test_invalidRequest() {
92 // Add rest requests Invalid POST, Valid GET
93 var configurationEntity = commonTestData.getInvalidConfigurationEntity();
94 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
96 var headers = commonTestData.getHeaders();
98 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
100 var client = new AcHttpClient();
101 assertDoesNotThrow(() -> client.run(configRequest, responseMap));
102 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
103 var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
104 assertThat(response.getKey()).isEqualTo(404);
108 void test_WrongUrl() {
109 // Add rest requests Invalid URL
110 var configurationEntity = commonTestData.getInvalidConfigurationEntity();
111 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
113 var headers = commonTestData.getHeaders();
115 new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
117 var client = new AcHttpClient();
118 assertDoesNotThrow(() -> client.run(configRequest, responseMap));
119 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
120 var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
121 assertThat(response.getKey()).isEqualTo(404);