91eb63339aa900f7a9ebc7aacf2b983a80dead37
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
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.IOException;
35 import java.io.PrintStream;
36 import java.nio.charset.StandardCharsets;
37 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 import java.time.Duration;
40 import java.time.Instant;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
44
45 import org.json.JSONObject;
46 import org.junit.jupiter.api.AfterEach;
47 import org.junit.jupiter.api.Test;
48 import org.junit.jupiter.api.extension.ExtendWith;
49 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
50 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
51 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
52 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
53 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig.RicConfigUpdate;
54 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
55 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
56 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
57 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
58 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.ServiceCallbackInfo;
59 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Service;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
70 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RefreshConfigTask;
71 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
72 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
73 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
74 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77 import org.springframework.beans.factory.annotation.Autowired;
78 import org.springframework.boot.test.context.SpringBootTest;
79 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
80 import org.springframework.boot.test.context.TestConfiguration;
81 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
82 import org.springframework.boot.web.server.LocalServerPort;
83 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
84 import org.springframework.context.ApplicationContext;
85 import org.springframework.context.annotation.Bean;
86 import org.springframework.http.HttpStatus;
87 import org.springframework.http.MediaType;
88 import org.springframework.http.ResponseEntity;
89 import org.springframework.test.context.TestPropertySource;
90 import org.springframework.test.context.junit.jupiter.SpringExtension;
91 import org.springframework.web.reactive.function.client.WebClientResponseException;
92
93 import reactor.core.publisher.Mono;
94 import reactor.test.StepVerifier;
95 import reactor.util.annotation.Nullable;
96
97 @ExtendWith(SpringExtension.class)
98 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
99 @TestPropertySource(properties = { //
100         "server.ssl.key-store=./config/keystore.jks", //
101         "app.webclient.trust-store=./config/truststore.jks", //
102         "app.vardata-directory=./target/testdata", //
103         "app.filepath=" //
104 })
105 class ApplicationTest {
106     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
107
108     @Autowired
109     ApplicationContext context;
110
111     @Autowired
112     private Rics rics;
113
114     @Autowired
115     private Policies policies;
116
117     @Autowired
118     private PolicyTypes policyTypes;
119
120     @Autowired
121     MockA1ClientFactory a1ClientFactory;
122
123     @Autowired
124     RicSupervision supervision;
125
126     @Autowired
127     ApplicationConfig applicationConfig;
128
129     @Autowired
130     Services services;
131
132     @Autowired
133     RappSimulatorController rAppSimulator;
134
135     @Autowired
136     RefreshConfigTask refreshConfigTask;
137
138     private static Gson gson = new GsonBuilder().create();
139
140     /**
141      * Overrides the BeanFactory.
142      */
143     @TestConfiguration
144     static class TestBeanFactory {
145
146         @Bean
147         A1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
148             return new MockA1ClientFactory(appConfig, types);
149         }
150
151         @Bean
152         public ServiceSupervision getServiceSupervision(@Autowired Services services,
153                 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
154             Duration checkInterval = Duration.ofMillis(1);
155             return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
156         }
157
158         @Bean
159         public ServletWebServerFactory servletContainer() {
160             return new TomcatServletWebServerFactory();
161         }
162     }
163
164     @LocalServerPort
165     private int port;
166
167     @AfterEach
168     void reset() {
169         rics.clear();
170         policies.clear();
171         policyTypes.clear();
172         services.clear();
173         a1ClientFactory.reset();
174         this.rAppSimulator.getTestResults().clear();
175         this.a1ClientFactory.setPolicyTypes(policyTypes); // Default same types in RIC and in this app
176     }
177
178     @AfterEach
179     void verifyNoRicLocks() {
180         for (Ric ric : this.rics.getRics()) {
181             ric.getLock().lockBlocking(LockType.EXCLUSIVE);
182             ric.getLock().unlockBlocking();
183             assertThat(ric.getLock().getLockCounter()).isZero();
184             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
185         }
186     }
187
188     @Test
189     void generateApiDoc() throws IOException {
190         String url = "https://localhost:" + this.port + "/v3/api-docs";
191         ResponseEntity<String> resp = restClient("", false).getForEntity(url).block();
192         assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
193         JSONObject jsonObj = new JSONObject(resp.getBody());
194         assertThat(jsonObj.remove("servers")).isNotNull();
195
196         String indented = (jsonObj).toString(4);
197         String docDir = "api/";
198         Files.createDirectories(Paths.get(docDir));
199         try (PrintStream out = new PrintStream(new FileOutputStream(docDir + "pms-api.json"))) {
200             out.print(indented);
201         }
202     }
203
204     @Test
205     void testPersistencyPolicies() throws ServiceException {
206         Ric ric = this.addRic("ric1");
207         PolicyType type = this.addPolicyType("type1", ric.id());
208
209         final int noOfPolicies = 100;
210         for (int i = 0; i < noOfPolicies; ++i) {
211             addPolicy("id" + i, type.getId(), "service", ric.id());
212         }
213
214         {
215             Policies policies = new Policies(this.applicationConfig);
216             policies.restoreFromDatabase(ric, this.policyTypes);
217             assertThat(policies.size()).isEqualTo(noOfPolicies);
218         }
219
220         {
221             restClient().delete("/policies/id2").block();
222             Policies policies = new Policies(this.applicationConfig);
223             policies.restoreFromDatabase(ric, this.policyTypes);
224             assertThat(policies.size()).isEqualTo(noOfPolicies - 1);
225         }
226     }
227
228     @Test
229     void testPersistencyPolicyTypes() throws ServiceException {
230         Ric ric = this.addRic("ric1");
231         this.addPolicyType("type1", ric.id());
232         PolicyTypes types = new PolicyTypes(this.applicationConfig);
233         assertThat(types.size()).isEqualTo(1);
234     }
235
236     @Test
237     void testPersistencyService() throws ServiceException {
238         final String SERVICE = "serviceName";
239         putService(SERVICE, 1234, HttpStatus.CREATED);
240         assertThat(this.services.size()).isEqualTo(1);
241         Service service = this.services.getService(SERVICE);
242
243         Services servicesRestored = new Services(this.applicationConfig);
244         Service serviceRestored = servicesRestored.getService(SERVICE);
245         assertThat(servicesRestored.size()).isEqualTo(1);
246         assertThat(serviceRestored.getCallbackUrl()).isEqualTo(service.getCallbackUrl());
247         assertThat(serviceRestored.getKeepAliveInterval()).isEqualTo(service.getKeepAliveInterval());
248
249         // check that the service can be deleted
250         this.services.remove(SERVICE);
251         servicesRestored = new Services(this.applicationConfig);
252         assertThat(servicesRestored.size()).isEqualTo(0);
253     }
254
255     @Test
256     void testAddingRicFromConfiguration() throws Exception {
257         // Test adding the RIC from configuration
258
259         final String RIC = "ric1";
260         final String TYPE = "type123";
261         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
262         nearRtRicPolicyTypes.put(createPolicyType(TYPE));
263         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
264
265         putService("service");
266
267         refreshConfigTask.handleUpdatedRicConfig( //
268                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
269                 .block();
270         waitForRicState(RIC, RicState.AVAILABLE);
271
272         // Test that the type has been synched
273         Ric addedRic = this.rics.getRic(RIC);
274         assertThat(addedRic.getSupportedPolicyTypes().size()).isEqualTo(1);
275         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
276
277         // Check that a service callback for the AVAILABLE RIC is invoked
278         RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
279         assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1);
280         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
281         assertThat(callbackInfo.ricId).isEqualTo(RIC);
282         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
283     }
284
285     @Test
286     void testAddingRicFromConfiguration_nonRespondingRic() throws ServiceException {
287         putService("service");
288
289         final String RIC = "NonRespondingRic";
290         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
291         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
292         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyTypeIdentities();
293
294         refreshConfigTask.handleUpdatedRicConfig( //
295                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
296                 .block();
297
298         waitForRicState(RIC, RicState.UNAVAILABLE);
299
300         // Check that no service callback for the UNAVAILABLE RIC is invoked
301         RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
302         assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(0);
303
304         // Run a synch and check that the AVAILABLE notificationis received
305         a1ClientFactory.reset();
306         supervision.checkAllRics();
307         waitForRicState(RIC, RicState.AVAILABLE);
308         receivedCallbacks = rAppSimulator.getTestResults();
309         assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1);
310     }
311
312     @Test
313     void testGetRics() throws Exception {
314         addRic("ric1");
315         this.addPolicyType("type1", "ric1");
316         String url = "/rics?policytype_id=type1";
317         String rsp = restClient().get(url).block();
318         assertThat(rsp).contains("ric1");
319
320         // nameless type for ORAN A1 1.1
321         addRic("ric2");
322         this.addPolicyType("", "ric2");
323         url = "/rics?policytype_id=";
324
325         // This tests also validation of trusted certs restClient(true)
326         rsp = restClient(true).get(url).block();
327         assertThat(rsp).contains("ric2") //
328                 .doesNotContain("ric1") //
329                 .contains("AVAILABLE");
330
331         // All RICs
332         rsp = restClient().get("/rics").block();
333         assertThat(rsp).contains("ric2") //
334                 .contains("ric1");
335
336         // Non existing policy type
337         url = "/rics?policytype_id=XXXX";
338         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
339     }
340
341     @Test
342     void testSynchronization() throws Exception {
343         // Two polictypes will be put in the NearRT RICs
344         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
345         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
346         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
347         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
348
349         // One type and one instance added to the Policy Management Service's storage
350         final String ric1Name = "ric1";
351         Ric ric1 = addRic(ric1Name);
352         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
353         Ric ric2 = addRic("ric2");
354
355         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
356         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
357
358         String policyId = "policyId";
359         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
360         supervision.checkAllRics(); // The created policy should be put in the RIC
361
362         // Wait until synch is completed
363         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
364         waitForRicState(ric1Name, RicState.AVAILABLE);
365         waitForRicState("ric2", RicState.AVAILABLE);
366
367         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
368         assertThat(ricPolicies.size()).isEqualTo(1);
369         Policy ricPolicy = ricPolicies.get(policyId);
370         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
371
372         // Both types should be in the Policy Management Service's storage after the
373         // synch
374         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
375         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
376     }
377
378     @Test
379     void testGetRic() throws Exception {
380         String ricId = "ric1";
381         String managedElementId = "kista_1";
382         addRic(ricId, managedElementId);
383
384         String url = "/rics/ric?managed_element_id=" + managedElementId;
385         String rsp = restClient().get(url).block();
386         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
387         assertThat(ricInfo.ricId).isEqualTo(ricId);
388
389         url = "/rics/ric?ric_id=" + ricId;
390         rsp = restClient().get(url).block();
391         ricInfo = gson.fromJson(rsp, RicInfo.class);
392         assertThat(ricInfo.ricId).isEqualTo(ricId);
393
394         // test GET RIC for ManagedElement that does not exist
395         url = "/rics/ric?managed_element_id=" + "junk";
396         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
397
398         url = "/rics/ric";
399         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
400     }
401
402     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
403             boolean isTransient) {
404         PolicyInfo info = new PolicyInfo();
405         info.policyId = policyInstanceId;
406         info.policyTypeId = policyTypeName;
407         info.ricId = ricId;
408         info.serviceId = serviceName;
409         info.policyData = gson.fromJson(jsonString(), Object.class);
410
411         if (isTransient) {
412             info.isTransient = isTransient;
413         }
414         info.statusNotificationUri = "statusNotificationUri";
415         return gson.toJson(info);
416     }
417
418     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
419         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
420     }
421
422     @Test
423     void testPutPolicy() throws Exception {
424         String serviceName = "service.1";
425         String ricId = "ric.1";
426         String policyTypeName = "type1_1.2.3";
427         String policyInstanceId = "instance_1.2.3";
428
429         putService(serviceName);
430         addPolicyType(policyTypeName, ricId);
431
432         // PUT a transient policy
433         String url = "/policies";
434         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
435         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
436
437         restClient().put(url, policyBody).block();
438
439         Policy policy = policies.getPolicy(policyInstanceId);
440         assertThat(policy).isNotNull();
441         assertThat(policy.getId()).isEqualTo(policyInstanceId);
442         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
443         assertThat(policy.getRic().id()).isEqualTo(ricId);
444         assertThat(policy.isTransient()).isTrue();
445
446         // Put a non transient policy
447         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
448         restClient().put(url, policyBody).block();
449         policy = policies.getPolicy(policyInstanceId);
450         assertThat(policy.isTransient()).isFalse();
451
452         url = "/policy-instances";
453         String rsp = restClient().get(url).block();
454         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
455
456         url = "/policies/" + policyInstanceId;
457         rsp = restClient().get(url).block();
458         assertThat(rsp).contains(policyBody);
459
460         // Test of error codes
461         url = "/policies";
462         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
463         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
464
465         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
466         addPolicyType(policyTypeName + "XX", "otherRic");
467         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
468
469         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
470         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
471         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
472         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
473     }
474
475     @Test
476     /**
477      * Test that HttpStatus and body from failing REST call to A1 is passed on to
478      * the caller.
479      *
480      * @throws ServiceException
481      */
482     void testErrorFromRic() throws ServiceException {
483         putService("service1");
484         addPolicyType("type1", "ric1");
485
486         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
487         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
488         String responseBody = "Refused";
489         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
490
491         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
492                 responseBodyBytes, StandardCharsets.UTF_8, null);
493         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
494
495         // PUT Policy
496         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
497         String url = "/policies";
498         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
499
500         // DELETE POLICY
501         this.addPolicy("instance1", "type1", "service1", "ric1");
502         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
503         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
504
505     }
506
507     @Test
508     void testPutTypelessPolicy() throws Exception {
509         putService("service1");
510         addPolicyType("", "ric1");
511         String body = putPolicyBody("service1", "ric1", "", "id1");
512         restClient().put("/policies", body).block();
513
514         String rsp = restClient().get("/policy-instances").block();
515         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
516         assertThat(info.policies).hasSize(1);
517         PolicyInfo policyInfo = info.policies.iterator().next();
518         assertThat(policyInfo.policyId).isEqualTo("id1");
519         assertThat(policyInfo.policyTypeId).isEmpty();
520     }
521
522     @Test
523     void testRefuseToUpdatePolicy() throws Exception {
524         // Test that only the json can be changed for a already created policy
525         // In this case service is attempted to be changed
526         this.addRic("ric1");
527         this.addRic("ricXXX");
528         this.addPolicy("instance1", "type1", "service1", "ric1");
529         this.addPolicy("instance2", "type1", "service1", "ricXXX");
530
531         // Try change ric1 -> ricXXX
532         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
533         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
534     }
535
536     @Test
537     void testGetPolicy() throws Exception {
538         String url = "/policies/id";
539         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
540         {
541             String rsp = restClient().get(url).block();
542             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
543             String policyStr = gson.toJson(info.policyData);
544             assertThat(policyStr).isEqualTo(policy.getJson());
545         }
546         {
547             policies.remove(policy);
548             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
549         }
550     }
551
552     @Test
553     void testDeletePolicy() throws Exception {
554         String policyId = "id.1";
555         addPolicy(policyId, "typeName", "service1", "ric1");
556         assertThat(policies.size()).isEqualTo(1);
557
558         String url = "/policies/" + policyId;
559         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
560
561         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
562         assertThat(policies.size()).isZero();
563
564         // Delete a non existing policy
565         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
566     }
567
568     @Test
569     void testGetPolicyType() throws Exception {
570         String typeId = "AC.D";
571         addPolicyType(typeId, "ric1");
572
573         waitForRicState("ric1", RicState.AVAILABLE);
574
575         String url = "/policy-types/" + typeId;
576
577         String rsp = this.restClient().get(url).block();
578
579         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
580         assertThat(info.schema).isNotNull();
581
582         // Get non existing schema
583         url = "/policy-types/JUNK";
584         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
585     }
586
587     String createPolicyTypesJson(String... types) {
588         List<String> list = new ArrayList<>();
589         Collections.addAll(list, types);
590         PolicyTypeIdList ids = new PolicyTypeIdList(list);
591         return gson.toJson(ids);
592     }
593
594     @Test
595     void testGetPolicyTypes() throws Exception {
596         addPolicyType("type1", "ric1");
597         addPolicyType("type2", "ric2");
598
599         String url = "/policy-types";
600         String rsp = restClient().get(url).block();
601         String expResp = createPolicyTypesJson("type2", "type1");
602         assertThat(rsp).isEqualTo(expResp);
603
604         url = "/policy-types?ric_id=ric1";
605         rsp = restClient().get(url).block();
606         expResp = createPolicyTypesJson("type1");
607         assertThat(rsp).isEqualTo(expResp);
608
609         // Get policy types for non existing RIC
610         url = "/policy-types?ric_id=ric1XXX";
611         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
612     }
613
614     @Test
615     void testGetPolicyInstances() throws Exception {
616         addPolicy("id1", "type1", "service1");
617
618         String url = "/policy-instances";
619         String rsp = restClient().get(url).block();
620         logger.info(rsp);
621         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
622         assertThat(info.policies).hasSize(1);
623         PolicyInfo policyInfo = info.policies.iterator().next();
624         assert (policyInfo.validate());
625         assertThat(policyInfo.policyId).isEqualTo("id1");
626         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
627         assertThat(policyInfo.serviceId).isEqualTo("service1");
628     }
629
630     @Test
631     void testGetPolicyInstancesFilter() throws Exception {
632         addPolicy("id1", "type1", "service1");
633         addPolicy("id2", "type1", "service2");
634         addPolicy("id3", "type2", "service1");
635
636         String url = "/policy-instances?policytype_id=type1";
637         String rsp = restClient().get(url).block();
638         logger.info(rsp);
639         assertThat(rsp).contains("id1") //
640                 .contains("id2") //
641                 .doesNotContain("id3");
642
643         url = "/policy-instances?policytype_id=type1&service_id=service2";
644         rsp = restClient().get(url).block();
645         logger.info(rsp);
646         assertThat(rsp).doesNotContain("id1") //
647                 .contains("id2") //
648                 .doesNotContain("id3");
649
650         // Test get policies for non existing type
651         url = "/policy-instances?policytype_id=type1XXX";
652         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
653
654         // Test get policies for non existing RIC
655         url = "/policy-instances?ric_id=XXX";
656         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
657     }
658
659     @Test
660     void testGetPolicyIdsFilter() throws Exception {
661         addPolicy("id1", "type1", "service1", "ric1");
662         addPolicy("id2", "type1", "service2", "ric1");
663         addPolicy("id3", "type2", "service1", "ric1");
664
665         String url = "/policies?policytype_id=type1";
666         String rsp = restClient().get(url).block();
667         logger.info(rsp);
668         assertThat(rsp).contains("id1") //
669                 .contains("id2") //
670                 .doesNotContain("id3");
671
672         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
673         rsp = restClient().get(url).block();
674         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
675         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
676
677         // Test get policy ids for non existing type
678         url = "/policies?policytype_id=type1XXX";
679         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
680
681         // Test get policy ids for non existing RIC
682         url = "/policies?ric_id=XXX";
683         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
684     }
685
686     @Test
687     void testPutAndGetService() throws Exception {
688         // PUT
689         String serviceName = "ac.dc";
690         putService(serviceName, 0, HttpStatus.CREATED);
691         putService(serviceName, 0, HttpStatus.OK);
692
693         // GET one service
694         String url = "/services?service_id=" + serviceName;
695         String rsp = restClient().get(url).block();
696         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
697         assertThat(info.statusList).hasSize(1);
698         ServiceStatus status = info.statusList.iterator().next();
699         assertThat(status.keepAliveIntervalSeconds).isZero();
700         assertThat(status.serviceId).isEqualTo(serviceName);
701
702         // GET (all)
703         url = "/services";
704         rsp = restClient().get(url).block();
705         assertThat(rsp).as("Response contains service name").contains(serviceName);
706         logger.info(rsp);
707
708         // Keep alive
709         url = "/services/" + serviceName + "/keepalive";
710         ResponseEntity<?> entity = restClient().putForEntity(url).block();
711         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
712
713         // DELETE service
714         assertThat(services.size()).isEqualTo(1);
715         url = "/services/" + serviceName;
716         restClient().delete(url).block();
717         assertThat(services.size()).isZero();
718
719         // Keep alive, no registered service
720         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
721
722         // PUT service with bad payload
723         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
724         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
725         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
726                 false);
727         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
728                 HttpStatus.BAD_REQUEST, false);
729
730         // GET non existing service
731         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
732     }
733
734     @Test
735     void testServiceSupervision() throws Exception {
736         putService("service1", 1, HttpStatus.CREATED);
737         addPolicyType("type1", "ric1");
738
739         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
740         restClient().put("/policies", policyBody).block();
741
742         assertThat(policies.size()).isEqualTo(1);
743         assertThat(services.size()).isEqualTo(1);
744
745         // Timeout after ~1 second
746         await().untilAsserted(() -> assertThat(policies.size()).isZero());
747         assertThat(services.size()).isZero();
748     }
749
750     @Test
751     void testGetPolicyStatus() throws Exception {
752         addPolicy("id", "typeName", "service1", "ric1");
753         assertThat(policies.size()).isEqualTo(1);
754
755         String url = "/policies/id/status";
756         String rsp = restClient().get(url).block();
757         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
758         assertThat(info.status).isEqualTo("OK");
759
760         // GET non existing policy status
761         url = "/policies/XXX/status";
762         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
763
764         // GET STATUS, the NearRT RIC returns error
765         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
766         url = "/policies/id/status";
767         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
768         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
769         rsp = restClient().get(url).block();
770         info = gson.fromJson(rsp, PolicyStatusInfo.class);
771         assertThat(info.status).hasToString("{}");
772     }
773
774     @Test
775     void testServiceNotification() throws ServiceException {
776         putService("junkService");
777         Service junkService = this.services.get("junkService");
778         junkService.setCallbackUrl("https://junk");
779         putService("service");
780
781         Ric ric = addRic("ric1");
782         ric.setState(Ric.RicState.UNAVAILABLE);
783         supervision.checkAllRics();
784         waitForRicState("ric1", RicState.AVAILABLE);
785
786         RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
787         assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1);
788         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
789         assertThat(callbackInfo.ricId).isEqualTo("ric1");
790         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
791     }
792
793     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
794         addRic(ric);
795         Policy policy = Policy.builder() //
796                 .id(id) //
797                 .json(jsonString()) //
798                 .ownerServiceId(service) //
799                 .ric(rics.getRic(ric)) //
800                 .type(addPolicyType(typeName, ric)) //
801                 .lastModified(Instant.now()) //
802                 .isTransient(false) //
803                 .statusNotificationUri("/policy-status?id=XXX") //
804                 .build();
805         policies.put(policy);
806         return policy;
807     }
808
809     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
810         return addPolicy(id, typeName, service, "ric");
811     }
812
813     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
814         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
815         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
816     }
817
818     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
819         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
820
821         String json = gson.toJson(service);
822         return json;
823     }
824
825     private void putService(String name) {
826         putService(name, 0, null);
827     }
828
829     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
830         String url = "/services";
831         String body = createServiceJson(name, keepAliveIntervalSeconds);
832         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
833         if (expectedStatus != null) {
834             assertEquals(expectedStatus, resp.getStatusCode(), "");
835         }
836     }
837
838     private String jsonString() {
839         return "{\"servingCellNrcgi\":\"1\"}";
840     }
841
842     @Test
843     void testConcurrency() throws Exception {
844         logger.info("Concurrency test starting");
845         final Instant startTime = Instant.now();
846         List<Thread> threads = new ArrayList<>();
847         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
848         a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
849         addRic("ric");
850         addPolicyType("type1", "ric");
851         addPolicyType("type2", "ric");
852
853         for (int i = 0; i < 10; ++i) {
854             AsyncRestClient restClient = restClient();
855             ConcurrencyTestRunnable test =
856                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
857             Thread thread = new Thread(test, "TestThread_" + i);
858             thread.start();
859             threads.add(thread);
860             tests.add(test);
861         }
862         for (Thread t : threads) {
863             t.join();
864         }
865         for (ConcurrencyTestRunnable test : tests) {
866             assertThat(test.isFailed()).isFalse();
867         }
868         assertThat(policies.size()).isZero();
869         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
870     }
871
872     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
873         WebClientConfig config = this.applicationConfig.getWebClientConfig();
874         config = ImmutableWebClientConfig.builder() //
875                 .keyStoreType(config.keyStoreType()) //
876                 .keyStorePassword(config.keyStorePassword()) //
877                 .keyStore(config.keyStore()) //
878                 .keyPassword(config.keyPassword()) //
879                 .isTrustStoreUsed(useTrustValidation) //
880                 .trustStore(config.trustStore()) //
881                 .trustStorePassword(config.trustStorePassword()) //
882                 .httpProxyConfig(config.httpProxyConfig()) //
883                 .build();
884
885         AsyncRestClientFactory f = new AsyncRestClientFactory(config);
886         return f.createRestClientNoHttpProxy(baseUrl);
887
888     }
889
890     private String baseUrl() {
891         return "https://localhost:" + port;
892     }
893
894     private AsyncRestClient restClient(boolean useTrustValidation) {
895         String baseUrl = "https://localhost:" + port + Consts.V2_API_ROOT;
896         return restClient(baseUrl, useTrustValidation);
897     }
898
899     private AsyncRestClient restClient() {
900         return restClient(false);
901     }
902
903     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
904         testErrorCode(request, expStatus, "", true);
905     }
906
907     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
908         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
909     }
910
911     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
912         testErrorCode(request, expStatus, responseContains, true);
913     }
914
915     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
916             boolean expectApplicationProblemJsonMediaType) {
917         StepVerifier.create(request) //
918                 .expectSubscription() //
919                 .expectErrorMatches(
920                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
921                 .verify();
922     }
923
924     private void waitForRicState(String ricId, RicState state) throws ServiceException {
925         Ric ric = rics.getRic(ricId);
926         await().untilAsserted(() -> state.equals(ric.getState()));
927     }
928
929     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
930             boolean expectApplicationProblemJsonMediaType) {
931         assertTrue(throwable instanceof WebClientResponseException);
932         WebClientResponseException responseException = (WebClientResponseException) throwable;
933         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
934         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
935         if (expectApplicationProblemJsonMediaType) {
936             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
937         }
938         return true;
939     }
940
941     private MockA1Client getA1Client(String ricId) throws ServiceException {
942         return a1ClientFactory.getOrCreateA1Client(ricId);
943     }
944
945     private PolicyType createPolicyType(String policyTypeName) {
946         return PolicyType.builder() //
947                 .id(policyTypeName) //
948                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
949                 .build();
950     }
951
952     private PolicyType addPolicyType(String policyTypeName, String ricId) {
953         PolicyType type = createPolicyType(policyTypeName);
954         policyTypes.put(type);
955         addRic(ricId).addSupportedPolicyType(type);
956         return type;
957     }
958
959     private Ric addRic(String ricId) {
960         return addRic(ricId, null);
961     }
962
963     private RicConfig ricConfig(String ricId, String managedElement) {
964         List<String> mes = new ArrayList<>();
965         if (managedElement != null) {
966             mes.add(managedElement);
967         }
968         return ImmutableRicConfig.builder() //
969                 .ricId(ricId) //
970                 .baseUrl(ricId) //
971                 .managedElementIds(mes) //
972                 .controllerName("") //
973                 .build();
974     }
975
976     private Ric addRic(String ricId, String managedElement) {
977         if (rics.get(ricId) != null) {
978             return rics.get(ricId);
979         }
980
981         RicConfig conf = ricConfig(ricId, managedElement);
982         Ric ric = new Ric(conf);
983         ric.setState(Ric.RicState.AVAILABLE);
984         this.rics.put(ric);
985         return ric;
986     }
987
988 }