9f0473dfdfe941be1c313640928b532cb9221b4a
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
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;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
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;
45
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.DisplayName;
50 import org.junit.jupiter.api.MethodOrderer;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.TestMethodOrder;
53 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
54 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
55 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
56 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
60 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
61 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
62 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
73 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
74 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
75 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
76 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
77 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
78 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81 import org.springframework.beans.factory.annotation.Autowired;
82 import org.springframework.boot.test.context.SpringBootTest;
83 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
84 import org.springframework.boot.test.context.TestConfiguration;
85 import org.springframework.boot.test.web.server.LocalServerPort;
86 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
87 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
88 import org.springframework.context.ApplicationContext;
89 import org.springframework.context.annotation.Bean;
90 import org.springframework.http.HttpStatus;
91 import org.springframework.http.MediaType;
92 import org.springframework.http.ResponseEntity;
93 import org.springframework.test.context.TestPropertySource;
94 import org.springframework.util.FileSystemUtils;
95 import org.springframework.web.reactive.function.client.WebClientRequestException;
96 import org.springframework.web.reactive.function.client.WebClientResponseException;
97
98 import reactor.core.publisher.Mono;
99 import reactor.test.StepVerifier;
100 import reactor.util.annotation.Nullable;
101
102 @TestMethodOrder(MethodOrderer.MethodName.class)
103 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
104 @TestPropertySource(properties = { //
105         "server.ssl.key-store=./config/keystore.jks", //
106         "app.webclient.trust-store=./config/truststore.jks", //
107         "app.webclient.trust-store-used=true", //
108         "app.vardata-directory=/tmp/pmstest", //
109         "app.filepath=", //
110         "app.s3.bucket=" // If this is set, S3 will be used to store data.
111 })
112 class ApplicationTest {
113     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
114
115     @Autowired
116     ApplicationContext context;
117
118     @Autowired
119     private Rics rics;
120
121     @Autowired
122     private Policies policies;
123
124     @Autowired
125     private PolicyTypes policyTypes;
126
127     @Autowired
128     MockA1ClientFactory a1ClientFactory;
129
130     @Autowired
131     RicSupervision supervision;
132
133     @Autowired
134     ApplicationConfig applicationConfig;
135
136     @Autowired
137     Services services;
138
139     @Autowired
140     RappSimulatorController rAppSimulator;
141
142     @Autowired
143     RefreshConfigTask refreshConfigTask;
144
145     @Autowired
146     SecurityContext securityContext;
147
148     private static Gson gson = new GsonBuilder().create();
149
150     /**
151      * Overrides the BeanFactory.
152      */
153     @TestConfiguration
154     static class TestBeanFactory {
155
156         @Bean
157         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
158             return new MockA1ClientFactory(appConfig, types);
159         }
160
161         @Bean
162         public ServiceSupervision getServiceSupervision(@Autowired Services services,
163                 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
164             Duration checkInterval = Duration.ofMillis(1);
165             return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
166         }
167
168         @Bean
169         public ServletWebServerFactory servletContainer() {
170             return new TomcatServletWebServerFactory();
171         }
172     }
173
174     @LocalServerPort
175     private int port;
176
177     @AfterEach
178     void reset() {
179         rics.clear();
180         policies.clear();
181         policyTypes.clear();
182         services.clear();
183         a1ClientFactory.reset();
184         this.rAppSimulator.getTestResults().clear();
185         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
186         this.securityContext.setAuthTokenFilePath(null);
187     }
188
189     @AfterAll
190     static void clearTestDir() {
191         try {
192             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
193         } catch (Exception e) {
194             logger.warn("Could test directory : {}", e.getMessage());
195         }
196     }
197
198     @AfterEach
199     void verifyNoRicLocks() {
200         for (Ric ric : this.rics.getRics()) {
201             Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
202             grant.unlockBlocking();
203             assertThat(ric.getLock().getLockCounter()).isZero();
204             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
205         }
206     }
207
208     @Test
209     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
210     @DisplayName("test ZZ Actuator")
211     void testZZActuator() throws Exception {
212         // The test must be run last, hence the "ZZ" in the name. All succeeding tests
213         // will fail.
214         AsyncRestClient client = restClient(baseUrl(), false);
215
216         client.post("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice",
217                 "{\"configuredLevel\":\"trace\"}").block();
218
219         String resp = client.get("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice").block();
220         assertThat(resp).contains("TRACE");
221
222         client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
223
224         // This will stop the web server and all coming tests will fail.
225         client.post("/actuator/shutdown", "").block();
226
227         Thread.sleep(1000);
228
229         StepVerifier.create(restClient().get("/rics")) // Any call
230                 .expectSubscription() //
231                 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
232                 .verify();
233
234     }
235
236     @Test
237     @DisplayName("test generate Api Doc")
238     void generateApiDoc() throws Exception {
239         String url = "https://localhost:" + this.port + "/v3/api-docs";
240         ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
241         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
242         JSONObject jsonObj = new JSONObject(resp.getBody());
243         assertThat(jsonObj.remove("servers")).isNotNull();
244
245         String indented = (jsonObj).toString(4);
246         String docDir = "api/";
247         Files.createDirectories(Paths.get(docDir));
248         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
249             out.print(indented);
250         }
251     }
252
253     @Test
254     @DisplayName("test Persistency Policies")
255     void testPersistencyPolicies() throws Exception {
256         Ric ric = this.addRic("ric1");
257         PolicyType type = this.addPolicyType("type1", ric.id());
258
259         final int noOfPolicies = 100;
260         for (int i = 0; i < noOfPolicies; ++i) {
261             addPolicy("id" + i, type.getId(), "service", ric.id());
262         }
263         waitforS3();
264
265         {
266             Policies policies = new Policies(this.applicationConfig);
267             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
268             assertThat(policies.size()).isEqualTo(noOfPolicies);
269         }
270
271         {
272             restClient().delete("/policies/id2").block();
273             Policies policies = new Policies(this.applicationConfig);
274             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
275             assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
276         }
277     }
278
279     @Test
280     @DisplayName("test Persistency Policy Types")
281     void testPersistencyPolicyTypes() throws Exception {
282         Ric ric = this.addRic("ric1");
283         this.addPolicyType("type1", ric.id());
284         waitforS3();
285
286         PolicyTypes types = new PolicyTypes(this.applicationConfig);
287         types.restoreFromDatabase().blockLast();
288         assertThat(types.size()).isEqualTo(1);
289     }
290
291     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
292     private void waitforS3() throws Exception {
293         if (applicationConfig.isS3Enabled()) {
294             Thread.sleep(1000);
295         }
296     }
297
298     @Test
299     @DisplayName("test Persistency Service")
300     void testPersistencyService() throws Exception {
301         final String SERVICE = "serviceName";
302         putService(SERVICE, 1234, HttpStatus.CREATED);
303         assertThat(this.services.size()).isEqualTo(1);
304         Service service = this.services.getService(SERVICE);
305         waitforS3();
306
307         Services servicesRestored = new Services(this.applicationConfig);
308         servicesRestored.restoreFromDatabase().blockLast();
309         Service serviceRestored = servicesRestored.getService(SERVICE);
310         assertThat(servicesRestored.size()).isEqualTo(1);
311         assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
312         assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
313
314         // check that the service can be deleted
315         this.services.remove(SERVICE);
316         servicesRestored = new Services(this.applicationConfig);
317         assertThat(servicesRestored.size()).isZero();
318     }
319
320     @Test
321     @DisplayName("test Adding Ric From Configuration")
322     void testAddingRicFromConfiguration() throws Exception {
323         // Test adding the RIC from configuration
324
325         final String RIC = "ric1";
326         final String TYPE = "type123";
327         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
328         nearRtRicPolicyTypes.put(createPolicyType(TYPE));
329         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
330
331         putService("service");
332
333         refreshConfigTask.handleUpdatedRicConfig( //
334                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
335                 .block();
336         waitForRicState(RIC, RicState.AVAILABLE);
337
338         // Test that the type has been synched
339         Ric addedRic = this.rics.getRic(RIC);
340         assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
341         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
342
343         // Check that a service callback for the AVAILABLE RIC is invoked
344         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
345         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
346         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
347         assertThat(callbackInfo.ricId).isEqualTo(RIC);
348         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
349     }
350
351     @Test
352     @DisplayName("test Adding Ric From Configuration non Responding Ric")
353     void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
354         putService("service");
355
356         final String RIC = "NonRespondingRic";
357         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
358         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
359
360         refreshConfigTask.handleUpdatedRicConfig( //
361                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
362                 .block();
363
364         waitForRicState(RIC, RicState.UNAVAILABLE);
365
366         // Check that no service callback for the UNAVAILABLE RIC is invoked
367         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
368         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
369
370         // Run a synch and check that the AVAILABLE notification is received
371         a1ClientFactory.reset();
372         supervision.checkAllRics();
373         waitForRicState(RIC, RicState.AVAILABLE);
374
375         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
376     }
377
378     @Test
379     @DisplayName("test Trust Validation")
380     void testTrustValidation() {
381         addRic("ric1");
382
383         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
384         assertThat(rsp).contains("ric1");
385
386     }
387
388     @Test
389     @DisplayName("test Get Rics")
390     void testGetRics() throws Exception {
391         addRic("ric1");
392         this.addPolicyType("type1", "ric1");
393         String url = "/rics?policytype_id=type1";
394         String rsp = restClient().get(url).block();
395         assertThat(rsp).contains("ric1");
396
397         // nameless type for ORAN A1 1.1
398         addRic("ric2");
399         this.addPolicyType("", "ric2");
400         url = "/rics?policytype_id=";
401         rsp = restClient().get(url).block();
402         assertThat(rsp).contains("ric2") //
403                 .doesNotContain("ric1") //
404                 .contains("AVAILABLE");
405
406         // All RICs
407         rsp = restClient().get("/rics").block();
408         assertThat(rsp).contains("ric2") //
409                 .contains("ric1");
410
411         // Non existing policy type
412         url = "/rics?policytype_id=XXXX";
413         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
414     }
415
416     @Test
417     @DisplayName("test Synchronization")
418     void testSynchronization() throws Exception {
419         // Two polictypes will be put in the NearRT RICs
420         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
421         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
422         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
423         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
424
425         // One type and one instance added to the Policy Management Service's storage
426         final String ric1Name = "ric1";
427         Ric ric1 = addRic(ric1Name);
428         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
429         Ric ric2 = addRic("ric2");
430
431         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
432         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
433
434         String policyId = "policyId";
435         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
436         supervision.checkAllRics(); // The created policy should be put in the RIC
437
438         // Wait until synch is completed
439         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
440         waitForRicState(ric1Name, RicState.AVAILABLE);
441         waitForRicState("ric2", RicState.AVAILABLE);
442
443         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
444         assertThat(ricPolicies.size()).isEqualTo(1);
445         Policy ricPolicy = ricPolicies.get(policyId);
446         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
447
448         // Both types should be in the Policy Management Service's storage after the
449         // synch
450         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
451         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
452     }
453
454     @Test
455     @DisplayName("test Get Ric")
456     void testGetRic() throws Exception {
457         String ricId = "ric1";
458         String managedElementId = "kista_1";
459         addRic(ricId, managedElementId);
460
461         String url = "/rics/ric?managed_element_id=" + managedElementId;
462         String rsp = restClient().get(url).block();
463         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
464         assertThat(ricInfo.ricId).isEqualTo(ricId);
465
466         url = "/rics/ric?ric_id=" + ricId;
467         rsp = restClient().get(url).block();
468         ricInfo = gson.fromJson(rsp, RicInfo.class);
469         assertThat(ricInfo.ricId).isEqualTo(ricId);
470
471         // test GET RIC for ManagedElement that does not exist
472         url = "/rics/ric?managed_element_id=" + "junk";
473         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
474
475         url = "/rics/ric";
476         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
477     }
478
479     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
480             boolean isTransient, String statusNotificationUri) {
481         PolicyInfo info = new PolicyInfo();
482         info.policyId = policyInstanceId;
483         info.policyTypeId = policyTypeName;
484         info.ricId = ricId;
485         info.serviceId = serviceName;
486         info.policyData = gson.fromJson(jsonString(), Object.class);
487
488         if (isTransient) {
489             info.isTransient = isTransient;
490         }
491         info.statusNotificationUri = statusNotificationUri;
492         return gson.toJson(info);
493     }
494
495     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
496         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
497     }
498
499     @Test
500     @DisplayName("test Put Policy")
501     void testPutPolicy() throws Exception {
502         String serviceName = "service.1";
503         String ricId = "ric.1";
504         String policyTypeName = "type1_1.2.3";
505         String policyInstanceId = "instance_1.2.3";
506
507         putService(serviceName);
508         addPolicyType(policyTypeName, ricId);
509
510         // PUT a transient policy
511         String url = "/policies";
512         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
513         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
514
515         restClient().put(url, policyBody).block();
516
517         Policy policy = policies.getPolicy(policyInstanceId);
518         assertThat(policy).isNotNull();
519         assertThat(policy.getId()).isEqualTo(policyInstanceId);
520         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
521         assertThat(policy.getRic().id()).isEqualTo(ricId);
522         assertThat(policy.isTransient()).isTrue();
523
524         // Put a non transient policy
525         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
526         restClient().put(url, policyBody).block();
527         policy = policies.getPolicy(policyInstanceId);
528         assertThat(policy.isTransient()).isFalse();
529
530         url = "/policy-instances";
531         String rsp = restClient().get(url).block();
532         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
533
534         url = "/policies/" + policyInstanceId;
535         rsp = restClient().get(url).block();
536         assertThat(rsp).contains(policyBody);
537
538         // Test of error codes
539         url = "/policies";
540         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
541         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
542
543         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
544         addPolicyType(policyTypeName + "XX", "otherRic");
545         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
546
547         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
548         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
549         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
550         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
551     }
552
553     @Test
554     @DisplayName("test Put Policy No Service No Status Uri")
555     void testPutPolicy_NoServiceNoStatusUri() throws Exception {
556         String ricId = "ric.1";
557         String policyTypeName = "type1_1.2.3";
558         String policyInstanceId = "instance_1.2.3";
559
560         addPolicyType(policyTypeName, ricId);
561
562         // PUT a transient policy
563         String url = "/policies";
564         String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
565         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
566
567         restClient().put(url, policyBody).block();
568
569         Policy policy = policies.getPolicy(policyInstanceId);
570         assertThat(policy).isNotNull();
571         assertThat(policy.getOwnerServiceId()).isBlank();
572         assertThat(policy.getStatusNotificationUri()).isBlank();
573     }
574
575     @Test
576     /**
577      * Test that HttpStatus and body from failing REST call to A1 is passed on to
578      * the caller.
579      *
580      * @throws ServiceException
581      */
582     @DisplayName("test Error From Ric")
583     void testErrorFromRic() throws ServiceException {
584         putService("service1");
585         addPolicyType("type1", "ric1");
586
587         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
588         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
589         String responseBody = "Refused";
590         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
591
592         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
593                 responseBodyBytes, StandardCharsets.UTF_8, null);
594         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
595
596         // PUT Policy
597         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
598         String url = "/policies";
599         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
600
601         // DELETE POLICY
602         this.addPolicy("instance1", "type1", "service1", "ric1");
603         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
604         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
605
606     }
607
608     @Test
609     @DisplayName("test Put Typeless Policy")
610     void testPutTypelessPolicy() throws Exception {
611         putService("service1");
612         addPolicyType("", "ric1");
613         String body = putPolicyBody("service1", "ric1", "", "id1");
614         restClient().put("/policies", body).block();
615
616         String rsp = restClient().get("/policy-instances").block();
617         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
618         assertThat(info.policies).hasSize(1);
619         PolicyInfo policyInfo = info.policies.iterator().next();
620         assertThat(policyInfo.policyId).isEqualTo("id1");
621         assertThat(policyInfo.policyTypeId).isEmpty();
622     }
623
624     @Test
625     @DisplayName("test Update Service")
626     void testUpdateService() throws Exception {
627         this.addRic("ric1");
628         this.addPolicy("p", "type1", "", "ric1");
629
630         String url = "/policies?service_id=";
631         String resp = restClient().get(url).block();
632         assertThat(resp).contains("[\"p\"]");
633
634         this.addPolicy("p", "type1", "service", "ric1");
635         url = "/policies?service_id=";
636         resp = restClient().get(url).block();
637         assertThat(resp).contains("[]");
638
639         url = "/policies?service_id=service";
640         resp = restClient().get(url).block();
641         assertThat(resp).contains("[\"p\"]");
642     }
643
644     @Test
645     @DisplayName("test Refuse To Update Policy")
646     void testRefuseToUpdatePolicy() throws Exception {
647         // Test that only the json can be changed for a already created policy
648         // In this case service is attempted to be changed
649         this.addRic("ric1");
650         this.addRic("ricXXX");
651         this.addPolicy("instance1", "type1", "service1", "ric1");
652         this.addPolicy("instance2", "type1", "service1", "ricXXX");
653
654         // Try change ric1 -> ricXXX
655         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
656         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
657     }
658
659     @Test
660     @DisplayName("test Get Policy")
661     void testGetPolicy() throws Exception {
662         String url = "/policies/id";
663         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
664         {
665             String rsp = restClient().get(url).block();
666             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
667             String policyStr = gson.toJson(info.policyData);
668             assertThat(policyStr).isEqualTo(policy.getJson());
669         }
670         {
671             policies.remove(policy);
672             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
673         }
674     }
675
676     @Test
677     @DisplayName("test Delete Policy")
678     void testDeletePolicy() throws Exception {
679         String policyId = "id.1";
680         addPolicy(policyId, "typeName", "service1", "ric1");
681         assertThat(policies.size()).isEqualTo(1);
682
683         String url = "/policies/" + policyId;
684         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
685
686         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
687         assertThat(policies.size()).isZero();
688
689         // Delete a non existing policy
690         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
691     }
692
693     @Test
694     @DisplayName("test Get Policy Type")
695     void testGetPolicyType() throws Exception {
696         String typeId = "AC.D";
697         addPolicyType(typeId, "ric1");
698
699         waitForRicState("ric1", RicState.AVAILABLE);
700
701         String url = "/policy-types/" + typeId;
702
703         String rsp = this.restClient().get(url).block();
704
705         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
706         assertThat(info.schema).isNotNull();
707
708         // Get non existing schema
709         url = "/policy-types/JUNK";
710         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
711     }
712
713     String createPolicyTypesJson(String... types) {
714         List<String> list = new ArrayList<>();
715         Collections.addAll(list, types);
716         PolicyTypeIdList ids = new PolicyTypeIdList(list);
717         return gson.toJson(ids);
718     }
719
720     @Test
721     @DisplayName("test Get Policy Types")
722     void testGetPolicyTypes() throws Exception {
723         String TYPE_ID_1 = "A_type1_1.9.0";
724         String TYPE_ID_2 = "A_type1_2.0.0";
725         String TYPE_ID_3 = "A_type1_1.5.0";
726         String TYPE_ID_4 = "type3_1.9.0";
727         addPolicyType(TYPE_ID_1, "ric1");
728         addPolicyType(TYPE_ID_2, "ric2");
729         addPolicyType(TYPE_ID_3, "ric2");
730         addPolicyType(TYPE_ID_4, "ric2");
731
732         addPolicyType("junk", "ric2");
733         addPolicyType("junk_a.b.c", "ric2");
734
735         String url = "/policy-types";
736         String rsp = restClient().get(url).block();
737         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
738
739         url = "/policy-types?ric_id=ric1";
740         rsp = restClient().get(url).block();
741         String expResp = createPolicyTypesJson(TYPE_ID_1);
742         assertThat(rsp).isEqualTo(expResp);
743
744         // Get policy types for non existing RIC
745         url = "/policy-types?ric_id=ric1XXX";
746         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
747
748         // All types with a type_name
749         url = "/policy-types?type_name=A_type1";
750         rsp = restClient().get(url).block();
751         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
752
753         // All types compatible with type1_1.5.0 (which is type1_1.9.0)
754         url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
755         rsp = restClient().get(url).block();
756         expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
757         assertThat(rsp).isEqualTo(expResp);
758
759         url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
760         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
761
762         url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
763         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
764
765         url = "/policy-types?compatible_with_version=1.5.0";
766         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
767     }
768
769     @Test
770     @DisplayName("test Get Policy Instances")
771     void testGetPolicyInstances() throws Exception {
772         addPolicy("id1", "type1", "service1");
773
774         String url = "/policy-instances";
775         String rsp = restClient().get(url).block();
776         logger.info(rsp);
777         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
778         assertThat(info.policies).hasSize(1);
779         PolicyInfo policyInfo = info.policies.iterator().next();
780         assert (policyInfo.validate());
781         assertThat(policyInfo.policyId).isEqualTo("id1");
782         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
783         assertThat(policyInfo.serviceId).isEqualTo("service1");
784     }
785
786     @Test
787     @DisplayName("test Get Policy Instances Filter")
788     void testGetPolicyInstancesFilter() throws Exception {
789         addPolicy("id1", "type1", "service1");
790         addPolicy("id2", "type1", "service2");
791         addPolicy("id3", "type2", "service1");
792         addPolicy("id4", "type1_1.0.0", "service1");
793
794         String url = "/policy-instances?policytype_id=type1";
795         String rsp = restClient().get(url).block();
796         logger.info(rsp);
797         assertThat(rsp).contains("id1") //
798                 .contains("id2") //
799                 .doesNotContain("id3");
800
801         url = "/policy-instances?policytype_id=type1&service_id=service2";
802         rsp = restClient().get(url).block();
803         logger.info(rsp);
804         assertThat(rsp).doesNotContain("id1") //
805                 .contains("id2") //
806                 .doesNotContain("id3");
807
808         url = "/policy-instances?type_name=type1";
809         rsp = restClient().get(url).block();
810         assertThat(rsp).contains("id1") //
811                 .contains("id2") //
812                 .doesNotContain("id3") //
813                 .contains("id4");
814
815         // Test get policies for non existing type
816         url = "/policy-instances?policytype_id=type1XXX";
817         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
818
819         // Test get policies for non existing RIC
820         url = "/policy-instances?ric_id=XXX";
821         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
822     }
823
824     @Test
825     @DisplayName("test Get Policy Ids Filter")
826     void testGetPolicyIdsFilter() throws Exception {
827         addPolicy("id1", "type1", "service1", "ric1");
828         addPolicy("id2", "type1", "service2", "ric1");
829         addPolicy("id3", "type2", "service1", "ric1");
830         addPolicy("id4", "type1_1.0.0", "service1");
831
832         String url = "/policies?policytype_id=type1";
833         String rsp = restClient().get(url).block();
834         logger.info(rsp);
835         assertThat(rsp).contains("id1") //
836                 .contains("id2") //
837                 .doesNotContain("id3");
838
839         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
840         rsp = restClient().get(url).block();
841         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
842         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
843
844         url = "/policies?policytype_name=type1&service_id=service1";
845         rsp = restClient().get(url).block();
846         assertThat(rsp).contains("id1") //
847                 .contains("id3") //
848                 .contains("id4");
849         assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
850
851         // Test get policy ids for non existing type
852         url = "/policies?policytype_id=type1XXX";
853         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
854
855         // Test get policy ids for non existing RIC
856         url = "/policies?ric_id=XXX";
857         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
858     }
859
860     @Test
861     @DisplayName("test Put And Get Service")
862     void testPutAndGetService() throws Exception {
863         // PUT
864         String serviceName = "ac.dc";
865         putService(serviceName, 0, HttpStatus.CREATED);
866         putService(serviceName, 0, HttpStatus.OK);
867
868         // GET one service
869         String url = "/services?service_id=" + serviceName;
870         String rsp = restClient().get(url).block();
871         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
872         assertThat(info.statusList).hasSize(1);
873         ServiceStatus status = info.statusList.iterator().next();
874         assertThat(status.keepAliveIntervalSeconds).isZero();
875         assertThat(status.serviceId).isEqualTo(serviceName);
876
877         // GET (all)
878         url = "/services";
879         rsp = restClient().get(url).block();
880         assertThat(rsp).as("Response contains service name").contains(serviceName);
881         logger.info(rsp);
882
883         // Keep alive
884         url = "/services/" + serviceName + "/keepalive";
885         ResponseEntity<?> entity = restClient().putForEntity(url).block();
886         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
887
888         // DELETE service
889         assertThat(services.size()).isEqualTo(1);
890         url = "/services/" + serviceName;
891         restClient().delete(url).block();
892         assertThat(services.size()).isZero();
893
894         // Keep alive, no registered service
895         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
896
897         // PUT service with bad payload
898         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
899         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
900         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
901                 false);
902         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
903                 HttpStatus.BAD_REQUEST, false);
904
905         // GET non existing service
906         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
907     }
908
909     @Test
910     @DisplayName("test Service Supervision")
911     void testServiceSupervision() throws Exception {
912         putService("service1", 1, HttpStatus.CREATED);
913         addPolicyType("type1", "ric1");
914
915         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
916         restClient().put("/policies", policyBody).block();
917
918         assertThat(policies.size()).isEqualTo(1);
919         assertThat(services.size()).isEqualTo(1);
920
921         // Timeout after ~1 second
922         await().untilAsserted(() -> assertThat(policies.size()).isZero());
923         assertThat(services.size()).isZero();
924     }
925
926     @Test
927     @DisplayName("test Get Policy Status")
928     void testGetPolicyStatus() throws Exception {
929         addPolicy("id", "typeName", "service1", "ric1");
930         assertThat(policies.size()).isEqualTo(1);
931
932         String url = "/policies/id/status";
933         String rsp = restClient().get(url).block();
934         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
935         assertThat(info.status).isEqualTo("OK");
936
937         // GET non existing policy status
938         url = "/policies/XXX/status";
939         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
940
941         // GET STATUS, the NearRT RIC returns error
942         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
943         url = "/policies/id/status";
944         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
945         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
946         rsp = restClient().get(url).block();
947         info = gson.fromJson(rsp, PolicyStatusInfo.class);
948         assertThat(info.status).hasToString("{}");
949     }
950
951     @Test
952     @DisplayName("test Get Service Status")
953     void testGetServiceStatus() throws Exception {
954         String url = "/status";
955         String rsp = restClient().get(url).block();
956         assertThat(rsp).contains("hunky dory");
957
958         rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
959         assertThat(rsp).isEqualTo("hunky dory");
960     }
961
962     @Test
963     @DisplayName("test Service Notification")
964     void testServiceNotification() throws Exception {
965
966         final String AUTH_TOKEN = "testToken";
967         Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
968         Files.write(authFile, AUTH_TOKEN.getBytes());
969         this.securityContext.setAuthTokenFilePath(authFile);
970
971         putService("junkService");
972         Service junkService = this.services.get("junkService");
973         junkService.setCallbackUrl("https://junk");
974         putService("service");
975
976         Ric ric = addRic("ric1");
977         ric.setState(Ric.RicState.UNAVAILABLE);
978         supervision.checkAllRics();
979         waitForRicState("ric1", RicState.AVAILABLE);
980
981         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
982         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
983         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
984         assertThat(callbackInfo.ricId).isEqualTo("ric1");
985         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
986
987         var headers = receivedCallbacks.receivedHeaders.get(0);
988         assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
989
990         Files.delete(authFile);
991     }
992
993     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
994         addRic(ric);
995         Policy policy = Policy.builder() //
996                 .id(id) //
997                 .json(jsonString()) //
998                 .ownerServiceId(service) //
999                 .ric(rics.getRic(ric)) //
1000                 .type(addPolicyType(typeName, ric)) //
1001                 .lastModified(Instant.now()) //
1002                 .isTransient(false) //
1003                 .statusNotificationUri("/policy-status?id=XXX") //
1004                 .build();
1005         policies.put(policy);
1006         return policy;
1007     }
1008
1009     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
1010         return addPolicy(id, typeName, service, "ric");
1011     }
1012
1013     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
1014         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
1015         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
1016     }
1017
1018     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
1019         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
1020
1021         String json = gson.toJson(service);
1022         return json;
1023     }
1024
1025     private void putService(String name) {
1026         putService(name, 0, null);
1027     }
1028
1029     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
1030         String url = "/services";
1031         String body = createServiceJson(name, keepAliveIntervalSeconds);
1032         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
1033         if (expectedStatus != null) {
1034             assertEquals(expectedStatus, resp.getStatusCode(), "");
1035         }
1036     }
1037
1038     private String jsonString() {
1039         return "{\"servingCellNrcgi\":\"1\"}";
1040     }
1041
1042     @Test
1043     @DisplayName("test Concurrency")
1044     void testConcurrency() throws Exception {
1045         logger.info("Concurrency test starting");
1046         final Instant startTime = Instant.now();
1047         List<Thread> threads = new ArrayList<>();
1048         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
1049         a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
1050         addRic("ric");
1051         addPolicyType("type1", "ric");
1052         addPolicyType("type2", "ric");
1053
1054         final String NON_RESPONDING_RIC = "NonRespondingRic";
1055         Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
1056         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
1057         a1Client.setErrorInject("errorInject");
1058
1059         for (int i = 0; i < 10; ++i) {
1060             AsyncRestClient restClient = restClient();
1061             ConcurrencyTestRunnable test =
1062                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1063             Thread thread = new Thread(test, "TestThread_" + i);
1064             thread.start();
1065             threads.add(thread);
1066             tests.add(test);
1067         }
1068         for (Thread t : threads) {
1069             t.join();
1070         }
1071         for (ConcurrencyTestRunnable test : tests) {
1072             assertThat(test.isFailed()).isFalse();
1073         }
1074         assertThat(policies.size()).isZero();
1075         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1076
1077         assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1078         nonRespondingRic.setState(RicState.AVAILABLE);
1079     }
1080
1081     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1082         WebClientConfig config = this.applicationConfig.getWebClientConfig();
1083         config = WebClientConfig.builder() //
1084                 .keyStoreType(config.getKeyStoreType()) //
1085                 .keyStorePassword(config.getKeyStorePassword()) //
1086                 .keyStore(config.getKeyStore()) //
1087                 .keyPassword(config.getKeyPassword()) //
1088                 .isTrustStoreUsed(useTrustValidation) //
1089                 .trustStore(config.getTrustStore()) //
1090                 .trustStorePassword(config.getTrustStorePassword()) //
1091                 .httpProxyConfig(config.getHttpProxyConfig()) //
1092                 .build();
1093
1094         AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1095         return f.createRestClientNoHttpProxy(baseUrl);
1096
1097     }
1098
1099     private String baseUrl() {
1100         return "https://localhost:" + port;
1101     }
1102
1103     private AsyncRestClient restClient(boolean useTrustValidation) {
1104         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1105     }
1106
1107     private AsyncRestClient restClient() {
1108         return restClient(false);
1109     }
1110
1111     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1112         testErrorCode(request, expStatus, "", true);
1113     }
1114
1115     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1116         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1117     }
1118
1119     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1120         testErrorCode(request, expStatus, responseContains, true);
1121     }
1122
1123     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1124             boolean expectApplicationProblemJsonMediaType) {
1125         StepVerifier.create(request) //
1126                 .expectSubscription() //
1127                 .expectErrorMatches(
1128                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1129                 .verify();
1130     }
1131
1132     private void waitForRicState(String ricId, RicState state) throws ServiceException {
1133         Ric ric = rics.getRic(ricId);
1134         await().untilAsserted(() -> state.equals(ric.getState()));
1135     }
1136
1137     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1138             boolean expectApplicationProblemJsonMediaType) {
1139         assertTrue(throwable instanceof WebClientResponseException);
1140         WebClientResponseException responseException = (WebClientResponseException) throwable;
1141         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1142         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
1143         if (expectApplicationProblemJsonMediaType) {
1144             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1145         }
1146         return true;
1147     }
1148
1149     private MockA1Client getA1Client(String ricId) throws ServiceException {
1150         return a1ClientFactory.getOrCreateA1Client(ricId);
1151     }
1152
1153     private PolicyType createPolicyType(String policyTypeName) {
1154         return PolicyType.builder() //
1155                 .id(policyTypeName) //
1156                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1157                 .build();
1158     }
1159
1160     private PolicyType addPolicyType(String policyTypeName, String ricId) {
1161         PolicyType type = createPolicyType(policyTypeName);
1162         policyTypes.put(type);
1163         addRic(ricId).addSupportedPolicyType(type);
1164         return type;
1165     }
1166
1167     private Ric addRic(String ricId) {
1168         return addRic(ricId, null);
1169     }
1170
1171     private RicConfig ricConfig(String ricId, String managedElement) {
1172         List<String> mes = new ArrayList<>();
1173         if (managedElement != null) {
1174             mes.add(managedElement);
1175         }
1176         return RicConfig.builder() //
1177                 .ricId(ricId) //
1178                 .baseUrl(ricId) //
1179                 .managedElementIds(mes) //
1180                 .build();
1181     }
1182
1183     private Ric addRic(String ricId, String managedElement) {
1184         if (rics.get(ricId) != null) {
1185             return rics.get(ricId);
1186         }
1187
1188         RicConfig conf = ricConfig(ricId, managedElement);
1189         Ric ric = new Ric(conf);
1190         ric.setState(Ric.RicState.AVAILABLE);
1191         this.rics.put(ric);
1192         return ric;
1193     }
1194
1195 }