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.ImmutableRicConfig;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
58 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
69 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
70 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
71 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
72 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
73 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76 import org.springframework.beans.factory.annotation.Autowired;
77 import org.springframework.boot.test.context.SpringBootTest;
78 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
79 import org.springframework.boot.test.context.TestConfiguration;
80 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
81 import org.springframework.boot.web.server.LocalServerPort;
82 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
83 import org.springframework.context.ApplicationContext;
84 import org.springframework.context.annotation.Bean;
85 import org.springframework.http.HttpStatus;
86 import org.springframework.http.MediaType;
87 import org.springframework.http.ResponseEntity;
88 import org.springframework.test.context.TestPropertySource;
89 import org.springframework.test.context.junit.jupiter.SpringExtension;
90 import org.springframework.web.reactive.function.client.WebClientResponseException;
92 import reactor.core.publisher.Mono;
93 import reactor.test.StepVerifier;
94 import reactor.util.annotation.Nullable;
96 @ExtendWith(SpringExtension.class)
97 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
98 @TestPropertySource(properties = { //
99 "server.ssl.key-store=./config/keystore.jks", //
100 "app.webclient.trust-store=./config/truststore.jks", //
101 "app.vardata-directory=./target/testdata", //
104 class ApplicationTest {
105 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
108 ApplicationContext context;
114 private Policies policies;
117 private PolicyTypes policyTypes;
120 MockA1ClientFactory a1ClientFactory;
123 RicSupervision supervision;
126 ApplicationConfig applicationConfig;
132 RappSimulatorController rAppSimulator;
135 RefreshConfigTask refreshConfigTask;
137 private static Gson gson = new GsonBuilder().create();
140 * Overrides the BeanFactory.
143 static class TestBeanFactory {
146 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
147 return new MockA1ClientFactory(appConfig, types);
151 public ServiceSupervision getServiceSupervision(@Autowired Services services,
152 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
153 Duration checkInterval = Duration.ofMillis(1);
154 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
158 public ServletWebServerFactory servletContainer() {
159 return new TomcatServletWebServerFactory();
172 a1ClientFactory.reset();
173 this.rAppSimulator.getTestResults().clear();
174 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
178 void verifyNoRicLocks() {
179 for (Ric ric : this.rics.getRics()) {
180 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
181 ric.getLock().unlockBlocking();
182 assertThat(ric.getLock().getLockCounter()).isZero();
183 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
188 void generateApiDoc() throws IOException {
189 String url = "https://localhost:" + this.port + "/v3/api-docs";
190 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
191 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
192 JSONObject jsonObj = new JSONObject(resp.getBody());
193 assertThat(jsonObj.remove("servers")).isNotNull();
195 String indented = (jsonObj).toString(4);
196 String docDir = "api/";
197 Files.createDirectories(Paths.get(docDir));
198 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
204 void testPersistencyPolicies() throws ServiceException {
205 Ric ric = this.addRic("ric1");
206 PolicyType type = this.addPolicyType("type1", ric.id());
208 final int noOfPolicies = 100;
209 for (int i = 0; i < noOfPolicies; ++i) {
210 addPolicy("id" + i, type.getId(), "service", ric.id());
214 Policies policies = new Policies(this.applicationConfig);
215 policies.restoreFromDatabase(ric, this.policyTypes);
216 assertThat(policies.size()).isEqualTo(noOfPolicies);
220 restClient().delete("/policies/id2").block();
221 Policies policies = new Policies(this.applicationConfig);
222 policies.restoreFromDatabase(ric, this.policyTypes);
223 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
228 void testPersistencyPolicyTypes() throws ServiceException {
229 Ric ric = this.addRic("ric1");
230 this.addPolicyType("type1", ric.id());
231 PolicyTypes types = new PolicyTypes(this.applicationConfig);
232 assertThat(types.size()).isEqualTo(1);
236 void testPersistencyService() throws ServiceException {
237 final String SERVICE = "serviceName";
238 putService(SERVICE, 1234, HttpStatus.CREATED);
239 assertThat(this.services.size()).isEqualTo(1);
240 Service service = this.services.getService(SERVICE);
242 Services servicesRestored = new Services(this.applicationConfig);
243 Service serviceRestored = servicesRestored.getService(SERVICE);
244 assertThat(servicesRestored.size()).isEqualTo(1);
245 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
246 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
248 // check that the service can be deleted
249 this.services.remove(SERVICE);
250 servicesRestored = new Services(this.applicationConfig);
251 assertThat(servicesRestored.size()).isEqualTo(0);
255 void testAddingRicFromConfiguration() throws Exception {
256 // Test adding the RIC from configuration
258 final String RIC = "ric1";
259 final String TYPE = "type123";
260 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
261 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
262 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
264 putService("service");
266 RicConfig config = ricConfig(RIC, "me1");
267 ApplicationConfig.RicConfigUpdate update =
268 new ApplicationConfig.RicConfigUpdate(config, ApplicationConfig.RicConfigUpdate.Type.ADDED);
269 refreshConfigTask.handleUpdatedRicConfig(update).block();
270 waitForRicState(RIC, RicState.AVAILABLE);
272 // Test that the type has been synched
273 Ric addedRic = this.rics.getRic(RIC);
274 assertThat(addedRic.getSupportedPolicyTypes().size()).isEqualTo(1);
275 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
277 // Check that a service callback for the AVAILABLE RIC is invoked
278 RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
279 assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1);
280 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
281 assertThat(callbackInfo.ricId).isEqualTo(RIC);
282 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
286 void testGetRics() throws Exception {
288 this.addPolicyType("type1", "ric1");
289 String url = "/rics?policytype_id=type1";
290 String rsp = restClient().get(url).block();
291 assertThat(rsp).contains("ric1");
293 // nameless type for ORAN A1 1.1
295 this.addPolicyType("", "ric2");
296 url = "/rics?policytype_id=";
298 // This tests also validation of trusted certs restClient(true)
299 rsp = restClient(true).get(url).block();
300 assertThat(rsp).contains("ric2") //
301 .doesNotContain("ric1") //
302 .contains("AVAILABLE");
305 rsp = restClient().get("/rics").block();
306 assertThat(rsp).contains("ric2") //
309 // Non existing policy type
310 url = "/rics?policytype_id=XXXX";
311 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
315 void testSynchronization() throws Exception {
316 // Two polictypes will be put in the NearRT RICs
317 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
318 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
319 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
320 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
322 // One type and one instance added to the Policy Management Service's storage
323 final String ric1Name = "ric1";
324 Ric ric1 = addRic(ric1Name);
325 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
326 Ric ric2 = addRic("ric2");
328 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
329 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
331 String policyId = "policyId";
332 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
333 supervision.checkAllRics(); // The created policy should be put in the RIC
335 // Wait until synch is completed
336 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
337 waitForRicState(ric1Name, RicState.AVAILABLE);
338 waitForRicState("ric2", RicState.AVAILABLE);
340 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
341 assertThat(ricPolicies.size()).isEqualTo(1);
342 Policy ricPolicy = ricPolicies.get(policyId);
343 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
345 // Both types should be in the Policy Management Service's storage after the
347 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
348 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
352 void testGetRic() throws Exception {
353 String ricId = "ric1";
354 String managedElementId = "kista_1";
355 addRic(ricId, managedElementId);
357 String url = "/rics/ric?managed_element_id=" + managedElementId;
358 String rsp = restClient().get(url).block();
359 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
360 assertThat(ricInfo.ricId).isEqualTo(ricId);
362 url = "/rics/ric?ric_id=" + ricId;
363 rsp = restClient().get(url).block();
364 ricInfo = gson.fromJson(rsp, RicInfo.class);
365 assertThat(ricInfo.ricId).isEqualTo(ricId);
367 // test GET RIC for ManagedElement that does not exist
368 url = "/rics/ric?managed_element_id=" + "junk";
369 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
372 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
375 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
376 boolean isTransient) {
377 PolicyInfo info = new PolicyInfo();
378 info.policyId = policyInstanceId;
379 info.policyTypeId = policyTypeName;
381 info.serviceId = serviceName;
382 info.policyData = gson.fromJson(jsonString(), Object.class);
385 info.isTransient = isTransient;
387 info.statusNotificationUri = "statusNotificationUri";
388 return gson.toJson(info);
391 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
392 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
396 void testPutPolicy() throws Exception {
397 String serviceName = "service.1";
398 String ricId = "ric.1";
399 String policyTypeName = "type1_1.2.3";
400 String policyInstanceId = "instance_1.2.3";
402 putService(serviceName);
403 addPolicyType(policyTypeName, ricId);
405 // PUT a transient policy
406 String url = "/policies";
407 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
408 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
410 restClient().put(url, policyBody).block();
412 Policy policy = policies.getPolicy(policyInstanceId);
413 assertThat(policy).isNotNull();
414 assertThat(policy.getId()).isEqualTo(policyInstanceId);
415 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
416 assertThat(policy.getRic().id()).isEqualTo(ricId);
417 assertThat(policy.isTransient()).isTrue();
419 // Put a non transient policy
420 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
421 restClient().put(url, policyBody).block();
422 policy = policies.getPolicy(policyInstanceId);
423 assertThat(policy.isTransient()).isFalse();
425 url = "/policy-instances";
426 String rsp = restClient().get(url).block();
427 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
429 url = "/policies/" + policyInstanceId;
430 rsp = restClient().get(url).block();
431 assertThat(rsp).contains(policyBody);
433 // Test of error codes
435 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
436 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
438 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
439 addPolicyType(policyTypeName + "XX", "otherRic");
440 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
442 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
443 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
444 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
445 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
450 * Test that HttpStatus and body from failing REST call to A1 is passed on to
453 * @throws ServiceException
455 void testErrorFromRic() throws ServiceException {
456 putService("service1");
457 addPolicyType("type1", "ric1");
459 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
460 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
461 String responseBody = "Refused";
462 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
464 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
465 responseBodyBytes, StandardCharsets.UTF_8, null);
466 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
469 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
470 String url = "/policies";
471 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
474 this.addPolicy("instance1", "type1", "service1", "ric1");
475 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
476 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
481 void testPutTypelessPolicy() throws Exception {
482 putService("service1");
483 addPolicyType("", "ric1");
484 String body = putPolicyBody("service1", "ric1", "", "id1");
485 restClient().put("/policies", body).block();
487 String rsp = restClient().get("/policy-instances").block();
488 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
489 assertThat(info.policies).hasSize(1);
490 PolicyInfo policyInfo = info.policies.iterator().next();
491 assertThat(policyInfo.policyId).isEqualTo("id1");
492 assertThat(policyInfo.policyTypeId).isEmpty();
496 void testRefuseToUpdatePolicy() throws Exception {
497 // Test that only the json can be changed for a already created policy
498 // In this case service is attempted to be changed
500 this.addRic("ricXXX");
501 this.addPolicy("instance1", "type1", "service1", "ric1");
502 this.addPolicy("instance2", "type1", "service1", "ricXXX");
504 // Try change ric1 -> ricXXX
505 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
506 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
510 void testGetPolicy() throws Exception {
511 String url = "/policies/id";
512 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
514 String rsp = restClient().get(url).block();
515 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
516 String policyStr = gson.toJson(info.policyData);
517 assertThat(policyStr).isEqualTo(policy.getJson());
520 policies.remove(policy);
521 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
526 void testDeletePolicy() throws Exception {
527 String policyId = "id.1";
528 addPolicy(policyId, "typeName", "service1", "ric1");
529 assertThat(policies.size()).isEqualTo(1);
531 String url = "/policies/" + policyId;
532 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
534 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
535 assertThat(policies.size()).isZero();
537 // Delete a non existing policy
538 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
542 void testGetPolicyType() throws Exception {
543 String typeId = "AC.D";
544 addPolicyType(typeId, "ric1");
546 waitForRicState("ric1", RicState.AVAILABLE);
548 String url = "/policy-types/" + typeId;
550 String rsp = this.restClient().get(url).block();
552 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
553 assertThat(info.schema).isNotNull();
555 // Get non existing schema
556 url = "/policy-types/JUNK";
557 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
560 String createPolicyTypesJson(String... types) {
561 List<String> list = new ArrayList<>();
562 Collections.addAll(list, types);
563 PolicyTypeIdList ids = new PolicyTypeIdList(list);
564 return gson.toJson(ids);
568 void testGetPolicyTypes() throws Exception {
569 addPolicyType("type1", "ric1");
570 addPolicyType("type2", "ric2");
572 String url = "/policy-types";
573 String rsp = restClient().get(url).block();
574 String expResp = createPolicyTypesJson("type2", "type1");
575 assertThat(rsp).isEqualTo(expResp);
577 url = "/policy-types?ric_id=ric1";
578 rsp = restClient().get(url).block();
579 expResp = createPolicyTypesJson("type1");
580 assertThat(rsp).isEqualTo(expResp);
582 // Get policy types for non existing RIC
583 url = "/policy-types?ric_id=ric1XXX";
584 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
588 void testGetPolicyInstances() throws Exception {
589 addPolicy("id1", "type1", "service1");
591 String url = "/policy-instances";
592 String rsp = restClient().get(url).block();
594 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
595 assertThat(info.policies).hasSize(1);
596 PolicyInfo policyInfo = info.policies.iterator().next();
597 assert (policyInfo.validate());
598 assertThat(policyInfo.policyId).isEqualTo("id1");
599 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
600 assertThat(policyInfo.serviceId).isEqualTo("service1");
604 void testGetPolicyInstancesFilter() throws Exception {
605 addPolicy("id1", "type1", "service1");
606 addPolicy("id2", "type1", "service2");
607 addPolicy("id3", "type2", "service1");
609 String url = "/policy-instances?policytype_id=type1";
610 String rsp = restClient().get(url).block();
612 assertThat(rsp).contains("id1") //
614 .doesNotContain("id3");
616 url = "/policy-instances?policytype_id=type1&service_id=service2";
617 rsp = restClient().get(url).block();
619 assertThat(rsp).doesNotContain("id1") //
621 .doesNotContain("id3");
623 // Test get policies for non existing type
624 url = "/policy-instances?policytype_id=type1XXX";
625 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
627 // Test get policies for non existing RIC
628 url = "/policy-instances?ric_id=XXX";
629 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
633 void testGetPolicyIdsFilter() throws Exception {
634 addPolicy("id1", "type1", "service1", "ric1");
635 addPolicy("id2", "type1", "service2", "ric1");
636 addPolicy("id3", "type2", "service1", "ric1");
638 String url = "/policies?policytype_id=type1";
639 String rsp = restClient().get(url).block();
641 assertThat(rsp).contains("id1") //
643 .doesNotContain("id3");
645 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
646 rsp = restClient().get(url).block();
647 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
648 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
650 // Test get policy ids for non existing type
651 url = "/policies?policytype_id=type1XXX";
652 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
654 // Test get policy ids for non existing RIC
655 url = "/policies?ric_id=XXX";
656 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
660 void testPutAndGetService() throws Exception {
662 String serviceName = "ac.dc";
663 putService(serviceName, 0, HttpStatus.CREATED);
664 putService(serviceName, 0, HttpStatus.OK);
667 String url = "/services?service_id=" + serviceName;
668 String rsp = restClient().get(url).block();
669 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
670 assertThat(info.statusList).hasSize(1);
671 ServiceStatus status = info.statusList.iterator().next();
672 assertThat(status.keepAliveIntervalSeconds).isZero();
673 assertThat(status.serviceId).isEqualTo(serviceName);
677 rsp = restClient().get(url).block();
678 assertThat(rsp).as("Response contains service name").contains(serviceName);
682 url = "/services/" + serviceName + "/keepalive";
683 ResponseEntity<?> entity = restClient().putForEntity(url).block();
684 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
687 assertThat(services.size()).isEqualTo(1);
688 url = "/services/" + serviceName;
689 restClient().delete(url).block();
690 assertThat(services.size()).isZero();
692 // Keep alive, no registered service
693 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
695 // PUT service with bad payload
696 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
697 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
698 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
700 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
701 HttpStatus.BAD_REQUEST, false);
703 // GET non existing service
704 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
708 void testServiceSupervision() throws Exception {
709 putService("service1", 1, HttpStatus.CREATED);
710 addPolicyType("type1", "ric1");
712 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
713 restClient().put("/policies", policyBody).block();
715 assertThat(policies.size()).isEqualTo(1);
716 assertThat(services.size()).isEqualTo(1);
718 // Timeout after ~1 second
719 await().untilAsserted(() -> assertThat(policies.size()).isZero());
720 assertThat(services.size()).isZero();
724 void testGetPolicyStatus() throws Exception {
725 addPolicy("id", "typeName", "service1", "ric1");
726 assertThat(policies.size()).isEqualTo(1);
728 String url = "/policies/id/status";
729 String rsp = restClient().get(url).block();
730 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
731 assertThat(info.status).isEqualTo("OK");
733 // GET non existing policy status
734 url = "/policies/XXX/status";
735 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
737 // GET STATUS, the NearRT RIC returns error
738 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
739 url = "/policies/id/status";
740 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
741 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
742 rsp = restClient().get(url).block();
743 info = gson.fromJson(rsp, PolicyStatusInfo.class);
744 assertThat(info.status).hasToString("{}");
748 void testServiceNotification() throws ServiceException {
749 putService("junkService");
750 Service junkService = this.services.get("junkService");
751 junkService.setCallbackUrl("https://junk");
752 putService("service");
754 Ric ric = addRic("ric1");
755 ric.setState(Ric.RicState.UNAVAILABLE);
756 supervision.checkAllRics();
757 waitForRicState("ric1", RicState.AVAILABLE);
759 RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
760 assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1);
761 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
762 assertThat(callbackInfo.ricId).isEqualTo("ric1");
763 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
766 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
768 Policy policy = Policy.builder() //
770 .json(jsonString()) //
771 .ownerServiceId(service) //
772 .ric(rics.getRic(ric)) //
773 .type(addPolicyType(typeName, ric)) //
774 .lastModified(Instant.now()) //
775 .isTransient(false) //
776 .statusNotificationUri("/policy-status?id=XXX") //
778 policies.put(policy);
782 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
783 return addPolicy(id, typeName, service, "ric");
786 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
787 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
788 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
791 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
792 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
794 String json = gson.toJson(service);
798 private void putService(String name) {
799 putService(name, 0, null);
802 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
803 String url = "/services";
804 String body = createServiceJson(name, keepAliveIntervalSeconds);
805 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
806 if (expectedStatus != null) {
807 assertEquals(expectedStatus, resp.getStatusCode(), "");
811 private String jsonString() {
812 return "{\"servingCellNrcgi\":\"1\"}";
816 void testConcurrency() throws Exception {
817 logger.info("Concurrency test starting");
818 final Instant startTime = Instant.now();
819 List<Thread> threads = new ArrayList<>();
820 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
821 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
823 addPolicyType("type1", "ric");
824 addPolicyType("type2", "ric");
826 for (int i = 0; i < 10; ++i) {
827 AsyncRestClient restClient = restClient();
828 ConcurrencyTestRunnable test =
829 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
830 Thread thread = new Thread(test, "TestThread_" + i);
835 for (Thread t : threads) {
838 for (ConcurrencyTestRunnable test : tests) {
839 assertThat(test.isFailed()).isFalse();
841 assertThat(policies.size()).isZero();
842 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
845 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
846 WebClientConfig config = this.applicationConfig.getWebClientConfig();
847 config = ImmutableWebClientConfig.builder() //
848 .keyStoreType(config.keyStoreType()) //
849 .keyStorePassword(config.keyStorePassword()) //
850 .keyStore(config.keyStore()) //
851 .keyPassword(config.keyPassword()) //
852 .isTrustStoreUsed(useTrustValidation) //
853 .trustStore(config.trustStore()) //
854 .trustStorePassword(config.trustStorePassword()) //
855 .httpProxyConfig(config.httpProxyConfig()) //
858 AsyncRestClientFactory f = new AsyncRestClientFactory(config);
859 return f.createRestClientNoHttpProxy(baseUrl);
863 private String baseUrl() {
864 return "https://localhost:" + port;
867 private AsyncRestClient restClient(boolean useTrustValidation) {
868 String baseUrl = "https://localhost:" + port + Consts.V2_API_ROOT;
869 return restClient(baseUrl, useTrustValidation);
872 private AsyncRestClient restClient() {
873 return restClient(false);
876 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
877 testErrorCode(request, expStatus, "", true);
880 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
881 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
884 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
885 testErrorCode(request, expStatus, responseContains, true);
888 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
889 boolean expectApplicationProblemJsonMediaType) {
890 StepVerifier.create(request) //
891 .expectSubscription() //
893 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
897 private void waitForRicState(String ricId, RicState state) throws ServiceException {
898 Ric ric = rics.getRic(ricId);
899 await().untilAsserted(() -> state.equals(ric.getState()));
902 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
903 boolean expectApplicationProblemJsonMediaType) {
904 assertTrue(throwable instanceof WebClientResponseException);
905 WebClientResponseException responseException = (WebClientResponseException) throwable;
906 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
907 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
908 if (expectApplicationProblemJsonMediaType) {
909 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
914 private MockA1Client getA1Client(String ricId) throws ServiceException {
915 return a1ClientFactory.getOrCreateA1Client(ricId);
918 private PolicyType createPolicyType(String policyTypeName) {
919 return PolicyType.builder() //
920 .id(policyTypeName) //
921 .schema("{\"title\":\"" + policyTypeName + "\"}") //
925 private PolicyType addPolicyType(String policyTypeName, String ricId) {
926 PolicyType type = createPolicyType(policyTypeName);
927 policyTypes.put(type);
928 addRic(ricId).addSupportedPolicyType(type);
932 private Ric addRic(String ricId) {
933 return addRic(ricId, null);
936 private RicConfig ricConfig(String ricId, String managedElement) {
937 List<String> mes = new ArrayList<>();
938 if (managedElement != null) {
939 mes.add(managedElement);
941 return ImmutableRicConfig.builder() //
944 .managedElementIds(mes) //
945 .controllerName("") //
949 private Ric addRic(String ricId, String managedElement) {
950 if (rics.get(ricId) != null) {
951 return rics.get(ricId);
954 RicConfig conf = ricConfig(ricId, managedElement);
955 Ric ric = new Ric(conf);
956 ric.setState(Ric.RicState.AVAILABLE);