2  * ========================LICENSE_START=================================
 
   4  * ======================================================================
 
   5  * Copyright (C) 2019-2022 Nordix Foundation. All rights reserved.
 
   6  * ======================================================================
 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
 
   8  * you may not use this file except in compliance with the License.
 
   9  * You may obtain a copy of the License at
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  13  * Unless required by applicable law or agreed to in writing, software
 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  * See the License for the specific language governing permissions and
 
  17  * limitations under the License.
 
  18  * ========================LICENSE_END===================================
 
  21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
 
  23 import static org.assertj.core.api.Assertions.assertThat;
 
  24 import static org.awaitility.Awaitility.await;
 
  25 import static org.junit.jupiter.api.Assertions.assertEquals;
 
  26 import static org.junit.jupiter.api.Assertions.assertTrue;
 
  27 import static org.mockito.ArgumentMatchers.any;
 
  28 import static org.mockito.Mockito.doReturn;
 
  30 import com.google.gson.Gson;
 
  31 import com.google.gson.GsonBuilder;
 
  33 import java.io.FileOutputStream;
 
  34 import java.io.PrintStream;
 
  35 import java.lang.invoke.MethodHandles;
 
  36 import java.nio.charset.StandardCharsets;
 
  37 import java.nio.file.Files;
 
  38 import java.nio.file.Path;
 
  39 import java.nio.file.Paths;
 
  40 import java.time.Duration;
 
  41 import java.time.Instant;
 
  42 import java.util.ArrayList;
 
  43 import java.util.Collections;
 
  44 import java.util.List;
 
  46 import org.json.JSONObject;
 
  47 import org.junit.jupiter.api.AfterAll;
 
  48 import org.junit.jupiter.api.AfterEach;
 
  49 import org.junit.jupiter.api.BeforeEach;
 
  50 import org.junit.jupiter.api.DisplayName;
 
  51 import org.junit.jupiter.api.MethodOrderer;
 
  52 import org.junit.jupiter.api.Test;
 
  53 import org.junit.jupiter.api.TestMethodOrder;
 
  54 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
 
  55 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
 
  56 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
 
  57 import org.onap.ccsdk.oran.a1policymanagementservice.clients.SecurityContext;
 
  58 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
 
  59 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
 
  60 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
 
  61 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
 
  62 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.OpenPolicyAgentSimulatorController;
 
  63 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
 
  64 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest;
 
  65 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.authorization.PolicyAuthorizationRequest.Input.AccessType;
 
  66 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
 
  67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock;
 
  68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
 
  69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
 
  70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
 
  71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
 
  72 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
 
  73 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
 
  74 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
 
  75 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
 
  76 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
 
  77 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
 
  78 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
 
  79 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
 
  80 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
 
  81 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
 
  82 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
 
  83 import org.slf4j.Logger;
 
  84 import org.slf4j.LoggerFactory;
 
  85 import org.springframework.beans.factory.annotation.Autowired;
 
  86 import org.springframework.boot.test.context.SpringBootTest;
 
  87 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
 
  88 import org.springframework.boot.test.context.TestConfiguration;
 
  89 import org.springframework.boot.test.web.server.LocalServerPort;
 
  90 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
 
  91 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
 
  92 import org.springframework.context.ApplicationContext;
 
  93 import org.springframework.context.annotation.Bean;
 
  94 import org.springframework.http.HttpStatus;
 
  95 import org.springframework.http.MediaType;
 
  96 import org.springframework.http.ResponseEntity;
 
  97 import org.springframework.test.context.TestPropertySource;
 
  98 import org.springframework.util.FileSystemUtils;
 
  99 import org.springframework.web.reactive.function.client.WebClientRequestException;
 
 100 import org.springframework.web.reactive.function.client.WebClientResponseException;
 
 102 import reactor.core.publisher.Mono;
 
 103 import reactor.test.StepVerifier;
 
 104 import reactor.util.annotation.Nullable;
 
 106 @TestMethodOrder(MethodOrderer.MethodName.class)
 
 107 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
 
 108 @TestPropertySource(properties = { //
 
 109         "server.ssl.key-store=./config/keystore.jks", //
 
 110         "app.webclient.trust-store=./config/truststore.jks", //
 
 111         "app.webclient.trust-store-used=true", //
 
 112         "app.vardata-directory=/tmp/pmstest", //
 
 114         "app.s3.bucket=" // If this is set, S3 will be used to store data.
 
 116 class ApplicationTest {
 
 117     private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
 120     ApplicationContext context;
 
 126     private Policies policies;
 
 129     private PolicyTypes policyTypes;
 
 132     MockA1ClientFactory a1ClientFactory;
 
 135     RicSupervision supervision;
 
 138     ApplicationConfig applicationConfig;
 
 144     RappSimulatorController rAppSimulator;
 
 147     RefreshConfigTask refreshConfigTask;
 
 150     SecurityContext securityContext;
 
 153     OpenPolicyAgentSimulatorController openPolicyAgentSimulatorController;
 
 155     private static Gson gson = new GsonBuilder().create();
 
 158      * Overrides the BeanFactory.
 
 161     static class TestBeanFactory {
 
 164         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
 
 165             return new MockA1ClientFactory(appConfig, types);
 
 169         public ServiceSupervision getServiceSupervision(@Autowired Services services,
 
 170                 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
 
 171             Duration checkInterval = Duration.ofMillis(1);
 
 172             return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
 
 176         public ServletWebServerFactory servletContainer() {
 
 177             return new TomcatServletWebServerFactory();
 
 186         this.applicationConfig.setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL);
 
 195         a1ClientFactory.reset();
 
 196         this.rAppSimulator.getTestResults().clear();
 
 197         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
 
 198         this.securityContext.setAuthTokenFilePath(null);
 
 199         this.openPolicyAgentSimulatorController.getTestResults().reset();
 
 203     static void clearTestDir() {
 
 205             FileSystemUtils.deleteRecursively(Path.of("/tmp/pmstest"));
 
 206         } catch (Exception e) {
 
 207             logger.warn("Could test directory : {}", e.getMessage());
 
 212     void verifyNoRicLocks() {
 
 213         for (Ric ric : this.rics.getRics()) {
 
 214             Lock.Grant grant = ric.getLock().lockBlocking(LockType.EXCLUSIVE, "verifyNoRicLocks");
 
 215             grant.unlockBlocking();
 
 216             assertThat(ric.getLock().getLockCounter()).isZero();
 
 217             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
 
 222     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
 
 223     @DisplayName("test ZZ Actuator")
 
 224     void testZZActuator() throws Exception {
 
 225         // The test must be run last, hence the "ZZ" in the name. All succeeding tests
 
 227         AsyncRestClient client = restClient(baseUrl(), false);
 
 229         client.post("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice",
 
 230                 "{\"configuredLevel\":\"trace\"}").block();
 
 232         String resp = client.get("/actuator/loggers/org.onap.ccsdk.oran.a1policymanagementservice").block();
 
 233         assertThat(resp).contains("TRACE");
 
 235         client.post("/actuator/loggers/org.springframework.boot.actuate", "{\"configuredLevel\":\"trace\"}").block();
 
 237         // This will stop the web server and all coming tests will fail.
 
 238         client.post("/actuator/shutdown", "").block();
 
 242         StepVerifier.create(restClient().get("/rics")) // Any call
 
 243                 .expectSubscription() //
 
 244                 .expectErrorMatches(t -> t instanceof WebClientRequestException) //
 
 250     @DisplayName("test generate Api Doc")
 
 251     void generateApiDoc() throws Exception {
 
 252         String url = "https://localhost:" + this.port + "/v3/api-docs";
 
 253         ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
 
 254         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
 
 255         JSONObject jsonObj = new JSONObject(resp.getBody());
 
 256         assertThat(jsonObj.remove("servers")).isNotNull();
 
 258         String indented = (jsonObj).toString(4);
 
 259         String docDir = "api/";
 
 260         Files.createDirectories(Paths.get(docDir));
 
 261         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
 
 267     @DisplayName("test Persistency Policies")
 
 268     void testPersistencyPolicies() throws Exception {
 
 269         Ric ric = this.addRic("ric1");
 
 270         PolicyType type = this.addPolicyType("type1", ric.id());
 
 272         final int noOfPolicies = 100;
 
 273         for (int i = 0; i < noOfPolicies; ++i) {
 
 274             addPolicy("id" + i, type.getId(), "service", ric.id());
 
 279             Policies policies = new Policies(this.applicationConfig);
 
 280             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
 
 281             assertThat(policies.size()).isEqualTo(noOfPolicies);
 
 285             restClient().delete("/policies/id2").block();
 
 286             Policies policies = new Policies(this.applicationConfig);
 
 287             policies.restoreFromDatabase(ric, this.policyTypes).blockLast();
 
 288             assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
 
 293     @DisplayName("test Persistency Policy Types")
 
 294     void testPersistencyPolicyTypes() throws Exception {
 
 295         Ric ric = this.addRic("ric1");
 
 296         this.addPolicyType("type1", ric.id());
 
 299         PolicyTypes types = new PolicyTypes(this.applicationConfig);
 
 300         types.restoreFromDatabase().blockLast();
 
 301         assertThat(types.size()).isEqualTo(1);
 
 304     @SuppressWarnings("squid:S2925") // "Thread.sleep" should not be used in tests.
 
 305     private void waitforS3() throws Exception {
 
 306         if (applicationConfig.isS3Enabled()) {
 
 312     @DisplayName("test Persistency Service")
 
 313     void testPersistencyService() throws Exception {
 
 314         final String SERVICE = "serviceName";
 
 315         putService(SERVICE, 1234, HttpStatus.CREATED);
 
 316         assertThat(this.services.size()).isEqualTo(1);
 
 317         Service service = this.services.getService(SERVICE);
 
 320         Services servicesRestored = new Services(this.applicationConfig);
 
 321         servicesRestored.restoreFromDatabase().blockLast();
 
 322         Service serviceRestored = servicesRestored.getService(SERVICE);
 
 323         assertThat(servicesRestored.size()).isEqualTo(1);
 
 324         assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
 
 325         assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
 
 327         // check that the service can be deleted
 
 328         this.services.remove(SERVICE);
 
 329         servicesRestored = new Services(this.applicationConfig);
 
 330         assertThat(servicesRestored.size()).isZero();
 
 334     @DisplayName("test Adding Ric From Configuration")
 
 335     void testAddingRicFromConfiguration() throws Exception {
 
 336         // Test adding the RIC from configuration
 
 338         final String RIC = "ric1";
 
 339         final String TYPE = "type123";
 
 340         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
 
 341         nearRtRicPolicyTypes.put(createPolicyType(TYPE));
 
 342         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
 
 344         putService("service");
 
 346         refreshConfigTask.handleUpdatedRicConfig( //
 
 347                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
 
 349         waitForRicState(RIC, RicState.AVAILABLE);
 
 351         // Test that the type has been synched
 
 352         Ric addedRic = this.rics.getRic(RIC);
 
 353         assertThat(addedRic.getSupportedPolicyTypes()).hasSize(1);
 
 354         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
 
 356         // Check that a service callback for the AVAILABLE RIC is invoked
 
 357         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
 
 358         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
 
 359         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
 
 360         assertThat(callbackInfo.ricId).isEqualTo(RIC);
 
 361         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
 
 365     @DisplayName("test Adding Ric From Configuration non Responding Ric")
 
 366     void testAddingRicFromConfiguration_nonRespondingRic() throws Exception {
 
 367         putService("service");
 
 369         final String RIC = "NonRespondingRic";
 
 370         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
 
 371         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
 
 373         refreshConfigTask.handleUpdatedRicConfig( //
 
 374                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
 
 377         waitForRicState(RIC, RicState.UNAVAILABLE);
 
 379         // Check that no service callback for the UNAVAILABLE RIC is invoked
 
 380         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
 
 381         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
 
 383         // Run a synch and check that the AVAILABLE notification is received
 
 384         a1ClientFactory.reset();
 
 385         supervision.checkAllRics();
 
 386         waitForRicState(RIC, RicState.AVAILABLE);
 
 388         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).hasSize(1));
 
 392     @DisplayName("test Trust Validation")
 
 393     void testTrustValidation() {
 
 396         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
 
 397         assertThat(rsp).contains("ric1");
 
 402     @DisplayName("test Get Rics")
 
 403     void testGetRics() throws Exception {
 
 405         this.addPolicyType("type1", "ric1");
 
 406         String url = "/rics?policytype_id=type1";
 
 407         String rsp = restClient().get(url).block();
 
 408         assertThat(rsp).contains("ric1");
 
 410         // nameless type for ORAN A1 1.1
 
 412         this.addPolicyType("", "ric2");
 
 413         url = "/rics?policytype_id=";
 
 414         rsp = restClient().get(url).block();
 
 415         assertThat(rsp).contains("ric2") //
 
 416                 .doesNotContain("ric1") //
 
 417                 .contains("AVAILABLE");
 
 420         rsp = restClient().get("/rics").block();
 
 421         assertThat(rsp).contains("ric2") //
 
 424         // Non existing policy type
 
 425         url = "/rics?policytype_id=XXXX";
 
 426         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 430     @DisplayName("test Synchronization")
 
 431     void testSynchronization() throws Exception {
 
 432         // Two polictypes will be put in the NearRT RICs
 
 433         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
 
 434         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
 
 435         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
 
 436         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
 
 438         // One type and one instance added to the Policy Management Service's storage
 
 439         final String ric1Name = "ric1";
 
 440         Ric ric1 = addRic(ric1Name);
 
 441         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
 
 442         Ric ric2 = addRic("ric2");
 
 444         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
 
 445         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
 
 447         String policyId = "policyId";
 
 448         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
 
 449         supervision.checkAllRics(); // The created policy should be put in the RIC
 
 451         // Wait until synch is completed
 
 452         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
 
 453         waitForRicState(ric1Name, RicState.AVAILABLE);
 
 454         waitForRicState("ric2", RicState.AVAILABLE);
 
 456         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
 
 457         assertThat(ricPolicies.size()).isEqualTo(1);
 
 458         Policy ricPolicy = ricPolicies.get(policyId);
 
 459         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
 
 461         // Both types should be in the Policy Management Service's storage after the
 
 463         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
 
 464         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
 
 468     @DisplayName("test Get Ric")
 
 469     void testGetRic() throws Exception {
 
 470         String ricId = "ric1";
 
 471         String managedElementId = "kista_1";
 
 472         addRic(ricId, managedElementId);
 
 474         String url = "/rics/ric?managed_element_id=" + managedElementId;
 
 475         String rsp = restClient().get(url).block();
 
 476         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
 
 477         assertThat(ricInfo.ricId).isEqualTo(ricId);
 
 479         url = "/rics/ric?ric_id=" + ricId;
 
 480         rsp = restClient().get(url).block();
 
 481         ricInfo = gson.fromJson(rsp, RicInfo.class);
 
 482         assertThat(ricInfo.ricId).isEqualTo(ricId);
 
 484         // test GET RIC for ManagedElement that does not exist
 
 485         url = "/rics/ric?managed_element_id=" + "junk";
 
 486         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 489         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
 
 492     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
 
 493             boolean isTransient, String statusNotificationUri) {
 
 494         PolicyInfo info = new PolicyInfo();
 
 495         info.policyId = policyInstanceId;
 
 496         info.policyTypeId = policyTypeName;
 
 498         info.serviceId = serviceName;
 
 499         info.policyData = gson.fromJson(jsonString(), Object.class);
 
 502             info.isTransient = isTransient;
 
 504         info.statusNotificationUri = statusNotificationUri;
 
 506         return gson.toJson(info);
 
 509     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
 
 510         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false, "statusUri");
 
 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";
 
 521         putService(serviceName);
 
 522         addPolicyType(policyTypeName, ricId);
 
 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);
 
 529         restClient().put(url, policyBody).block();
 
 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);
 
 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();
 
 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();
 
 553         url = "/policy-instances";
 
 554         String rsp = restClient().get(url).block();
 
 555         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
 
 557         url = "/policies/" + policyInstanceId;
 
 558         rsp = restClient().get(url).block();
 
 559         assertThat(rsp).contains(policyBody);
 
 561         // Test of error codes
 
 563         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
 
 564         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
 
 566         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
 
 567         addPolicyType(policyTypeName + "XX", "otherRic");
 
 568         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
 
 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);
 
 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);
 
 584         this.applicationConfig
 
 585                 .setAuthProviderUrl(baseUrl() + OpenPolicyAgentSimulatorController.ACCESS_CONTROL_URL_REJECT);
 
 587         String url = "/policy-instances";
 
 588         String rsp = restClient().get(url).block();
 
 589         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
 
 591         url = "/policies/" + POLICY_ID;
 
 592         testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
 
 595         String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
 
 596         testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
 
 598         rsp = restClient().get(url).block();
 
 599         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
 
 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);
 
 610         this.applicationConfig.setAuthProviderUrl("junk");
 
 612         String url = "/policy-instances";
 
 613         String rsp = restClient().get(url).block();
 
 614         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
 
 616         url = "/policies/" + POLICY_ID;
 
 617         testErrorCode(restClient().delete(url), HttpStatus.UNAUTHORIZED, "Not authorized");
 
 620         String policyBody = putPolicyBody(null, RIC_ID, TYPE_ID, POLICY_ID, false, null);
 
 621         testErrorCode(restClient().put(url, policyBody), HttpStatus.UNAUTHORIZED, "Not authorized");
 
 623         rsp = restClient().get(url).block();
 
 624         assertThat(rsp).as("Response contains no policy instance ID.").contains("[]");
 
 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";
 
 634         addPolicyType(policyTypeName, ricId);
 
 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);
 
 641         restClient().put(url, policyBody).block();
 
 643         Policy policy = policies.getPolicy(policyInstanceId);
 
 644         assertThat(policy).isNotNull();
 
 645         assertThat(policy.getOwnerServiceId()).isBlank();
 
 646         assertThat(policy.getStatusNotificationUri()).isBlank();
 
 651      * Test that HttpStatus and body from failing REST call to A1 is passed on to
 
 654      * @throws ServiceException
 
 656     @DisplayName("test Error From Ric")
 
 657     void testErrorFromRic() throws ServiceException {
 
 658         putService("service1");
 
 659         addPolicyType("type1", "ric1");
 
 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);
 
 666         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
 
 667                 responseBodyBytes, StandardCharsets.UTF_8, null);
 
 668         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
 
 671         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
 
 672         String url = "/policies";
 
 673         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
 
 676         this.addPolicy("instance1", "type1", "service1", "ric1");
 
 677         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
 
 678         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
 
 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();
 
 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();
 
 699     @DisplayName("test Update Service")
 
 700     void testUpdateService() throws Exception {
 
 702         this.addPolicy("p", "type1", "", "ric1");
 
 704         String url = "/policies?service_id=";
 
 705         String resp = restClient().get(url).block();
 
 706         assertThat(resp).contains("[\"p\"]");
 
 708         this.addPolicy("p", "type1", "service", "ric1");
 
 709         url = "/policies?service_id=";
 
 710         resp = restClient().get(url).block();
 
 711         assertThat(resp).contains("[]");
 
 713         url = "/policies?service_id=service";
 
 714         resp = restClient().get(url).block();
 
 715         assertThat(resp).contains("[\"p\"]");
 
 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
 
 724         this.addRic("ricXXX");
 
 725         this.addPolicy("instance1", "type1", "service1", "ric1");
 
 726         this.addPolicy("instance2", "type1", "service1", "ricXXX");
 
 728         // Try change ric1 -> ricXXX
 
 729         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
 
 730         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
 
 734     @DisplayName("test Get Policy")
 
 735     void testGetPolicy() throws Exception {
 
 736         String url = "/policies/id";
 
 737         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
 
 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());
 
 745             policies.remove(policy);
 
 746             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 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);
 
 757         String url = "/policies/" + policyId;
 
 758         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
 
 760         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
 
 761         assertThat(policies.size()).isZero();
 
 763         // Delete a non existing policy
 
 764         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 768     @DisplayName("test Get Policy Type")
 
 769     void testGetPolicyType() throws Exception {
 
 770         String typeId = "AC.D";
 
 771         addPolicyType(typeId, "ric1");
 
 773         waitForRicState("ric1", RicState.AVAILABLE);
 
 775         String url = "/policy-types/" + typeId;
 
 777         String rsp = this.restClient().get(url).block();
 
 779         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
 
 780         assertThat(info.schema).isNotNull();
 
 782         // Get non existing schema
 
 783         url = "/policy-types/JUNK";
 
 784         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 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);
 
 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");
 
 806         addPolicyType("junk", "ric2");
 
 807         addPolicyType("junk_a.b.c", "ric2");
 
 809         String url = "/policy-types";
 
 810         String rsp = restClient().get(url).block();
 
 811         assertThat(rsp).contains(TYPE_ID_1, TYPE_ID_2);
 
 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);
 
 818         // Get policy types for non existing RIC
 
 819         url = "/policy-types?ric_id=ric1XXX";
 
 820         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 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);
 
 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);
 
 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");
 
 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");
 
 839         url = "/policy-types?compatible_with_version=1.5.0";
 
 840         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST, "type_name");
 
 844     @DisplayName("test Get Policy Instances")
 
 845     void testGetPolicyInstances() throws Exception {
 
 846         addPolicy("id1", "type1", "service1");
 
 848         String url = "/policy-instances";
 
 849         String rsp = restClient().get(url).block();
 
 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");
 
 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");
 
 868         String url = "/policy-instances?policytype_id=type1";
 
 869         String rsp = restClient().get(url).block();
 
 871         assertThat(rsp).contains("id1") //
 
 873                 .doesNotContain("id3");
 
 875         url = "/policy-instances?policytype_id=type1&service_id=service2";
 
 876         rsp = restClient().get(url).block();
 
 878         assertThat(rsp).doesNotContain("id1") //
 
 880                 .doesNotContain("id3");
 
 882         url = "/policy-instances?type_name=type1";
 
 883         rsp = restClient().get(url).block();
 
 884         assertThat(rsp).contains("id1") //
 
 886                 .doesNotContain("id3") //
 
 889         // Test get policies for non existing type
 
 890         url = "/policy-instances?policytype_id=type1XXX";
 
 891         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 893         // Test get policies for non existing RIC
 
 894         url = "/policy-instances?ric_id=XXX";
 
 895         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 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");
 
 906         String url = "/policies?policytype_id=type1";
 
 907         String rsp = restClient().get(url).block();
 
 909         assertThat(rsp).contains("id1") //
 
 911                 .doesNotContain("id3");
 
 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");
 
 918         url = "/policies?policytype_name=type1&service_id=service1";
 
 919         rsp = restClient().get(url).block();
 
 920         assertThat(rsp).contains("id1") //
 
 923         assertThat(gson.fromJson(rsp, PolicyIdList.class).policyIds).hasSize(3);
 
 925         // Test get policy ids for non existing type
 
 926         url = "/policies?policytype_id=type1XXX";
 
 927         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 929         // Test get policy ids for non existing RIC
 
 930         url = "/policies?ric_id=XXX";
 
 931         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
 935     @DisplayName("test Put And Get Service")
 
 936     void testPutAndGetService() throws Exception {
 
 938         String serviceName = "ac.dc";
 
 939         putService(serviceName, 0, HttpStatus.CREATED);
 
 940         putService(serviceName, 0, HttpStatus.OK);
 
 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);
 
 953         rsp = restClient().get(url).block();
 
 954         assertThat(rsp).as("Response contains service name").contains(serviceName);
 
 958         url = "/services/" + serviceName + "/keepalive";
 
 959         ResponseEntity<?> entity = restClient().putForEntity(url).block();
 
 960         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
 
 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();
 
 969         // Keep alive, no registered service
 
 970         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
 
 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,
 
 977         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
 
 978                 HttpStatus.BAD_REQUEST, false);
 
 980         // GET non existing service
 
 981         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
 
 985     @DisplayName("test delete Service with no authorization")
 
 986     void testDeleteServiceWithNoAuth() throws Exception {
 
 988         String serviceName = "ac.dc";
 
 989         putService(serviceName, 0, HttpStatus.CREATED);
 
 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);
 
1004     @DisplayName("test delete Service with no service")
 
1005     void testDeleteServiceWithNoService() {
 
1006         String url = "/services/" + "NoService";
 
1007         testErrorCode(restClient().delete(url), HttpStatus.NOT_FOUND);
 
1011     @DisplayName("test Service Supervision")
 
1012     void testServiceSupervision() throws Exception {
 
1013         putService("service1", 1, HttpStatus.CREATED);
 
1014         addPolicyType("type1", "ric1");
 
1016         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
 
1017         restClient().put("/policies", policyBody).block();
 
1019         assertThat(policies.size()).isEqualTo(1);
 
1020         assertThat(services.size()).isEqualTo(1);
 
1022         // Timeout after ~1 second
 
1023         await().untilAsserted(() -> assertThat(policies.size()).isZero());
 
1024         assertThat(services.size()).isZero();
 
1028     @DisplayName("test Get Policy Status")
 
1029     void testGetPolicyStatus() throws Exception {
 
1030         addPolicy("id", "typeName", "service1", "ric1");
 
1031         assertThat(policies.size()).isEqualTo(1);
 
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");
 
1038         // GET non existing policy status
 
1039         url = "/policies/XXX/status";
 
1040         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
 
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("{}");
 
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");
 
1059         rsp = restClient(baseUrl(), false).get(url).block(); // V1 status is used by a readinessProbe
 
1060         assertThat(rsp).isEqualTo("success");
 
1064     @DisplayName("test Service Notification")
 
1065     void testServiceNotification() throws Exception {
 
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);
 
1072         putService("junkService");
 
1073         Service junkService = this.services.get("junkService");
 
1074         junkService.setCallbackUrl("https://junk");
 
1075         putService("service");
 
1077         Ric ric = addRic("ric1");
 
1078         ric.setState(Ric.RicState.UNAVAILABLE);
 
1079         supervision.checkAllRics();
 
1080         waitForRicState("ric1", RicState.AVAILABLE);
 
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);
 
1088         var headers = receivedCallbacks.receivedHeaders.get(0);
 
1089         assertThat(headers).containsEntry("authorization", "Bearer " + AUTH_TOKEN);
 
1091         Files.delete(authFile);
 
1094     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
 
1096         Policy policy = Policy.builder() //
 
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") //
 
1106         policies.put(policy);
 
1110     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
 
1111         return addPolicy(id, typeName, service, "ric");
 
1114     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
 
1115         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
 
1116         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
 
1119     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
 
1120         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
 
1122         String json = gson.toJson(service);
 
1126     private void putService(String name) {
 
1127         putService(name, 0, null);
 
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(), "");
 
1139     private String jsonString() {
 
1140         return "{\"servingCellNrcgi\":\"1\"}";
 
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));
 
1153         addPolicyType("type1", "ric");
 
1154         addPolicyType("type2", "ric");
 
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");
 
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);
 
1167             threads.add(thread);
 
1170         for (Thread t : threads) {
 
1173         for (ConcurrencyTestRunnable test : tests) {
 
1174             assertThat(test.isFailed()).isFalse();
 
1176         assertThat(policies.size()).isZero();
 
1177         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
 
1179         assertThat(nonRespondingRic.getState()).isEqualTo(RicState.UNAVAILABLE);
 
1180         nonRespondingRic.setState(RicState.AVAILABLE);
 
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()) //
 
1196         AsyncRestClientFactory f = new AsyncRestClientFactory(config, new SecurityContext(""));
 
1197         return f.createRestClientNoHttpProxy(baseUrl);
 
1201     private String baseUrl() {
 
1202         return "https://localhost:" + port;
 
1205     private AsyncRestClient restClient(boolean useTrustValidation) {
 
1206         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
 
1209     private AsyncRestClient restClient() {
 
1210         return restClient(false);
 
1213     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
 
1214         testErrorCode(request, expStatus, "", true);
 
1217     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
 
1218         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
 
1221     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
 
1222         testErrorCode(request, expStatus, responseContains, true);
 
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)) //
 
1234     private void waitForRicState(String ricId, RicState state) throws ServiceException {
 
1235         Ric ric = rics.getRic(ricId);
 
1236         await().untilAsserted(() -> state.equals(ric.getState()));
 
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);
 
1247         if (expectApplicationProblemJsonMediaType) {
 
1248             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
 
1253     private MockA1Client getA1Client(String ricId) throws ServiceException {
 
1254         return a1ClientFactory.getOrCreateA1Client(ricId);
 
1257     private PolicyType createPolicyType(String policyTypeName) {
 
1258         return PolicyType.builder() //
 
1259                 .id(policyTypeName) //
 
1260                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
 
1264     private PolicyType addPolicyType(String policyTypeName, String ricId) {
 
1265         PolicyType type = createPolicyType(policyTypeName);
 
1266         policyTypes.put(type);
 
1267         addRic(ricId).addSupportedPolicyType(type);
 
1271     private Ric addRic(String ricId) {
 
1272         return addRic(ricId, null);
 
1275     private RicConfig ricConfig(String ricId, String managedElement) {
 
1276         List<String> mes = new ArrayList<>();
 
1277         if (managedElement != null) {
 
1278             mes.add(managedElement);
 
1280         return RicConfig.builder() //
 
1283                 .managedElementIds(mes) //
 
1287     private Ric addRic(String ricId, String managedElement) {
 
1288         if (rics.get(ricId) != null) {
 
1289             return rics.get(ricId);
 
1292         RicConfig conf = ricConfig(ricId, managedElement);
 
1293         Ric ric = new Ric(conf);
 
1294         ric.setState(Ric.RicState.AVAILABLE);