1de3b2e962c38469156f2e956830803cf3808a93
[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.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;
101
102 import reactor.core.publisher.Mono;
103 import reactor.test.StepVerifier;
104 import reactor.util.annotation.Nullable;
105
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", //
113         "app.filepath=", //
114         "app.s3.bucket=" // If this is set, S3 will be used to store data.
115 })
116 class ApplicationTest {
117     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
118
119     @Autowired
120     ApplicationContext context;
121
122     @Autowired
123     private Rics rics;
124
125     @Autowired
126     private Policies policies;
127
128     @Autowired
129     private PolicyTypes policyTypes;
130
131     @Autowired
132     MockA1ClientFactory a1ClientFactory;
133
134     @Autowired
135     RicSupervision supervision;
136
137     @Autowired
138     ApplicationConfig applicationConfig;
139
140     @Autowired
141     Services services;
142
143     @Autowired
144     RappSimulatorController rAppSimulator;
145
146     @Autowired
147     RefreshConfigTask refreshConfigTask;
148
149     @Autowired
150     SecurityContext securityContext;
151
152     @Autowired
153     OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
154
155     private static Gson gson = new GsonBuilder().create();
156
157     /**
158      * Overrides the BeanFactory.
159      */
160     @TestConfiguration
161     static class TestBeanFactory {
162
163         @Bean
164         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
165             return new MockA1ClientFactory(appConfig, types);
166         }
167
168         @Bean
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);
173         }
174
175         @Bean
176         public ServletWebServerFactory servletContainer() {
177             return new TomcatServletWebServerFactory();
178         }
179     }
180
181     @LocalServerPort
182     private int port;
183
184     @BeforeEach
185     void init() {
186         this.applicationConfig.setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
187     }
188
189     @AfterEach
190     void reset() {
191         rics.clear();
192         policies.clear();
193         policyTypes.clear();
194         services.clear();
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();
200     }
201
202     @AfterAll
203     static void clearTestDir() {
204         try {
205             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
206         } catch (Exception e) {
207             logger.warn("Could test directory : {}", e.getMessage());
208         }
209     }
210
211     @AfterEach
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);
218         }
219     }
220
221     @Test
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
226         // will fail.
227         AsyncRestClient client = restClient(baseUrl(), false);
228
229         client.post("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice",
230                 "{\"configuredLevel\":\"trace\"}").block();
231
232         String resp = client.get("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice").block();
233         assertThat(resp).contains("TRACE");
234
235         client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
236
237         // This will stop the web server and all coming tests will fail.
238         client.post("/actuator/shutdown", "").block();
239
240         Thread.sleep(1000);
241
242         StepVerifier.create(restClient().get("/rics")) // Any call
243                 .expectSubscription() //
244                 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
245                 .verify();
246
247     }
248
249     @Test
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();
257
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"))) {
262             out.print(indented);
263         }
264     }
265
266     @Test
267     @DisplayName("test Persistency Policies")
268     void testPersistencyPolicies() throws Exception {
269         Ric ric = this.addRic("ric1");
270         PolicyType type = this.addPolicyType("type1", ric.id());
271
272         final int noOfPolicies = 100;
273         for (int i = 0; i < noOfPolicies; ++i) {
274             addPolicy("id" + i, type.getId(), "service", ric.id());
275         }
276         waitforS3();
277
278         {
279             Policies policies = new Policies(this.applicationConfig);
280             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
281             assertThat(policies.size()).isEqualTo(noOfPolicies);
282         }
283
284         {
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);
289         }
290     }
291
292     @Test
293     @DisplayName("test Persistency Policy Types")
294     void testPersistencyPolicyTypes() throws Exception {
295         Ric ric = this.addRic("ric1");
296         this.addPolicyType("type1", ric.id());
297         waitforS3();
298
299         PolicyTypes types = new PolicyTypes(this.applicationConfig);
300         types.restoreFromDatabase().blockLast();
301         assertThat(types.size()).isEqualTo(1);
302     }
303
304     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
305     private void waitforS3() throws Exception {
306         if (applicationConfig.isS3Enabled()) {
307             Thread.sleep(1000);
308         }
309     }
310
311     @Test
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);
318         waitforS3();
319
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());
326
327         // check that the service can be deleted
328         this.services.remove(SERVICE);
329         servicesRestored = new Services(this.applicationConfig);
330         assertThat(servicesRestored.size()).isZero();
331     }
332
333     @Test
334     @DisplayName("test Adding Ric From Configuration")
335     void testAddingRicFromConfiguration() throws Exception {
336         // Test adding the RIC from configuration
337
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);
343
344         putService("service");
345
346         refreshConfigTask.handleUpdatedRicConfig( //
347                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
348                 .block();
349         waitForRicState(RIC, RicState.AVAILABLE);
350
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);
355
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);
362     }
363
364     @Test
365     @DisplayName("test Adding Ric From Configuration non Responding Ric")
366     void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
367         putService("service");
368
369         final String RIC = "NonRespondingRic";
370         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
371         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
372
373         refreshConfigTask.handleUpdatedRicConfig( //
374                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
375                 .block();
376
377         waitForRicState(RIC, RicState.UNAVAILABLE);
378
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());
382
383         // Run a synch and check that the AVAILABLE notification is received
384         a1ClientFactory.reset();
385         supervision.checkAllRics();
386         waitForRicState(RIC, RicState.AVAILABLE);
387
388         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
389     }
390
391     @Test
392     @DisplayName("test Trust Validation")
393     void testTrustValidation() {
394         addRic("ric1");
395
396         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
397         assertThat(rsp).contains("ric1");
398
399     }
400
401     @Test
402     @DisplayName("test Get Rics")
403     void testGetRics() throws Exception {
404         addRic("ric1");
405         this.addPolicyType("type1", "ric1");
406         String url = "/rics?policytype_id=type1";
407         String rsp = restClient().get(url).block();
408         assertThat(rsp).contains("ric1");
409
410         // nameless type for ORAN A1 1.1
411         addRic("ric2");
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");
418
419         // All RICs
420         rsp = restClient().get("/rics").block();
421         assertThat(rsp).contains("ric2") //
422                 .contains("ric1");
423
424         // Non existing policy type
425         url = "/rics?policytype_id=XXXX";
426         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
427     }
428
429     @Test
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);
437
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");
443
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
446
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
450
451         // Wait until synch is completed
452         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
453         waitForRicState(ric1Name, RicState.AVAILABLE);
454         waitForRicState("ric2", RicState.AVAILABLE);
455
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());
460
461         // Both types should be in the Policy Management Service's storage after the
462         // synch
463         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
464         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
465     }
466
467     @Test
468     @DisplayName("test Get Ric")
469     void testGetRic() throws Exception {
470         String ricId = "ric1";
471         String managedElementId = "kista_1";
472         addRic(ricId, managedElementId);
473
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);
478
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);
483
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);
487
488         url = "/rics/ric";
489         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
490     }
491
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;
497         info.ricId = ricId;
498         info.serviceId = serviceName;
499         info.policyData = gson.fromJson(jsonString(), Object.class);
500
501         if (isTransient) {
502             info.isTransient = isTransient;
503         }
504         info.statusNotificationUri = statusNotificationUri;
505         info.validate();
506         return gson.toJson(info);
507     }
508
509     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
510         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
511     }
512
513     @Test
514     @DisplayName("test Put Policy")
515     void testPutPolicy() throws Exception {
516         String serviceName = "service.1";
517         String ricId = "ric.1";
518         String policyTypeName = "type1_1.2.3";
519         String policyInstanceId = "instance_1.2.3";
520
521         putService(serviceName);
522         addPolicyType(policyTypeName, ricId);
523
524         // PUT a transient policy
525         String url = "/policies";
526         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true, "statusNotif");
527         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
528
529         restClient().put(url, policyBody).block();
530         {
531             // Check the authorization request
532             OpenPolicyAgentSimulatorController.TestResults res =
533                     this.openPolicyAgentSimulatorController.getTestResults();
534             assertThat(res.receivedRequests).hasSize(1);
535             PolicyAuthorizationRequest req = res.receivedRequests.get(0);
536             assertThat(req.getInput().getAccessType()).isEqualTo(AccessType.WRITE);
537             assertThat(req.getInput().getPolicyTypeId()).isEqualTo(policyTypeName);
538         }
539
540         Policy policy = policies.getPolicy(policyInstanceId);
541         assertThat(policy).isNotNull();
542         assertThat(policy.getId()).isEqualTo(policyInstanceId);
543         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
544         assertThat(policy.getRic().id()).isEqualTo(ricId);
545         assertThat(policy.isTransient()).isTrue();
546
547         // Put a non transient policy
548         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
549         restClient().put(url, policyBody).block();
550         policy = policies.getPolicy(policyInstanceId);
551         assertThat(policy.isTransient()).isFalse();
552
553         url = "/policy-instances";
554         String rsp = restClient().get(url).block();
555         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
556
557         url = "/policies/" + policyInstanceId;
558         rsp = restClient().get(url).block();
559         assertThat(rsp).contains(policyBody);
560
561         // Test of error codes
562         url = "/policies";
563         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
564         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
565
566         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
567         addPolicyType(policyTypeName + "XX", "otherRic");
568         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
569
570         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
571         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
572         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
573         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
574     }
575
576     @Test
577     void testFineGrainedAuth() throws Exception {
578         final String POLICY_ID = "policyId";
579         final String RIC_ID = "ric1";
580         final String TYPE_ID = "typeName";
581         addPolicy(POLICY_ID, TYPE_ID, null, RIC_ID);
582         assertThat(policies.size()).isEqualTo(1);
583
584         this.applicationConfig
585                 .setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL_REJECT);
586
587         String url = "/policy-instances";
588         String rsp = restClient().get(url).block();
589         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
590
591         url = "/policies/" + POLICY_ID;
592         testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
593
594         url = "/policies";
595         String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
596         testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
597
598         rsp = restClient().get(url).block();
599         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
600     }
601
602     @Test
603     void testFineGrainedAuth_OPA_UNAVALIABLE() throws Exception {
604         final String POLICY_ID = "policyId";
605         final String RIC_ID = "ric1";
606         final String TYPE_ID = "typeName";
607         addPolicy(POLICY_ID, TYPE_ID, null, RIC_ID);
608         assertThat(policies.size()).isEqualTo(1);
609
610         this.applicationConfig.setAuthProviderUrl("junk");
611
612         String url = "/policy-instances";
613         String rsp = restClient().get(url).block();
614         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
615
616         url = "/policies/" + POLICY_ID;
617         testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
618
619         url = "/policies";
620         String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
621         testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
622
623         rsp = restClient().get(url).block();
624         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
625     }
626
627     @Test
628     @DisplayName("test Put Policy No Service No Status Uri")
629     void testPutPolicy_NoServiceNoStatusUri() throws Exception {
630         String ricId = "ric.1";
631         String policyTypeName = "type1_1.2.3";
632         String policyInstanceId = "instance_1.2.3";
633
634         addPolicyType(policyTypeName, ricId);
635
636         // PUT a transient policy
637         String url = "/policies";
638         String policyBody = putPolicyBody(null, ricId, policyTypeName, policyInstanceId, true, null);
639         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
640
641         restClient().put(url, policyBody).block();
642
643         Policy policy = policies.getPolicy(policyInstanceId);
644         assertThat(policy).isNotNull();
645         assertThat(policy.getOwnerServiceId()).isBlank();
646         assertThat(policy.getStatusNotificationUri()).isBlank();
647     }
648
649     @Test
650     /**
651      * Test that HttpStatus and body from failing REST call to A1 is passed on to
652      * the caller.
653      *
654      * @throws ServiceException
655      */
656     @DisplayName("test Error From Ric")
657     void testErrorFromRic() throws ServiceException {
658         putService("service1");
659         addPolicyType("type1", "ric1");
660
661         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
662         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
663         String responseBody = "Refused";
664         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
665
666         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
667                 responseBodyBytes, StandardCharsets.UTF_8, null);
668         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
669
670         // PUT Policy
671         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
672         String url = "/policies";
673         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
674
675         // DELETE POLICY
676         this.addPolicy("instance1", "type1", "service1", "ric1");
677         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
678         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
679
680     }
681
682     @Test
683     @DisplayName("test Put Typeless Policy")
684     void testPutTypelessPolicy() throws Exception {
685         putService("service1");
686         addPolicyType("", "ric1");
687         String body = putPolicyBody("service1", "ric1", "", "id1");
688         restClient().put("/policies", body).block();
689
690         String rsp = restClient().get("/policy-instances").block();
691         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
692         assertThat(info.policies).hasSize(1);
693         PolicyInfo policyInfo = info.policies.iterator().next();
694         assertThat(policyInfo.policyId).isEqualTo("id1");
695         assertThat(policyInfo.policyTypeId).isEmpty();
696     }
697
698     @Test
699     @DisplayName("test Update Service")
700     void testUpdateService() throws Exception {
701         this.addRic("ric1");
702         this.addPolicy("p", "type1", "", "ric1");
703
704         String url = "/policies?service_id=";
705         String resp = restClient().get(url).block();
706         assertThat(resp).contains("[\"p\"]");
707
708         this.addPolicy("p", "type1", "service", "ric1");
709         url = "/policies?service_id=";
710         resp = restClient().get(url).block();
711         assertThat(resp).contains("[]");
712
713         url = "/policies?service_id=service";
714         resp = restClient().get(url).block();
715         assertThat(resp).contains("[\"p\"]");
716     }
717
718     @Test
719     @DisplayName("test Refuse To Update Policy")
720     void testRefuseToUpdatePolicy() throws Exception {
721         // Test that only the json can be changed for a already created policy
722         // In this case service is attempted to be changed
723         this.addRic("ric1");
724         this.addRic("ricXXX");
725         this.addPolicy("instance1", "type1", "service1", "ric1");
726         this.addPolicy("instance2", "type1", "service1", "ricXXX");
727
728         // Try change ric1 -> ricXXX
729         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
730         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
731     }
732
733     @Test
734     @DisplayName("test Get Policy")
735     void testGetPolicy() throws Exception {
736         String url = "/policies/id";
737         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
738         {
739             String rsp = restClient().get(url).block();
740             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
741             String policyStr = gson.toJson(info.policyData);
742             assertThat(policyStr).isEqualTo(policy.getJson());
743         }
744         {
745             policies.remove(policy);
746             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
747         }
748     }
749
750     @Test
751     @DisplayName("test Delete Policy")
752     void testDeletePolicy() throws Exception {
753         String policyId = "id.1";
754         addPolicy(policyId, "typeName", "service1", "ric1");
755         assertThat(policies.size()).isEqualTo(1);
756
757         String url = "/policies/" + policyId;
758         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
759
760         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
761         assertThat(policies.size()).isZero();
762
763         // Delete a non existing policy
764         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
765     }
766
767     @Test
768     @DisplayName("test Get Policy Type")
769     void testGetPolicyType() throws Exception {
770         String typeId = "AC.D";
771         addPolicyType(typeId, "ric1");
772
773         waitForRicState("ric1", RicState.AVAILABLE);
774
775         String url = "/policy-types/" + typeId;
776
777         String rsp = this.restClient().get(url).block();
778
779         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
780         assertThat(info.schema).isNotNull();
781
782         // Get non existing schema
783         url = "/policy-types/JUNK";
784         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
785     }
786
787     String createPolicyTypesJson(String... types) {
788         List<String> list = new ArrayList<>();
789         Collections.addAll(list, types);
790         PolicyTypeIdList ids = new PolicyTypeIdList(list);
791         return gson.toJson(ids);
792     }
793
794     @Test
795     @DisplayName("test Get Policy Types")
796     void testGetPolicyTypes() throws Exception {
797         String TYPE_ID_1 = "A_type1_1.9.0";
798         String TYPE_ID_2 = "A_type1_2.0.0";
799         String TYPE_ID_3 = "A_type1_1.5.0";
800         String TYPE_ID_4 = "type3_1.9.0";
801         addPolicyType(TYPE_ID_1, "ric1");
802         addPolicyType(TYPE_ID_2, "ric2");
803         addPolicyType(TYPE_ID_3, "ric2");
804         addPolicyType(TYPE_ID_4, "ric2");
805
806         addPolicyType("junk", "ric2");
807         addPolicyType("junk_a.b.c", "ric2");
808
809         String url = "/policy-types";
810         String rsp = restClient().get(url).block();
811         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
812
813         url = "/policy-types?ric_id=ric1";
814         rsp = restClient().get(url).block();
815         String expResp = createPolicyTypesJson(TYPE_ID_1);
816         assertThat(rsp).isEqualTo(expResp);
817
818         // Get policy types for non existing RIC
819         url = "/policy-types?ric_id=ric1XXX";
820         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
821
822         // All types with a type_name
823         url = "/policy-types?type_name=A_type1";
824         rsp = restClient().get(url).block();
825         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
826
827         // All types compatible with type1_1.5.0 (which is type1_1.9.0)
828         url = "/policy-types?type_name=A_type1&&compatible_with_version=1.5.0";
829         rsp = restClient().get(url).block();
830         expResp = createPolicyTypesJson(TYPE_ID_3, TYPE_ID_1);
831         assertThat(rsp).isEqualTo(expResp);
832
833         url = "/policy-types?type_name=A_type1&&compatible_with_version=junk";
834         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Version must contain major.minor.patch code");
835
836         url = "/policy-types?type_name=A_type1&&compatible_with_version=a.b.c";
837         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "Syntax error in");
838
839         url = "/policy-types?compatible_with_version=1.5.0";
840         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
841     }
842
843     @Test
844     @DisplayName("test Get Policy Instances")
845     void testGetPolicyInstances() throws Exception {
846         addPolicy("id1", "type1", "service1");
847
848         String url = "/policy-instances";
849         String rsp = restClient().get(url).block();
850         logger.info(rsp);
851         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
852         assertThat(info.policies).hasSize(1);
853         PolicyInfo policyInfo = info.policies.iterator().next();
854         assert (policyInfo.validate());
855         assertThat(policyInfo.policyId).isEqualTo("id1");
856         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
857         assertThat(policyInfo.serviceId).isEqualTo("service1");
858     }
859
860     @Test
861     @DisplayName("test Get Policy Instances Filter")
862     void testGetPolicyInstancesFilter() throws Exception {
863         addPolicy("id1", "type1", "service1");
864         addPolicy("id2", "type1", "service2");
865         addPolicy("id3", "type2", "service1");
866         addPolicy("id4", "type1_1.0.0", "service1");
867
868         String url = "/policy-instances?policytype_id=type1";
869         String rsp = restClient().get(url).block();
870         logger.info(rsp);
871         assertThat(rsp).contains("id1") //
872                 .contains("id2") //
873                 .doesNotContain("id3");
874
875         url = "/policy-instances?policytype_id=type1&service_id=service2";
876         rsp = restClient().get(url).block();
877         logger.info(rsp);
878         assertThat(rsp).doesNotContain("id1") //
879                 .contains("id2") //
880                 .doesNotContain("id3");
881
882         url = "/policy-instances?type_name=type1";
883         rsp = restClient().get(url).block();
884         assertThat(rsp).contains("id1") //
885                 .contains("id2") //
886                 .doesNotContain("id3") //
887                 .contains("id4");
888
889         // Test get policies for non existing type
890         url = "/policy-instances?policytype_id=type1XXX";
891         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
892
893         // Test get policies for non existing RIC
894         url = "/policy-instances?ric_id=XXX";
895         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
896     }
897
898     @Test
899     @DisplayName("test Get Policy Ids Filter")
900     void testGetPolicyIdsFilter() throws Exception {
901         addPolicy("id1", "type1", "service1", "ric1");
902         addPolicy("id2", "type1", "service2", "ric1");
903         addPolicy("id3", "type2", "service1", "ric1");
904         addPolicy("id4", "type1_1.0.0", "service1");
905
906         String url = "/policies?policytype_id=type1";
907         String rsp = restClient().get(url).block();
908         logger.info(rsp);
909         assertThat(rsp).contains("id1") //
910                 .contains("id2") //
911                 .doesNotContain("id3");
912
913         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
914         rsp = restClient().get(url).block();
915         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
916         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
917
918         url = "/policies?policytype_name=type1&service_id=service1";
919         rsp = restClient().get(url).block();
920         assertThat(rsp).contains("id1") //
921                 .contains("id3") //
922                 .contains("id4");
923         assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
924
925         // Test get policy ids for non existing type
926         url = "/policies?policytype_id=type1XXX";
927         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
928
929         // Test get policy ids for non existing RIC
930         url = "/policies?ric_id=XXX";
931         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
932     }
933
934     @Test
935     @DisplayName("test Put And Get Service")
936     void testPutAndGetService() throws Exception {
937         // PUT
938         String serviceName = "ac.dc";
939         putService(serviceName, 0, HttpStatus.CREATED);
940         putService(serviceName, 0, HttpStatus.OK);
941
942         // GET one service
943         String url = "/services?service_id=" + serviceName;
944         String rsp = restClient().get(url).block();
945         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
946         assertThat(info.statusList).hasSize(1);
947         ServiceStatus status = info.statusList.iterator().next();
948         assertThat(status.keepAliveIntervalSeconds).isZero();
949         assertThat(status.serviceId).isEqualTo(serviceName);
950
951         // GET (all)
952         url = "/services";
953         rsp = restClient().get(url).block();
954         assertThat(rsp).as("Response contains service name").contains(serviceName);
955         logger.info(rsp);
956
957         // Keep alive
958         url = "/services/" + serviceName + "/keepalive";
959         ResponseEntity<?> entity = restClient().putForEntity(url).block();
960         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
961
962         // DELETE service
963         addPolicy("id1", "type1", serviceName);
964         assertThat(services.size()).isEqualTo(1);
965         url = "/services/" + serviceName;
966         restClient().delete(url).block();
967         assertThat(services.size()).isZero();
968
969         // Keep alive, no registered service
970         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
971
972         // PUT service with bad payload
973         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
974         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
975         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
976                 false);
977         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
978                 HttpStatus.BAD_REQUEST, false);
979
980         // GET non existing service
981         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
982     }
983
984     @Test
985     @DisplayName("test delete Service with no authorization")
986     void testDeleteServiceWithNoAuth() throws Exception {
987         // PUT service
988         String serviceName = "ac.dc";
989         putService(serviceName, 0, HttpStatus.CREATED);
990
991         // No Authorization to Delete
992         this.applicationConfig
993                 .setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL_REJECT);
994         addPolicy("id1", "type1", serviceName);
995         assertThat(services.size()).isEqualTo(1);
996         String url = "/services/" + serviceName;
997         restClient().delete(url).block();
998         assertThat(services.size()).isZero();
999         assertThat(policies.size()).isEqualTo(1);
1000         testErrorCode(restClient().get("/policies/id1"), HttpStatus.UNAUTHORIZED);
1001     }
1002
1003     @Test
1004     @DisplayName("test delete Service with no service")
1005     void testDeleteServiceWithNoService() {
1006         String url = "/services/" + "NoService";
1007         testErrorCode(restClient().delete(url), HttpStatus.NOT_FOUND);
1008     }
1009
1010     @Test
1011     @DisplayName("test Service Supervision")
1012     void testServiceSupervision() throws Exception {
1013         putService("service1", 1, HttpStatus.CREATED);
1014         addPolicyType("type1", "ric1");
1015
1016         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
1017         restClient().put("/policies", policyBody).block();
1018
1019         assertThat(policies.size()).isEqualTo(1);
1020         assertThat(services.size()).isEqualTo(1);
1021
1022         // Timeout after ~1 second
1023         await().untilAsserted(() -> assertThat(policies.size()).isZero());
1024         assertThat(services.size()).isZero();
1025     }
1026
1027     @Test
1028     @DisplayName("test Get Policy Status")
1029     void testGetPolicyStatus() throws Exception {
1030         addPolicy("id", "typeName", "service1", "ric1");
1031         assertThat(policies.size()).isEqualTo(1);
1032
1033         String url = "/policies/id/status";
1034         String rsp = restClient().get(url).block();
1035         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
1036         assertThat(info.status).isEqualTo("OK");
1037
1038         // GET non existing policy status
1039         url = "/policies/XXX/status";
1040         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
1041
1042         // GET STATUS, the NearRT RIC returns error
1043         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
1044         url = "/policies/id/status";
1045         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
1046         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
1047         rsp = restClient().get(url).block();
1048         info = gson.fromJson(rsp, PolicyStatusInfo.class);
1049         assertThat(info.status).hasToString("{}");
1050     }
1051
1052     @Test
1053     @DisplayName("test Get Service Status")
1054     void testGetServiceStatus() throws Exception {
1055         String url = "/status";
1056         String rsp = restClient().get(url).block();
1057         assertThat(rsp).contains("success");
1058
1059         rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
1060         assertThat(rsp).isEqualTo("success");
1061     }
1062
1063     @Test
1064     @DisplayName("test Service Notification")
1065     void testServiceNotification() throws Exception {
1066
1067         final String AUTH_TOKEN = "testToken";
1068         Path authFile = Files.createTempFile("pmsTestAuthToken", ".txt");
1069         Files.write(authFile, AUTH_TOKEN.getBytes());
1070         this.securityContext.setAuthTokenFilePath(authFile);
1071
1072         putService("junkService");
1073         Service junkService = this.services.get("junkService");
1074         junkService.setCallbackUrl("https://junk");
1075         putService("service");
1076
1077         Ric ric = addRic("ric1");
1078         ric.setState(Ric.RicState.UNAVAILABLE);
1079         supervision.checkAllRics();
1080         waitForRicState("ric1", RicState.AVAILABLE);
1081
1082         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
1083         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
1084         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
1085         assertThat(callbackInfo.ricId).isEqualTo("ric1");
1086         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
1087
1088         var headers = receivedCallbacks.receivedHeaders.get(0);
1089         assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
1090
1091         Files.delete(authFile);
1092     }
1093
1094     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
1095         addRic(ric);
1096         Policy policy = Policy.builder() //
1097                 .id(id) //
1098                 .json(jsonString()) //
1099                 .ownerServiceId(service) //
1100                 .ric(rics.getRic(ric)) //
1101                 .type(addPolicyType(typeName, ric)) //
1102                 .lastModified(Instant.now()) //
1103                 .isTransient(false) //
1104                 .statusNotificationUri("/policy-status?id=XXX") //
1105                 .build();
1106         policies.put(policy);
1107         return policy;
1108     }
1109
1110     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
1111         return addPolicy(id, typeName, service, "ric");
1112     }
1113
1114     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
1115         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
1116         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
1117     }
1118
1119     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
1120         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
1121
1122         String json = gson.toJson(service);
1123         return json;
1124     }
1125
1126     private void putService(String name) {
1127         putService(name, 0, null);
1128     }
1129
1130     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
1131         String url = "/services";
1132         String body = createServiceJson(name, keepAliveIntervalSeconds);
1133         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
1134         if (expectedStatus != null) {
1135             assertEquals(expectedStatus, resp.getStatusCode(), "");
1136         }
1137     }
1138
1139     private String jsonString() {
1140         return "{\"servingCellNrcgi\":\"1\"}";
1141     }
1142
1143     @Test
1144     @DisplayName("test Concurrency")
1145     void testConcurrency() throws Exception {
1146         this.applicationConfig.setAuthProviderUrl("");
1147         logger.info("Concurrency test starting");
1148         final Instant startTime = Instant.now();
1149         List<Thread> threads = new ArrayList<>();
1150         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
1151         a1ClientFactory.setResponseDelay(Duration.ofMillis(2));
1152         addRic("ric");
1153         addPolicyType("type1", "ric");
1154         addPolicyType("type2", "ric");
1155
1156         final String NON_RESPONDING_RIC = "NonRespondingRic";
1157         Ric nonRespondingRic = addRic(NON_RESPONDING_RIC);
1158         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(NON_RESPONDING_RIC);
1159         a1Client.setErrorInject("errorInject");
1160
1161         for (int i = 0; i < 10; ++i) {
1162             AsyncRestClient restClient = restClient();
1163             ConcurrencyTestRunnable test =
1164                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
1165             Thread thread = new Thread(test, "TestThread_" + i);
1166             thread.start();
1167             threads.add(thread);
1168             tests.add(test);
1169         }
1170         for (Thread t : threads) {
1171             t.join();
1172         }
1173         for (ConcurrencyTestRunnable test : tests) {
1174             assertThat(test.isFailed()).isFalse();
1175         }
1176         assertThat(policies.size()).isZero();
1177         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
1178
1179         assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
1180         nonRespondingRic.setState(RicState.AVAILABLE);
1181     }
1182
1183     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
1184         WebClientConfig config = this.applicationConfig.getWebClientConfig();
1185         config = WebClientConfig.builder() //
1186                 .keyStoreType(config.getKeyStoreType()) //
1187                 .keyStorePassword(config.getKeyStorePassword()) //
1188                 .keyStore(config.getKeyStore()) //
1189                 .keyPassword(config.getKeyPassword()) //
1190                 .isTrustStoreUsed(useTrustValidation) //
1191                 .trustStore(config.getTrustStore()) //
1192                 .trustStorePassword(config.getTrustStorePassword()) //
1193                 .httpProxyConfig(config.getHttpProxyConfig()) //
1194                 .build();
1195
1196         AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
1197         return f.createRestClientNoHttpProxy(baseUrl);
1198
1199     }
1200
1201     private String baseUrl() {
1202         return "https://localhost:" + port;
1203     }
1204
1205     private AsyncRestClient restClient(boolean useTrustValidation) {
1206         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
1207     }
1208
1209     private AsyncRestClient restClient() {
1210         return restClient(false);
1211     }
1212
1213     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
1214         testErrorCode(request, expStatus, "", true);
1215     }
1216
1217     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
1218         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
1219     }
1220
1221     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
1222         testErrorCode(request, expStatus, responseContains, true);
1223     }
1224
1225     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
1226             boolean expectApplicationProblemJsonMediaType) {
1227         StepVerifier.create(request) //
1228                 .expectSubscription() //
1229                 .expectErrorMatches(
1230                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
1231                 .verify();
1232     }
1233
1234     private void waitForRicState(String ricId, RicState state) throws ServiceException {
1235         Ric ric = rics.getRic(ricId);
1236         await().untilAsserted(() -> state.equals(ric.getState()));
1237     }
1238
1239     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
1240             boolean expectApplicationProblemJsonMediaType) {
1241         assertTrue(throwable instanceof WebClientResponseException);
1242         WebClientResponseException responseException = (WebClientResponseException) throwable;
1243         String body = responseException.getResponseBodyAsString();
1244         assertThat(body).contains(responseContains);
1245         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
1246
1247         if (expectApplicationProblemJsonMediaType) {
1248             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
1249         }
1250         return true;
1251     }
1252
1253     private MockA1Client getA1Client(String ricId) throws ServiceException {
1254         return a1ClientFactory.getOrCreateA1Client(ricId);
1255     }
1256
1257     private PolicyType createPolicyType(String policyTypeName) {
1258         return PolicyType.builder() //
1259                 .id(policyTypeName) //
1260                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
1261                 .build();
1262     }
1263
1264     private PolicyType addPolicyType(String policyTypeName, String ricId) {
1265         PolicyType type = createPolicyType(policyTypeName);
1266         policyTypes.put(type);
1267         addRic(ricId).addSupportedPolicyType(type);
1268         return type;
1269     }
1270
1271     private Ric addRic(String ricId) {
1272         return addRic(ricId, null);
1273     }
1274
1275     private RicConfig ricConfig(String ricId, String managedElement) {
1276         List<String> mes = new ArrayList<>();
1277         if (managedElement != null) {
1278             mes.add(managedElement);
1279         }
1280         return RicConfig.builder() //
1281                 .ricId(ricId) //
1282                 .baseUrl(ricId) //
1283                 .managedElementIds(mes) //
1284                 .build();
1285     }
1286
1287     private Ric addRic(String ricId, String managedElement) {
1288         if (rics.get(ricId) != null) {
1289             return rics.get(ricId);
1290         }
1291
1292         RicConfig conf = ricConfig(ricId, managedElement);
1293         Ric ric = new Ric(conf);
1294         ric.setState(Ric.RicState.AVAILABLE);
1295         this.rics.put(ric);
1296         return ric;
1297     }
1298
1299 }