2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2022 Nordix Foundation. All rights reserved.
6 * ======================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
33 import java.io.FileOutputStream;
34 import java.io.PrintStream;
35 import java.lang.invoke.MethodHandles;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.time.Duration;
41 import java.time.Instant;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
46 import org.json.JSONObject;
47 import org.junit.jupiter.api.AfterAll;
48 import org.junit.jupiter.api.AfterEach;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.DisplayName;
51 import org.junit.jupiter.api.MethodOrderer;
52 import org.junit.jupiter.api.Test;
53 import org.junit.jupiter.api.TestMethodOrder;
54 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
55 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
56 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
57 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
60 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
61 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
62 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
63 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
64 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest;
65 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
66 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
73 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
74 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
75 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
76 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
77 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
78 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
79 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
80 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
81 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
82 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
83 import org.slf4j.Logger;
84 import org.slf4j.LoggerFactory;
85 import org.springframework.beans.factory.annotation.Autowired;
86 import org.springframework.boot.test.context.SpringBootTest;
87 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
88 import org.springframework.boot.test.context.TestConfiguration;
89 import org.springframework.boot.test.web.server.LocalServerPort;
90 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
91 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
92 import org.springframework.context.ApplicationContext;
93 import org.springframework.context.annotation.Bean;
94 import org.springframework.http.HttpStatus;
95 import org.springframework.http.MediaType;
96 import org.springframework.http.ResponseEntity;
97 import org.springframework.test.context.TestPropertySource;
98 import org.springframework.util.FileSystemUtils;
99 import org.springframework.web.reactive.function.client.WebClientRequestException;
100 import org.springframework.web.reactive.function.client.WebClientResponseException;
102 import reactor.core.publisher.Mono;
103 import reactor.test.StepVerifier;
104 import reactor.util.annotation.Nullable;
106 @TestMethodOrder(MethodOrderer.MethodName.class)
107 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
108 @TestPropertySource(properties = { //
109 "server.ssl.key-store=./config/keystore.jks", //
110 "app.webclient.trust-store=./config/truststore.jks", //
111 "app.webclient.trust-store-used=true", //
112 "app.vardata-directory=/tmp/pmstest", //
114 "app.s3.bucket=" // If this is set, S3 will be used to store data.
116 class ApplicationTest {
117 private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
120 ApplicationContext context;
126 private Policies policies;
129 private PolicyTypes policyTypes;
132 MockA1ClientFactory a1ClientFactory;
135 RicSupervision supervision;
138 ApplicationConfig applicationConfig;
144 RappSimulatorController rAppSimulator;
147 RefreshConfigTask refreshConfigTask;
150 SecurityContext securityContext;
153 OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
155 private static Gson gson = new GsonBuilder().create();
158 * Overrides the BeanFactory.
161 static class TestBeanFactory {
164 A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
165 return new MockA1ClientFactory(appConfig, types);
169 public ServiceSupervision getServiceSupervision(@Autowired Services services,
170 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
171 Duration checkInterval = Duration.ofMillis(1);
172 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
176 public ServletWebServerFactory servletContainer() {
177 return new TomcatServletWebServerFactory();
186 this.applicationConfig.setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
195 a1ClientFactory.reset();
196 this.rAppSimulator.getTestResults().clear();
197 this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
198 this.securityContext.setAuthTokenFilePath(null);
199 this.openPolicyAgentSimulatorController.getTestResults().reset();
203 static void clearTestDir() {
205 FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
206 } catch (Exception e) {
207 logger.warn("Could test directory : {}", e.getMessage());
212 void verifyNoRicLocks() {
213 for (Ric ric : this.rics.getRics()) {
214 Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
215 grant.unlockBlocking();
216 assertThat(ric.getLock().getLockCounter()).isZero();
217 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
222 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
223 @DisplayName("test ZZ Actuator")
224 void testZZActuator() throws Exception {
225 // The test must be run last, hence the "ZZ" in the name. All succeeding tests
227 AsyncRestClient client = restClient(baseUrl(), false);
229 client.post("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice",
230 "{\"configuredLevel\":\"trace\"}").block();
232 String resp = client.get("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice").block();
233 assertThat(resp).contains("TRACE");
235 client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
237 // This will stop the web server and all coming tests will fail.
238 client.post("/actuator/shutdown", "").block();
242 StepVerifier.create(restClient().get("/rics")) // Any call
243 .expectSubscription() //
244 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
250 @DisplayName("test generate Api Doc")
251 void generateApiDoc() throws Exception {
252 String url = "https://localhost:" + this.port + "/v3/api-docs";
253 ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
254 assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
255 JSONObject jsonObj = new JSONObject(resp.getBody());
256 assertThat(jsonObj.remove("servers")).isNotNull();
258 String indented = (jsonObj).toString(4);
259 String docDir = "api/";
260 Files.createDirectories(Paths.get(docDir));
261 try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
267 @DisplayName("test Persistency Policies")
268 void testPersistencyPolicies() throws Exception {
269 Ric ric = this.addRic("ric1");
270 PolicyType type = this.addPolicyType("type1", ric.id());
272 final int noOfPolicies = 100;
273 for (int i = 0; i < noOfPolicies; ++i) {
274 addPolicy("id" + i, type.getId(), "service", ric.id());
279 Policies policies = new Policies(this.applicationConfig);
280 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
281 assertThat(policies.size()).isEqualTo(noOfPolicies);
285 restClient().delete("/policies/id2").block();
286 Policies policies = new Policies(this.applicationConfig);
287 policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
288 assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
293 @DisplayName("test Persistency Policy Types")
294 void testPersistencyPolicyTypes() throws Exception {
295 Ric ric = this.addRic("ric1");
296 this.addPolicyType("type1", ric.id());
299 PolicyTypes types = new PolicyTypes(this.applicationConfig);
300 types.restoreFromDatabase().blockLast();
301 assertThat(types.size()).isEqualTo(1);
304 @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
305 private void waitforS3() throws Exception {
306 if (applicationConfig.isS3Enabled()) {
312 @DisplayName("test Persistency Service")
313 void testPersistencyService() throws Exception {
314 final String SERVICE = "serviceName";
315 putService(SERVICE, 1234, HttpStatus.CREATED);
316 assertThat(this.services.size()).isEqualTo(1);
317 Service service = this.services.getService(SERVICE);
320 Services servicesRestored = new Services(this.applicationConfig);
321 servicesRestored.restoreFromDatabase().blockLast();
322 Service serviceRestored = servicesRestored.getService(SERVICE);
323 assertThat(servicesRestored.size()).isEqualTo(1);
324 assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
325 assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
327 // check that the service can be deleted
328 this.services.remove(SERVICE);
329 servicesRestored = new Services(this.applicationConfig);
330 assertThat(servicesRestored.size()).isZero();
334 @DisplayName("test Adding Ric From Configuration")
335 void testAddingRicFromConfiguration() throws Exception {
336 // Test adding the RIC from configuration
338 final String RIC = "ric1";
339 final String TYPE = "type123";
340 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
341 nearRtRicPolicyTypes.put(createPolicyType(TYPE));
342 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
344 putService("service");
346 refreshConfigTask.handleUpdatedRicConfig( //
347 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
349 waitForRicState(RIC, RicState.AVAILABLE);
351 // Test that the type has been synched
352 Ric addedRic = this.rics.getRic(RIC);
353 assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
354 assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
356 // Check that a service callback for the AVAILABLE RIC is invoked
357 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
358 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
359 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
360 assertThat(callbackInfo.ricId).isEqualTo(RIC);
361 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
365 @DisplayName("test Adding Ric From Configuration non Responding Ric")
366 void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
367 putService("service");
369 final String RIC = "NonRespondingRic";
370 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
371 doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
373 refreshConfigTask.handleUpdatedRicConfig( //
374 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
377 waitForRicState(RIC, RicState.UNAVAILABLE);
379 // Check that no service callback for the UNAVAILABLE RIC is invoked
380 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
381 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
383 // Run a synch and check that the AVAILABLE notification is received
384 a1ClientFactory.reset();
385 supervision.checkAllRics();
386 waitForRicState(RIC, RicState.AVAILABLE);
388 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
392 @DisplayName("test Trust Validation")
393 void testTrustValidation() {
396 String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
397 assertThat(rsp).contains("ric1");
402 @DisplayName("test Get Rics")
403 void testGetRics() throws Exception {
405 this.addPolicyType("type1", "ric1");
406 String url = "/rics?policytype_id=type1";
407 String rsp = restClient().get(url).block();
408 assertThat(rsp).contains("ric1");
410 // nameless type for ORAN A1 1.1
412 this.addPolicyType("", "ric2");
413 url = "/rics?policytype_id=";
414 rsp = restClient().get(url).block();
415 assertThat(rsp).contains("ric2") //
416 .doesNotContain("ric1") //
417 .contains("AVAILABLE");
420 rsp = restClient().get("/rics").block();
421 assertThat(rsp).contains("ric2") //
424 // Non existing policy type
425 url = "/rics?policytype_id=XXXX";
426 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
430 @DisplayName("test Synchronization")
431 void testSynchronization() throws Exception {
432 // Two polictypes will be put in the NearRT RICs
433 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
434 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
435 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
436 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
438 // One type and one instance added to the Policy Management Service's storage
439 final String ric1Name = "ric1";
440 Ric ric1 = addRic(ric1Name);
441 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
442 Ric ric2 = addRic("ric2");
444 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
445 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
447 String policyId = "policyId";
448 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
449 supervision.checkAllRics(); // The created policy should be put in the RIC
451 // Wait until synch is completed
452 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
453 waitForRicState(ric1Name, RicState.AVAILABLE);
454 waitForRicState("ric2", RicState.AVAILABLE);
456 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
457 assertThat(ricPolicies.size()).isEqualTo(1);
458 Policy ricPolicy = ricPolicies.get(policyId);
459 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
461 // Both types should be in the Policy Management Service's storage after the
463 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
464 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
468 @DisplayName("test Get Ric")
469 void testGetRic() throws Exception {
470 String ricId = "ric1";
471 String managedElementId = "kista_1";
472 addRic(ricId, managedElementId);
474 String url = "/rics/ric?managed_element_id=" + managedElementId;
475 String rsp = restClient().get(url).block();
476 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
477 assertThat(ricInfo.ricId).isEqualTo(ricId);
479 url = "/rics/ric?ric_id=" + ricId;
480 rsp = restClient().get(url).block();
481 ricInfo = gson.fromJson(rsp, RicInfo.class);
482 assertThat(ricInfo.ricId).isEqualTo(ricId);
484 // test GET RIC for ManagedElement that does not exist
485 url = "/rics/ric?managed_element_id=" + "junk";
486 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
489 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
492 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
493 boolean isTransient, String statusNotificationUri) {
494 PolicyInfo info = new PolicyInfo();
495 info.policyId = policyInstanceId;
496 info.policyTypeId = policyTypeName;
498 info.serviceId = serviceName;
499 info.policyData = gson.fromJson(jsonString(), Object.class);
502 info.isTransient = isTransient;
504 info.statusNotificationUri = statusNotificationUri;
505 return gson.toJson(info);
508 private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
509 return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
513 @DisplayName("test Put Policy")
514 void testPutPolicy() throws Exception {
515 String serviceName = "service.1";
516 String ricId = "ric.1";
517 String policyTypeName = "type1_1.2.3";
518 String policyInstanceId = "instance_1.2.3";
520 putService(serviceName);
521 addPolicyType(policyTypeName, ricId);
523 // PUT a transient policy
524 String url = "/policies";
525 String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
526 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
528 restClient().put(url, policyBody).block();
530 // Check the authorization request
531 OpenPolicyAgentSimulatorController.TestResults res =
532 this.openPolicyAgentSimulatorController.getTestResults();
533 assertThat(res.receivedRequests).hasSize(1);
534 PolicyAuthorizationRequest req = res.receivedRequests.get(0);
535 assertThat(req.getInput().getAccessType()).isEqualTo(AccessType.WRITE);
536 assertThat(req.getInput().getPolicyTypeId()).isEqualTo(policyTypeName);
539 Policy policy = policies.getPolicy(policyInstanceId);
540 assertThat(policy).isNotNull();
541 assertThat(policy.getId()).isEqualTo(policyInstanceId);
542 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
543 assertThat(policy.getRic().id()).isEqualTo(ricId);
544 assertThat(policy.isTransient()).isTrue();
546 // Put a non transient policy
547 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
548 restClient().put(url, policyBody).block();
549 policy = policies.getPolicy(policyInstanceId);
550 assertThat(policy.isTransient()).isFalse();
552 url = "/policy-instances";
553 String rsp = restClient().get(url).block();
554 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
556 url = "/policies/" + policyInstanceId;
557 rsp = restClient().get(url).block();
558 assertThat(rsp).contains(policyBody);
560 // Test of error codes
562 policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
563 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
565 policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
566 addPolicyType(policyTypeName + "XX", "otherRic");
567 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
569 policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
570 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
571 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
572 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
576 void testFineGrainedAuth() throws Exception {
577 final String POLICY_ID = "policyId";
578 final String RIC_ID = "ric1";
579 final String TYPE_ID = "typeName";
580 addPolicy(POLICY_ID, TYPE_ID, null, RIC_ID);
581 assertThat(policies.size()).isEqualTo(1);
583 this.applicationConfig
584 .setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL_REJECT);
586 String url = "/policy-instances";
587 String rsp = restClient().get(url).block();
588 assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
590 url = "/policies/" + POLICY_ID;
591 testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
594 String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
595 testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
597 rsp = restClient().get(url).block();
598 assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
602 void testFineGrainedAuth_OPA_UNAVALIABLE() throws Exception {
603 final String POLICY_ID = "policyId";
604 final String RIC_ID = "ric1";
605 final String TYPE_ID = "typeName";
606 addPolicy(POLICY_ID, TYPE_ID, null, RIC_ID);
607 assertThat(policies.size()).isEqualTo(1);
609 this.applicationConfig.setAuthProviderUrl("junk");
611 String url = "/policy-instances";
612 String rsp = restClient().get(url).block();
613 assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
615 url = "/policies/" + POLICY_ID;
616 testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
619 String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
620 testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
622 rsp = restClient().get(url).block();
623 assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
627 @DisplayName("test Put Policy No Service No Status Uri")
628 void testPutPolicy_NoServiceNoStatusUri() throws Exception {
629 String ricId = "ric.1";
630 String policyTypeName = "type1_1.2.3";
631 String policyInstanceId = "instance_1.2.3";
633 addPolicyType(policyTypeName, ricId);
635 // PUT a transient policy
636 String url = "/policies";
637 String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
638 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
640 restClient().put(url, policyBody).block();
642 Policy policy = policies.getPolicy(policyInstanceId);
643 assertThat(policy).isNotNull();
644 assertThat(policy.getOwnerServiceId()).isBlank();
645 assertThat(policy.getStatusNotificationUri()).isBlank();
650 * Test that HttpStatus and body from failing REST call to A1 is passed on to
653 * @throws ServiceException
655 @DisplayName("test Error From Ric")
656 void testErrorFromRic() throws ServiceException {
657 putService("service1");
658 addPolicyType("type1", "ric1");
660 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
661 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
662 String responseBody = "Refused";
663 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
665 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
666 responseBodyBytes, StandardCharsets.UTF_8, null);
667 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
670 String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
671 String url = "/policies";
672 testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
675 this.addPolicy("instance1", "type1", "service1", "ric1");
676 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
677 testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
682 @DisplayName("test Put Typeless Policy")
683 void testPutTypelessPolicy() throws Exception {
684 putService("service1");
685 addPolicyType("", "ric1");
686 String body = putPolicyBody("service1", "ric1", "", "id1");
687 restClient().put("/policies", body).block();
689 String rsp = restClient().get("/policy-instances").block();
690 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
691 assertThat(info.policies).hasSize(1);
692 PolicyInfo policyInfo = info.policies.iterator().next();
693 assertThat(policyInfo.policyId).isEqualTo("id1");
694 assertThat(policyInfo.policyTypeId).isEmpty();
698 @DisplayName("test Update Service")
699 void testUpdateService() throws Exception {
701 this.addPolicy("p", "type1", "", "ric1");
703 String url = "/policies?service_id=";
704 String resp = restClient().get(url).block();
705 assertThat(resp).contains("[\"p\"]");
707 this.addPolicy("p", "type1", "service", "ric1");
708 url = "/policies?service_id=";
709 resp = restClient().get(url).block();
710 assertThat(resp).contains("[]");
712 url = "/policies?service_id=service";
713 resp = restClient().get(url).block();
714 assertThat(resp).contains("[\"p\"]");
718 @DisplayName("test Refuse To Update Policy")
719 void testRefuseToUpdatePolicy() throws Exception {
720 // Test that only the json can be changed for a already created policy
721 // In this case service is attempted to be changed
723 this.addRic("ricXXX");
724 this.addPolicy("instance1", "type1", "service1", "ric1");
725 this.addPolicy("instance2", "type1", "service1", "ricXXX");
727 // Try change ric1 -> ricXXX
728 String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
729 testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
733 @DisplayName("test Get Policy")
734 void testGetPolicy() throws Exception {
735 String url = "/policies/id";
736 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
738 String rsp = restClient().get(url).block();
739 PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
740 String policyStr = gson.toJson(info.policyData);
741 assertThat(policyStr).isEqualTo(policy.getJson());
744 policies.remove(policy);
745 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
750 @DisplayName("test Delete Policy")
751 void testDeletePolicy() throws Exception {
752 String policyId = "id.1";
753 addPolicy(policyId, "typeName", "service1", "ric1");
754 assertThat(policies.size()).isEqualTo(1);
756 String url = "/policies/" + policyId;
757 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
759 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
760 assertThat(policies.size()).isZero();
762 // Delete a non existing policy
763 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
767 @DisplayName("test Get Policy Type")
768 void testGetPolicyType() throws Exception {
769 String typeId = "AC.D";
770 addPolicyType(typeId, "ric1");
772 waitForRicState("ric1", RicState.AVAILABLE);
774 String url = "/policy-types/" + typeId;
776 String rsp = this.restClient().get(url).block();
778 PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
779 assertThat(info.schema).isNotNull();
781 // Get non existing schema
782 url = "/policy-types/JUNK";
783 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
786 String createPolicyTypesJson(String... types) {
787 List<String> list = new ArrayList<>();
788 Collections.addAll(list, types);
789 PolicyTypeIdList ids = new PolicyTypeIdList(list);
790 return gson.toJson(ids);
794 @DisplayName("test Get Policy Types")
795 void testGetPolicyTypes() throws Exception {
796 String TYPE_ID_1 = "A_type1_1.9.0";
797 String TYPE_ID_2 = "A_type1_2.0.0";
798 String TYPE_ID_3 = "A_type1_1.5.0";
799 String TYPE_ID_4 = "type3_1.9.0";
800 addPolicyType(TYPE_ID_1, "ric1");
801 addPolicyType(TYPE_ID_2, "ric2");
802 addPolicyType(TYPE_ID_3, "ric2");
803 addPolicyType(TYPE_ID_4, "ric2");
805 addPolicyType("junk", "ric2");
806 addPolicyType("junk_a.b.c", "ric2");
808 String url = "/policy-types";
809 String rsp = restClient().get(url).block();
810 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
812 url = "/policy-types?ric_id=ric1";
813 rsp = restClient().get(url).block();
814 String expResp = createPolicyTypesJson(TYPE_ID_1);
815 assertThat(rsp).isEqualTo(expResp);
817 // Get policy types for non existing RIC
818 url = "/policy-types?ric_id=ric1XXX";
819 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
821 // All types with a type_name
822 url = "/policy-types?type_name=A_type1";
823 rsp = restClient().get(url).block();
824 assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
826 // All types compatible with type1_1.5.0 (which is type1_1.9.0)
827 url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
828 rsp = restClient().get(url).block();
829 expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
830 assertThat(rsp).isEqualTo(expResp);
832 url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
833 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
835 url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
836 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
838 url = "/policy-types?compatible_with_version=1.5.0";
839 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
843 @DisplayName("test Get Policy Instances")
844 void testGetPolicyInstances() throws Exception {
845 addPolicy("id1", "type1", "service1");
847 String url = "/policy-instances";
848 String rsp = restClient().get(url).block();
850 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
851 assertThat(info.policies).hasSize(1);
852 PolicyInfo policyInfo = info.policies.iterator().next();
853 assert (policyInfo.validate());
854 assertThat(policyInfo.policyId).isEqualTo("id1");
855 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
856 assertThat(policyInfo.serviceId).isEqualTo("service1");
860 @DisplayName("test Get Policy Instances Filter")
861 void testGetPolicyInstancesFilter() throws Exception {
862 addPolicy("id1", "type1", "service1");
863 addPolicy("id2", "type1", "service2");
864 addPolicy("id3", "type2", "service1");
865 addPolicy("id4", "type1_1.0.0", "service1");
867 String url = "/policy-instances?policytype_id=type1";
868 String rsp = restClient().get(url).block();
870 assertThat(rsp).contains("id1") //
872 .doesNotContain("id3");
874 url = "/policy-instances?policytype_id=type1&service_id=service2";
875 rsp = restClient().get(url).block();
877 assertThat(rsp).doesNotContain("id1") //
879 .doesNotContain("id3");
881 url = "/policy-instances?type_name=type1";
882 rsp = restClient().get(url).block();
883 assertThat(rsp).contains("id1") //
885 .doesNotContain("id3") //
888 // Test get policies for non existing type
889 url = "/policy-instances?policytype_id=type1XXX";
890 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
892 // Test get policies for non existing RIC
893 url = "/policy-instances?ric_id=XXX";
894 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
898 @DisplayName("test Get Policy Ids Filter")
899 void testGetPolicyIdsFilter() throws Exception {
900 addPolicy("id1", "type1", "service1", "ric1");
901 addPolicy("id2", "type1", "service2", "ric1");
902 addPolicy("id3", "type2", "service1", "ric1");
903 addPolicy("id4", "type1_1.0.0", "service1");
905 String url = "/policies?policytype_id=type1";
906 String rsp = restClient().get(url).block();
908 assertThat(rsp).contains("id1") //
910 .doesNotContain("id3");
912 url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
913 rsp = restClient().get(url).block();
914 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
915 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
917 url = "/policies?policytype_name=type1&service_id=service1";
918 rsp = restClient().get(url).block();
919 assertThat(rsp).contains("id1") //
922 assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
924 // Test get policy ids for non existing type
925 url = "/policies?policytype_id=type1XXX";
926 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
928 // Test get policy ids for non existing RIC
929 url = "/policies?ric_id=XXX";
930 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
934 @DisplayName("test Put And Get Service")
935 void testPutAndGetService() throws Exception {
937 String serviceName = "ac.dc";
938 putService(serviceName, 0, HttpStatus.CREATED);
939 putService(serviceName, 0, HttpStatus.OK);
942 String url = "/services?service_id=" + serviceName;
943 String rsp = restClient().get(url).block();
944 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
945 assertThat(info.statusList).hasSize(1);
946 ServiceStatus status = info.statusList.iterator().next();
947 assertThat(status.keepAliveIntervalSeconds).isZero();
948 assertThat(status.serviceId).isEqualTo(serviceName);
952 rsp = restClient().get(url).block();
953 assertThat(rsp).as("Response contains service name").contains(serviceName);
957 url = "/services/" + serviceName + "/keepalive";
958 ResponseEntity<?> entity = restClient().putForEntity(url).block();
959 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
962 addPolicy("id1", "type1", serviceName);
963 assertThat(services.size()).isEqualTo(1);
964 url = "/services/" + serviceName;
965 restClient().delete(url).block();
966 assertThat(services.size()).isZero();
968 // Keep alive, no registered service
969 testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
971 // PUT service with bad payload
972 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
973 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
974 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
976 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
977 HttpStatus.BAD_REQUEST, false);
979 // GET non existing service
980 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
984 @DisplayName("test delete Service with no authorization")
985 void testDeleteServiceWithNoAuth() throws Exception {
987 String serviceName = "ac.dc";
988 putService(serviceName, 0, HttpStatus.CREATED);
990 // No Authorization to Delete
991 this.applicationConfig
992 .setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL_REJECT);
993 addPolicy("id1", "type1", serviceName);
994 assertThat(services.size()).isEqualTo(1);
995 String url = "/services/" + serviceName;
996 restClient().delete(url).block();
997 assertThat(services.size()).isZero();
998 assertThat(policies.size()).isEqualTo(1);
999 testErrorCode(restClient().get("/policies/id1"), HttpStatus.UNAUTHORIZED);
1003 @DisplayName("test delete Service with no service")
1004 void testDeleteServiceWithNoService() {
1005 String url = "/services/" + "NoService";
1006 testErrorCode(restClient().delete(url), HttpStatus.NOT_FOUND);
1010 @DisplayName("test Service Supervision")
1011 void testServiceSupervision() throws Exception {
1012 putService("service1", 1, HttpStatus.CREATED);
1013 addPolicyType("type1", "ric1");
1015 String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
1016 restClient().put("/policies", policyBody).block();
1018 assertThat(policies.size()).isEqualTo(1);
1019 assertThat(services.size()).isEqualTo(1);
1021 // Timeout after ~1 second
1022 await().untilAsserted(() -> assertThat(policies.size()).isZero());
1023 assertThat(services.size()).isZero();
1027 @DisplayName("test Get Policy Status")
1028 void testGetPolicyStatus() throws Exception {
1029 addPolicy("id", "typeName", "service1", "ric1");
1030 assertThat(policies.size()).isEqualTo(1);
1032 String url = "/policies/id/status";
1033 String rsp = restClient().get(url).block();
1034 PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
1035 assertThat(info.status).isEqualTo("OK");
1037 // GET non existing policy status
1038 url = "/policies/XXX/status";
1039 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
1041 // GET STATUS, the NearRT RIC returns error
1042 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
1043 url = "/policies/id/status";
1044 WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
1045 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
1046 rsp = restClient().get(url).block();
1047 info = gson.fromJson(rsp, PolicyStatusInfo.class);
1048 assertThat(info.status).hasToString("{}");
1052 @DisplayName("test Get Service Status")
1053 void testGetServiceStatus() throws Exception {
1054 String url = "/status";
1055 String rsp = restClient().get(url).block();
1056 assertThat(rsp).contains("success");
1058 rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
1059 assertThat(rsp).isEqualTo("success");
1063 @DisplayName("test Service Notification")
1064 void testServiceNotification() throws Exception {
1066 final String AUTH_TOKEN = "testToken";
1067 Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
1068 Files.write(authFile, AUTH_TOKEN.getBytes());
1069 this.securityContext.setAuthTokenFilePath(authFile);
1071 putService("junkService");
1072 Service junkService = this.services.get("junkService");
1073 junkService.setCallbackUrl("https://junk");
1074 putService("service");
1076 Ric ric = addRic("ric1");
1077 ric.setState(Ric.RicState.UNAVAILABLE);
1078 supervision.checkAllRics();
1079 waitForRicState("ric1", RicState.AVAILABLE);
1081 final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
1082 await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
1083 ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
1084 assertThat(callbackInfo.ricId).isEqualTo("ric1");
1085 assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
1087 var headers = receivedCallbacks.receivedHeaders.get(0);
1088 assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
1090 Files.delete(authFile);
1093 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
1095 Policy policy = Policy.builder() //
1097 .json(jsonString()) //
1098 .ownerServiceId(service) //
1099 .ric(rics.getRic(ric)) //
1100 .type(addPolicyType(typeName, ric)) //
1101 .lastModified(Instant.now()) //
1102 .isTransient(false) //
1103 .statusNotificationUri("/policy-status?id=XXX") //
1105 policies.put(policy);
1109 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
1110 return addPolicy(id, typeName, service, "ric");
1113 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
1114 String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
1115 return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
1118 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
1119 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
1121 String json = gson.toJson(service);
1125 private void putService(String name) {
1126 putService(name, 0, null);
1129 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
1130 String url = "/services";
1131 String body = createServiceJson(name, keepAliveIntervalSeconds);
1132 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
1133 if (expectedStatus != null) {
1134 assertEquals(expectedStatus, resp.getStatusCode(), "");
1138 private String jsonString() {
1139 return "{\"servingCellNrcgi\":\"1\"}";
1143 @DisplayName("test Concurrency")
1144 void testConcurrency() throws Exception {
1145 this.applicationConfig.setAuthProviderUrl("");
1146 logger.info("Concurrency test starting");
1147 final Instant startTime = Instant.now();
1148 List<Thread> threads = new ArrayList<>();
1149 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
1150 a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
1152 addPolicyType("type1", "ric");
1153 addPolicyType("type2", "ric");
1155 final String NON_RESPONDING_RIC = "NonRespondingRic";
1156 Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
1157 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
1158 a1Client.setErrorInject("errorInject");
1160 for (int i = 0; i < 10; ++i) {
1161 AsyncRestClient restClient = restClient();
1162 ConcurrencyTestRunnable test =
1163 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1164 Thread thread = new Thread(test, "TestThread_" + i);
1166 threads.add(thread);
1169 for (Thread t : threads) {
1172 for (ConcurrencyTestRunnable test : tests) {
1173 assertThat(test.isFailed()).isFalse();
1175 assertThat(policies.size()).isZero();
1176 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1178 assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1179 nonRespondingRic.setState(RicState.AVAILABLE);
1182 private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1183 WebClientConfig config = this.applicationConfig.getWebClientConfig();
1184 config = WebClientConfig.builder() //
1185 .keyStoreType(config.getKeyStoreType()) //
1186 .keyStorePassword(config.getKeyStorePassword()) //
1187 .keyStore(config.getKeyStore()) //
1188 .keyPassword(config.getKeyPassword()) //
1189 .isTrustStoreUsed(useTrustValidation) //
1190 .trustStore(config.getTrustStore()) //
1191 .trustStorePassword(config.getTrustStorePassword()) //
1192 .httpProxyConfig(config.getHttpProxyConfig()) //
1195 AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1196 return f.createRestClientNoHttpProxy(baseUrl);
1200 private String baseUrl() {
1201 return "https://localhost:" + port;
1204 private AsyncRestClient restClient(boolean useTrustValidation) {
1205 return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1208 private AsyncRestClient restClient() {
1209 return restClient(false);
1212 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1213 testErrorCode(request, expStatus, "", true);
1216 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1217 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1220 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1221 testErrorCode(request, expStatus, responseContains, true);
1224 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1225 boolean expectApplicationProblemJsonMediaType) {
1226 StepVerifier.create(request) //
1227 .expectSubscription() //
1228 .expectErrorMatches(
1229 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1233 private void waitForRicState(String ricId, RicState state) throws ServiceException {
1234 Ric ric = rics.getRic(ricId);
1235 await().untilAsserted(() -> state.equals(ric.getState()));
1238 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1239 boolean expectApplicationProblemJsonMediaType) {
1240 assertTrue(throwable instanceof WebClientResponseException);
1241 WebClientResponseException responseException = (WebClientResponseException) throwable;
1242 String body = responseException.getResponseBodyAsString();
1243 assertThat(body).contains(responseContains);
1244 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1246 if (expectApplicationProblemJsonMediaType) {
1247 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1252 private MockA1Client getA1Client(String ricId) throws ServiceException {
1253 return a1ClientFactory.getOrCreateA1Client(ricId);
1256 private PolicyType createPolicyType(String policyTypeName) {
1257 return PolicyType.builder() //
1258 .id(policyTypeName) //
1259 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1263 private PolicyType addPolicyType(String policyTypeName, String ricId) {
1264 PolicyType type = createPolicyType(policyTypeName);
1265 policyTypes.put(type);
1266 addRic(ricId).addSupportedPolicyType(type);
1270 private Ric addRic(String ricId) {
1271 return addRic(ricId, null);
1274 private RicConfig ricConfig(String ricId, String managedElement) {
1275 List<String> mes = new ArrayList<>();
1276 if (managedElement != null) {
1277 mes.add(managedElement);
1279 return RicConfig.builder() //
1282 .managedElementIds(mes) //
1286 private Ric addRic(String ricId, String managedElement) {
1287 if (rics.get(ricId) != null) {
1288 return rics.get(ricId);
1291 RicConfig conf = ricConfig(ricId, managedElement);
1292 Ric ric = new Ric(conf);
1293 ric.setState(Ric.RicState.AVAILABLE);