d8e0c9b587ef37cb89a2fc77f4a32ebb1664094a
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.http.webclient;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
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;
42
43 @ExtendWith(SpringExtension.class)
44 class AcHttpClientTest {
45
46     private static CommonTestData commonTestData;
47
48     private static int mockServerPort;
49
50     private final String testMockUrl = "http://localhost";
51
52     private static MockServerRest mockServer;
53
54     /**
55      * Set up Mock server.
56      */
57     @BeforeAll
58     static void setUpMockServer() throws IOException, InterruptedException {
59         mockServerPort = NetworkUtil.allocPort();
60         mockServer = new MockServerRest(mockServerPort);
61         mockServer.validate();
62         commonTestData = new CommonTestData();
63     }
64
65     @AfterAll
66     public static void stopServer() throws Exception {
67         mockServer.close();
68         mockServer = null;
69     }
70
71     @Test
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<>();
76
77         var headers = commonTestData.getHeaders();
78         var configRequest =
79                 new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
80
81         var client = new AcHttpClient();
82         assertDoesNotThrow(() -> client.run(configRequest, responseMap));
83         assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
84
85         var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
86         assertThat(restResponseMap.getKey()).isEqualTo(200);
87     }
88
89     @Test
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<>();
94
95         var headers = commonTestData.getHeaders();
96         var configRequest =
97                 new ConfigRequest(testMockUrl + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
98
99         var client = new AcHttpClient();
100         assertDoesNotThrow(() -> client.run(configRequest, responseMap));
101         assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
102         var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
103         assertThat(response.getKey()).isEqualTo(404);
104     }
105 }