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;
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 final String testMockUrl = "http://localhost";
52 private static MockServerRest mockServer;
58 static void setUpMockServer() throws IOException, InterruptedException {
59 mockServerPort = NetworkUtil.allocPort();
60 mockServer = new MockServerRest(mockServerPort);
61 mockServer.validate();
62 commonTestData = new CommonTestData();
66 public static void stopServer() throws Exception {
72 void test_validRequest() {
73 // Add valid rest requests POST, GET
74 var configurationEntity = commonTestData.getConfigurationEntity();
75 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
77 var headers = commonTestData.getHeaders();
79 new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
81 var client = new AcHttpClient(configRequest, responseMap);
82 assertDoesNotThrow(client::run);
83 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
85 var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
86 assertThat(restResponseMap.getKey()).isEqualTo(200);
90 void test_invalidRequest() {
91 // Add rest requests Invalid POST, Valid GET
92 var configurationEntity = commonTestData.getInvalidConfigurationEntity();
93 Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
95 var headers = commonTestData.getHeaders();
97 new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
99 var client = new AcHttpClient(configRequest, responseMap);
100 assertDoesNotThrow(client::run);
101 assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
102 var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
103 assertThat(response.getKey()).isEqualTo(404);