349a6446dbc09ec938bd70e154be3913f7045020
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2022, 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.policy.client;
22
23 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.io.IOException;
27 import okhttp3.mockwebserver.Dispatcher;
28 import okhttp3.mockwebserver.MockResponse;
29 import okhttp3.mockwebserver.MockWebServer;
30 import okhttp3.mockwebserver.RecordedRequest;
31 import org.jetbrains.annotations.NotNull;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.clamp.acm.participant.policy.concepts.DeploymentSubGroup;
35 import org.onap.policy.clamp.acm.participant.policy.main.parameters.CommonTestData;
36 import org.onap.policy.clamp.acm.participant.policy.main.parameters.ParticipantPolicyParameters;
37 import org.onap.policy.clamp.acm.participant.policy.main.parameters.RestClientParameters;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
39 import org.springframework.web.reactive.function.client.WebClientException;
40
41 /**
42  * Tests for api and pap http clients.
43  */
44 class HttpClientTest {
45
46     private static PolicyApiHttpClient apiHttpClient;
47
48     private static PolicyPapHttpClient papHttpClient;
49
50     private static ToscaServiceTemplate serviceTemplate;
51
52     /**
53      * Set up Mock server.
54      */
55     @BeforeAll
56     static void setUpMockServer() throws IOException {
57         serviceTemplate = CommonTestData
58                 .getToscaServiceTemplate("clamp/acm/pmsh/funtional-pmsh-usecase-migration.yaml");
59         // Setup mock web server
60         int mockServerPort = 42545;
61         var mockServer = new MockWebServer();
62         mockServer.start(mockServerPort);
63         mockServer.setDispatcher(new Dispatcher() {
64             @NotNull
65             @Override
66             public MockResponse dispatch(@NotNull RecordedRequest request) {
67                 String path = request.getPath();
68                 assert path != null;
69                 if (path.equals("/policy/api/v1/policytypes") && "POST".equals(request.getMethod())) {
70                     return new MockResponse()
71                             .setResponseCode(200)
72                             .addHeader("Content-Type", "application/json");
73                 }
74                 if (path.equals("/policy/api/v1/policies") && "POST".equals(request.getMethod())
75                         && request.getBody().size() < 40) {
76                     return new MockResponse().setResponseCode(404);
77                 }
78                 if (path.equals("/policy/api/v1/policies") && "POST".equals(request.getMethod())) {
79                     return new MockResponse()
80                             .setResponseCode(200)
81                             .addHeader("Content-Type", "application/json");
82                 }
83                 if (path.matches("^/policy/api/v1/policytypes/[^/]+/versions/[^/]+$")
84                         && "DELETE".equals(request.getMethod())) {
85                     return new MockResponse().setResponseCode(200);
86                 }
87                 if (path.matches("^/policy/api/v1/policies/wrongPolicy/versions/[^/]+$")
88                         && "DELETE".equals(request.getMethod())) {
89                     return new MockResponse().setResponseCode(404);
90                 }
91                 if (path.matches("^/policy/api/v1/policies/[^/]+/versions/[^/]+$")
92                         && "DELETE".equals(request.getMethod())) {
93                     return new MockResponse().setResponseCode(200);
94                 }
95                 if (path.equals("/policy/pap/v1/pdps/deployments/batch")
96                         && "POST".equals(request.getMethod())) {
97                     return new MockResponse()
98                             .setResponseCode(200)
99                             .addHeader("Content-Type", "application/json");
100                 }
101                 return new MockResponse().setResponseCode(404);
102             }
103         });
104
105         // Setup mock api and pap client
106         var params = new ParticipantPolicyParameters();
107         var restClientParameters = getMockClientParameters(mockServerPort);
108         params.setPolicyApiParameters(restClientParameters);
109         params.setPolicyPapParameters(restClientParameters);
110
111         apiHttpClient = new PolicyApiHttpClient(params);
112         papHttpClient = new PolicyPapHttpClient(params);
113     }
114
115     @Test
116     void testCreatePolicy() {
117         assertDoesNotThrow(() -> apiHttpClient.createPolicy(serviceTemplate));
118     }
119
120     @Test
121     void testCreateBabPolicy() {
122         var badServiceTemplate = new ToscaServiceTemplate();
123         assertThrows(WebClientException.class, () -> apiHttpClient.createPolicy(badServiceTemplate));
124     }
125
126     @Test
127     void testCreatePolicyTypes() {
128         assertDoesNotThrow(() -> apiHttpClient.createPolicyType(serviceTemplate));
129     }
130
131     @Test
132     void testDeletePolicy() {
133         assertDoesNotThrow(() -> apiHttpClient.deletePolicy("dummyPolicy", "1.0.0"));
134     }
135
136     @Test
137     void testBadDeletePolicy() {
138         assertThrows(WebClientException.class, () -> apiHttpClient.deletePolicy("wrongPolicy", "1.0.0"));
139     }
140
141     @Test
142     void testDeletePolicyType() {
143         assertDoesNotThrow(() -> apiHttpClient.deletePolicyType("dummyPolicy", "1.0.0"));
144     }
145
146     @Test
147     void testDeployPolicy() {
148         assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
149                 DeploymentSubGroup.Action.POST));
150     }
151
152     @Test
153     void testUnDeployPolicy() {
154         assertDoesNotThrow(() -> papHttpClient.handlePolicyDeployOrUndeploy("dummyPolicy", "1.0.0",
155                 DeploymentSubGroup.Action.DELETE));
156     }
157
158     @Test
159     void testInvalidEndpoint() {
160         assertThrows(WebClientException.class, () -> apiHttpClient.executePost("/invalid", serviceTemplate));
161     }
162
163     private static RestClientParameters getMockClientParameters(int port) {
164         var params = new RestClientParameters();
165         params.setClientName("policyClient");
166         params.setHostname("localhost");
167         params.setUserName("user");
168         params.setPassword("pwd");
169         params.setPort(port);
170         return params;
171     }
172 }