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.AfterEach;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.extension.ExtendWith;
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.test.context.junit.jupiter.SpringExtension;
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 @ExtendWith(SpringExtension.class)
99 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
100 @TestPropertySource(properties = { //
101 "server.ssl.key-store=./src/test/resources/keystore.jks", //
102 "app.webclient.trust-store=./src/test/resources/truststore.jks", //
103 "app.webclient.trust-store-used=true", //
104 "app.vardata-directory=./target/testdata", //
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 void verifyNoRicLocks() {
186 for (Ric ric : this.rics.getRics()) {
187 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "");
188 grant.unlockBlocking();
189 assertThat(ric.getLock().getLockCounter()).isZero();
190 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
195 void generateApiDoc() throws IOException {
196 String url = "https://localhost:" + this.port + "/v3/api-docs";
197 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
198 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
199 JSONObject jsonObj = new JSONObject(resp.getBody());
200 assertThat(jsonObj.remove("servers")).isNotNull();
202 String indented = (jsonObj).toString(4);
203 String docDir = "api/";
204 Files.createDirectories(Paths.get(docDir));
205 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
211 void testPersistencyPolicies() throws ServiceException {
212 Ric ric = this.addRic("ric1");
213 PolicyType type = this.addPolicyType("type1", ric.id());
215 final int noOfPolicies = 100;
216 for (int i = 0; i < noOfPolicies; ++i) {
217 addPolicy("id" + i, type.getId(), "service", ric.id());
221 Policies policies = new Policies(this.applicationConfig);
222 policies.restoreFromDatabase(ric, this.policyTypes);
223 assertThat(policies.size()).isEqualTo(noOfPolicies);
227 restClient().delete("/policies/id2").block();
228 Policies policies = new Policies(this.applicationConfig);
229 policies.restoreFromDatabase(ric, this.policyTypes);
230 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
235 void testPersistencyPolicyTypes() throws ServiceException {
236 Ric ric = this.addRic("ric1");
237 this.addPolicyType("type1", ric.id());
238 PolicyTypes types = new PolicyTypes(this.applicationConfig);
239 assertThat(types.size()).isEqualTo(1);
243 void testPersistencyService() throws ServiceException {
244 final String SERVICE = "serviceName";
245 putService(SERVICE, 1234, HttpStatus.CREATED);
246 assertThat(this.services.size()).isEqualTo(1);
247 Service service = this.services.getService(SERVICE);
249 Services servicesRestored = new Services(this.applicationConfig);
250 Service serviceRestored = servicesRestored.getService(SERVICE);
251 assertThat(servicesRestored.size()).isEqualTo(1);
252 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
253 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
255 // check that the service can be deleted
256 this.services.remove(SERVICE);
257 servicesRestored = new Services(this.applicationConfig);
258 assertThat(servicesRestored.size()).isZero();
262 void testAddingRicFromConfiguration() throws Exception {
263 // Test adding the RIC from configuration
265 final String RIC = "ric1";
266 final String TYPE = "type123";
267 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
268 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
269 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
271 putService("service");
273 refreshConfigTask.handleUpdatedRicConfig( //
274 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
276 waitForRicState(RIC, RicState.AVAILABLE);
278 // Test that the type has been synched
279 Ric addedRic = this.rics.getRic(RIC);
280 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
281 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
283 // Check that a service callback for the AVAILABLE RIC is invoked
284 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
285 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
286 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
287 assertThat(callbackInfo.ricId).isEqualTo(RIC);
288 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
292 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
293 putService("service");
295 final String RIC = "NonRespondingRic";
296 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
297 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
299 refreshConfigTask.handleUpdatedRicConfig( //
300 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
303 waitForRicState(RIC, RicState.UNAVAILABLE);
305 // Check that no service callback for the UNAVAILABLE RIC is invoked
306 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
307 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
309 // Run a synch and check that the AVAILABLE notification is received
310 a1ClientFactory.reset();
311 supervision.checkAllRics();
312 waitForRicState(RIC, RicState.AVAILABLE);
314 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
318 void testTrustValidation() {
320 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
321 assertThat(rsp).contains("ric1");
325 void testGetRics() throws Exception {
327 this.addPolicyType("type1", "ric1");
328 String url = "/rics?policytype_id=type1";
329 String rsp = restClient().get(url).block();
330 assertThat(rsp).contains("ric1");
332 // nameless type for ORAN A1 1.1
334 this.addPolicyType("", "ric2");
335 url = "/rics?policytype_id=";
336 rsp = restClient().get(url).block();
337 assertThat(rsp).contains("ric2") //
338 .doesNotContain("ric1") //
339 .contains("AVAILABLE");
342 rsp = restClient().get("/rics").block();
343 assertThat(rsp).contains("ric2") //
346 // Non existing policy type
347 url = "/rics?policytype_id=XXXX";
348 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
352 void testSynchronization() throws Exception {
353 // Two polictypes will be put in the NearRT RICs
354 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
355 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
356 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
357 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
359 // One type and one instance added to the Policy Management Service's storage
360 final String ric1Name = "ric1";
361 Ric ric1 = addRic(ric1Name);
362 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
363 Ric ric2 = addRic("ric2");
365 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
366 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
368 String policyId = "policyId";
369 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
370 supervision.checkAllRics(); // The created policy should be put in the RIC
372 // Wait until synch is completed
373 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
374 waitForRicState(ric1Name, RicState.AVAILABLE);
375 waitForRicState("ric2", RicState.AVAILABLE);
377 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
378 assertThat(ricPolicies.size()).isEqualTo(1);
379 Policy ricPolicy = ricPolicies.get(policyId);
380 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
382 // Both types should be in the Policy Management Service's storage after the
384 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
385 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
389 void testGetRic() throws Exception {
390 String ricId = "ric1";
391 String managedElementId = "kista_1";
392 addRic(ricId, managedElementId);
394 String url = "/rics/ric?managed_element_id=" + managedElementId;
395 String rsp = restClient().get(url).block();
396 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
397 assertThat(ricInfo.ricId).isEqualTo(ricId);
399 url = "/rics/ric?ric_id=" + ricId;
400 rsp = restClient().get(url).block();
401 ricInfo = gson.fromJson(rsp, RicInfo.class);
402 assertThat(ricInfo.ricId).isEqualTo(ricId);
404 // test GET RIC for ManagedElement that does not exist
405 url = "/rics/ric?managed_element_id=" + "junk";
406 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
409 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
412 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
413 boolean isTransient) {
414 PolicyInfo info = new PolicyInfo();
415 info.policyId = policyInstanceId;
416 info.policyTypeId = policyTypeName;
418 info.serviceId = serviceName;
419 info.policyData = gson.fromJson(jsonString(), Object.class);
422 info.isTransient = isTransient;
424 info.statusNotificationUri = "statusNotificationUri";
425 return gson.toJson(info);
428 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
429 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
433 void testPutPolicy() throws Exception {
434 String serviceName = "service.1";
435 String ricId = "ric.1";
436 String policyTypeName = "type1_1.2.3";
437 String policyInstanceId = "instance_1.2.3";
439 putService(serviceName);
440 addPolicyType(policyTypeName, ricId);
442 // PUT a transient policy
443 String url = "/policies";
444 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
445 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
447 restClient().put(url, policyBody).block();
449 Policy policy = policies.getPolicy(policyInstanceId);
450 assertThat(policy).isNotNull();
451 assertThat(policy.getId()).isEqualTo(policyInstanceId);
452 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
453 assertThat(policy.getRic().id()).isEqualTo(ricId);
454 assertThat(policy.isTransient()).isTrue();
456 // Put a non transient policy
457 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
458 restClient().put(url, policyBody).block();
459 policy = policies.getPolicy(policyInstanceId);
460 assertThat(policy.isTransient()).isFalse();
462 url = "/policy-instances";
463 String rsp = restClient().get(url).block();
464 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
466 url = "/policies/" + policyInstanceId;
467 rsp = restClient().get(url).block();
468 assertThat(rsp).contains(policyBody);
470 // Test of error codes
472 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
473 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
475 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
476 addPolicyType(policyTypeName + "XX", "otherRic");
477 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
479 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
480 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
481 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
482 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
487 * Test that HttpStatus and body from failing REST call to A1 is passed on to
490 * @throws ServiceException
492 void testErrorFromRic() throws ServiceException {
493 putService("service1");
494 addPolicyType("type1", "ric1");
496 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
497 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
498 String responseBody = "Refused";
499 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
501 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
502 responseBodyBytes, StandardCharsets.UTF_8, null);
503 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
506 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
507 String url = "/policies";
508 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
511 this.addPolicy("instance1", "type1", "service1", "ric1");
512 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
513 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
518 void testPutTypelessPolicy() throws Exception {
519 putService("service1");
520 addPolicyType("", "ric1");
521 String body = putPolicyBody("service1", "ric1", "", "id1");
522 restClient().put("/policies", body).block();
524 String rsp = restClient().get("/policy-instances").block();
525 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
526 assertThat(info.policies).hasSize(1);
527 PolicyInfo policyInfo = info.policies.iterator().next();
528 assertThat(policyInfo.policyId).isEqualTo("id1");
529 assertThat(policyInfo.policyTypeId).isEmpty();
533 void testRefuseToUpdatePolicy() throws Exception {
534 // Test that only the json can be changed for a already created policy
535 // In this case service is attempted to be changed
537 this.addRic("ricXXX");
538 this.addPolicy("instance1", "type1", "service1", "ric1");
539 this.addPolicy("instance2", "type1", "service1", "ricXXX");
541 // Try change ric1 -> ricXXX
542 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
543 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
547 void testGetPolicy() throws Exception {
548 String url = "/policies/id";
549 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
551 String rsp = restClient().get(url).block();
552 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
553 String policyStr = gson.toJson(info.policyData);
554 assertThat(policyStr).isEqualTo(policy.getJson());
557 policies.remove(policy);
558 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
563 void testDeletePolicy() throws Exception {
564 String policyId = "id.1";
565 addPolicy(policyId, "typeName", "service1", "ric1");
566 assertThat(policies.size()).isEqualTo(1);
568 String url = "/policies/" + policyId;
569 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
571 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
572 assertThat(policies.size()).isZero();
574 // Delete a non existing policy
575 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
579 void testGetPolicyType() throws Exception {
580 String typeId = "AC.D";
581 addPolicyType(typeId, "ric1");
583 waitForRicState("ric1", RicState.AVAILABLE);
585 String url = "/policy-types/" + typeId;
587 String rsp = this.restClient().get(url).block();
589 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
590 assertThat(info.schema).isNotNull();
592 // Get non existing schema
593 url = "/policy-types/JUNK";
594 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
597 String createPolicyTypesJson(String... types) {
598 List<String> list = new ArrayList<>();
599 Collections.addAll(list, types);
600 PolicyTypeIdList ids = new PolicyTypeIdList(list);
601 return gson.toJson(ids);
605 void testGetPolicyTypes() throws Exception {
606 String TYPE_ID_1 = "A_type1_1.9.0";
607 String TYPE_ID_2 = "A_type1_2.0.0";
608 String TYPE_ID_3 = "A_type1_1.5.0";
609 String TYPE_ID_4 = "type3_1.9.0";
610 addPolicyType(TYPE_ID_1, "ric1");
611 addPolicyType(TYPE_ID_2, "ric2");
612 addPolicyType(TYPE_ID_3, "ric2");
613 addPolicyType(TYPE_ID_4, "ric2");
615 addPolicyType("junk", "ric2");
616 addPolicyType("junk_a.b.c", "ric2");
618 String url = "/policy-types";
619 String rsp = restClient().get(url).block();
620 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
622 url = "/policy-types?ric_id=ric1";
623 rsp = restClient().get(url).block();
624 String expResp = createPolicyTypesJson(TYPE_ID_1);
625 assertThat(rsp).isEqualTo(expResp);
627 // Get policy types for non existing RIC
628 url = "/policy-types?ric_id=ric1XXX";
629 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
631 // All types with a type_name
632 url = "/policy-types?type_name=A_type1";
633 rsp = restClient().get(url).block();
634 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
636 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
637 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
638 rsp = restClient().get(url).block();
639 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
640 assertThat(rsp).isEqualTo(expResp);
642 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
643 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
645 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
646 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
648 url = "/policy-types?compatible_with_version=1.5.0";
649 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
653 void testGetPolicyInstances() throws Exception {
654 addPolicy("id1", "type1", "service1");
656 String url = "/policy-instances";
657 String rsp = restClient().get(url).block();
659 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
660 assertThat(info.policies).hasSize(1);
661 PolicyInfo policyInfo = info.policies.iterator().next();
662 assert (policyInfo.validate());
663 assertThat(policyInfo.policyId).isEqualTo("id1");
664 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
665 assertThat(policyInfo.serviceId).isEqualTo("service1");
669 void testGetPolicyInstancesFilter() throws Exception {
670 addPolicy("id1", "type1", "service1");
671 addPolicy("id2", "type1", "service2");
672 addPolicy("id3", "type2", "service1");
673 addPolicy("id4", "type1_1.0.0", "service1");
675 String url = "/policy-instances?policytype_id=type1";
676 String rsp = restClient().get(url).block();
678 assertThat(rsp).contains("id1") //
680 .doesNotContain("id3");
682 url = "/policy-instances?policytype_id=type1&service_id=service2";
683 rsp = restClient().get(url).block();
685 assertThat(rsp).doesNotContain("id1") //
687 .doesNotContain("id3");
689 url = "/policy-instances?type_name=type1";
690 rsp = restClient().get(url).block();
691 assertThat(rsp).contains("id1") //
693 .doesNotContain("id3") //
696 // Test get policies for non existing type
697 url = "/policy-instances?policytype_id=type1XXX";
698 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
700 // Test get policies for non existing RIC
701 url = "/policy-instances?ric_id=XXX";
702 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
706 void testGetPolicyIdsFilter() throws Exception {
707 addPolicy("id1", "type1", "service1", "ric1");
708 addPolicy("id2", "type1", "service2", "ric1");
709 addPolicy("id3", "type2", "service1", "ric1");
710 addPolicy("id4", "type1_1.0.0", "service1");
712 String url = "/policies?policytype_id=type1";
713 String rsp = restClient().get(url).block();
715 assertThat(rsp).contains("id1") //
717 .doesNotContain("id3");
719 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
720 rsp = restClient().get(url).block();
721 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
722 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
724 url = "/policies?policytype_name=type1&service_id=service1";
725 rsp = restClient().get(url).block();
726 assertThat(rsp).contains("id1") //
729 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
731 // Test get policy ids for non existing type
732 url = "/policies?policytype_id=type1XXX";
733 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
735 // Test get policy ids for non existing RIC
736 url = "/policies?ric_id=XXX";
737 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
741 void testPutAndGetService() throws Exception {
743 String serviceName = "ac.dc";
744 putService(serviceName, 0, HttpStatus.CREATED);
745 putService(serviceName, 0, HttpStatus.OK);
748 String url = "/services?service_id=" + serviceName;
749 String rsp = restClient().get(url).block();
750 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
751 assertThat(info.statusList).hasSize(1);
752 ServiceStatus status = info.statusList.iterator().next();
753 assertThat(status.keepAliveIntervalSeconds).isZero();
754 assertThat(status.serviceId).isEqualTo(serviceName);
758 rsp = restClient().get(url).block();
759 assertThat(rsp).as("Response contains service name").contains(serviceName);
763 url = "/services/" + serviceName + "/keepalive";
764 ResponseEntity<?> entity = restClient().putForEntity(url).block();
765 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
768 assertThat(services.size()).isEqualTo(1);
769 url = "/services/" + serviceName;
770 restClient().delete(url).block();
771 assertThat(services.size()).isZero();
773 // Keep alive, no registered service
774 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
776 // PUT service with bad payload
777 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
778 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
779 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
781 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
782 HttpStatus.BAD_REQUEST, false);
784 // GET non existing service
785 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
789 void testServiceSupervision() throws Exception {
790 putService("service1", 1, HttpStatus.CREATED);
791 addPolicyType("type1", "ric1");
793 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
794 restClient().put("/policies", policyBody).block();
796 assertThat(policies.size()).isEqualTo(1);
797 assertThat(services.size()).isEqualTo(1);
799 // Timeout after ~1 second
800 await().untilAsserted(() -> assertThat(policies.size()).isZero());
801 assertThat(services.size()).isZero();
805 void testGetPolicyStatus() throws Exception {
806 addPolicy("id", "typeName", "service1", "ric1");
807 assertThat(policies.size()).isEqualTo(1);
809 String url = "/policies/id/status";
810 String rsp = restClient().get(url).block();
811 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
812 assertThat(info.status).isEqualTo("OK");
814 // GET non existing policy status
815 url = "/policies/XXX/status";
816 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
818 // GET STATUS, the NearRT RIC returns error
819 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
820 url = "/policies/id/status";
821 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
822 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
823 rsp = restClient().get(url).block();
824 info = gson.fromJson(rsp, PolicyStatusInfo.class);
825 assertThat(info.status).hasToString("{}");
829 void testGetServiceStatus() throws Exception {
830 String url = "/status";
831 String rsp = restClient().get(url).block();
832 assertThat(rsp).contains("hunky dory");
834 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
835 assertThat(rsp).isEqualTo("hunky dory");
839 void testServiceNotification() throws Exception {
841 final String AUTH_TOKEN = "testToken";
842 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
843 Files.write(authFile, AUTH_TOKEN.getBytes());
844 this.securityContext.setAuthTokenFilePath(authFile);
846 putService("junkService");
847 Service junkService = this.services.get("junkService");
848 junkService.setCallbackUrl("https://junk");
849 putService("service");
851 Ric ric = addRic("ric1");
852 ric.setState(Ric.RicState.UNAVAILABLE);
853 supervision.checkAllRics();
854 waitForRicState("ric1", RicState.AVAILABLE);
856 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
857 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
858 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
859 assertThat(callbackInfo.ricId).isEqualTo("ric1");
860 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
862 var headers = receivedCallbacks.receivedHeaders.get(0);
863 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
865 Files.delete(authFile);
868 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
870 Policy policy = Policy.builder() //
872 .json(jsonString()) //
873 .ownerServiceId(service) //
874 .ric(rics.getRic(ric)) //
875 .type(addPolicyType(typeName, ric)) //
876 .lastModified(Instant.now()) //
877 .isTransient(false) //
878 .statusNotificationUri("/policy-status?id=XXX") //
880 policies.put(policy);
884 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
885 return addPolicy(id, typeName, service, "ric");
888 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
889 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
890 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
893 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
894 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
896 String json = gson.toJson(service);
900 private void putService(String name) {
901 putService(name, 0, null);
904 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
905 String url = "/services";
906 String body = createServiceJson(name, keepAliveIntervalSeconds);
907 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
908 if (expectedStatus != null) {
909 assertEquals(expectedStatus, resp.getStatusCode(), "");
913 private String jsonString() {
914 return "{\"servingCellNrcgi\":\"1\"}";
918 void testConcurrency() throws Exception {
919 logger.info("Concurrency test starting");
920 final Instant startTime = Instant.now();
921 List<Thread> threads = new ArrayList<>();
922 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
923 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
925 addPolicyType("type1", "ric");
926 addPolicyType("type2", "ric");
928 for (int i = 0; i < 10; ++i) {
929 AsyncRestClient restClient = restClient();
930 ConcurrencyTestRunnable test =
931 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
932 Thread thread = new Thread(test, "TestThread_" + i);
937 for (Thread t : threads) {
940 for (ConcurrencyTestRunnable test : tests) {
941 assertThat(test.isFailed()).isFalse();
943 assertThat(policies.size()).isZero();
944 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
947 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
948 WebClientConfig config = this.applicationConfig.getWebClientConfig();
949 config = WebClientConfig.builder() //
950 .keyStoreType(config.getKeyStoreType()) //
951 .keyStorePassword(config.getKeyStorePassword()) //
952 .keyStore(config.getKeyStore()) //
953 .keyPassword(config.getKeyPassword()) //
954 .isTrustStoreUsed(useTrustValidation) //
955 .trustStore(config.getTrustStore()) //
956 .trustStorePassword(config.getTrustStorePassword()) //
957 .httpProxyConfig(config.getHttpProxyConfig()) //
960 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
961 return f.createRestClientNoHttpProxy(baseUrl);
965 private String baseUrl() {
966 return "https://localhost:" + port;
969 private AsyncRestClient restClient(boolean useTrustValidation) {
970 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
973 private AsyncRestClient restClient() {
974 return restClient(false);
977 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
978 testErrorCode(request, expStatus, "", true);
981 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
982 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
985 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
986 testErrorCode(request, expStatus, responseContains, true);
989 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
990 boolean expectApplicationProblemJsonMediaType) {
991 StepVerifier.create(request) //
992 .expectSubscription() //
994 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
998 private void waitForRicState(String ricId, RicState state) throws ServiceException {
999 Ric ric = rics.getRic(ricId);
1000 await().untilAsserted(() -> state.equals(ric.getState()));
1003 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1004 boolean expectApplicationProblemJsonMediaType) {
1005 assertTrue(throwable instanceof WebClientResponseException);
1006 WebClientResponseException responseException = (WebClientResponseException) throwable;
1007 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1008 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1009 if (expectApplicationProblemJsonMediaType) {
1010 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1015 private MockA1Client getA1Client(String ricId) throws ServiceException {
1016 return a1ClientFactory.getOrCreateA1Client(ricId);
1019 private PolicyType createPolicyType(String policyTypeName) {
1020 return PolicyType.builder() //
1021 .id(policyTypeName) //
1022 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1026 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1027 PolicyType type = createPolicyType(policyTypeName);
1028 policyTypes.put(type);
1029 addRic(ricId).addSupportedPolicyType(type);
1033 private Ric addRic(String ricId) {
1034 return addRic(ricId, null);
1037 private RicConfig ricConfig(String ricId, String managedElement) {
1038 List<String> mes = new ArrayList<>();
1039 if (managedElement != null) {
1040 mes.add(managedElement);
1042 return RicConfig.builder() //
1045 .managedElementIds(mes) //
1049 private Ric addRic(String ricId, String managedElement) {
1050 if (rics.get(ricId) != null) {
1051 return rics.get(ricId);
1054 RicConfig conf = ricConfig(ricId, managedElement);
1055 Ric ric = new Ric(conf);
1056 ric.setState(Ric.RicState.AVAILABLE);