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