2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 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.Paths;
39 import java.time.Duration;
40 import java.time.Instant;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
45 import org.json.JSONObject;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.Test;
48 import org.junit.jupiter.api.extension.ExtendWith;
49 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
52 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
53 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
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.vardata-directory=./target/testdata", //
106 class ApplicationTest {
107 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
110 ApplicationContext context;
116 private Policies policies;
119 private PolicyTypes policyTypes;
122 MockA1ClientFactory a1ClientFactory;
125 RicSupervision supervision;
128 ApplicationConfig applicationConfig;
134 RappSimulatorController rAppSimulator;
137 RefreshConfigTask refreshConfigTask;
139 private static Gson gson = new GsonBuilder().create();
142 * Overrides the BeanFactory.
145 static class TestBeanFactory {
148 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
149 return new MockA1ClientFactory(appConfig, types);
153 public ServiceSupervision getServiceSupervision(@Autowired Services services,
154 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
155 Duration checkInterval = Duration.ofMillis(1);
156 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
160 public ServletWebServerFactory servletContainer() {
161 return new TomcatServletWebServerFactory();
174 a1ClientFactory.reset();
175 this.rAppSimulator.getTestResults().clear();
176 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
180 void verifyNoRicLocks() {
181 for (Ric ric : this.rics.getRics()) {
182 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "");
183 grant.unlockBlocking();
184 assertThat(ric.getLock().getLockCounter()).isZero();
185 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
190 void generateApiDoc() throws IOException {
191 String url = "https://localhost:" + this.port + "/v3/api-docs";
192 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
193 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
194 JSONObject jsonObj = new JSONObject(resp.getBody());
195 assertThat(jsonObj.remove("servers")).isNotNull();
197 String indented = (jsonObj).toString(4);
198 String docDir = "api/";
199 Files.createDirectories(Paths.get(docDir));
200 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
206 void testPersistencyPolicies() throws ServiceException {
207 Ric ric = this.addRic("ric1");
208 PolicyType type = this.addPolicyType("type1", ric.id());
210 final int noOfPolicies = 100;
211 for (int i = 0; i < noOfPolicies; ++i) {
212 addPolicy("id" + i, type.getId(), "service", ric.id());
216 Policies policies = new Policies(this.applicationConfig);
217 policies.restoreFromDatabase(ric, this.policyTypes);
218 assertThat(policies.size()).isEqualTo(noOfPolicies);
222 restClient().delete("/policies/id2").block();
223 Policies policies = new Policies(this.applicationConfig);
224 policies.restoreFromDatabase(ric, this.policyTypes);
225 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
230 void testPersistencyPolicyTypes() throws ServiceException {
231 Ric ric = this.addRic("ric1");
232 this.addPolicyType("type1", ric.id());
233 PolicyTypes types = new PolicyTypes(this.applicationConfig);
234 assertThat(types.size()).isEqualTo(1);
238 void testPersistencyService() throws ServiceException {
239 final String SERVICE = "serviceName";
240 putService(SERVICE, 1234, HttpStatus.CREATED);
241 assertThat(this.services.size()).isEqualTo(1);
242 Service service = this.services.getService(SERVICE);
244 Services servicesRestored = new Services(this.applicationConfig);
245 Service serviceRestored = servicesRestored.getService(SERVICE);
246 assertThat(servicesRestored.size()).isEqualTo(1);
247 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
248 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
250 // check that the service can be deleted
251 this.services.remove(SERVICE);
252 servicesRestored = new Services(this.applicationConfig);
253 assertThat(servicesRestored.size()).isZero();
257 void testAddingRicFromConfiguration() throws Exception {
258 // Test adding the RIC from configuration
260 final String RIC = "ric1";
261 final String TYPE = "type123";
262 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
263 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
264 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
266 putService("service");
268 refreshConfigTask.handleUpdatedRicConfig( //
269 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
271 waitForRicState(RIC, RicState.AVAILABLE);
273 // Test that the type has been synched
274 Ric addedRic = this.rics.getRic(RIC);
275 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
276 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
278 // Check that a service callback for the AVAILABLE RIC is invoked
279 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
280 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
281 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
282 assertThat(callbackInfo.ricId).isEqualTo(RIC);
283 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
287 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
288 putService("service");
290 final String RIC = "NonRespondingRic";
291 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
292 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
294 refreshConfigTask.handleUpdatedRicConfig( //
295 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
298 waitForRicState(RIC, RicState.UNAVAILABLE);
300 // Check that no service callback for the UNAVAILABLE RIC is invoked
301 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
302 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
304 // Run a synch and check that the AVAILABLE notification is received
305 a1ClientFactory.reset();
306 supervision.checkAllRics();
307 waitForRicState(RIC, RicState.AVAILABLE);
309 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
313 void testTrustValidation() {
315 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
316 assertThat(rsp).contains("ric1");
320 void testGetRics() throws Exception {
322 this.addPolicyType("type1", "ric1");
323 String url = "/rics?policytype_id=type1";
324 String rsp = restClient().get(url).block();
325 assertThat(rsp).contains("ric1");
327 // nameless type for ORAN A1 1.1
329 this.addPolicyType("", "ric2");
330 url = "/rics?policytype_id=";
331 rsp = restClient().get(url).block();
332 assertThat(rsp).contains("ric2") //
333 .doesNotContain("ric1") //
334 .contains("AVAILABLE");
337 rsp = restClient().get("/rics").block();
338 assertThat(rsp).contains("ric2") //
341 // Non existing policy type
342 url = "/rics?policytype_id=XXXX";
343 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
347 void testSynchronization() throws Exception {
348 // Two polictypes will be put in the NearRT RICs
349 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
350 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
351 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
352 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
354 // One type and one instance added to the Policy Management Service's storage
355 final String ric1Name = "ric1";
356 Ric ric1 = addRic(ric1Name);
357 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
358 Ric ric2 = addRic("ric2");
360 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
361 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
363 String policyId = "policyId";
364 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
365 supervision.checkAllRics(); // The created policy should be put in the RIC
367 // Wait until synch is completed
368 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
369 waitForRicState(ric1Name, RicState.AVAILABLE);
370 waitForRicState("ric2", RicState.AVAILABLE);
372 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
373 assertThat(ricPolicies.size()).isEqualTo(1);
374 Policy ricPolicy = ricPolicies.get(policyId);
375 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
377 // Both types should be in the Policy Management Service's storage after the
379 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
380 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
384 void testGetRic() throws Exception {
385 String ricId = "ric1";
386 String managedElementId = "kista_1";
387 addRic(ricId, managedElementId);
389 String url = "/rics/ric?managed_element_id=" + managedElementId;
390 String rsp = restClient().get(url).block();
391 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
392 assertThat(ricInfo.ricId).isEqualTo(ricId);
394 url = "/rics/ric?ric_id=" + ricId;
395 rsp = restClient().get(url).block();
396 ricInfo = gson.fromJson(rsp, RicInfo.class);
397 assertThat(ricInfo.ricId).isEqualTo(ricId);
399 // test GET RIC for ManagedElement that does not exist
400 url = "/rics/ric?managed_element_id=" + "junk";
401 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
404 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
407 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
408 boolean isTransient) {
409 PolicyInfo info = new PolicyInfo();
410 info.policyId = policyInstanceId;
411 info.policyTypeId = policyTypeName;
413 info.serviceId = serviceName;
414 info.policyData = gson.fromJson(jsonString(), Object.class);
417 info.isTransient = isTransient;
419 info.statusNotificationUri = "statusNotificationUri";
420 return gson.toJson(info);
423 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
424 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
428 void testPutPolicy() throws Exception {
429 String serviceName = "service.1";
430 String ricId = "ric.1";
431 String policyTypeName = "type1_1.2.3";
432 String policyInstanceId = "instance_1.2.3";
434 putService(serviceName);
435 addPolicyType(policyTypeName, ricId);
437 // PUT a transient policy
438 String url = "/policies";
439 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
440 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
442 restClient().put(url, policyBody).block();
444 Policy policy = policies.getPolicy(policyInstanceId);
445 assertThat(policy).isNotNull();
446 assertThat(policy.getId()).isEqualTo(policyInstanceId);
447 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
448 assertThat(policy.getRic().id()).isEqualTo(ricId);
449 assertThat(policy.isTransient()).isTrue();
451 // Put a non transient policy
452 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
453 restClient().put(url, policyBody).block();
454 policy = policies.getPolicy(policyInstanceId);
455 assertThat(policy.isTransient()).isFalse();
457 url = "/policy-instances";
458 String rsp = restClient().get(url).block();
459 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
461 url = "/policies/" + policyInstanceId;
462 rsp = restClient().get(url).block();
463 assertThat(rsp).contains(policyBody);
465 // Test of error codes
467 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
468 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
470 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
471 addPolicyType(policyTypeName + "XX", "otherRic");
472 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
474 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
475 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
476 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
477 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
482 * Test that HttpStatus and body from failing REST call to A1 is passed on to
485 * @throws ServiceException
487 void testErrorFromRic() throws ServiceException {
488 putService("service1");
489 addPolicyType("type1", "ric1");
491 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
492 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
493 String responseBody = "Refused";
494 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
496 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
497 responseBodyBytes, StandardCharsets.UTF_8, null);
498 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
501 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
502 String url = "/policies";
503 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
506 this.addPolicy("instance1", "type1", "service1", "ric1");
507 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
508 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
513 void testPutTypelessPolicy() throws Exception {
514 putService("service1");
515 addPolicyType("", "ric1");
516 String body = putPolicyBody("service1", "ric1", "", "id1");
517 restClient().put("/policies", body).block();
519 String rsp = restClient().get("/policy-instances").block();
520 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
521 assertThat(info.policies).hasSize(1);
522 PolicyInfo policyInfo = info.policies.iterator().next();
523 assertThat(policyInfo.policyId).isEqualTo("id1");
524 assertThat(policyInfo.policyTypeId).isEmpty();
528 void testRefuseToUpdatePolicy() throws Exception {
529 // Test that only the json can be changed for a already created policy
530 // In this case service is attempted to be changed
532 this.addRic("ricXXX");
533 this.addPolicy("instance1", "type1", "service1", "ric1");
534 this.addPolicy("instance2", "type1", "service1", "ricXXX");
536 // Try change ric1 -> ricXXX
537 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
538 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
542 void testGetPolicy() throws Exception {
543 String url = "/policies/id";
544 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
546 String rsp = restClient().get(url).block();
547 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
548 String policyStr = gson.toJson(info.policyData);
549 assertThat(policyStr).isEqualTo(policy.getJson());
552 policies.remove(policy);
553 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
558 void testDeletePolicy() throws Exception {
559 String policyId = "id.1";
560 addPolicy(policyId, "typeName", "service1", "ric1");
561 assertThat(policies.size()).isEqualTo(1);
563 String url = "/policies/" + policyId;
564 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
566 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
567 assertThat(policies.size()).isZero();
569 // Delete a non existing policy
570 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
574 void testGetPolicyType() throws Exception {
575 String typeId = "AC.D";
576 addPolicyType(typeId, "ric1");
578 waitForRicState("ric1", RicState.AVAILABLE);
580 String url = "/policy-types/" + typeId;
582 String rsp = this.restClient().get(url).block();
584 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
585 assertThat(info.schema).isNotNull();
587 // Get non existing schema
588 url = "/policy-types/JUNK";
589 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
592 String createPolicyTypesJson(String... types) {
593 List<String> list = new ArrayList<>();
594 Collections.addAll(list, types);
595 PolicyTypeIdList ids = new PolicyTypeIdList(list);
596 return gson.toJson(ids);
600 void testGetPolicyTypes() throws Exception {
601 String TYPE_ID_1 = "A_type1_1.9.0";
602 String TYPE_ID_2 = "A_type1_2.0.0";
603 String TYPE_ID_3 = "A_type1_1.5.0";
604 String TYPE_ID_4 = "type3_1.9.0";
605 addPolicyType(TYPE_ID_1, "ric1");
606 addPolicyType(TYPE_ID_2, "ric2");
607 addPolicyType(TYPE_ID_3, "ric2");
608 addPolicyType(TYPE_ID_4, "ric2");
610 addPolicyType("junk", "ric2");
611 addPolicyType("junk_a.b.c", "ric2");
613 String url = "/policy-types";
614 String rsp = restClient().get(url).block();
615 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
617 url = "/policy-types?ric_id=ric1";
618 rsp = restClient().get(url).block();
619 String expResp = createPolicyTypesJson(TYPE_ID_1);
620 assertThat(rsp).isEqualTo(expResp);
622 // Get policy types for non existing RIC
623 url = "/policy-types?ric_id=ric1XXX";
624 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
626 // All types with a type_name
627 url = "/policy-types?type_name=A_type1";
628 rsp = restClient().get(url).block();
629 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
631 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
632 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
633 rsp = restClient().get(url).block();
634 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
635 assertThat(rsp).isEqualTo(expResp);
637 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
638 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
640 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
641 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
643 url = "/policy-types?compatible_with_version=1.5.0";
644 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
648 void testGetPolicyInstances() throws Exception {
649 addPolicy("id1", "type1", "service1");
651 String url = "/policy-instances";
652 String rsp = restClient().get(url).block();
654 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
655 assertThat(info.policies).hasSize(1);
656 PolicyInfo policyInfo = info.policies.iterator().next();
657 assert (policyInfo.validate());
658 assertThat(policyInfo.policyId).isEqualTo("id1");
659 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
660 assertThat(policyInfo.serviceId).isEqualTo("service1");
664 void testGetPolicyInstancesFilter() throws Exception {
665 addPolicy("id1", "type1", "service1");
666 addPolicy("id2", "type1", "service2");
667 addPolicy("id3", "type2", "service1");
668 addPolicy("id4", "type1_1.0.0", "service1");
670 String url = "/policy-instances?policytype_id=type1";
671 String rsp = restClient().get(url).block();
673 assertThat(rsp).contains("id1") //
675 .doesNotContain("id3");
677 url = "/policy-instances?policytype_id=type1&service_id=service2";
678 rsp = restClient().get(url).block();
680 assertThat(rsp).doesNotContain("id1") //
682 .doesNotContain("id3");
684 url = "/policy-instances?type_name=type1";
685 rsp = restClient().get(url).block();
686 assertThat(rsp).contains("id1") //
688 .doesNotContain("id3") //
691 // Test get policies for non existing type
692 url = "/policy-instances?policytype_id=type1XXX";
693 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
695 // Test get policies for non existing RIC
696 url = "/policy-instances?ric_id=XXX";
697 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
701 void testGetPolicyIdsFilter() throws Exception {
702 addPolicy("id1", "type1", "service1", "ric1");
703 addPolicy("id2", "type1", "service2", "ric1");
704 addPolicy("id3", "type2", "service1", "ric1");
705 addPolicy("id4", "type1_1.0.0", "service1");
707 String url = "/policies?policytype_id=type1";
708 String rsp = restClient().get(url).block();
710 assertThat(rsp).contains("id1") //
712 .doesNotContain("id3");
714 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
715 rsp = restClient().get(url).block();
716 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
717 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
719 url = "/policies?policytype_name=type1&service_id=service1";
720 rsp = restClient().get(url).block();
721 assertThat(rsp).contains("id1") //
724 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
726 // Test get policy ids for non existing type
727 url = "/policies?policytype_id=type1XXX";
728 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
730 // Test get policy ids for non existing RIC
731 url = "/policies?ric_id=XXX";
732 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
736 void testPutAndGetService() throws Exception {
738 String serviceName = "ac.dc";
739 putService(serviceName, 0, HttpStatus.CREATED);
740 putService(serviceName, 0, HttpStatus.OK);
743 String url = "/services?service_id=" + serviceName;
744 String rsp = restClient().get(url).block();
745 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
746 assertThat(info.statusList).hasSize(1);
747 ServiceStatus status = info.statusList.iterator().next();
748 assertThat(status.keepAliveIntervalSeconds).isZero();
749 assertThat(status.serviceId).isEqualTo(serviceName);
753 rsp = restClient().get(url).block();
754 assertThat(rsp).as("Response contains service name").contains(serviceName);
758 url = "/services/" + serviceName + "/keepalive";
759 ResponseEntity<?> entity = restClient().putForEntity(url).block();
760 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
763 assertThat(services.size()).isEqualTo(1);
764 url = "/services/" + serviceName;
765 restClient().delete(url).block();
766 assertThat(services.size()).isZero();
768 // Keep alive, no registered service
769 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
771 // PUT service with bad payload
772 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
773 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
774 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
776 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
777 HttpStatus.BAD_REQUEST, false);
779 // GET non existing service
780 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
784 void testServiceSupervision() throws Exception {
785 putService("service1", 1, HttpStatus.CREATED);
786 addPolicyType("type1", "ric1");
788 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
789 restClient().put("/policies", policyBody).block();
791 assertThat(policies.size()).isEqualTo(1);
792 assertThat(services.size()).isEqualTo(1);
794 // Timeout after ~1 second
795 await().untilAsserted(() -> assertThat(policies.size()).isZero());
796 assertThat(services.size()).isZero();
800 void testGetPolicyStatus() throws Exception {
801 addPolicy("id", "typeName", "service1", "ric1");
802 assertThat(policies.size()).isEqualTo(1);
804 String url = "/policies/id/status";
805 String rsp = restClient().get(url).block();
806 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
807 assertThat(info.status).isEqualTo("OK");
809 // GET non existing policy status
810 url = "/policies/XXX/status";
811 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
813 // GET STATUS, the NearRT RIC returns error
814 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
815 url = "/policies/id/status";
816 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
817 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
818 rsp = restClient().get(url).block();
819 info = gson.fromJson(rsp, PolicyStatusInfo.class);
820 assertThat(info.status).hasToString("{}");
824 void testGetServiceStatus() throws Exception {
825 String url = "/status";
826 String rsp = restClient().get(url).block();
827 assertThat(rsp).contains("hunky dory");
829 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
830 assertThat(rsp).isEqualTo("hunky dory");
834 void testServiceNotification() throws ServiceException {
835 putService("junkService");
836 Service junkService = this.services.get("junkService");
837 junkService.setCallbackUrl("https://junk");
838 putService("service");
840 Ric ric = addRic("ric1");
841 ric.setState(Ric.RicState.UNAVAILABLE);
842 supervision.checkAllRics();
843 waitForRicState("ric1", RicState.AVAILABLE);
845 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
846 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
847 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
848 assertThat(callbackInfo.ricId).isEqualTo("ric1");
849 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
852 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
854 Policy policy = Policy.builder() //
856 .json(jsonString()) //
857 .ownerServiceId(service) //
858 .ric(rics.getRic(ric)) //
859 .type(addPolicyType(typeName, ric)) //
860 .lastModified(Instant.now()) //
861 .isTransient(false) //
862 .statusNotificationUri("/policy-status?id=XXX") //
864 policies.put(policy);
868 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
869 return addPolicy(id, typeName, service, "ric");
872 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
873 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
874 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
877 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
878 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
880 String json = gson.toJson(service);
884 private void putService(String name) {
885 putService(name, 0, null);
888 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
889 String url = "/services";
890 String body = createServiceJson(name, keepAliveIntervalSeconds);
891 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
892 if (expectedStatus != null) {
893 assertEquals(expectedStatus, resp.getStatusCode(), "");
897 private String jsonString() {
898 return "{\"servingCellNrcgi\":\"1\"}";
902 void testConcurrency() throws Exception {
903 logger.info("Concurrency test starting");
904 final Instant startTime = Instant.now();
905 List<Thread> threads = new ArrayList<>();
906 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
907 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
909 addPolicyType("type1", "ric");
910 addPolicyType("type2", "ric");
912 for (int i = 0; i < 10; ++i) {
913 AsyncRestClient restClient = restClient();
914 ConcurrencyTestRunnable test =
915 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
916 Thread thread = new Thread(test, "TestThread_" + i);
921 for (Thread t : threads) {
924 for (ConcurrencyTestRunnable test : tests) {
925 assertThat(test.isFailed()).isFalse();
927 assertThat(policies.size()).isZero();
928 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
931 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
932 WebClientConfig config = this.applicationConfig.getWebClientConfig();
933 config = ImmutableWebClientConfig.builder() //
934 .keyStoreType(config.keyStoreType()) //
935 .keyStorePassword(config.keyStorePassword()) //
936 .keyStore(config.keyStore()) //
937 .keyPassword(config.keyPassword()) //
938 .isTrustStoreUsed(useTrustValidation) //
939 .trustStore(config.trustStore()) //
940 .trustStorePassword(config.trustStorePassword()) //
941 .httpProxyConfig(config.httpProxyConfig()) //
944 AsyncRestClientFactory f = new AsyncRestClientFactory(config);
945 return f.createRestClientNoHttpProxy(baseUrl);
949 private String baseUrl() {
950 return "https://localhost:" + port;
953 private AsyncRestClient restClient(boolean useTrustValidation) {
954 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
957 private AsyncRestClient restClient() {
958 return restClient(false);
961 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
962 testErrorCode(request, expStatus, "", true);
965 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
966 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
969 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
970 testErrorCode(request, expStatus, responseContains, true);
973 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
974 boolean expectApplicationProblemJsonMediaType) {
975 StepVerifier.create(request) //
976 .expectSubscription() //
978 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
982 private void waitForRicState(String ricId, RicState state) throws ServiceException {
983 Ric ric = rics.getRic(ricId);
984 await().untilAsserted(() -> state.equals(ric.getState()));
987 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
988 boolean expectApplicationProblemJsonMediaType) {
989 assertTrue(throwable instanceof WebClientResponseException);
990 WebClientResponseException responseException = (WebClientResponseException) throwable;
991 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
992 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
993 if (expectApplicationProblemJsonMediaType) {
994 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
999 private MockA1Client getA1Client(String ricId) throws ServiceException {
1000 return a1ClientFactory.getOrCreateA1Client(ricId);
1003 private PolicyType createPolicyType(String policyTypeName) {
1004 return PolicyType.builder() //
1005 .id(policyTypeName) //
1006 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1010 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1011 PolicyType type = createPolicyType(policyTypeName);
1012 policyTypes.put(type);
1013 addRic(ricId).addSupportedPolicyType(type);
1017 private Ric addRic(String ricId) {
1018 return addRic(ricId, null);
1021 private RicConfig ricConfig(String ricId, String managedElement) {
1022 List<String> mes = new ArrayList<>();
1023 if (managedElement != null) {
1024 mes.add(managedElement);
1026 return ImmutableRicConfig.builder() //
1029 .managedElementIds(mes) //
1030 .controllerName("") //
1034 private Ric addRic(String ricId, String managedElement) {
1035 if (rics.get(ricId) != null) {
1036 return rics.get(ricId);
1039 RicConfig conf = ricConfig(ricId, managedElement);
1040 Ric ric = new Ric(conf);
1041 ric.setState(Ric.RicState.AVAILABLE);