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.ImmutableRicConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
60 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
61 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
73 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
74 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
75 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
76 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
77 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
78 import org.slf4j.Logger;
79 import org.slf4j.LoggerFactory;
80 import org.springframework.beans.factory.annotation.Autowired;
81 import org.springframework.boot.test.context.SpringBootTest;
82 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
83 import org.springframework.boot.test.context.TestConfiguration;
84 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
85 import org.springframework.boot.web.server.LocalServerPort;
86 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
87 import org.springframework.context.ApplicationContext;
88 import org.springframework.context.annotation.Bean;
89 import org.springframework.http.HttpStatus;
90 import org.springframework.http.MediaType;
91 import org.springframework.http.ResponseEntity;
92 import org.springframework.test.context.TestPropertySource;
93 import org.springframework.test.context.junit.jupiter.SpringExtension;
94 import org.springframework.web.reactive.function.client.WebClientResponseException;
96 import reactor.core.publisher.Mono;
97 import reactor.test.StepVerifier;
98 import reactor.util.annotation.Nullable;
100 @ExtendWith(SpringExtension.class)
101 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
102 @TestPropertySource(properties = { //
103 "server.ssl.key-store=./src/test/resources/keystore.jks", //
104 "app.webclient.trust-store=./src/test/resources/truststore.jks", //
105 "app.webclient.trust-store-used=true", //
106 "app.vardata-directory=./target/testdata", //
109 class ApplicationTest {
110 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
113 ApplicationContext context;
119 private Policies policies;
122 private PolicyTypes policyTypes;
125 MockA1ClientFactory a1ClientFactory;
128 RicSupervision supervision;
131 ApplicationConfig applicationConfig;
137 RappSimulatorController rAppSimulator;
140 RefreshConfigTask refreshConfigTask;
143 SecurityContext securityContext;
145 private static Gson gson = new GsonBuilder().create();
148 * Overrides the BeanFactory.
151 static class TestBeanFactory {
154 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
155 return new MockA1ClientFactory(appConfig, types);
159 public ServiceSupervision getServiceSupervision(@Autowired Services services,
160 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
161 Duration checkInterval = Duration.ofMillis(1);
162 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
166 public ServletWebServerFactory servletContainer() {
167 return new TomcatServletWebServerFactory();
180 a1ClientFactory.reset();
181 this.rAppSimulator.getTestResults().clear();
182 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
183 this.securityContext.setAuthTokenFilePath(null);
187 void verifyNoRicLocks() {
188 for (Ric ric : this.rics.getRics()) {
189 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "");
190 grant.unlockBlocking();
191 assertThat(ric.getLock().getLockCounter()).isZero();
192 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
197 void generateApiDoc() throws IOException {
198 String url = "https://localhost:" + this.port + "/v3/api-docs";
199 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
200 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
201 JSONObject jsonObj = new JSONObject(resp.getBody());
202 assertThat(jsonObj.remove("servers")).isNotNull();
204 String indented = (jsonObj).toString(4);
205 String docDir = "api/";
206 Files.createDirectories(Paths.get(docDir));
207 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
213 void testPersistencyPolicies() throws ServiceException {
214 Ric ric = this.addRic("ric1");
215 PolicyType type = this.addPolicyType("type1", ric.id());
217 final int noOfPolicies = 100;
218 for (int i = 0; i < noOfPolicies; ++i) {
219 addPolicy("id" + i, type.getId(), "service", ric.id());
223 Policies policies = new Policies(this.applicationConfig);
224 policies.restoreFromDatabase(ric, this.policyTypes);
225 assertThat(policies.size()).isEqualTo(noOfPolicies);
229 restClient().delete("/policies/id2").block();
230 Policies policies = new Policies(this.applicationConfig);
231 policies.restoreFromDatabase(ric, this.policyTypes);
232 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
237 void testPersistencyPolicyTypes() throws ServiceException {
238 Ric ric = this.addRic("ric1");
239 this.addPolicyType("type1", ric.id());
240 PolicyTypes types = new PolicyTypes(this.applicationConfig);
241 assertThat(types.size()).isEqualTo(1);
245 void testPersistencyService() throws ServiceException {
246 final String SERVICE = "serviceName";
247 putService(SERVICE, 1234, HttpStatus.CREATED);
248 assertThat(this.services.size()).isEqualTo(1);
249 Service service = this.services.getService(SERVICE);
251 Services servicesRestored = new Services(this.applicationConfig);
252 Service serviceRestored = servicesRestored.getService(SERVICE);
253 assertThat(servicesRestored.size()).isEqualTo(1);
254 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
255 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
257 // check that the service can be deleted
258 this.services.remove(SERVICE);
259 servicesRestored = new Services(this.applicationConfig);
260 assertThat(servicesRestored.size()).isZero();
264 void testAddingRicFromConfiguration() throws Exception {
265 // Test adding the RIC from configuration
267 final String RIC = "ric1";
268 final String TYPE = "type123";
269 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
270 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
271 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
273 putService("service");
275 refreshConfigTask.handleUpdatedRicConfig( //
276 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
278 waitForRicState(RIC, RicState.AVAILABLE);
280 // Test that the type has been synched
281 Ric addedRic = this.rics.getRic(RIC);
282 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
283 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
285 // Check that a service callback for the AVAILABLE RIC is invoked
286 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
287 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
288 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
289 assertThat(callbackInfo.ricId).isEqualTo(RIC);
290 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
294 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
295 putService("service");
297 final String RIC = "NonRespondingRic";
298 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
299 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
301 refreshConfigTask.handleUpdatedRicConfig( //
302 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
305 waitForRicState(RIC, RicState.UNAVAILABLE);
307 // Check that no service callback for the UNAVAILABLE RIC is invoked
308 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
309 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
311 // Run a synch and check that the AVAILABLE notification is received
312 a1ClientFactory.reset();
313 supervision.checkAllRics();
314 waitForRicState(RIC, RicState.AVAILABLE);
316 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
320 void testTrustValidation() {
322 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
323 assertThat(rsp).contains("ric1");
327 void testGetRics() throws Exception {
329 this.addPolicyType("type1", "ric1");
330 String url = "/rics?policytype_id=type1";
331 String rsp = restClient().get(url).block();
332 assertThat(rsp).contains("ric1");
334 // nameless type for ORAN A1 1.1
336 this.addPolicyType("", "ric2");
337 url = "/rics?policytype_id=";
338 rsp = restClient().get(url).block();
339 assertThat(rsp).contains("ric2") //
340 .doesNotContain("ric1") //
341 .contains("AVAILABLE");
344 rsp = restClient().get("/rics").block();
345 assertThat(rsp).contains("ric2") //
348 // Non existing policy type
349 url = "/rics?policytype_id=XXXX";
350 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
354 void testSynchronization() throws Exception {
355 // Two polictypes will be put in the NearRT RICs
356 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
357 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
358 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
359 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
361 // One type and one instance added to the Policy Management Service's storage
362 final String ric1Name = "ric1";
363 Ric ric1 = addRic(ric1Name);
364 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
365 Ric ric2 = addRic("ric2");
367 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
368 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
370 String policyId = "policyId";
371 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
372 supervision.checkAllRics(); // The created policy should be put in the RIC
374 // Wait until synch is completed
375 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
376 waitForRicState(ric1Name, RicState.AVAILABLE);
377 waitForRicState("ric2", RicState.AVAILABLE);
379 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
380 assertThat(ricPolicies.size()).isEqualTo(1);
381 Policy ricPolicy = ricPolicies.get(policyId);
382 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
384 // Both types should be in the Policy Management Service's storage after the
386 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
387 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
391 void testGetRic() throws Exception {
392 String ricId = "ric1";
393 String managedElementId = "kista_1";
394 addRic(ricId, managedElementId);
396 String url = "/rics/ric?managed_element_id=" + managedElementId;
397 String rsp = restClient().get(url).block();
398 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
399 assertThat(ricInfo.ricId).isEqualTo(ricId);
401 url = "/rics/ric?ric_id=" + ricId;
402 rsp = restClient().get(url).block();
403 ricInfo = gson.fromJson(rsp, RicInfo.class);
404 assertThat(ricInfo.ricId).isEqualTo(ricId);
406 // test GET RIC for ManagedElement that does not exist
407 url = "/rics/ric?managed_element_id=" + "junk";
408 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
411 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
414 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
415 boolean isTransient) {
416 PolicyInfo info = new PolicyInfo();
417 info.policyId = policyInstanceId;
418 info.policyTypeId = policyTypeName;
420 info.serviceId = serviceName;
421 info.policyData = gson.fromJson(jsonString(), Object.class);
424 info.isTransient = isTransient;
426 info.statusNotificationUri = "statusNotificationUri";
427 return gson.toJson(info);
430 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
431 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
435 void testPutPolicy() throws Exception {
436 String serviceName = "service.1";
437 String ricId = "ric.1";
438 String policyTypeName = "type1_1.2.3";
439 String policyInstanceId = "instance_1.2.3";
441 putService(serviceName);
442 addPolicyType(policyTypeName, ricId);
444 // PUT a transient policy
445 String url = "/policies";
446 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
447 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
449 restClient().put(url, policyBody).block();
451 Policy policy = policies.getPolicy(policyInstanceId);
452 assertThat(policy).isNotNull();
453 assertThat(policy.getId()).isEqualTo(policyInstanceId);
454 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
455 assertThat(policy.getRic().id()).isEqualTo(ricId);
456 assertThat(policy.isTransient()).isTrue();
458 // Put a non transient policy
459 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
460 restClient().put(url, policyBody).block();
461 policy = policies.getPolicy(policyInstanceId);
462 assertThat(policy.isTransient()).isFalse();
464 url = "/policy-instances";
465 String rsp = restClient().get(url).block();
466 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
468 url = "/policies/" + policyInstanceId;
469 rsp = restClient().get(url).block();
470 assertThat(rsp).contains(policyBody);
472 // Test of error codes
474 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
475 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
477 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
478 addPolicyType(policyTypeName + "XX", "otherRic");
479 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
481 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
482 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
483 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
484 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
489 * Test that HttpStatus and body from failing REST call to A1 is passed on to
492 * @throws ServiceException
494 void testErrorFromRic() throws ServiceException {
495 putService("service1");
496 addPolicyType("type1", "ric1");
498 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
499 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
500 String responseBody = "Refused";
501 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
503 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
504 responseBodyBytes, StandardCharsets.UTF_8, null);
505 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
508 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
509 String url = "/policies";
510 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
513 this.addPolicy("instance1", "type1", "service1", "ric1");
514 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
515 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
520 void testPutTypelessPolicy() throws Exception {
521 putService("service1");
522 addPolicyType("", "ric1");
523 String body = putPolicyBody("service1", "ric1", "", "id1");
524 restClient().put("/policies", body).block();
526 String rsp = restClient().get("/policy-instances").block();
527 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
528 assertThat(info.policies).hasSize(1);
529 PolicyInfo policyInfo = info.policies.iterator().next();
530 assertThat(policyInfo.policyId).isEqualTo("id1");
531 assertThat(policyInfo.policyTypeId).isEmpty();
535 void testRefuseToUpdatePolicy() throws Exception {
536 // Test that only the json can be changed for a already created policy
537 // In this case service is attempted to be changed
539 this.addRic("ricXXX");
540 this.addPolicy("instance1", "type1", "service1", "ric1");
541 this.addPolicy("instance2", "type1", "service1", "ricXXX");
543 // Try change ric1 -> ricXXX
544 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
545 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
549 void testGetPolicy() throws Exception {
550 String url = "/policies/id";
551 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
553 String rsp = restClient().get(url).block();
554 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
555 String policyStr = gson.toJson(info.policyData);
556 assertThat(policyStr).isEqualTo(policy.getJson());
559 policies.remove(policy);
560 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
565 void testDeletePolicy() throws Exception {
566 String policyId = "id.1";
567 addPolicy(policyId, "typeName", "service1", "ric1");
568 assertThat(policies.size()).isEqualTo(1);
570 String url = "/policies/" + policyId;
571 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
573 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
574 assertThat(policies.size()).isZero();
576 // Delete a non existing policy
577 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
581 void testGetPolicyType() throws Exception {
582 String typeId = "AC.D";
583 addPolicyType(typeId, "ric1");
585 waitForRicState("ric1", RicState.AVAILABLE);
587 String url = "/policy-types/" + typeId;
589 String rsp = this.restClient().get(url).block();
591 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
592 assertThat(info.schema).isNotNull();
594 // Get non existing schema
595 url = "/policy-types/JUNK";
596 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
599 String createPolicyTypesJson(String... types) {
600 List<String> list = new ArrayList<>();
601 Collections.addAll(list, types);
602 PolicyTypeIdList ids = new PolicyTypeIdList(list);
603 return gson.toJson(ids);
607 void testGetPolicyTypes() throws Exception {
608 String TYPE_ID_1 = "A_type1_1.9.0";
609 String TYPE_ID_2 = "A_type1_2.0.0";
610 String TYPE_ID_3 = "A_type1_1.5.0";
611 String TYPE_ID_4 = "type3_1.9.0";
612 addPolicyType(TYPE_ID_1, "ric1");
613 addPolicyType(TYPE_ID_2, "ric2");
614 addPolicyType(TYPE_ID_3, "ric2");
615 addPolicyType(TYPE_ID_4, "ric2");
617 addPolicyType("junk", "ric2");
618 addPolicyType("junk_a.b.c", "ric2");
620 String url = "/policy-types";
621 String rsp = restClient().get(url).block();
622 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
624 url = "/policy-types?ric_id=ric1";
625 rsp = restClient().get(url).block();
626 String expResp = createPolicyTypesJson(TYPE_ID_1);
627 assertThat(rsp).isEqualTo(expResp);
629 // Get policy types for non existing RIC
630 url = "/policy-types?ric_id=ric1XXX";
631 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
633 // All types with a type_name
634 url = "/policy-types?type_name=A_type1";
635 rsp = restClient().get(url).block();
636 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
638 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
639 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
640 rsp = restClient().get(url).block();
641 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
642 assertThat(rsp).isEqualTo(expResp);
644 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
645 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
647 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
648 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
650 url = "/policy-types?compatible_with_version=1.5.0";
651 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
655 void testGetPolicyInstances() throws Exception {
656 addPolicy("id1", "type1", "service1");
658 String url = "/policy-instances";
659 String rsp = restClient().get(url).block();
661 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
662 assertThat(info.policies).hasSize(1);
663 PolicyInfo policyInfo = info.policies.iterator().next();
664 assert (policyInfo.validate());
665 assertThat(policyInfo.policyId).isEqualTo("id1");
666 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
667 assertThat(policyInfo.serviceId).isEqualTo("service1");
671 void testGetPolicyInstancesFilter() throws Exception {
672 addPolicy("id1", "type1", "service1");
673 addPolicy("id2", "type1", "service2");
674 addPolicy("id3", "type2", "service1");
675 addPolicy("id4", "type1_1.0.0", "service1");
677 String url = "/policy-instances?policytype_id=type1";
678 String rsp = restClient().get(url).block();
680 assertThat(rsp).contains("id1") //
682 .doesNotContain("id3");
684 url = "/policy-instances?policytype_id=type1&service_id=service2";
685 rsp = restClient().get(url).block();
687 assertThat(rsp).doesNotContain("id1") //
689 .doesNotContain("id3");
691 url = "/policy-instances?type_name=type1";
692 rsp = restClient().get(url).block();
693 assertThat(rsp).contains("id1") //
695 .doesNotContain("id3") //
698 // Test get policies for non existing type
699 url = "/policy-instances?policytype_id=type1XXX";
700 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
702 // Test get policies for non existing RIC
703 url = "/policy-instances?ric_id=XXX";
704 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
708 void testGetPolicyIdsFilter() throws Exception {
709 addPolicy("id1", "type1", "service1", "ric1");
710 addPolicy("id2", "type1", "service2", "ric1");
711 addPolicy("id3", "type2", "service1", "ric1");
712 addPolicy("id4", "type1_1.0.0", "service1");
714 String url = "/policies?policytype_id=type1";
715 String rsp = restClient().get(url).block();
717 assertThat(rsp).contains("id1") //
719 .doesNotContain("id3");
721 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
722 rsp = restClient().get(url).block();
723 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
724 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
726 url = "/policies?policytype_name=type1&service_id=service1";
727 rsp = restClient().get(url).block();
728 assertThat(rsp).contains("id1") //
731 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
733 // Test get policy ids for non existing type
734 url = "/policies?policytype_id=type1XXX";
735 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
737 // Test get policy ids for non existing RIC
738 url = "/policies?ric_id=XXX";
739 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
743 void testPutAndGetService() throws Exception {
745 String serviceName = "ac.dc";
746 putService(serviceName, 0, HttpStatus.CREATED);
747 putService(serviceName, 0, HttpStatus.OK);
750 String url = "/services?service_id=" + serviceName;
751 String rsp = restClient().get(url).block();
752 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
753 assertThat(info.statusList).hasSize(1);
754 ServiceStatus status = info.statusList.iterator().next();
755 assertThat(status.keepAliveIntervalSeconds).isZero();
756 assertThat(status.serviceId).isEqualTo(serviceName);
760 rsp = restClient().get(url).block();
761 assertThat(rsp).as("Response contains service name").contains(serviceName);
765 url = "/services/" + serviceName + "/keepalive";
766 ResponseEntity<?> entity = restClient().putForEntity(url).block();
767 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
770 assertThat(services.size()).isEqualTo(1);
771 url = "/services/" + serviceName;
772 restClient().delete(url).block();
773 assertThat(services.size()).isZero();
775 // Keep alive, no registered service
776 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
778 // PUT service with bad payload
779 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
780 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
781 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
783 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
784 HttpStatus.BAD_REQUEST, false);
786 // GET non existing service
787 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
791 void testServiceSupervision() throws Exception {
792 putService("service1", 1, HttpStatus.CREATED);
793 addPolicyType("type1", "ric1");
795 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
796 restClient().put("/policies", policyBody).block();
798 assertThat(policies.size()).isEqualTo(1);
799 assertThat(services.size()).isEqualTo(1);
801 // Timeout after ~1 second
802 await().untilAsserted(() -> assertThat(policies.size()).isZero());
803 assertThat(services.size()).isZero();
807 void testGetPolicyStatus() throws Exception {
808 addPolicy("id", "typeName", "service1", "ric1");
809 assertThat(policies.size()).isEqualTo(1);
811 String url = "/policies/id/status";
812 String rsp = restClient().get(url).block();
813 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
814 assertThat(info.status).isEqualTo("OK");
816 // GET non existing policy status
817 url = "/policies/XXX/status";
818 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
820 // GET STATUS, the NearRT RIC returns error
821 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
822 url = "/policies/id/status";
823 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
824 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
825 rsp = restClient().get(url).block();
826 info = gson.fromJson(rsp, PolicyStatusInfo.class);
827 assertThat(info.status).hasToString("{}");
831 void testGetServiceStatus() throws Exception {
832 String url = "/status";
833 String rsp = restClient().get(url).block();
834 assertThat(rsp).contains("hunky dory");
836 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
837 assertThat(rsp).isEqualTo("hunky dory");
841 void testServiceNotification() throws Exception {
843 final String AUTH_TOKEN = "testToken";
844 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
845 Files.write(authFile, AUTH_TOKEN.getBytes());
846 this.securityContext.setAuthTokenFilePath(authFile);
848 putService("junkService");
849 Service junkService = this.services.get("junkService");
850 junkService.setCallbackUrl("https://junk");
851 putService("service");
853 Ric ric = addRic("ric1");
854 ric.setState(Ric.RicState.UNAVAILABLE);
855 supervision.checkAllRics();
856 waitForRicState("ric1", RicState.AVAILABLE);
858 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
859 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
860 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
861 assertThat(callbackInfo.ricId).isEqualTo("ric1");
862 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
864 var headers = receivedCallbacks.receivedHeaders.get(0);
865 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
867 Files.delete(authFile);
870 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
872 Policy policy = Policy.builder() //
874 .json(jsonString()) //
875 .ownerServiceId(service) //
876 .ric(rics.getRic(ric)) //
877 .type(addPolicyType(typeName, ric)) //
878 .lastModified(Instant.now()) //
879 .isTransient(false) //
880 .statusNotificationUri("/policy-status?id=XXX") //
882 policies.put(policy);
886 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
887 return addPolicy(id, typeName, service, "ric");
890 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
891 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
892 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
895 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
896 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
898 String json = gson.toJson(service);
902 private void putService(String name) {
903 putService(name, 0, null);
906 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
907 String url = "/services";
908 String body = createServiceJson(name, keepAliveIntervalSeconds);
909 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
910 if (expectedStatus != null) {
911 assertEquals(expectedStatus, resp.getStatusCode(), "");
915 private String jsonString() {
916 return "{\"servingCellNrcgi\":\"1\"}";
920 void testConcurrency() throws Exception {
921 logger.info("Concurrency test starting");
922 final Instant startTime = Instant.now();
923 List<Thread> threads = new ArrayList<>();
924 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
925 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
927 addPolicyType("type1", "ric");
928 addPolicyType("type2", "ric");
930 for (int i = 0; i < 10; ++i) {
931 AsyncRestClient restClient = restClient();
932 ConcurrencyTestRunnable test =
933 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
934 Thread thread = new Thread(test, "TestThread_" + i);
939 for (Thread t : threads) {
942 for (ConcurrencyTestRunnable test : tests) {
943 assertThat(test.isFailed()).isFalse();
945 assertThat(policies.size()).isZero();
946 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
949 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
950 WebClientConfig config = this.applicationConfig.getWebClientConfig();
951 config = ImmutableWebClientConfig.builder() //
952 .keyStoreType(config.keyStoreType()) //
953 .keyStorePassword(config.keyStorePassword()) //
954 .keyStore(config.keyStore()) //
955 .keyPassword(config.keyPassword()) //
956 .isTrustStoreUsed(useTrustValidation) //
957 .trustStore(config.trustStore()) //
958 .trustStorePassword(config.trustStorePassword()) //
959 .httpProxyConfig(config.httpProxyConfig()) //
962 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
963 return f.createRestClientNoHttpProxy(baseUrl);
967 private String baseUrl() {
968 return "https://localhost:" + port;
971 private AsyncRestClient restClient(boolean useTrustValidation) {
972 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
975 private AsyncRestClient restClient() {
976 return restClient(false);
979 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
980 testErrorCode(request, expStatus, "", true);
983 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
984 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
987 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
988 testErrorCode(request, expStatus, responseContains, true);
991 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
992 boolean expectApplicationProblemJsonMediaType) {
993 StepVerifier.create(request) //
994 .expectSubscription() //
996 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1000 private void waitForRicState(String ricId, RicState state) throws ServiceException {
1001 Ric ric = rics.getRic(ricId);
1002 await().untilAsserted(() -> state.equals(ric.getState()));
1005 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1006 boolean expectApplicationProblemJsonMediaType) {
1007 assertTrue(throwable instanceof WebClientResponseException);
1008 WebClientResponseException responseException = (WebClientResponseException) throwable;
1009 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1010 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1011 if (expectApplicationProblemJsonMediaType) {
1012 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1017 private MockA1Client getA1Client(String ricId) throws ServiceException {
1018 return a1ClientFactory.getOrCreateA1Client(ricId);
1021 private PolicyType createPolicyType(String policyTypeName) {
1022 return PolicyType.builder() //
1023 .id(policyTypeName) //
1024 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1028 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1029 PolicyType type = createPolicyType(policyTypeName);
1030 policyTypes.put(type);
1031 addRic(ricId).addSupportedPolicyType(type);
1035 private Ric addRic(String ricId) {
1036 return addRic(ricId, null);
1039 private RicConfig ricConfig(String ricId, String managedElement) {
1040 List<String> mes = new ArrayList<>();
1041 if (managedElement != null) {
1042 mes.add(managedElement);
1044 return ImmutableRicConfig.builder() //
1047 .managedElementIds(mes) //
1048 .controllerName("") //
1052 private Ric addRic(String ricId, String managedElement) {
1053 if (rics.get(ricId) != null) {
1054 return rics.get(ricId);
1057 RicConfig conf = ricConfig(ricId, managedElement);
1058 Ric ric = new Ric(conf);
1059 ric.setState(Ric.RicState.AVAILABLE);