97695bc9538b0afd96874a116ba4897bd6b81bf9
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v3;
22
23 import org.junit.jupiter.api.*;
24 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
25 import org.onap.ccsdk.oran.a1policymanagementservice.config.TestConfig;
26 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
27 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
28 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RappSimulatorController;
29 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
30 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
31 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
32 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
33 import org.onap.ccsdk.oran.a1policymanagementservice.util.v3.Helper;
34 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
35 import org.onap.ccsdk.oran.a1policymanagementservice.utils.v3.TestHelperTest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.test.context.SpringBootTest;
40 import org.springframework.boot.test.web.server.LocalServerPort;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.TestPropertySource;
45 import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
46 import org.springframework.util.FileSystemUtils;
47 import reactor.core.publisher.Mono;
48
49 import java.lang.invoke.MethodHandles;
50 import java.nio.file.Path;
51 import java.util.Objects;
52
53 import static org.mockito.ArgumentMatchers.any;
54 import static org.mockito.Mockito.when;
55
56 @TestMethodOrder(MethodOrderer.MethodName.class)
57 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
58 @ContextConfiguration(classes = TestConfig.class)
59 @TestPropertySource(properties = { //
60         "server.ssl.key-store=./config/keystore.jks", //
61         "app.webclient.trust-store=./config/truststore.jks", //
62         "app.webclient.trust-store-used=true", //
63         "app.vardata-directory=/tmp/pmstestv3", //
64         "app.filepath=", //
65         "app.s3.bucket=" // If this is set, S3 will be used to store data.
66 })
67 class PolicyControllerV3Test {
68     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
69
70     @Autowired
71     private ApplicationConfig applicationConfig;
72
73     @Autowired
74     private TestHelperTest testHelperTest;
75
76     @Autowired
77     private Rics rics;
78
79     @Autowired
80     private Policies policies;
81
82     @Autowired
83     private PolicyTypes policyTypes;
84
85     @Autowired
86     private Services services;
87
88     @Autowired
89     private MockA1ClientFactory a1ClientFactory;
90
91     @Autowired
92     private RappSimulatorController rAppSimulator;
93
94     @Autowired
95     private SecurityContext securityContext;
96
97     @Autowired
98     private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
99
100     @LocalServerPort
101     private int port;
102
103     @MockitoSpyBean
104     private Helper helper;
105
106     @BeforeEach
107     void init() {
108         testHelperTest.port = port;
109         this.applicationConfig.setAuthProviderUrl(testHelperTest.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
110     }
111
112     @AfterEach
113     void reset() {
114         rics.clear();
115         policies.clear();
116         policyTypes.clear();
117         services.clear();
118         a1ClientFactory.reset();
119         this.rAppSimulator.getTestResults().clear();
120         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
121         this.securityContext.setAuthTokenFilePath(null);
122         this.openPolicyAgentSimulatorController.getTestResults().reset();
123     }
124
125     @AfterAll
126     static void clearTestDir() {
127         try {
128             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
129         } catch (Exception e) {
130             logger.warn("Could test directory : {}", e.getMessage());
131         }
132     }
133
134     @Test
135     @DisplayName("test Create Policy")
136     void testPostPolicy() throws Exception {
137         String nonRtRicId = "ric.1";
138         String policyTypeName = "type1_1.2.3";
139         String url = "/policies";
140         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
141         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
142         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
143         testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
144                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
145         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
146     }
147
148     @Test
149     @DisplayName("test Create Policy with PolicyID sending")
150     void testPostPolicyWithPolicyID() throws Exception {
151         String nonRtRicId = "ric.1";
152         String policyTypeName = "type1_1.2.3";
153         String url = "/policies";
154         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
155         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "1");
156         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
157         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/1"));
158     }
159
160     @Test
161     @DisplayName("test Create Policy with existing policy id")
162     void testPostPolicyWithExistingPolicyID() throws Exception {
163         String nonRtRicId = "ric.1";
164         String policyTypeName = "type1_1.2.3";
165         String url = "/policies";
166         String policyId = "policy_5g";
167         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
168         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
169         testHelperTest.restClientV3().postForEntity(url, policyBody).block();
170         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
171         testHelperTest.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId);
172     }
173
174     @Test
175     @DisplayName("test delete Policy")
176     void testDeletePolicy() throws Exception {
177         String nonRtRicId = "ric.1";
178         String policyTypeName = "type1_1.2.3";
179         String url = "/policies";
180         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
181         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
182         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
183         String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
184                 .get("location")).get(0).split("/");
185         String policyID = locationHeader[(locationHeader.length) - 1];
186         Mono<ResponseEntity<String>> responseMonoDelete = testHelperTest.restClientV3().deleteForEntity(url+"/" +policyID);
187         testHelperTest.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true);
188     }
189
190     @Test
191     @DisplayName("test Create Policy schema validation fail case")
192     void testPolicySchemaValidationFail() throws Exception {
193         String nonRtRicId = "ric.1";
194         String policyTypeName = "type1_1.2.3";
195         String url = "/policies";
196         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
197         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE);
198         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
199         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
200         testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed");
201     }
202
203     @Test
204     @DisplayName("test Create Policy No Ric fail case")
205     void testCreatePolicyNoRic() throws Exception {
206         String policyTypeName = "type1_1.2.3";
207         String url = "/policies";
208         testHelperTest.addPolicyType(policyTypeName, " ");
209         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
210         String policyBody = testHelperTest.postPolicyBody("noRic", policyTypeName, "");
211         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
212         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic");
213     }
214
215     @Test
216     @DisplayName("test Create Policy with No Policy Type fail case")
217     void testCreatePolicyNoPolicyType() throws Exception {
218         String policyTypeName = "type1_1.2.3";
219         String nonRtRicId = "ricOne";
220         String url = "/policies";
221         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
222         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
223         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, "noPolicyType", "");
224         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
225         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType");
226     }
227
228     @Test
229     void testGetPolicyTypesNoRicFound() throws Exception{
230         String policyTypeName = "type1_1.2.3";
231         String nonRtRicId = "ricOne";
232         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
233         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().getForEntity("/policy-types" + "?nearRtRicId=\"noRic\"");
234         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:");
235     }
236
237     @Test
238     @DisplayName("test get Policy")
239     void testGetPolicy() throws Exception {
240         String nonRtRicId = "ric.1";
241         String policyTypeName = "type1_1.2.3";
242         String url = "/policies";
243         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
244         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
245         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
246         String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
247                 .get("location")).get(0).split("/");
248         String policyID = locationHeader[(locationHeader.length) - 1];
249         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyID);
250         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
251                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
252     }
253
254     @Test
255     @DisplayName("test get all Policies")
256     void testGetAllPolicies() throws Exception {
257         String nonRtRicIdOne = "ric.11";
258         String nonRtRicIdTwo = "ric.22";
259         String policyTypeName = "type1_1.2.3";
260         String url = "/policies";
261         testHelperTest.addPolicyType(policyTypeName, nonRtRicIdOne);
262         String policyBodyOne = testHelperTest.postPolicyBody(nonRtRicIdOne, policyTypeName, "policyOne");
263         testHelperTest.addPolicyType(policyTypeName, nonRtRicIdTwo);
264         String policyBodyTwo = testHelperTest.postPolicyBody(nonRtRicIdTwo, policyTypeName, "policyTwo");
265         testHelperTest.restClientV3().postForEntity(url, policyBodyOne).block();
266         testHelperTest.restClientV3().postForEntity(url, policyBodyTwo).block();
267         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
268         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
269                 responseBody.contains("[{\"policyId\":\"policyTwo\",\"nearRtRicId\":\"ric.22\"},{\"policyId\":\"policyOne\",\"nearRtRicId\":\"ric.11\"}]"));
270 }
271
272     @Test
273     @DisplayName("test get PolicyType")
274     void testGetPolicyType() throws Exception {
275         String nonRtRicId = "ric.1";
276         String policyTypeName = "type1_1.2.3";
277         String url = "/policy-types";
278         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
279         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyTypeName);
280         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> !(responseBody.isEmpty()));
281     }
282
283     @Test
284     @DisplayName("test get All PolicyTypes")
285     void testGetAllPolicyTypes() throws Exception {
286         String nonRtRicId = "ric.1";
287         String policyTypeName = "type1_1.2.3";
288         String url = "/policy-types";
289         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
290         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
291         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> responseBody.contains(
292                 "{\"policyTypeId\":\"type1_1.2.3\",\"nearRtRicId\":\"ric.1\"}]"
293         ));
294     }
295
296     @Test
297     @DisplayName("test update Policy")
298     void testUpdatePolicy() throws Exception {
299         String nonRtRicId = "ric.1";
300         String policyTypeName = "type1_1.2.3";
301         String url = "/policies";
302         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
303         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
304         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
305         String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
306                 "qos5200", "5200.0");
307         testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
308         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
309         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
310                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
311     }
312
313     @Test
314     @DisplayName("test get Policy Status")
315     void testGetPolicyStatus() throws Exception {
316         String nonRtRicId = "ric.1";
317         String policyTypeName = "type1_1.2.3";
318         String url = "/policies";
319         String policyId = "policyOne";
320         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
321         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
322         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
323         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/"+ policyId +"/status");
324         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
325                 responseBody.contains("OK"));
326     }
327 }