834b2186ebf86916781f2c3c0369713d2f38022a
[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 import reactor.test.StepVerifier;
49
50 import java.io.IOException;
51 import java.lang.invoke.MethodHandles;
52 import java.nio.file.Path;
53 import java.util.Objects;
54
55 import static org.mockito.ArgumentMatchers.any;
56 import static org.mockito.Mockito.when;
57
58 @TestMethodOrder(MethodOrderer.MethodName.class)
59 @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ContextConfiguration(classes = TestConfig.class)
61 @TestPropertySource(properties = { //
62         "server.ssl.key-store=./config/keystore.jks", //
63         "app.webclient.trust-store=./config/truststore.jks", //
64         "app.webclient.trust-store-used=true", //
65         "app.vardata-directory=/tmp/pmstestv3", //
66         "app.filepath=", //
67         "app.s3.bucket=" // If this is set, S3 will be used to store data.
68 })
69 class PolicyControllerV3Test {
70     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
71
72     @Autowired
73     private ApplicationConfig applicationConfig;
74
75     @Autowired
76     private TestHelperTest testHelperTest;
77
78     @Autowired
79     private Rics rics;
80
81     @Autowired
82     private Policies policies;
83
84     @Autowired
85     private PolicyTypes policyTypes;
86
87     @Autowired
88     private Services services;
89
90     @Autowired
91     private MockA1ClientFactory a1ClientFactory;
92
93     @Autowired
94     private RappSimulatorController rAppSimulator;
95
96     @Autowired
97     private SecurityContext securityContext;
98
99     @Autowired
100     private OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
101
102     @LocalServerPort
103     private int port;
104
105     @MockitoSpyBean
106     private Helper helper;
107
108     private final String bearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."
109     + "eyJpc3MiOiJleGFtcGxlX2lzc3VlciIsInN1YiI6IjEyMzQ1Njc4OTAiLCJhdWQiOiJteWNsaWVudCIs"
110     + "ImV4cCI6MzAwMDAwMDAwMCwiY2xpZW50X2lkIjoibXljbGllbnQiLCJyb2xlIjoidXNlciJ9."
111     + "O5QN_SWN4J1mWKyXk_-PCvOA6GF3ypv1rSdg2uTb_Ls";
112
113     private final String emptyBearerToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IiJ9."
114     + "eyJpYXQiOjE1MTYyMzkwMjJ9.uE72OfhNzhIFuyHhZyI0eYVPG6QJ7s7A-SVeKsLubCQ";
115
116     @BeforeEach
117     void init() {
118         testHelperTest.port = port;
119         this.applicationConfig.setAuthProviderUrl(testHelperTest.baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
120         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.NONE);
121     }
122
123     @AfterEach
124     void reset() {
125         rics.clear();
126         policies.clear();
127         policyTypes.clear();
128         services.clear();
129         a1ClientFactory.reset();
130         this.rAppSimulator.getTestResults().clear();
131         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
132         this.securityContext.setAuthTokenFilePath(null);
133         this.openPolicyAgentSimulatorController.getTestResults().reset();
134     }
135
136     @AfterAll
137     static void clearTestDir() {
138         try {
139             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstestv3"));
140         } catch (Exception e) {
141             logger.warn("Could test directory : {}", e.getMessage());
142         }
143     }
144
145     @Test
146     @DisplayName("test Create Policy")
147     void testPostPolicy() throws Exception {
148         String nonRtRicId = "ric.1";
149         String policyTypeName = "type1_1.2.3";
150         String url = "/policies";
151         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
152         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
153         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
154         testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
155                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
156         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
157     }
158
159
160     @Test
161     @DisplayName("test Create Policy Success when schema validation set to FAIL")
162     void testPolicyTypeSchemaValidationFail() throws Exception {
163         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
164         String nonRtRicId = "ric.1";
165         String policyTypeName = "type1_1.2.3";
166         String url = "/policies";
167         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
168         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
169         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
170         testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
171                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
172         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
173     }
174
175
176     @Test
177     @DisplayName("test Create Policy Success when schema validation set to INFO")
178     void testPolicyTypeSchemaValidationInfo() throws Exception {
179         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
180         String nonRtRicId = "ric.1";
181         String policyTypeName = "type1_1.2.3";
182         String url = "/policies";
183         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
184         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
185         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
186         testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
187                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
188         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
189     }
190
191
192     @Test
193     @DisplayName("test Create Policy Success when schema validation set to WARN")
194     void testPolicyTypeSchemaValidationWarn() throws Exception {
195         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
196         String nonRtRicId = "ric.1";
197         String policyTypeName = "type1_1.2.3";
198         String url = "/policies";
199         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
200         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
201         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
202         testHelperTest.testSuccessResponse(responseMono, HttpStatus.CREATED, responseBody ->
203                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
204         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
205     }
206
207     @Test
208     @DisplayName("test bad Create Policy when schema validation set to FAIL")
209     void testBadPolicyTypeSchemaValidationFail() throws Exception {
210         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
211         String nonRtRicId = "ric.1";
212         String policyTypeName = "type1_1.2.3";
213         String url = "/policies";
214         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
215         String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
216         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
217         testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed");
218     }
219
220     @Test
221     @DisplayName("test bad Create Policy when schema validation set to INFO")
222     void testBadPolicyTypeSchemaValidationInfo() throws Exception {
223         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
224         String nonRtRicId = "ric.1";
225         String policyTypeName = "type1_1.2.3";
226         String url = "/policies";
227         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
228         String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
229         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
230         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
231     }
232
233     @Test
234     @DisplayName("test bad Create Policy when schema validation set to WARN")
235     void testBadPolicyTypeSchemaValidationWarn() throws Exception {
236         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
237         String nonRtRicId = "ric.1";
238         String policyTypeName = "type1_1.2.3";
239         String url = "/policies";
240         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
241         String policyBody = testHelperTest.postBadPolicyBody(nonRtRicId, policyTypeName, "");
242         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
243         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/"));
244     }
245
246     @Test
247     @DisplayName("test Create Policy with PolicyID sending")
248     void testPostPolicyWithPolicyID() throws Exception {
249         String nonRtRicId = "ric.1";
250         String policyTypeName = "type1_1.2.3";
251         String url = "/policies";
252         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
253         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "1");
254         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
255         testHelperTest.testSuccessHeader(responseMono, "location", headerValue -> headerValue.contains("https://localhost:" + port + "/a1-policy-management/v1/policies/1"));
256     }
257
258     @Test
259     @DisplayName("test Create Policy with existing policy id")
260     void testPostPolicyWithExistingPolicyID() throws Exception {
261         String nonRtRicId = "ric.1";
262         String policyTypeName = "type1_1.2.3";
263         String url = "/policies";
264         String policyId = "policy_5g";
265         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
266         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
267         testHelperTest.restClientV3().postForEntity(url, policyBody).block();
268         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
269         testHelperTest.testErrorCode(responseMono, HttpStatus.CONFLICT, "Policy already created with ID: " +policyId);
270     }
271
272     @Test
273     @DisplayName("test delete Policy")
274     void testDeletePolicy() throws Exception {
275         String nonRtRicId = "ric.1";
276         String policyTypeName = "type1_1.2.3";
277         String url = "/policies";
278         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
279         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
280         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
281         String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
282                 .get("location")).get(0).split("/");
283         String policyID = locationHeader[(locationHeader.length) - 1];
284         Mono<ResponseEntity<String>> responseMonoDelete = testHelperTest.restClientV3().deleteForEntity(url+"/" +policyID);
285         testHelperTest.testSuccessResponse(responseMonoDelete, HttpStatus.NO_CONTENT, responseBody -> true);
286     }
287
288     @Test
289     @DisplayName("test Create Policy schema validation fail case")
290     void testPolicySchemaValidationFail() throws Exception {
291         String nonRtRicId = "ric.1";
292         String policyTypeName = "type1_1.2.3";
293         String url = "/policies";
294         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
295         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.FALSE);
296         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
297         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
298         testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, " Schema validation failed");
299     }
300
301     @Test
302     @DisplayName("test Create Policy No Ric fail case")
303     void testCreatePolicyNoRic() throws Exception {
304         String policyTypeName = "type1_1.2.3";
305         String url = "/policies";
306         testHelperTest.addPolicyType(policyTypeName, " ");
307         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
308         String policyBody = testHelperTest.postPolicyBody("noRic", policyTypeName, "");
309         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
310         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, " Could not find ric: noRic");
311     }
312
313     @Test
314     @DisplayName("test Create Policy with No Policy Type fail case")
315     void testCreatePolicyNoPolicyType() throws Exception {
316         String policyTypeName = "type1_1.2.3";
317         String nonRtRicId = "ricOne";
318         String url = "/policies";
319         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
320         when(helper.jsonSchemaValidation(any())).thenReturn(Boolean.TRUE);
321         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, "noPolicyType", "");
322         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
323         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Could not find type: noPolicyType");
324     }
325
326     @Test
327     void testGetPolicyTypesNoRicFound() throws Exception{
328         String policyTypeName = "type1_1.2.3";
329         String nonRtRicId = "ricOne";
330         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
331         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().getForEntity("/policy-types" + "?nearRtRicId=\"noRic\"");
332         testHelperTest.testErrorCode(responseMono, HttpStatus.NOT_FOUND, "Near-RT RIC not Found using ID:");
333     }
334
335     @Test
336     @DisplayName("test get Policy")
337     void testGetPolicy() throws Exception {
338         String nonRtRicId = "ric.1";
339         String policyTypeName = "type1_1.2.3";
340         String url = "/policies";
341         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
342         String policyBody = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "");
343         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().postForEntity(url, policyBody);
344         String []locationHeader = Objects.requireNonNull(Objects.requireNonNull(responseMono.block()).getHeaders()
345                 .get("location")).get(0).split("/");
346         String policyID = locationHeader[(locationHeader.length) - 1];
347         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyID);
348         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
349                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5100\",\"qosId\":\"qos5100\"},\"qosObjectives\":{\"priorityLevel\":5100.0}}"));
350     }
351
352     @Test
353     @DisplayName("test get all Policies")
354     void testGetAllPolicies() throws Exception {
355         String nonRtRicIdOne = "ric.11";
356         String nonRtRicIdTwo = "ric.22";
357         String policyTypeName = "type1_1.2.3";
358         String url = "/policies";
359         testHelperTest.addPolicyType(policyTypeName, nonRtRicIdOne);
360         String policyBodyOne = testHelperTest.postPolicyBody(nonRtRicIdOne, policyTypeName, "policyOne");
361         testHelperTest.addPolicyType(policyTypeName, nonRtRicIdTwo);
362         String policyBodyTwo = testHelperTest.postPolicyBody(nonRtRicIdTwo, policyTypeName, "policyTwo");
363         testHelperTest.restClientV3().postForEntity(url, policyBodyOne).block();
364         testHelperTest.restClientV3().postForEntity(url, policyBodyTwo).block();
365         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
366         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
367                 responseBody.contains("[{\"policyId\":\"policyTwo\",\"nearRtRicId\":\"ric.22\"},{\"policyId\":\"policyOne\",\"nearRtRicId\":\"ric.11\"}]"));
368 }
369
370     @Test
371     @DisplayName("test get PolicyType")
372     void testGetPolicyType() throws Exception {
373         String nonRtRicId = "ric.1";
374         String policyTypeName = "type1_1.2.3";
375         String url = "/policy-types";
376         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
377         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/" +policyTypeName);
378         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> !(responseBody.isEmpty()));
379     }
380
381     @Test
382     @DisplayName("test get All PolicyTypes")
383     void testGetAllPolicyTypes() throws Exception {
384         String nonRtRicId = "ric.1";
385         String policyTypeName = "type1_1.2.3";
386         String url = "/policy-types";
387         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
388         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url);
389         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody -> responseBody.contains(
390                 "{\"policyTypeId\":\"type1_1.2.3\",\"nearRtRicId\":\"ric.1\"}]"
391         ));
392     }
393
394     @Test
395     @DisplayName("test update Policy")
396     void testUpdatePolicy() throws Exception {
397         String nonRtRicId = "ric.1";
398         String policyTypeName = "type1_1.2.3";
399         String url = "/policies";
400         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
401         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
402         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
403         String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
404                 "qos5200", "5200.0");
405         testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
406         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
407         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
408                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
409     }
410
411
412     @Test
413     @DisplayName("test Update Policy Success when schema validation set to FAIL")
414     void testUpdatePolicyTypeSchemaValidationFail() throws Exception {
415         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
416         String nonRtRicId = "ric.1";
417         String policyTypeName = "type1_1.2.3";
418         String url = "/policies";
419         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
420         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
421         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
422         String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
423                 "qos5200", "5200.0");
424         testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
425         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
426         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
427                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
428     }
429
430
431     @Test
432     @DisplayName("test Update Policy Success when schema validation set to INFO")
433     void testUpdatePolicyTypeSchemaValidationInfo() throws Exception {
434         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
435         String nonRtRicId = "ric.1";
436         String policyTypeName = "type1_1.2.3";
437         String url = "/policies";
438         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
439         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
440         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
441         String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
442                 "qos5200", "5200.0");
443         testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
444         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
445         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
446                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
447     }
448
449
450     @Test
451     @DisplayName("test Update Policy Success when schema validation set to WARN")
452     void testUpdatePolicyTypeSchemaValidationWarn() throws Exception {
453         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
454         String nonRtRicId = "ric.1";
455         String policyTypeName = "type1_1.2.3";
456         String url = "/policies";
457         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
458         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
459         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
460         String policyBodyForPut = testHelperTest.putPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
461                 "qos5200", "5200.0");
462         testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut).block();
463         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/policyOne");
464         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
465                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\"},\"qosObjectives\":{\"priorityLevel\":5200.0}"));
466     }
467
468     @Test
469     @DisplayName("test bad Update Policy when schema validation set to FAIL")
470     void testUpdateBadPolicyTypeSchemaValidationFail() throws Exception {
471         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.FAIL);
472         String nonRtRicId = "ric.1";
473         String policyTypeName = "type1_1.2.3";
474         String url = "/policies";
475         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
476         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
477         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
478         String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
479                 "qos5200", "5200.0", "bar");
480         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
481         testHelperTest.testErrorCode(responseMono, HttpStatus.BAD_REQUEST, "Policy Type Schema validation failed");
482     }
483
484     @Test
485     @DisplayName("test bad Update Policy when schema validation set to WARN")
486     void testUpdateBadPolicyTypeSchemaValidationWarn() throws Exception {
487         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.WARN);
488         String nonRtRicId = "ric.1";
489         String policyTypeName = "type1_1.2.3";
490         String url = "/policies";
491         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
492         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
493         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
494         String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
495                 "qos5200", "5200.0", "bar");
496         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
497         testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody ->
498                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}"));
499     }
500
501     @Test
502     @DisplayName("test bad Update Policy when schema validation set to INFO")
503     void testUpdateBadPolicyTypeSchemaValidationInfo() throws Exception {
504         this.applicationConfig.setValidatePolicyInstanceSchema(ApplicationConfig.ValidateSchema.INFO);
505         String nonRtRicId = "ric.1";
506         String policyTypeName = "type1_1.2.3";
507         String url = "/policies";
508         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
509         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, "policyOne");
510         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
511         String policyBodyForPut = testHelperTest.putBadPolicyBody(nonRtRicId, policyTypeName, "policyOne", "ue5200",
512                 "qos5200", "5200.0", "bar");
513         Mono<ResponseEntity<String>> responseMono = testHelperTest.restClientV3().putForEntity(url+"/policyOne", policyBodyForPut);
514         testHelperTest.testSuccessResponse(responseMono, HttpStatus.OK, responseBody ->
515                 responseBody.contains("{\"scope\":{\"ueId\":\"ue5200\",\"qosId\":\"qos5200\",\"foo\":\"bar\"},\"qosObjectives\":{\"priorityLevel\":5200.0}}"));
516     }
517
518     private void postPolicyWithTokenAndVerify(String clientId, String serviceId, String result) throws IOException {
519         testHelperTest.addPolicyType("type1_1.2.3", "ric.1");
520         String policyBody = testHelperTest.postPolicyBody("ric.1", "type1_1.2.3", "1");
521
522         if (serviceId != null) {
523             policyBody = policyBody.replace("\"serviceId\":\"\"", "\"serviceId\":\"" + serviceId + "\"");
524         }
525
526         StepVerifier.create(testHelperTest.restClientV3().postWithToken("/policies", policyBody, clientId)
527                     .then(testHelperTest.restClientV3().getForEntity("/policies" + ((serviceId != null || clientId != null) ? "?serviceId=" + result : ""))))
528                     .expectNextMatches(response -> response.getBody().contains("\"policyId\":\"1\""))
529                     .expectComplete()
530                     .verify();
531     }
532
533     @Test
534     @DisplayName("client_id VALID + service_id NULL/EMPTY = client_id")
535     void testPostPolicyWithToken() throws IOException {
536         postPolicyWithTokenAndVerify(bearerToken, null, "myclient");
537     }
538
539     @Test
540     @DisplayName("client_id VALID + service_id VALID = service_id")
541     void testPostPolicyWithTokenAndServiceID() throws IOException {
542         postPolicyWithTokenAndVerify(bearerToken, "notmyclient", "notmyclient");
543     }
544
545     @Test
546     @DisplayName("client_id NULL + service_id EMPTY = empty")
547     void testClientIdNullServiceIdEmpty() throws Exception {
548         postPolicyWithTokenAndVerify(null, null, "");
549     }
550
551     @Test
552     @DisplayName("client_id NULL + service_id VALID = service_id")
553     void testClientIdNullServiceIdValid() throws Exception {
554         postPolicyWithTokenAndVerify(null, "validServiceId", "validServiceId");
555     }
556
557     @Test
558     @DisplayName("client_id EMPTY + service_id NULL/EMPTY = empty")
559     void testClientIdEmptyServiceIdEmpty() throws Exception {
560         postPolicyWithTokenAndVerify(emptyBearerToken, null, "");
561     }
562
563     @Test
564     @DisplayName("client_id EMPTY + service_id VALID = service_id")
565     void testEmptyClientIdServiceIdValid() throws Exception {
566         postPolicyWithTokenAndVerify(emptyBearerToken, "validServiceId", "validServiceId");
567     }
568
569     @Test
570     @DisplayName("test get Policy Status")
571     void testGetPolicyStatus() throws Exception {
572         String nonRtRicId = "ric.1";
573         String policyTypeName = "type1_1.2.3";
574         String url = "/policies";
575         String policyId = "policyOne";
576         testHelperTest.addPolicyType(policyTypeName, nonRtRicId);
577         String policyBodyForPost = testHelperTest.postPolicyBody(nonRtRicId, policyTypeName, policyId);
578         testHelperTest.restClientV3().postForEntity(url, policyBodyForPost).block();
579         Mono<ResponseEntity<String>> responseMonoGet = testHelperTest.restClientV3().getForEntity(url+"/"+ policyId +"/status");
580         testHelperTest.testSuccessResponse(responseMonoGet, HttpStatus.OK, responseBody ->
581                 responseBody.contains("OK"));
582     }
583 }