44ef50848ef93746e5c2cfee9b205e4678f4590d
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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 String testMockUrl = "http://localhost";
57
58     private Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
59
60     private static ClientAndServer mockServer;
61
62     /**
63      * Set up Mock server.
64      */
65     @BeforeAll
66     static void setUpMockServer() throws IOException {
67         mockServerPort = NetworkUtil.allocPort();
68         mockServer = ClientAndServer.startClientAndServer(mockServerPort);
69         commonTestData = new CommonTestData();
70         List<Parameter> queryParams = new ArrayList<>();
71         commonTestData.getQueryParams().forEach((k, v) -> queryParams.add(new Parameter(k, v)));
72
73         mockServer.when(request().withMethod("GET").withPath("/get")
74             .withHeader("Content-type", MediaType.APPLICATION_JSON)
75             .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams))
76             .respond(response().withBody("dummy body").withStatusCode(200)
77                 .withHeader("Content-Type", MediaType.APPLICATION_JSON));
78
79         mockServer.when(request().withMethod("POST").withPath("/post")
80             .withHeader("Content-type", MediaType.APPLICATION_JSON)
81             .withHeader("Accept", MediaType.APPLICATION_JSON).withQueryStringParameters(queryParams)
82             .withBody("Test body"))
83             .respond(response().withStatusCode(200));
84     }
85
86     @AfterAll
87     public static void stopServer() {
88         mockServer.stop();
89         mockServer = null;
90     }
91
92
93     @Test
94     void test_validRequest() {
95         //Add valid rest requests POST, GET
96         ConfigurationEntity configurationEntity = commonTestData.getConfigurationEntity();
97
98         Map<String, String> headers = commonTestData.getHeaders();
99         ConfigRequest configRequest = new ConfigRequest(testMockUrl + ":" + mockServerPort, headers,
100             List.of(configurationEntity), 10);
101
102         AcHttpClient client = new AcHttpClient(configRequest, responseMap);
103         assertDoesNotThrow(client::run);
104         assertThat(responseMap).hasSize(2).containsKey(commonTestData
105             .restParamsWithGet().getRestRequestId());
106
107         Pair<Integer, String> restResponseMap = responseMap.get(commonTestData
108             .restParamsWithGet().getRestRequestId());
109         assertThat(restResponseMap.getKey()).isEqualTo(200);
110     }
111
112     @Test
113     void test_invalidRequest() {
114         //Add rest requests Invalid POST, Valid GET
115         ConfigurationEntity configurationEntity = commonTestData.getInvalidConfigurationEntity();
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 }