ebeba52974d042db826194753725ba94c7311e43
[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
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;
37
38 @ExtendWith(SpringExtension.class)
39 class AcHttpClientTest {
40
41     private static CommonTestData commonTestData;
42
43     private static int mockServerPort;
44
45     private static final String MOCK_URL = "http://localhost";
46     private static final String WRONG_URL = "http://wrong-url";
47
48     private static MockServerRest mockServer;
49
50     /**
51      * Set up Mock server.
52      */
53     @BeforeAll
54     static void setUpMockServer() throws IOException, InterruptedException {
55         mockServerPort = NetworkUtil.allocPort();
56         mockServer = new MockServerRest(mockServerPort);
57         mockServer.validate();
58         commonTestData = new CommonTestData();
59     }
60
61     @AfterAll
62     public static void stopServer() throws Exception {
63         mockServer.close();
64         mockServer = null;
65     }
66
67     @Test
68     void test_validRequest() {
69         // Add valid rest requests POST, GET
70         var configurationEntity = commonTestData.getConfigurationEntity();
71
72         var headers = commonTestData.getHeaders();
73         var configRequest =
74                 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
75
76         var client = new AcHttpClient();
77         var responseMap = client.run(configRequest);
78         assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
79
80         var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
81         assertThat(restResponseMap.getKey()).isEqualTo(200);
82     }
83
84     @Test
85     void test_invalidRequest() {
86         // Add rest requests Invalid POST, Valid GET
87         var configurationEntity = commonTestData.getInvalidConfigurationEntity();
88
89         var headers = commonTestData.getHeaders();
90         var configRequest =
91                 new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
92
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);
98     }
99
100     @Test
101     void test_WrongUrl() {
102         // Add rest requests Invalid URL
103         var configurationEntity = commonTestData.getInvalidConfigurationEntity();
104
105         var headers = commonTestData.getHeaders();
106         var configRequest =
107                 new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
108
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);
114     }
115 }