2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2022 Nordix Foundation. 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.io.PrintStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.time.Duration;
41 import java.time.Instant;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import org.json.JSONObject;
47 import org.junit.jupiter.api.AfterAll;
48 import org.junit.jupiter.api.AfterEach;
49 import org.junit.jupiter.api.Test;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
52 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
53 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
59 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
71 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
72 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
73 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
74 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
75 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.springframework.beans.factory.annotation.Autowired;
79 import org.springframework.boot.test.context.SpringBootTest;
80 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
81 import org.springframework.boot.test.context.TestConfiguration;
82 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
83 import org.springframework.boot.web.server.LocalServerPort;
84 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
85 import org.springframework.context.ApplicationContext;
86 import org.springframework.context.annotation.Bean;
87 import org.springframework.http.HttpStatus;
88 import org.springframework.http.MediaType;
89 import org.springframework.http.ResponseEntity;
90 import org.springframework.test.context.TestPropertySource;
91 import org.springframework.util.FileSystemUtils;
92 import org.springframework.web.reactive.function.client.WebClientResponseException;
94 import reactor.core.publisher.Mono;
95 import reactor.test.StepVerifier;
96 import reactor.util.annotation.Nullable;
98 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
99 @TestPropertySource(properties = { //
100 "server.ssl.key-store=./config/keystore.jks", //
101 "app.webclient.trust-store=./config/truststore.jks", //
102 "app.webclient.trust-store-used=true", //
103 "app.vardata-directory=/tmp/pmstest", //
105 "app.s3.bucket=" // If this is set, S3 will be used to store data.
107 class ApplicationTest {
108 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
111 ApplicationContext context;
117 private Policies policies;
120 private PolicyTypes policyTypes;
123 MockA1ClientFactory a1ClientFactory;
126 RicSupervision supervision;
129 ApplicationConfig applicationConfig;
135 RappSimulatorController rAppSimulator;
138 RefreshConfigTask refreshConfigTask;
141 SecurityContext securityContext;
143 private static Gson gson = new GsonBuilder().create();
146 * Overrides the BeanFactory.
149 static class TestBeanFactory {
152 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
153 return new MockA1ClientFactory(appConfig, types);
157 public ServiceSupervision getServiceSupervision(@Autowired Services services,
158 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
159 Duration checkInterval = Duration.ofMillis(1);
160 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
164 public ServletWebServerFactory servletContainer() {
165 return new TomcatServletWebServerFactory();
178 a1ClientFactory.reset();
179 this.rAppSimulator.getTestResults().clear();
180 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
181 this.securityContext.setAuthTokenFilePath(null);
185 static void clearTestDir() {
187 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
188 } catch (Exception e) {
189 logger.warn("Could test directory : {}", e.getMessage());
194 void verifyNoRicLocks() {
195 for (Ric ric : this.rics.getRics()) {
196 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
197 grant.unlockBlocking();
198 assertThat(ric.getLock().getLockCounter()).isZero();
199 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
204 void generateApiDoc() throws IOException {
205 String url = "https://localhost:" + this.port + "/v3/api-docs";
206 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
207 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
208 JSONObject jsonObj = new JSONObject(resp.getBody());
209 assertThat(jsonObj.remove("servers")).isNotNull();
211 String indented = (jsonObj).toString(4);
212 String docDir = "api/";
213 Files.createDirectories(Paths.get(docDir));
214 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
220 void testPersistencyPolicies() throws Exception {
221 Ric ric = this.addRic("ric1");
222 PolicyType type = this.addPolicyType("type1", ric.id());
224 final int noOfPolicies = 100;
225 for (int i = 0; i < noOfPolicies; ++i) {
226 addPolicy("id" + i, type.getId(), "service", ric.id());
231 Policies policies = new Policies(this.applicationConfig);
232 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
233 assertThat(policies.size()).isEqualTo(noOfPolicies);
237 restClient().delete("/policies/id2").block();
238 Policies policies = new Policies(this.applicationConfig);
239 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
240 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
245 void testPersistencyPolicyTypes() throws Exception {
246 Ric ric = this.addRic("ric1");
247 this.addPolicyType("type1", ric.id());
250 PolicyTypes types = new PolicyTypes(this.applicationConfig);
251 types.restoreFromDatabase().blockLast();
252 assertThat(types.size()).isEqualTo(1);
255 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
256 private void waitforS3() throws Exception {
257 if (applicationConfig.isS3Enabled()) {
263 void testPersistencyService() throws Exception {
264 final String SERVICE = "serviceName";
265 putService(SERVICE, 1234, HttpStatus.CREATED);
266 assertThat(this.services.size()).isEqualTo(1);
267 Service service = this.services.getService(SERVICE);
270 Services servicesRestored = new Services(this.applicationConfig);
271 servicesRestored.restoreFromDatabase().blockLast();
272 Service serviceRestored = servicesRestored.getService(SERVICE);
273 assertThat(servicesRestored.size()).isEqualTo(1);
274 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
275 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
277 // check that the service can be deleted
278 this.services.remove(SERVICE);
279 servicesRestored = new Services(this.applicationConfig);
280 assertThat(servicesRestored.size()).isZero();
284 void testAddingRicFromConfiguration() throws Exception {
285 // Test adding the RIC from configuration
287 final String RIC = "ric1";
288 final String TYPE = "type123";
289 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
290 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
291 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
293 putService("service");
295 refreshConfigTask.handleUpdatedRicConfig( //
296 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
298 waitForRicState(RIC, RicState.AVAILABLE);
300 // Test that the type has been synched
301 Ric addedRic = this.rics.getRic(RIC);
302 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
303 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
305 // Check that a service callback for the AVAILABLE RIC is invoked
306 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
307 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
308 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
309 assertThat(callbackInfo.ricId).isEqualTo(RIC);
310 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
314 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
315 putService("service");
317 final String RIC = "NonRespondingRic";
318 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
319 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
321 refreshConfigTask.handleUpdatedRicConfig( //
322 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
325 waitForRicState(RIC, RicState.UNAVAILABLE);
327 // Check that no service callback for the UNAVAILABLE RIC is invoked
328 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
329 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
331 // Run a synch and check that the AVAILABLE notification is received
332 a1ClientFactory.reset();
333 supervision.checkAllRics();
334 waitForRicState(RIC, RicState.AVAILABLE);
336 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
340 void testTrustValidation() {
343 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
344 assertThat(rsp).contains("ric1");
349 void testGetRics() throws Exception {
351 this.addPolicyType("type1", "ric1");
352 String url = "/rics?policytype_id=type1";
353 String rsp = restClient().get(url).block();
354 assertThat(rsp).contains("ric1");
356 // nameless type for ORAN A1 1.1
358 this.addPolicyType("", "ric2");
359 url = "/rics?policytype_id=";
360 rsp = restClient().get(url).block();
361 assertThat(rsp).contains("ric2") //
362 .doesNotContain("ric1") //
363 .contains("AVAILABLE");
366 rsp = restClient().get("/rics").block();
367 assertThat(rsp).contains("ric2") //
370 // Non existing policy type
371 url = "/rics?policytype_id=XXXX";
372 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
376 void testSynchronization() throws Exception {
377 // Two polictypes will be put in the NearRT RICs
378 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
379 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
380 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
381 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
383 // One type and one instance added to the Policy Management Service's storage
384 final String ric1Name = "ric1";
385 Ric ric1 = addRic(ric1Name);
386 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
387 Ric ric2 = addRic("ric2");
389 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
390 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
392 String policyId = "policyId";
393 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
394 supervision.checkAllRics(); // The created policy should be put in the RIC
396 // Wait until synch is completed
397 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
398 waitForRicState(ric1Name, RicState.AVAILABLE);
399 waitForRicState("ric2", RicState.AVAILABLE);
401 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
402 assertThat(ricPolicies.size()).isEqualTo(1);
403 Policy ricPolicy = ricPolicies.get(policyId);
404 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
406 // Both types should be in the Policy Management Service's storage after the
408 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
409 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
413 void testGetRic() throws Exception {
414 String ricId = "ric1";
415 String managedElementId = "kista_1";
416 addRic(ricId, managedElementId);
418 String url = "/rics/ric?managed_element_id=" + managedElementId;
419 String rsp = restClient().get(url).block();
420 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
421 assertThat(ricInfo.ricId).isEqualTo(ricId);
423 url = "/rics/ric?ric_id=" + ricId;
424 rsp = restClient().get(url).block();
425 ricInfo = gson.fromJson(rsp, RicInfo.class);
426 assertThat(ricInfo.ricId).isEqualTo(ricId);
428 // test GET RIC for ManagedElement that does not exist
429 url = "/rics/ric?managed_element_id=" + "junk";
430 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
433 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
436 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
437 boolean isTransient, String statusNotificationUri) {
438 PolicyInfo info = new PolicyInfo();
439 info.policyId = policyInstanceId;
440 info.policyTypeId = policyTypeName;
442 info.serviceId = serviceName;
443 info.policyData = gson.fromJson(jsonString(), Object.class);
446 info.isTransient = isTransient;
448 info.statusNotificationUri = statusNotificationUri;
449 return gson.toJson(info);
452 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
453 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
457 void testPutPolicy() throws Exception {
458 String serviceName = "service.1";
459 String ricId = "ric.1";
460 String policyTypeName = "type1_1.2.3";
461 String policyInstanceId = "instance_1.2.3";
463 putService(serviceName);
464 addPolicyType(policyTypeName, ricId);
466 // PUT a transient policy
467 String url = "/policies";
468 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
469 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
471 restClient().put(url, policyBody).block();
473 Policy policy = policies.getPolicy(policyInstanceId);
474 assertThat(policy).isNotNull();
475 assertThat(policy.getId()).isEqualTo(policyInstanceId);
476 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
477 assertThat(policy.getRic().id()).isEqualTo(ricId);
478 assertThat(policy.isTransient()).isTrue();
480 // Put a non transient policy
481 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
482 restClient().put(url, policyBody).block();
483 policy = policies.getPolicy(policyInstanceId);
484 assertThat(policy.isTransient()).isFalse();
486 url = "/policy-instances";
487 String rsp = restClient().get(url).block();
488 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
490 url = "/policies/" + policyInstanceId;
491 rsp = restClient().get(url).block();
492 assertThat(rsp).contains(policyBody);
494 // Test of error codes
496 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
497 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
499 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
500 addPolicyType(policyTypeName + "XX", "otherRic");
501 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
503 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
504 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
505 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
506 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
510 void testPutPolicy_NoServiceNoStatusUri() throws Exception {
511 String ricId = "ric.1";
512 String policyTypeName = "type1_1.2.3";
513 String policyInstanceId = "instance_1.2.3";
515 addPolicyType(policyTypeName, ricId);
517 // PUT a transient policy
518 String url = "/policies";
519 String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
520 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
522 restClient().put(url, policyBody).block();
524 Policy policy = policies.getPolicy(policyInstanceId);
525 assertThat(policy).isNotNull();
526 assertThat(policy.getOwnerServiceId()).isBlank();
527 assertThat(policy.getStatusNotificationUri()).isBlank();
532 * Test that HttpStatus and body from failing REST call to A1 is passed on to
535 * @throws ServiceException
537 void testErrorFromRic() throws ServiceException {
538 putService("service1");
539 addPolicyType("type1", "ric1");
541 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
542 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
543 String responseBody = "Refused";
544 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
546 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
547 responseBodyBytes, StandardCharsets.UTF_8, null);
548 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
551 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
552 String url = "/policies";
553 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
556 this.addPolicy("instance1", "type1", "service1", "ric1");
557 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
558 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
563 void testPutTypelessPolicy() throws Exception {
564 putService("service1");
565 addPolicyType("", "ric1");
566 String body = putPolicyBody("service1", "ric1", "", "id1");
567 restClient().put("/policies", body).block();
569 String rsp = restClient().get("/policy-instances").block();
570 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
571 assertThat(info.policies).hasSize(1);
572 PolicyInfo policyInfo = info.policies.iterator().next();
573 assertThat(policyInfo.policyId).isEqualTo("id1");
574 assertThat(policyInfo.policyTypeId).isEmpty();
578 void testUpdateService() throws Exception {
580 this.addPolicy("p", "type1", "", "ric1");
582 String url = "/policies?service_id=";
583 String resp = restClient().get(url).block();
584 assertThat(resp).contains("[\"p\"]");
586 this.addPolicy("p", "type1", "service", "ric1");
587 url = "/policies?service_id=";
588 resp = restClient().get(url).block();
589 assertThat(resp).contains("[]");
591 url = "/policies?service_id=service";
592 resp = restClient().get(url).block();
593 assertThat(resp).contains("[\"p\"]");
597 void testRefuseToUpdatePolicy() throws Exception {
598 // Test that only the json can be changed for a already created policy
599 // In this case service is attempted to be changed
601 this.addRic("ricXXX");
602 this.addPolicy("instance1", "type1", "service1", "ric1");
603 this.addPolicy("instance2", "type1", "service1", "ricXXX");
605 // Try change ric1 -> ricXXX
606 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
607 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
611 void testGetPolicy() throws Exception {
612 String url = "/policies/id";
613 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
615 String rsp = restClient().get(url).block();
616 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
617 String policyStr = gson.toJson(info.policyData);
618 assertThat(policyStr).isEqualTo(policy.getJson());
621 policies.remove(policy);
622 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
627 void testDeletePolicy() throws Exception {
628 String policyId = "id.1";
629 addPolicy(policyId, "typeName", "service1", "ric1");
630 assertThat(policies.size()).isEqualTo(1);
632 String url = "/policies/" + policyId;
633 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
635 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
636 assertThat(policies.size()).isZero();
638 // Delete a non existing policy
639 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
643 void testGetPolicyType() throws Exception {
644 String typeId = "AC.D";
645 addPolicyType(typeId, "ric1");
647 waitForRicState("ric1", RicState.AVAILABLE);
649 String url = "/policy-types/" + typeId;
651 String rsp = this.restClient().get(url).block();
653 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
654 assertThat(info.schema).isNotNull();
656 // Get non existing schema
657 url = "/policy-types/JUNK";
658 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
661 String createPolicyTypesJson(String... types) {
662 List<String> list = new ArrayList<>();
663 Collections.addAll(list, types);
664 PolicyTypeIdList ids = new PolicyTypeIdList(list);
665 return gson.toJson(ids);
669 void testGetPolicyTypes() throws Exception {
670 String TYPE_ID_1 = "A_type1_1.9.0";
671 String TYPE_ID_2 = "A_type1_2.0.0";
672 String TYPE_ID_3 = "A_type1_1.5.0";
673 String TYPE_ID_4 = "type3_1.9.0";
674 addPolicyType(TYPE_ID_1, "ric1");
675 addPolicyType(TYPE_ID_2, "ric2");
676 addPolicyType(TYPE_ID_3, "ric2");
677 addPolicyType(TYPE_ID_4, "ric2");
679 addPolicyType("junk", "ric2");
680 addPolicyType("junk_a.b.c", "ric2");
682 String url = "/policy-types";
683 String rsp = restClient().get(url).block();
684 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
686 url = "/policy-types?ric_id=ric1";
687 rsp = restClient().get(url).block();
688 String expResp = createPolicyTypesJson(TYPE_ID_1);
689 assertThat(rsp).isEqualTo(expResp);
691 // Get policy types for non existing RIC
692 url = "/policy-types?ric_id=ric1XXX";
693 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
695 // All types with a type_name
696 url = "/policy-types?type_name=A_type1";
697 rsp = restClient().get(url).block();
698 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
700 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
701 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
702 rsp = restClient().get(url).block();
703 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
704 assertThat(rsp).isEqualTo(expResp);
706 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
707 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
709 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
710 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
712 url = "/policy-types?compatible_with_version=1.5.0";
713 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
717 void testGetPolicyInstances() throws Exception {
718 addPolicy("id1", "type1", "service1");
720 String url = "/policy-instances";
721 String rsp = restClient().get(url).block();
723 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
724 assertThat(info.policies).hasSize(1);
725 PolicyInfo policyInfo = info.policies.iterator().next();
726 assert (policyInfo.validate());
727 assertThat(policyInfo.policyId).isEqualTo("id1");
728 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
729 assertThat(policyInfo.serviceId).isEqualTo("service1");
733 void testGetPolicyInstancesFilter() throws Exception {
734 addPolicy("id1", "type1", "service1");
735 addPolicy("id2", "type1", "service2");
736 addPolicy("id3", "type2", "service1");
737 addPolicy("id4", "type1_1.0.0", "service1");
739 String url = "/policy-instances?policytype_id=type1";
740 String rsp = restClient().get(url).block();
742 assertThat(rsp).contains("id1") //
744 .doesNotContain("id3");
746 url = "/policy-instances?policytype_id=type1&service_id=service2";
747 rsp = restClient().get(url).block();
749 assertThat(rsp).doesNotContain("id1") //
751 .doesNotContain("id3");
753 url = "/policy-instances?type_name=type1";
754 rsp = restClient().get(url).block();
755 assertThat(rsp).contains("id1") //
757 .doesNotContain("id3") //
760 // Test get policies for non existing type
761 url = "/policy-instances?policytype_id=type1XXX";
762 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
764 // Test get policies for non existing RIC
765 url = "/policy-instances?ric_id=XXX";
766 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
770 void testGetPolicyIdsFilter() throws Exception {
771 addPolicy("id1", "type1", "service1", "ric1");
772 addPolicy("id2", "type1", "service2", "ric1");
773 addPolicy("id3", "type2", "service1", "ric1");
774 addPolicy("id4", "type1_1.0.0", "service1");
776 String url = "/policies?policytype_id=type1";
777 String rsp = restClient().get(url).block();
779 assertThat(rsp).contains("id1") //
781 .doesNotContain("id3");
783 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
784 rsp = restClient().get(url).block();
785 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
786 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
788 url = "/policies?policytype_name=type1&service_id=service1";
789 rsp = restClient().get(url).block();
790 assertThat(rsp).contains("id1") //
793 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
795 // Test get policy ids for non existing type
796 url = "/policies?policytype_id=type1XXX";
797 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
799 // Test get policy ids for non existing RIC
800 url = "/policies?ric_id=XXX";
801 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
805 void testPutAndGetService() throws Exception {
807 String serviceName = "ac.dc";
808 putService(serviceName, 0, HttpStatus.CREATED);
809 putService(serviceName, 0, HttpStatus.OK);
812 String url = "/services?service_id=" + serviceName;
813 String rsp = restClient().get(url).block();
814 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
815 assertThat(info.statusList).hasSize(1);
816 ServiceStatus status = info.statusList.iterator().next();
817 assertThat(status.keepAliveIntervalSeconds).isZero();
818 assertThat(status.serviceId).isEqualTo(serviceName);
822 rsp = restClient().get(url).block();
823 assertThat(rsp).as("Response contains service name").contains(serviceName);
827 url = "/services/" + serviceName + "/keepalive";
828 ResponseEntity<?> entity = restClient().putForEntity(url).block();
829 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
832 assertThat(services.size()).isEqualTo(1);
833 url = "/services/" + serviceName;
834 restClient().delete(url).block();
835 assertThat(services.size()).isZero();
837 // Keep alive, no registered service
838 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
840 // PUT service with bad payload
841 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
842 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
843 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
845 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
846 HttpStatus.BAD_REQUEST, false);
848 // GET non existing service
849 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
853 void testServiceSupervision() throws Exception {
854 putService("service1", 1, HttpStatus.CREATED);
855 addPolicyType("type1", "ric1");
857 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
858 restClient().put("/policies", policyBody).block();
860 assertThat(policies.size()).isEqualTo(1);
861 assertThat(services.size()).isEqualTo(1);
863 // Timeout after ~1 second
864 await().untilAsserted(() -> assertThat(policies.size()).isZero());
865 assertThat(services.size()).isZero();
869 void testGetPolicyStatus() throws Exception {
870 addPolicy("id", "typeName", "service1", "ric1");
871 assertThat(policies.size()).isEqualTo(1);
873 String url = "/policies/id/status";
874 String rsp = restClient().get(url).block();
875 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
876 assertThat(info.status).isEqualTo("OK");
878 // GET non existing policy status
879 url = "/policies/XXX/status";
880 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
882 // GET STATUS, the NearRT RIC returns error
883 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
884 url = "/policies/id/status";
885 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
886 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
887 rsp = restClient().get(url).block();
888 info = gson.fromJson(rsp, PolicyStatusInfo.class);
889 assertThat(info.status).hasToString("{}");
893 void testGetServiceStatus() throws Exception {
894 String url = "/status";
895 String rsp = restClient().get(url).block();
896 assertThat(rsp).contains("hunky dory");
898 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
899 assertThat(rsp).isEqualTo("hunky dory");
903 void testServiceNotification() throws Exception {
905 final String AUTH_TOKEN = "testToken";
906 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
907 Files.write(authFile, AUTH_TOKEN.getBytes());
908 this.securityContext.setAuthTokenFilePath(authFile);
910 putService("junkService");
911 Service junkService = this.services.get("junkService");
912 junkService.setCallbackUrl("https://junk");
913 putService("service");
915 Ric ric = addRic("ric1");
916 ric.setState(Ric.RicState.UNAVAILABLE);
917 supervision.checkAllRics();
918 waitForRicState("ric1", RicState.AVAILABLE);
920 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
921 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
922 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
923 assertThat(callbackInfo.ricId).isEqualTo("ric1");
924 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
926 var headers = receivedCallbacks.receivedHeaders.get(0);
927 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
929 Files.delete(authFile);
932 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
934 Policy policy = Policy.builder() //
936 .json(jsonString()) //
937 .ownerServiceId(service) //
938 .ric(rics.getRic(ric)) //
939 .type(addPolicyType(typeName, ric)) //
940 .lastModified(Instant.now()) //
941 .isTransient(false) //
942 .statusNotificationUri("/policy-status?id=XXX") //
944 policies.put(policy);
948 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
949 return addPolicy(id, typeName, service, "ric");
952 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
953 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
954 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
957 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
958 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
960 String json = gson.toJson(service);
964 private void putService(String name) {
965 putService(name, 0, null);
968 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
969 String url = "/services";
970 String body = createServiceJson(name, keepAliveIntervalSeconds);
971 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
972 if (expectedStatus != null) {
973 assertEquals(expectedStatus, resp.getStatusCode(), "");
977 private String jsonString() {
978 return "{\"servingCellNrcgi\":\"1\"}";
982 void testConcurrency() throws Exception {
983 logger.info("Concurrency test starting");
984 final Instant startTime = Instant.now();
985 List<Thread> threads = new ArrayList<>();
986 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
987 a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
989 addPolicyType("type1", "ric");
990 addPolicyType("type2", "ric");
992 final String NON_RESPONDING_RIC = "NonRespondingRic";
993 Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
994 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
995 a1Client.setErrorInject("errorInject");
997 for (int i = 0; i < 10; ++i) {
998 AsyncRestClient restClient = restClient();
999 ConcurrencyTestRunnable test =
1000 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1001 Thread thread = new Thread(test, "TestThread_" + i);
1003 threads.add(thread);
1006 for (Thread t : threads) {
1009 for (ConcurrencyTestRunnable test : tests) {
1010 assertThat(test.isFailed()).isFalse();
1012 assertThat(policies.size()).isZero();
1013 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1015 assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1016 nonRespondingRic.setState(RicState.AVAILABLE);
1019 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1020 WebClientConfig config = this.applicationConfig.getWebClientConfig();
1021 config = WebClientConfig.builder() //
1022 .keyStoreType(config.getKeyStoreType()) //
1023 .keyStorePassword(config.getKeyStorePassword()) //
1024 .keyStore(config.getKeyStore()) //
1025 .keyPassword(config.getKeyPassword()) //
1026 .isTrustStoreUsed(useTrustValidation) //
1027 .trustStore(config.getTrustStore()) //
1028 .trustStorePassword(config.getTrustStorePassword()) //
1029 .httpProxyConfig(config.getHttpProxyConfig()) //
1032 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1033 return f.createRestClientNoHttpProxy(baseUrl);
1037 private String baseUrl() {
1038 return "https://localhost:" + port;
1041 private AsyncRestClient restClient(boolean useTrustValidation) {
1042 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1045 private AsyncRestClient restClient() {
1046 return restClient(false);
1049 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1050 testErrorCode(request, expStatus, "", true);
1053 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1054 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1057 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1058 testErrorCode(request, expStatus, responseContains, true);
1061 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1062 boolean expectApplicationProblemJsonMediaType) {
1063 StepVerifier.create(request) //
1064 .expectSubscription() //
1065 .expectErrorMatches(
1066 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1070 private void waitForRicState(String ricId, RicState state) throws ServiceException {
1071 Ric ric = rics.getRic(ricId);
1072 await().untilAsserted(() -> state.equals(ric.getState()));
1075 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1076 boolean expectApplicationProblemJsonMediaType) {
1077 assertTrue(throwable instanceof WebClientResponseException);
1078 WebClientResponseException responseException = (WebClientResponseException) throwable;
1079 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1080 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1081 if (expectApplicationProblemJsonMediaType) {
1082 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1087 private MockA1Client getA1Client(String ricId) throws ServiceException {
1088 return a1ClientFactory.getOrCreateA1Client(ricId);
1091 private PolicyType createPolicyType(String policyTypeName) {
1092 return PolicyType.builder() //
1093 .id(policyTypeName) //
1094 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1098 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1099 PolicyType type = createPolicyType(policyTypeName);
1100 policyTypes.put(type);
1101 addRic(ricId).addSupportedPolicyType(type);
1105 private Ric addRic(String ricId) {
1106 return addRic(ricId, null);
1109 private RicConfig ricConfig(String ricId, String managedElement) {
1110 List<String> mes = new ArrayList<>();
1111 if (managedElement != null) {
1112 mes.add(managedElement);
1114 return RicConfig.builder() //
1117 .managedElementIds(mes) //
1121 private Ric addRic(String ricId, String managedElement) {
1122 if (rics.get(ricId) != null) {
1123 return rics.get(ricId);
1126 RicConfig conf = ricConfig(ricId, managedElement);
1127 Ric ric = new Ric(conf);
1128 ric.setState(Ric.RicState.AVAILABLE);