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