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.PrintStream;
35 import java.lang.invoke.MethodHandles;
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.DisplayName;
50 import org.junit.jupiter.api.MethodOrderer;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.TestMethodOrder;
53 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
54 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
55 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
56 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
60 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
61 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
62 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
73 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
74 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
75 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
76 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
77 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
78 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81 import org.springframework.beans.factory.annotation.Autowired;
82 import org.springframework.boot.test.context.SpringBootTest;
83 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
84 import org.springframework.boot.test.context.TestConfiguration;
85 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
86 import org.springframework.boot.web.server.LocalServerPort;
87 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
88 import org.springframework.context.ApplicationContext;
89 import org.springframework.context.annotation.Bean;
90 import org.springframework.http.HttpStatus;
91 import org.springframework.http.MediaType;
92 import org.springframework.http.ResponseEntity;
93 import org.springframework.test.context.TestPropertySource;
94 import org.springframework.util.FileSystemUtils;
95 import org.springframework.web.reactive.function.client.WebClientRequestException;
96 import org.springframework.web.reactive.function.client.WebClientResponseException;
98 import reactor.core.publisher.Mono;
99 import reactor.test.StepVerifier;
100 import reactor.util.annotation.Nullable;
102 @TestMethodOrder(MethodOrderer.MethodName.class)
103 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
104 @TestPropertySource(properties = { //
105 "server.ssl.key-store=./config/keystore.jks", //
106 "app.webclient.trust-store=./config/truststore.jks", //
107 "app.webclient.trust-store-used=true", //
108 "app.vardata-directory=/tmp/pmstest", //
110 "app.s3.bucket=" // If this is set, S3 will be used to store data.
112 class ApplicationTest {
113 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
116 ApplicationContext context;
122 private Policies policies;
125 private PolicyTypes policyTypes;
128 MockA1ClientFactory a1ClientFactory;
131 RicSupervision supervision;
134 ApplicationConfig applicationConfig;
140 RappSimulatorController rAppSimulator;
143 RefreshConfigTask refreshConfigTask;
146 SecurityContext securityContext;
148 private static Gson gson = new GsonBuilder().create();
151 * Overrides the BeanFactory.
154 static class TestBeanFactory {
157 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
158 return new MockA1ClientFactory(appConfig, types);
162 public ServiceSupervision getServiceSupervision(@Autowired Services services,
163 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
164 Duration checkInterval = Duration.ofMillis(1);
165 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
169 public ServletWebServerFactory servletContainer() {
170 return new TomcatServletWebServerFactory();
183 a1ClientFactory.reset();
184 this.rAppSimulator.getTestResults().clear();
185 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
186 this.securityContext.setAuthTokenFilePath(null);
190 static void clearTestDir() {
192 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
193 } catch (Exception e) {
194 logger.warn("Could test directory : {}", e.getMessage());
199 void verifyNoRicLocks() {
200 for (Ric ric : this.rics.getRics()) {
201 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
202 grant.unlockBlocking();
203 assertThat(ric.getLock().getLockCounter()).isZero();
204 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
209 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
210 @DisplayName("test ZZ Actuator")
211 void testZZActuator() throws Exception {
212 // The test must be run last, hence the "ZZ" in the name. All succeeding tests
214 AsyncRestClient client = restClient(baseUrl(), false);
216 client.post("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice",
217 "{\"configuredLevel\":\"trace\"}").block();
219 String resp = client.get("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice").block();
220 assertThat(resp).contains("TRACE");
222 client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
224 // This will stop the web server and all coming tests will fail.
225 client.post("/actuator/shutdown", "").block();
229 StepVerifier.create(restClient().get("/rics")) // Any call
230 .expectSubscription() //
231 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
237 @DisplayName("test generate Api Doc")
238 void generateApiDoc() throws Exception {
239 String url = "https://localhost:" + this.port + "/v3/api-docs";
240 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
241 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
242 JSONObject jsonObj = new JSONObject(resp.getBody());
243 assertThat(jsonObj.remove("servers")).isNotNull();
245 String indented = (jsonObj).toString(4);
246 String docDir = "api/";
247 Files.createDirectories(Paths.get(docDir));
248 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
254 @DisplayName("test Persistency Policies")
255 void testPersistencyPolicies() throws Exception {
256 Ric ric = this.addRic("ric1");
257 PolicyType type = this.addPolicyType("type1", ric.id());
259 final int noOfPolicies = 100;
260 for (int i = 0; i < noOfPolicies; ++i) {
261 addPolicy("id" + i, type.getId(), "service", ric.id());
266 Policies policies = new Policies(this.applicationConfig);
267 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
268 assertThat(policies.size()).isEqualTo(noOfPolicies);
272 restClient().delete("/policies/id2").block();
273 Policies policies = new Policies(this.applicationConfig);
274 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
275 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
280 @DisplayName("test Persistency Policy Types")
281 void testPersistencyPolicyTypes() throws Exception {
282 Ric ric = this.addRic("ric1");
283 this.addPolicyType("type1", ric.id());
286 PolicyTypes types = new PolicyTypes(this.applicationConfig);
287 types.restoreFromDatabase().blockLast();
288 assertThat(types.size()).isEqualTo(1);
291 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
292 private void waitforS3() throws Exception {
293 if (applicationConfig.isS3Enabled()) {
299 @DisplayName("test Persistency Service")
300 void testPersistencyService() throws Exception {
301 final String SERVICE = "serviceName";
302 putService(SERVICE, 1234, HttpStatus.CREATED);
303 assertThat(this.services.size()).isEqualTo(1);
304 Service service = this.services.getService(SERVICE);
307 Services servicesRestored = new Services(this.applicationConfig);
308 servicesRestored.restoreFromDatabase().blockLast();
309 Service serviceRestored = servicesRestored.getService(SERVICE);
310 assertThat(servicesRestored.size()).isEqualTo(1);
311 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
312 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
314 // check that the service can be deleted
315 this.services.remove(SERVICE);
316 servicesRestored = new Services(this.applicationConfig);
317 assertThat(servicesRestored.size()).isZero();
321 @DisplayName("test Adding Ric From Configuration")
322 void testAddingRicFromConfiguration() throws Exception {
323 // Test adding the RIC from configuration
325 final String RIC = "ric1";
326 final String TYPE = "type123";
327 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
328 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
329 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
331 putService("service");
333 refreshConfigTask.handleUpdatedRicConfig( //
334 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
336 waitForRicState(RIC, RicState.AVAILABLE);
338 // Test that the type has been synched
339 Ric addedRic = this.rics.getRic(RIC);
340 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
341 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
343 // Check that a service callback for the AVAILABLE RIC is invoked
344 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
345 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
346 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
347 assertThat(callbackInfo.ricId).isEqualTo(RIC);
348 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
352 @DisplayName("test Adding Ric From Configuration non Responding Ric")
353 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
354 putService("service");
356 final String RIC = "NonRespondingRic";
357 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
358 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
360 refreshConfigTask.handleUpdatedRicConfig( //
361 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
364 waitForRicState(RIC, RicState.UNAVAILABLE);
366 // Check that no service callback for the UNAVAILABLE RIC is invoked
367 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
368 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
370 // Run a synch and check that the AVAILABLE notification is received
371 a1ClientFactory.reset();
372 supervision.checkAllRics();
373 waitForRicState(RIC, RicState.AVAILABLE);
375 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
379 @DisplayName("test Trust Validation")
380 void testTrustValidation() {
383 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
384 assertThat(rsp).contains("ric1");
389 @DisplayName("test Get Rics")
390 void testGetRics() throws Exception {
392 this.addPolicyType("type1", "ric1");
393 String url = "/rics?policytype_id=type1";
394 String rsp = restClient().get(url).block();
395 assertThat(rsp).contains("ric1");
397 // nameless type for ORAN A1 1.1
399 this.addPolicyType("", "ric2");
400 url = "/rics?policytype_id=";
401 rsp = restClient().get(url).block();
402 assertThat(rsp).contains("ric2") //
403 .doesNotContain("ric1") //
404 .contains("AVAILABLE");
407 rsp = restClient().get("/rics").block();
408 assertThat(rsp).contains("ric2") //
411 // Non existing policy type
412 url = "/rics?policytype_id=XXXX";
413 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
417 @DisplayName("test Synchronization")
418 void testSynchronization() throws Exception {
419 // Two polictypes will be put in the NearRT RICs
420 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
421 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
422 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
423 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
425 // One type and one instance added to the Policy Management Service's storage
426 final String ric1Name = "ric1";
427 Ric ric1 = addRic(ric1Name);
428 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
429 Ric ric2 = addRic("ric2");
431 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
432 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
434 String policyId = "policyId";
435 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
436 supervision.checkAllRics(); // The created policy should be put in the RIC
438 // Wait until synch is completed
439 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
440 waitForRicState(ric1Name, RicState.AVAILABLE);
441 waitForRicState("ric2", RicState.AVAILABLE);
443 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
444 assertThat(ricPolicies.size()).isEqualTo(1);
445 Policy ricPolicy = ricPolicies.get(policyId);
446 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
448 // Both types should be in the Policy Management Service's storage after the
450 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
451 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
455 @DisplayName("test Get Ric")
456 void testGetRic() throws Exception {
457 String ricId = "ric1";
458 String managedElementId = "kista_1";
459 addRic(ricId, managedElementId);
461 String url = "/rics/ric?managed_element_id=" + managedElementId;
462 String rsp = restClient().get(url).block();
463 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
464 assertThat(ricInfo.ricId).isEqualTo(ricId);
466 url = "/rics/ric?ric_id=" + ricId;
467 rsp = restClient().get(url).block();
468 ricInfo = gson.fromJson(rsp, RicInfo.class);
469 assertThat(ricInfo.ricId).isEqualTo(ricId);
471 // test GET RIC for ManagedElement that does not exist
472 url = "/rics/ric?managed_element_id=" + "junk";
473 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
476 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
479 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
480 boolean isTransient, String statusNotificationUri) {
481 PolicyInfo info = new PolicyInfo();
482 info.policyId = policyInstanceId;
483 info.policyTypeId = policyTypeName;
485 info.serviceId = serviceName;
486 info.policyData = gson.fromJson(jsonString(), Object.class);
489 info.isTransient = isTransient;
491 info.statusNotificationUri = statusNotificationUri;
492 return gson.toJson(info);
495 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
496 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
500 @DisplayName("test Put Policy")
501 void testPutPolicy() throws Exception {
502 String serviceName = "service.1";
503 String ricId = "ric.1";
504 String policyTypeName = "type1_1.2.3";
505 String policyInstanceId = "instance_1.2.3";
507 putService(serviceName);
508 addPolicyType(policyTypeName, ricId);
510 // PUT a transient policy
511 String url = "/policies";
512 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
513 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
515 restClient().put(url, policyBody).block();
517 Policy policy = policies.getPolicy(policyInstanceId);
518 assertThat(policy).isNotNull();
519 assertThat(policy.getId()).isEqualTo(policyInstanceId);
520 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
521 assertThat(policy.getRic().id()).isEqualTo(ricId);
522 assertThat(policy.isTransient()).isTrue();
524 // Put a non transient policy
525 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
526 restClient().put(url, policyBody).block();
527 policy = policies.getPolicy(policyInstanceId);
528 assertThat(policy.isTransient()).isFalse();
530 url = "/policy-instances";
531 String rsp = restClient().get(url).block();
532 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
534 url = "/policies/" + policyInstanceId;
535 rsp = restClient().get(url).block();
536 assertThat(rsp).contains(policyBody);
538 // Test of error codes
540 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
541 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
543 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
544 addPolicyType(policyTypeName + "XX", "otherRic");
545 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
547 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
548 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
549 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
550 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
554 @DisplayName("test Put Policy No Service No Status Uri")
555 void testPutPolicy_NoServiceNoStatusUri() throws Exception {
556 String ricId = "ric.1";
557 String policyTypeName = "type1_1.2.3";
558 String policyInstanceId = "instance_1.2.3";
560 addPolicyType(policyTypeName, ricId);
562 // PUT a transient policy
563 String url = "/policies";
564 String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
565 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
567 restClient().put(url, policyBody).block();
569 Policy policy = policies.getPolicy(policyInstanceId);
570 assertThat(policy).isNotNull();
571 assertThat(policy.getOwnerServiceId()).isBlank();
572 assertThat(policy.getStatusNotificationUri()).isBlank();
577 * Test that HttpStatus and body from failing REST call to A1 is passed on to
580 * @throws ServiceException
582 @DisplayName("test Error From Ric")
583 void testErrorFromRic() throws ServiceException {
584 putService("service1");
585 addPolicyType("type1", "ric1");
587 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
588 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
589 String responseBody = "Refused";
590 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
592 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
593 responseBodyBytes, StandardCharsets.UTF_8, null);
594 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
597 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
598 String url = "/policies";
599 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
602 this.addPolicy("instance1", "type1", "service1", "ric1");
603 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
604 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
609 @DisplayName("test Put Typeless Policy")
610 void testPutTypelessPolicy() throws Exception {
611 putService("service1");
612 addPolicyType("", "ric1");
613 String body = putPolicyBody("service1", "ric1", "", "id1");
614 restClient().put("/policies", body).block();
616 String rsp = restClient().get("/policy-instances").block();
617 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
618 assertThat(info.policies).hasSize(1);
619 PolicyInfo policyInfo = info.policies.iterator().next();
620 assertThat(policyInfo.policyId).isEqualTo("id1");
621 assertThat(policyInfo.policyTypeId).isEmpty();
625 @DisplayName("test Update Service")
626 void testUpdateService() throws Exception {
628 this.addPolicy("p", "type1", "", "ric1");
630 String url = "/policies?service_id=";
631 String resp = restClient().get(url).block();
632 assertThat(resp).contains("[\"p\"]");
634 this.addPolicy("p", "type1", "service", "ric1");
635 url = "/policies?service_id=";
636 resp = restClient().get(url).block();
637 assertThat(resp).contains("[]");
639 url = "/policies?service_id=service";
640 resp = restClient().get(url).block();
641 assertThat(resp).contains("[\"p\"]");
645 @DisplayName("test Refuse To Update Policy")
646 void testRefuseToUpdatePolicy() throws Exception {
647 // Test that only the json can be changed for a already created policy
648 // In this case service is attempted to be changed
650 this.addRic("ricXXX");
651 this.addPolicy("instance1", "type1", "service1", "ric1");
652 this.addPolicy("instance2", "type1", "service1", "ricXXX");
654 // Try change ric1 -> ricXXX
655 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
656 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
660 @DisplayName("test Get Policy")
661 void testGetPolicy() throws Exception {
662 String url = "/policies/id";
663 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
665 String rsp = restClient().get(url).block();
666 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
667 String policyStr = gson.toJson(info.policyData);
668 assertThat(policyStr).isEqualTo(policy.getJson());
671 policies.remove(policy);
672 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
677 @DisplayName("test Delete Policy")
678 void testDeletePolicy() throws Exception {
679 String policyId = "id.1";
680 addPolicy(policyId, "typeName", "service1", "ric1");
681 assertThat(policies.size()).isEqualTo(1);
683 String url = "/policies/" + policyId;
684 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
686 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
687 assertThat(policies.size()).isZero();
689 // Delete a non existing policy
690 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
694 @DisplayName("test Get Policy Type")
695 void testGetPolicyType() throws Exception {
696 String typeId = "AC.D";
697 addPolicyType(typeId, "ric1");
699 waitForRicState("ric1", RicState.AVAILABLE);
701 String url = "/policy-types/" + typeId;
703 String rsp = this.restClient().get(url).block();
705 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
706 assertThat(info.schema).isNotNull();
708 // Get non existing schema
709 url = "/policy-types/JUNK";
710 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
713 String createPolicyTypesJson(String... types) {
714 List<String> list = new ArrayList<>();
715 Collections.addAll(list, types);
716 PolicyTypeIdList ids = new PolicyTypeIdList(list);
717 return gson.toJson(ids);
721 @DisplayName("test Get Policy Types")
722 void testGetPolicyTypes() throws Exception {
723 String TYPE_ID_1 = "A_type1_1.9.0";
724 String TYPE_ID_2 = "A_type1_2.0.0";
725 String TYPE_ID_3 = "A_type1_1.5.0";
726 String TYPE_ID_4 = "type3_1.9.0";
727 addPolicyType(TYPE_ID_1, "ric1");
728 addPolicyType(TYPE_ID_2, "ric2");
729 addPolicyType(TYPE_ID_3, "ric2");
730 addPolicyType(TYPE_ID_4, "ric2");
732 addPolicyType("junk", "ric2");
733 addPolicyType("junk_a.b.c", "ric2");
735 String url = "/policy-types";
736 String rsp = restClient().get(url).block();
737 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
739 url = "/policy-types?ric_id=ric1";
740 rsp = restClient().get(url).block();
741 String expResp = createPolicyTypesJson(TYPE_ID_1);
742 assertThat(rsp).isEqualTo(expResp);
744 // Get policy types for non existing RIC
745 url = "/policy-types?ric_id=ric1XXX";
746 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
748 // All types with a type_name
749 url = "/policy-types?type_name=A_type1";
750 rsp = restClient().get(url).block();
751 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
753 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
754 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
755 rsp = restClient().get(url).block();
756 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
757 assertThat(rsp).isEqualTo(expResp);
759 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
760 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
762 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
763 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
765 url = "/policy-types?compatible_with_version=1.5.0";
766 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
770 @DisplayName("test Get Policy Instances")
771 void testGetPolicyInstances() throws Exception {
772 addPolicy("id1", "type1", "service1");
774 String url = "/policy-instances";
775 String rsp = restClient().get(url).block();
777 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
778 assertThat(info.policies).hasSize(1);
779 PolicyInfo policyInfo = info.policies.iterator().next();
780 assert (policyInfo.validate());
781 assertThat(policyInfo.policyId).isEqualTo("id1");
782 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
783 assertThat(policyInfo.serviceId).isEqualTo("service1");
787 @DisplayName("test Get Policy Instances Filter")
788 void testGetPolicyInstancesFilter() throws Exception {
789 addPolicy("id1", "type1", "service1");
790 addPolicy("id2", "type1", "service2");
791 addPolicy("id3", "type2", "service1");
792 addPolicy("id4", "type1_1.0.0", "service1");
794 String url = "/policy-instances?policytype_id=type1";
795 String rsp = restClient().get(url).block();
797 assertThat(rsp).contains("id1") //
799 .doesNotContain("id3");
801 url = "/policy-instances?policytype_id=type1&service_id=service2";
802 rsp = restClient().get(url).block();
804 assertThat(rsp).doesNotContain("id1") //
806 .doesNotContain("id3");
808 url = "/policy-instances?type_name=type1";
809 rsp = restClient().get(url).block();
810 assertThat(rsp).contains("id1") //
812 .doesNotContain("id3") //
815 // Test get policies for non existing type
816 url = "/policy-instances?policytype_id=type1XXX";
817 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
819 // Test get policies for non existing RIC
820 url = "/policy-instances?ric_id=XXX";
821 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
825 @DisplayName("test Get Policy Ids Filter")
826 void testGetPolicyIdsFilter() throws Exception {
827 addPolicy("id1", "type1", "service1", "ric1");
828 addPolicy("id2", "type1", "service2", "ric1");
829 addPolicy("id3", "type2", "service1", "ric1");
830 addPolicy("id4", "type1_1.0.0", "service1");
832 String url = "/policies?policytype_id=type1";
833 String rsp = restClient().get(url).block();
835 assertThat(rsp).contains("id1") //
837 .doesNotContain("id3");
839 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
840 rsp = restClient().get(url).block();
841 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
842 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
844 url = "/policies?policytype_name=type1&service_id=service1";
845 rsp = restClient().get(url).block();
846 assertThat(rsp).contains("id1") //
849 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
851 // Test get policy ids for non existing type
852 url = "/policies?policytype_id=type1XXX";
853 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
855 // Test get policy ids for non existing RIC
856 url = "/policies?ric_id=XXX";
857 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
861 @DisplayName("test Put And Get Service")
862 void testPutAndGetService() throws Exception {
864 String serviceName = "ac.dc";
865 putService(serviceName, 0, HttpStatus.CREATED);
866 putService(serviceName, 0, HttpStatus.OK);
869 String url = "/services?service_id=" + serviceName;
870 String rsp = restClient().get(url).block();
871 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
872 assertThat(info.statusList).hasSize(1);
873 ServiceStatus status = info.statusList.iterator().next();
874 assertThat(status.keepAliveIntervalSeconds).isZero();
875 assertThat(status.serviceId).isEqualTo(serviceName);
879 rsp = restClient().get(url).block();
880 assertThat(rsp).as("Response contains service name").contains(serviceName);
884 url = "/services/" + serviceName + "/keepalive";
885 ResponseEntity<?> entity = restClient().putForEntity(url).block();
886 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
889 assertThat(services.size()).isEqualTo(1);
890 url = "/services/" + serviceName;
891 restClient().delete(url).block();
892 assertThat(services.size()).isZero();
894 // Keep alive, no registered service
895 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
897 // PUT service with bad payload
898 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
899 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
900 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
902 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
903 HttpStatus.BAD_REQUEST, false);
905 // GET non existing service
906 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
910 @DisplayName("test Service Supervision")
911 void testServiceSupervision() throws Exception {
912 putService("service1", 1, HttpStatus.CREATED);
913 addPolicyType("type1", "ric1");
915 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
916 restClient().put("/policies", policyBody).block();
918 assertThat(policies.size()).isEqualTo(1);
919 assertThat(services.size()).isEqualTo(1);
921 // Timeout after ~1 second
922 await().untilAsserted(() -> assertThat(policies.size()).isZero());
923 assertThat(services.size()).isZero();
927 @DisplayName("test Get Policy Status")
928 void testGetPolicyStatus() throws Exception {
929 addPolicy("id", "typeName", "service1", "ric1");
930 assertThat(policies.size()).isEqualTo(1);
932 String url = "/policies/id/status";
933 String rsp = restClient().get(url).block();
934 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
935 assertThat(info.status).isEqualTo("OK");
937 // GET non existing policy status
938 url = "/policies/XXX/status";
939 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
941 // GET STATUS, the NearRT RIC returns error
942 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
943 url = "/policies/id/status";
944 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
945 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
946 rsp = restClient().get(url).block();
947 info = gson.fromJson(rsp, PolicyStatusInfo.class);
948 assertThat(info.status).hasToString("{}");
952 @DisplayName("test Get Service Status")
953 void testGetServiceStatus() throws Exception {
954 String url = "/status";
955 String rsp = restClient().get(url).block();
956 assertThat(rsp).contains("hunky dory");
958 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
959 assertThat(rsp).isEqualTo("hunky dory");
963 @DisplayName("test Service Notification")
964 void testServiceNotification() throws Exception {
966 final String AUTH_TOKEN = "testToken";
967 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
968 Files.write(authFile, AUTH_TOKEN.getBytes());
969 this.securityContext.setAuthTokenFilePath(authFile);
971 putService("junkService");
972 Service junkService = this.services.get("junkService");
973 junkService.setCallbackUrl("https://junk");
974 putService("service");
976 Ric ric = addRic("ric1");
977 ric.setState(Ric.RicState.UNAVAILABLE);
978 supervision.checkAllRics();
979 waitForRicState("ric1", RicState.AVAILABLE);
981 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
982 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
983 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
984 assertThat(callbackInfo.ricId).isEqualTo("ric1");
985 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
987 var headers = receivedCallbacks.receivedHeaders.get(0);
988 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
990 Files.delete(authFile);
993 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
995 Policy policy = Policy.builder() //
997 .json(jsonString()) //
998 .ownerServiceId(service) //
999 .ric(rics.getRic(ric)) //
1000 .type(addPolicyType(typeName, ric)) //
1001 .lastModified(Instant.now()) //
1002 .isTransient(false) //
1003 .statusNotificationUri("/policy-status?id=XXX") //
1005 policies.put(policy);
1009 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
1010 return addPolicy(id, typeName, service, "ric");
1013 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
1014 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
1015 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
1018 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
1019 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
1021 String json = gson.toJson(service);
1025 private void putService(String name) {
1026 putService(name, 0, null);
1029 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
1030 String url = "/services";
1031 String body = createServiceJson(name, keepAliveIntervalSeconds);
1032 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
1033 if (expectedStatus != null) {
1034 assertEquals(expectedStatus, resp.getStatusCode(), "");
1038 private String jsonString() {
1039 return "{\"servingCellNrcgi\":\"1\"}";
1043 @DisplayName("test Concurrency")
1044 void testConcurrency() throws Exception {
1045 logger.info("Concurrency test starting");
1046 final Instant startTime = Instant.now();
1047 List<Thread> threads = new ArrayList<>();
1048 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
1049 a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
1051 addPolicyType("type1", "ric");
1052 addPolicyType("type2", "ric");
1054 final String NON_RESPONDING_RIC = "NonRespondingRic";
1055 Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
1056 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
1057 a1Client.setErrorInject("errorInject");
1059 for (int i = 0; i < 10; ++i) {
1060 AsyncRestClient restClient = restClient();
1061 ConcurrencyTestRunnable test =
1062 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1063 Thread thread = new Thread(test, "TestThread_" + i);
1065 threads.add(thread);
1068 for (Thread t : threads) {
1071 for (ConcurrencyTestRunnable test : tests) {
1072 assertThat(test.isFailed()).isFalse();
1074 assertThat(policies.size()).isZero();
1075 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1077 assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1078 nonRespondingRic.setState(RicState.AVAILABLE);
1081 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1082 WebClientConfig config = this.applicationConfig.getWebClientConfig();
1083 config = WebClientConfig.builder() //
1084 .keyStoreType(config.getKeyStoreType()) //
1085 .keyStorePassword(config.getKeyStorePassword()) //
1086 .keyStore(config.getKeyStore()) //
1087 .keyPassword(config.getKeyPassword()) //
1088 .isTrustStoreUsed(useTrustValidation) //
1089 .trustStore(config.getTrustStore()) //
1090 .trustStorePassword(config.getTrustStorePassword()) //
1091 .httpProxyConfig(config.getHttpProxyConfig()) //
1094 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1095 return f.createRestClientNoHttpProxy(baseUrl);
1099 private String baseUrl() {
1100 return "https://localhost:" + port;
1103 private AsyncRestClient restClient(boolean useTrustValidation) {
1104 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1107 private AsyncRestClient restClient() {
1108 return restClient(false);
1111 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1112 testErrorCode(request, expStatus, "", true);
1115 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1116 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1119 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1120 testErrorCode(request, expStatus, responseContains, true);
1123 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1124 boolean expectApplicationProblemJsonMediaType) {
1125 StepVerifier.create(request) //
1126 .expectSubscription() //
1127 .expectErrorMatches(
1128 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1132 private void waitForRicState(String ricId, RicState state) throws ServiceException {
1133 Ric ric = rics.getRic(ricId);
1134 await().untilAsserted(() -> state.equals(ric.getState()));
1137 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1138 boolean expectApplicationProblemJsonMediaType) {
1139 assertTrue(throwable instanceof WebClientResponseException);
1140 WebClientResponseException responseException = (WebClientResponseException) throwable;
1141 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1142 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1143 if (expectApplicationProblemJsonMediaType) {
1144 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1149 private MockA1Client getA1Client(String ricId) throws ServiceException {
1150 return a1ClientFactory.getOrCreateA1Client(ricId);
1153 private PolicyType createPolicyType(String policyTypeName) {
1154 return PolicyType.builder() //
1155 .id(policyTypeName) //
1156 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1160 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1161 PolicyType type = createPolicyType(policyTypeName);
1162 policyTypes.put(type);
1163 addRic(ricId).addSupportedPolicyType(type);
1167 private Ric addRic(String ricId) {
1168 return addRic(ricId, null);
1171 private RicConfig ricConfig(String ricId, String managedElement) {
1172 List<String> mes = new ArrayList<>();
1173 if (managedElement != null) {
1174 mes.add(managedElement);
1176 return RicConfig.builder() //
1179 .managedElementIds(mes) //
1183 private Ric addRic(String ricId, String managedElement) {
1184 if (rics.get(ricId) != null) {
1185 return rics.get(ricId);
1188 RicConfig conf = ricConfig(ricId, managedElement);
1189 Ric ric = new Ric(conf);
1190 ric.setState(Ric.RicState.AVAILABLE);