e0a04a12a987c75b78791e6c67b744924120902e
[policy/clamp.git] /
1 /*-
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
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 import static org.mockserver.model.HttpRequest.request;
26 import static org.mockserver.model.HttpResponse.response;
27
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import javax.ws.rs.core.MediaType;
34 import org.apache.commons.lang3.tuple.Pair;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockserver.integration.ClientAndServer;
40 import org.mockserver.model.Parameter;
41 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
42 import org.onap.policy.clamp.acm.participant.http.main.models.ConfigurationEntity;
43 import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
44 import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
45 import org.onap.policy.common.utils.network.NetworkUtil;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.springframework.test.context.junit.jupiter.SpringExtension;
48
49 @ExtendWith(SpringExtension.class)
50 class AcHttpClientTest {
51
52     private static CommonTestData commonTestData;
53
54     private static int mockServerPort;
55
56     private final String testMockUrl = "http://localhost";
57
58     private static ClientAndServer mockServer;
59
60     /**
61      * Set up Mock server.
62      */
63     @BeforeAll
64     static void setUpMockServer() throws IOException {
65         mockServerPort = NetworkUtil.allocPort();
66         mockServer = ClientAndServer.startClientAndServer(mockServerPort);
67         commonTestData = new CommonTestData();
68         List<Parameter> queryParams = new ArrayList<>();
69         commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v)));
70
71         mockServer.when(request().withMethod("GET").withPath("/get")
72             .withHeader("Content-type", MediaType.APPLICATION_JSON)
73             .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams))
74             .respond(response().withBody("dummy body").withStatusCode(200)
75                 .withHeader("Content-Type", MediaType.APPLICATION_JSON));
76
77         mockServer.when(request().withMethod("POST").withPath("/post")
78             .withHeader("Content-type", MediaType.APPLICATION_JSON)
79             .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)
80             .withBody("Test body"))
81             .respond(response().withStatusCode(200));
82     }
83
84     @AfterAll
85     public static void stopServer() {
86         mockServer.stop();
87         mockServer = null;
88     }
89
90
91     @Test
92     void test_validRequest() {
93         //Add valid rest requests POST, GET
94         ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity();
95         Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
96
97         Map<String, String> headers = commonTestData.getHeaders();
98         ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
99             List.of(configurationEntity), 10);
100
101         AcHttpClient client = new AcHttpClient(configRequest, responseMap);
102         assertDoesNotThrow(client::run);
103         assertThat(responseMap).hasSize(2).containsKey(commonTestData
104             .restParamsWithGet().getRestRequestId());
105
106         Pair<Integer, String> restResponseMap = responseMap.get(commonTestData
107             .restParamsWithGet().getRestRequestId());
108         assertThat(restResponseMap.getKey()).isEqualTo(200);
109     }
110
111     @Test
112     void test_invalidRequest() {
113         //Add rest requests Invalid POST, Valid GET
114         ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity();
115         Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
116
117         Map<String, String> headers = commonTestData.getHeaders();
118         ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
119             List.of(configurationEntity), 10);
120
121         AcHttpClient client = new AcHttpClient(configRequest, responseMap);
122         assertDoesNotThrow(client::run);
123         assertThat(responseMap).hasSize(2).containsKey(commonTestData
124             .restParamsWithGet().getRestRequestId());
125         Pair<Integer, String> response = responseMap
126             .get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
127         assertThat(response.getKey()).isEqualTo(404);
128     }
129 }