56170eb3ed37653cca052e3eb97b6e40ac3897c4
[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 static final String MOCK_URL = "http://localhost";
51     private static final String WRONG_URL = "http://wrong-url";
52
53     private static MockServerRest mockServer;
54
55     /**
56      * Set up Mock server.
57      */
58     @BeforeAll
59     static void setUpMockServer() throws IOException, InterruptedException {
60         mockServerPort = NetworkUtil.allocPort();
61         mockServer = new MockServerRest(mockServerPort);
62         mockServer.validate();
63         commonTestData = new CommonTestData();
64     }
65
66     @AfterAll
67     public static void stopServer() throws Exception {
68         mockServer.close();
69         mockServer = null;
70     }
71
72     @Test
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<>();
77
78         var headers = commonTestData.getHeaders();
79         var configRequest =
80                 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
81
82         var client = new AcHttpClient();
83         assertDoesNotThrow(() -> client.run(configRequest, responseMap));
84         assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
85
86         var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
87         assertThat(restResponseMap.getKey()).isEqualTo(200);
88     }
89
90     @Test
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<>();
95
96         var headers = commonTestData.getHeaders();
97         var configRequest =
98                 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
99
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);
105     }
106
107     @Test
108     void test_WrongUrl() {
109         // Add rest requests Invalid URL
110         var configurationEntity = commonTestData.getInvalidConfigurationEntity();
111         Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
112
113         var headers = commonTestData.getHeaders();
114         var configRequest =
115                 new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
116
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);
122     }
123 }