08350f81feb4d9db9d2d723f5d3c18af1050d19e
[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=./src/test/resources/keystore.jks", //
101         "app.webclient.trust-store=./src/test/resources/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()).isZero();
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()).hasSize(1);
275         assertThat(addedRic.getSupportedPolicyTypes().iterator().next().getId()).isEqualTo(TYPE);
276
277         // Check that a service callback for the AVAILABLE RIC is invoked
278         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
279         await().untilAsserted(() -> 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 Exception {
287         putService("service");
288
289         final String RIC = "NonRespondingRic";
290         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client(RIC);
291         doReturn(MockA1Client.monoError("error", HttpStatus.BAD_GATEWAY)).when(a1Client).getPolicyTypeIdentities();
292
293         refreshConfigTask.handleUpdatedRicConfig( //
294                 new RicConfigUpdate(ricConfig(RIC, "me1"), RicConfigUpdate.Type.ADDED)) //
295                 .block();
296
297         waitForRicState(RIC, RicState.UNAVAILABLE);
298
299         // Check that no service callback for the UNAVAILABLE RIC is invoked
300         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
301         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo()).isEmpty());
302
303         // Run a synch and check that the AVAILABLE notification is received
304         a1ClientFactory.reset();
305         supervision.checkAllRics();
306         waitForRicState(RIC, RicState.AVAILABLE);
307
308         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1));
309     }
310
311     @Test
312     void testTrustValidation() {
313         addRic("ric1");
314         String rsp = restClient(true).get("/rics").block(); // restClient(true) enables trust validation
315         assertThat(rsp).contains("ric1");
316     }
317
318     @Test
319     void testGetRics() throws Exception {
320         addRic("ric1");
321         this.addPolicyType("type1", "ric1");
322         String url = "/rics?policytype_id=type1";
323         String rsp = restClient().get(url).block();
324         assertThat(rsp).contains("ric1");
325
326         // nameless type for ORAN A1 1.1
327         addRic("ric2");
328         this.addPolicyType("", "ric2");
329         url = "/rics?policytype_id=";
330         rsp = restClient().get(url).block();
331         assertThat(rsp).contains("ric2") //
332                 .doesNotContain("ric1") //
333                 .contains("AVAILABLE");
334
335         // All RICs
336         rsp = restClient().get("/rics").block();
337         assertThat(rsp).contains("ric2") //
338                 .contains("ric1");
339
340         // Non existing policy type
341         url = "/rics?policytype_id=XXXX";
342         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
343     }
344
345     @Test
346     void testSynchronization() throws Exception {
347         // Two polictypes will be put in the NearRT RICs
348         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
349         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
350         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
351         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
352
353         // One type and one instance added to the Policy Management Service's storage
354         final String ric1Name = "ric1";
355         Ric ric1 = addRic(ric1Name);
356         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
357         Ric ric2 = addRic("ric2");
358
359         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
360         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
361
362         String policyId = "policyId";
363         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
364         supervision.checkAllRics(); // The created policy should be put in the RIC
365
366         // Wait until synch is completed
367         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
368         waitForRicState(ric1Name, RicState.AVAILABLE);
369         waitForRicState("ric2", RicState.AVAILABLE);
370
371         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
372         assertThat(ricPolicies.size()).isEqualTo(1);
373         Policy ricPolicy = ricPolicies.get(policyId);
374         assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
375
376         // Both types should be in the Policy Management Service's storage after the
377         // synch
378         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
379         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
380     }
381
382     @Test
383     void testGetRic() throws Exception {
384         String ricId = "ric1";
385         String managedElementId = "kista_1";
386         addRic(ricId, managedElementId);
387
388         String url = "/rics/ric?managed_element_id=" + managedElementId;
389         String rsp = restClient().get(url).block();
390         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
391         assertThat(ricInfo.ricId).isEqualTo(ricId);
392
393         url = "/rics/ric?ric_id=" + ricId;
394         rsp = restClient().get(url).block();
395         ricInfo = gson.fromJson(rsp, RicInfo.class);
396         assertThat(ricInfo.ricId).isEqualTo(ricId);
397
398         // test GET RIC for ManagedElement that does not exist
399         url = "/rics/ric?managed_element_id=" + "junk";
400         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
401
402         url = "/rics/ric";
403         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
404     }
405
406     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
407             boolean isTransient) {
408         PolicyInfo info = new PolicyInfo();
409         info.policyId = policyInstanceId;
410         info.policyTypeId = policyTypeName;
411         info.ricId = ricId;
412         info.serviceId = serviceName;
413         info.policyData = gson.fromJson(jsonString(), Object.class);
414
415         if (isTransient) {
416             info.isTransient = isTransient;
417         }
418         info.statusNotificationUri = "statusNotificationUri";
419         return gson.toJson(info);
420     }
421
422     private String putPolicyBody(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
423         return putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, false);
424     }
425
426     @Test
427     void testPutPolicy() throws Exception {
428         String serviceName = "service.1";
429         String ricId = "ric.1";
430         String policyTypeName = "type1_1.2.3";
431         String policyInstanceId = "instance_1.2.3";
432
433         putService(serviceName);
434         addPolicyType(policyTypeName, ricId);
435
436         // PUT a transient policy
437         String url = "/policies";
438         String policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId, true);
439         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
440
441         restClient().put(url, policyBody).block();
442
443         Policy policy = policies.getPolicy(policyInstanceId);
444         assertThat(policy).isNotNull();
445         assertThat(policy.getId()).isEqualTo(policyInstanceId);
446         assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
447         assertThat(policy.getRic().id()).isEqualTo(ricId);
448         assertThat(policy.isTransient()).isTrue();
449
450         // Put a non transient policy
451         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
452         restClient().put(url, policyBody).block();
453         policy = policies.getPolicy(policyInstanceId);
454         assertThat(policy.isTransient()).isFalse();
455
456         url = "/policy-instances";
457         String rsp = restClient().get(url).block();
458         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
459
460         url = "/policies/" + policyInstanceId;
461         rsp = restClient().get(url).block();
462         assertThat(rsp).contains(policyBody);
463
464         // Test of error codes
465         url = "/policies";
466         policyBody = putPolicyBody(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
467         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
468
469         policyBody = putPolicyBody(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
470         addPolicyType(policyTypeName + "XX", "otherRic");
471         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
472
473         policyBody = putPolicyBody(serviceName, ricId, policyTypeName, policyInstanceId);
474         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
475         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
476         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
477     }
478
479     @Test
480     /**
481      * Test that HttpStatus and body from failing REST call to A1 is passed on to
482      * the caller.
483      *
484      * @throws ServiceException
485      */
486     void testErrorFromRic() throws ServiceException {
487         putService("service1");
488         addPolicyType("type1", "ric1");
489
490         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
491         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
492         String responseBody = "Refused";
493         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
494
495         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
496                 responseBodyBytes, StandardCharsets.UTF_8, null);
497         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
498
499         // PUT Policy
500         String putBody = putPolicyBody("service1", "ric1", "type1", "id1");
501         String url = "/policies";
502         testErrorCode(restClient().put(url, putBody), httpStatus, responseBody);
503
504         // DELETE POLICY
505         this.addPolicy("instance1", "type1", "service1", "ric1");
506         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
507         testErrorCode(restClient().delete("/policies/instance1"), httpStatus, responseBody);
508
509     }
510
511     @Test
512     void testPutTypelessPolicy() throws Exception {
513         putService("service1");
514         addPolicyType("", "ric1");
515         String body = putPolicyBody("service1", "ric1", "", "id1");
516         restClient().put("/policies", body).block();
517
518         String rsp = restClient().get("/policy-instances").block();
519         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
520         assertThat(info.policies).hasSize(1);
521         PolicyInfo policyInfo = info.policies.iterator().next();
522         assertThat(policyInfo.policyId).isEqualTo("id1");
523         assertThat(policyInfo.policyTypeId).isEmpty();
524     }
525
526     @Test
527     void testRefuseToUpdatePolicy() throws Exception {
528         // Test that only the json can be changed for a already created policy
529         // In this case service is attempted to be changed
530         this.addRic("ric1");
531         this.addRic("ricXXX");
532         this.addPolicy("instance1", "type1", "service1", "ric1");
533         this.addPolicy("instance2", "type1", "service1", "ricXXX");
534
535         // Try change ric1 -> ricXXX
536         String bodyWrongRic = putPolicyBody("service1", "ricXXX", "type1", "instance1");
537         testErrorCode(restClient().put("/policies", bodyWrongRic), HttpStatus.CONFLICT);
538     }
539
540     @Test
541     void testGetPolicy() throws Exception {
542         String url = "/policies/id";
543         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
544         {
545             String rsp = restClient().get(url).block();
546             PolicyInfo info = gson.fromJson(rsp, PolicyInfo.class);
547             String policyStr = gson.toJson(info.policyData);
548             assertThat(policyStr).isEqualTo(policy.getJson());
549         }
550         {
551             policies.remove(policy);
552             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
553         }
554     }
555
556     @Test
557     void testDeletePolicy() throws Exception {
558         String policyId = "id.1";
559         addPolicy(policyId, "typeName", "service1", "ric1");
560         assertThat(policies.size()).isEqualTo(1);
561
562         String url = "/policies/" + policyId;
563         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
564
565         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
566         assertThat(policies.size()).isZero();
567
568         // Delete a non existing policy
569         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
570     }
571
572     @Test
573     void testGetPolicyType() throws Exception {
574         String typeId = "AC.D";
575         addPolicyType(typeId, "ric1");
576
577         waitForRicState("ric1", RicState.AVAILABLE);
578
579         String url = "/policy-types/" + typeId;
580
581         String rsp = this.restClient().get(url).block();
582
583         PolicyTypeInfo info = gson.fromJson(rsp, PolicyTypeInfo.class);
584         assertThat(info.schema).isNotNull();
585
586         // Get non existing schema
587         url = "/policy-types/JUNK";
588         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
589     }
590
591     String createPolicyTypesJson(String... types) {
592         List<String> list = new ArrayList<>();
593         Collections.addAll(list, types);
594         PolicyTypeIdList ids = new PolicyTypeIdList(list);
595         return gson.toJson(ids);
596     }
597
598     @Test
599     void testGetPolicyTypes() throws Exception {
600         addPolicyType("type1", "ric1");
601         addPolicyType("type2", "ric2");
602
603         String url = "/policy-types";
604         String rsp = restClient().get(url).block();
605         String expResp = createPolicyTypesJson("type2", "type1");
606         assertThat(rsp).isEqualTo(expResp);
607
608         url = "/policy-types?ric_id=ric1";
609         rsp = restClient().get(url).block();
610         expResp = createPolicyTypesJson("type1");
611         assertThat(rsp).isEqualTo(expResp);
612
613         // Get policy types for non existing RIC
614         url = "/policy-types?ric_id=ric1XXX";
615         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
616     }
617
618     @Test
619     void testGetPolicyInstances() throws Exception {
620         addPolicy("id1", "type1", "service1");
621
622         String url = "/policy-instances";
623         String rsp = restClient().get(url).block();
624         logger.info(rsp);
625         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
626         assertThat(info.policies).hasSize(1);
627         PolicyInfo policyInfo = info.policies.iterator().next();
628         assert (policyInfo.validate());
629         assertThat(policyInfo.policyId).isEqualTo("id1");
630         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
631         assertThat(policyInfo.serviceId).isEqualTo("service1");
632     }
633
634     @Test
635     void testGetPolicyInstancesFilter() throws Exception {
636         addPolicy("id1", "type1", "service1");
637         addPolicy("id2", "type1", "service2");
638         addPolicy("id3", "type2", "service1");
639
640         String url = "/policy-instances?policytype_id=type1";
641         String rsp = restClient().get(url).block();
642         logger.info(rsp);
643         assertThat(rsp).contains("id1") //
644                 .contains("id2") //
645                 .doesNotContain("id3");
646
647         url = "/policy-instances?policytype_id=type1&service_id=service2";
648         rsp = restClient().get(url).block();
649         logger.info(rsp);
650         assertThat(rsp).doesNotContain("id1") //
651                 .contains("id2") //
652                 .doesNotContain("id3");
653
654         // Test get policies for non existing type
655         url = "/policy-instances?policytype_id=type1XXX";
656         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
657
658         // Test get policies for non existing RIC
659         url = "/policy-instances?ric_id=XXX";
660         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
661     }
662
663     @Test
664     void testGetPolicyIdsFilter() throws Exception {
665         addPolicy("id1", "type1", "service1", "ric1");
666         addPolicy("id2", "type1", "service2", "ric1");
667         addPolicy("id3", "type2", "service1", "ric1");
668
669         String url = "/policies?policytype_id=type1";
670         String rsp = restClient().get(url).block();
671         logger.info(rsp);
672         assertThat(rsp).contains("id1") //
673                 .contains("id2") //
674                 .doesNotContain("id3");
675
676         url = "/policies?policytype_id=type1&service_id=service1&ric=ric1";
677         rsp = restClient().get(url).block();
678         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
679         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
680
681         // Test get policy ids for non existing type
682         url = "/policies?policytype_id=type1XXX";
683         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
684
685         // Test get policy ids for non existing RIC
686         url = "/policies?ric_id=XXX";
687         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
688     }
689
690     @Test
691     void testPutAndGetService() throws Exception {
692         // PUT
693         String serviceName = "ac.dc";
694         putService(serviceName, 0, HttpStatus.CREATED);
695         putService(serviceName, 0, HttpStatus.OK);
696
697         // GET one service
698         String url = "/services?service_id=" + serviceName;
699         String rsp = restClient().get(url).block();
700         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
701         assertThat(info.statusList).hasSize(1);
702         ServiceStatus status = info.statusList.iterator().next();
703         assertThat(status.keepAliveIntervalSeconds).isZero();
704         assertThat(status.serviceId).isEqualTo(serviceName);
705
706         // GET (all)
707         url = "/services";
708         rsp = restClient().get(url).block();
709         assertThat(rsp).as("Response contains service name").contains(serviceName);
710         logger.info(rsp);
711
712         // Keep alive
713         url = "/services/" + serviceName + "/keepalive";
714         ResponseEntity<?> entity = restClient().putForEntity(url).block();
715         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
716
717         // DELETE service
718         assertThat(services.size()).isEqualTo(1);
719         url = "/services/" + serviceName;
720         restClient().delete(url).block();
721         assertThat(services.size()).isZero();
722
723         // Keep alive, no registered service
724         testErrorCode(restClient().put("/services/junk/keepalive", ""), HttpStatus.NOT_FOUND);
725
726         // PUT service with bad payload
727         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
728         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
729         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
730                 false);
731         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
732                 HttpStatus.BAD_REQUEST, false);
733
734         // GET non existing service
735         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
736     }
737
738     @Test
739     void testServiceSupervision() throws Exception {
740         putService("service1", 1, HttpStatus.CREATED);
741         addPolicyType("type1", "ric1");
742
743         String policyBody = putPolicyBody("service1", "ric1", "type1", "instance1");
744         restClient().put("/policies", policyBody).block();
745
746         assertThat(policies.size()).isEqualTo(1);
747         assertThat(services.size()).isEqualTo(1);
748
749         // Timeout after ~1 second
750         await().untilAsserted(() -> assertThat(policies.size()).isZero());
751         assertThat(services.size()).isZero();
752     }
753
754     @Test
755     void testGetPolicyStatus() throws Exception {
756         addPolicy("id", "typeName", "service1", "ric1");
757         assertThat(policies.size()).isEqualTo(1);
758
759         String url = "/policies/id/status";
760         String rsp = restClient().get(url).block();
761         PolicyStatusInfo info = gson.fromJson(rsp, PolicyStatusInfo.class);
762         assertThat(info.status).isEqualTo("OK");
763
764         // GET non existing policy status
765         url = "/policies/XXX/status";
766         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
767
768         // GET STATUS, the NearRT RIC returns error
769         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
770         url = "/policies/id/status";
771         WebClientResponseException a1Exception = new WebClientResponseException(404, "", null, null, null);
772         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
773         rsp = restClient().get(url).block();
774         info = gson.fromJson(rsp, PolicyStatusInfo.class);
775         assertThat(info.status).hasToString("{}");
776     }
777
778     @Test
779     void testServiceNotification() throws ServiceException {
780         putService("junkService");
781         Service junkService = this.services.get("junkService");
782         junkService.setCallbackUrl("https://junk");
783         putService("service");
784
785         Ric ric = addRic("ric1");
786         ric.setState(Ric.RicState.UNAVAILABLE);
787         supervision.checkAllRics();
788         waitForRicState("ric1", RicState.AVAILABLE);
789
790         final RappSimulatorController.TestResults receivedCallbacks = rAppSimulator.getTestResults();
791         await().untilAsserted(() -> assertThat(receivedCallbacks.getReceivedInfo().size()).isEqualTo(1));
792         ServiceCallbackInfo callbackInfo = receivedCallbacks.getReceivedInfo().get(0);
793         assertThat(callbackInfo.ricId).isEqualTo("ric1");
794         assertThat(callbackInfo.eventType).isEqualTo(ServiceCallbackInfo.EventType.AVAILABLE);
795     }
796
797     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
798         addRic(ric);
799         Policy policy = Policy.builder() //
800                 .id(id) //
801                 .json(jsonString()) //
802                 .ownerServiceId(service) //
803                 .ric(rics.getRic(ric)) //
804                 .type(addPolicyType(typeName, ric)) //
805                 .lastModified(Instant.now()) //
806                 .isTransient(false) //
807                 .statusNotificationUri("/policy-status?id=XXX") //
808                 .build();
809         policies.put(policy);
810         return policy;
811     }
812
813     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
814         return addPolicy(id, typeName, service, "ric");
815     }
816
817     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
818         String callbackUrl = baseUrl() + RappSimulatorController.SERVICE_CALLBACK_URL;
819         return createServiceJson(name, keepAliveIntervalSeconds, callbackUrl);
820     }
821
822     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
823         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
824
825         String json = gson.toJson(service);
826         return json;
827     }
828
829     private void putService(String name) {
830         putService(name, 0, null);
831     }
832
833     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
834         String url = "/services";
835         String body = createServiceJson(name, keepAliveIntervalSeconds);
836         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
837         if (expectedStatus != null) {
838             assertEquals(expectedStatus, resp.getStatusCode(), "");
839         }
840     }
841
842     private String jsonString() {
843         return "{\"servingCellNrcgi\":\"1\"}";
844     }
845
846     @Test
847     void testConcurrency() throws Exception {
848         logger.info("Concurrency test starting");
849         final Instant startTime = Instant.now();
850         List<Thread> threads = new ArrayList<>();
851         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
852         a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
853         addRic("ric");
854         addPolicyType("type1", "ric");
855         addPolicyType("type2", "ric");
856
857         for (int i = 0; i < 10; ++i) {
858             AsyncRestClient restClient = restClient();
859             ConcurrencyTestRunnable test =
860                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
861             Thread thread = new Thread(test, "TestThread_" + i);
862             thread.start();
863             threads.add(thread);
864             tests.add(test);
865         }
866         for (Thread t : threads) {
867             t.join();
868         }
869         for (ConcurrencyTestRunnable test : tests) {
870             assertThat(test.isFailed()).isFalse();
871         }
872         assertThat(policies.size()).isZero();
873         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
874     }
875
876     private AsyncRestClient restClient(String baseUrl, boolean useTrustValidation) {
877         WebClientConfig config = this.applicationConfig.getWebClientConfig();
878         config = ImmutableWebClientConfig.builder() //
879                 .keyStoreType(config.keyStoreType()) //
880                 .keyStorePassword(config.keyStorePassword()) //
881                 .keyStore(config.keyStore()) //
882                 .keyPassword(config.keyPassword()) //
883                 .isTrustStoreUsed(useTrustValidation) //
884                 .trustStore(config.trustStore()) //
885                 .trustStorePassword(config.trustStorePassword()) //
886                 .httpProxyConfig(config.httpProxyConfig()) //
887                 .build();
888
889         AsyncRestClientFactory f = new AsyncRestClientFactory(config);
890         return f.createRestClientNoHttpProxy(baseUrl);
891
892     }
893
894     private String baseUrl() {
895         return "https://localhost:" + port;
896     }
897
898     private AsyncRestClient restClient(boolean useTrustValidation) {
899         return restClient(baseUrl() + Consts.V2_API_ROOT, useTrustValidation);
900     }
901
902     private AsyncRestClient restClient() {
903         return restClient(false);
904     }
905
906     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
907         testErrorCode(request, expStatus, "", true);
908     }
909
910     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
911         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
912     }
913
914     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
915         testErrorCode(request, expStatus, responseContains, true);
916     }
917
918     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
919             boolean expectApplicationProblemJsonMediaType) {
920         StepVerifier.create(request) //
921                 .expectSubscription() //
922                 .expectErrorMatches(
923                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
924                 .verify();
925     }
926
927     private void waitForRicState(String ricId, RicState state) throws ServiceException {
928         Ric ric = rics.getRic(ricId);
929         await().untilAsserted(() -> state.equals(ric.getState()));
930     }
931
932     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
933             boolean expectApplicationProblemJsonMediaType) {
934         assertTrue(throwable instanceof WebClientResponseException);
935         WebClientResponseException responseException = (WebClientResponseException) throwable;
936         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
937         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
938         if (expectApplicationProblemJsonMediaType) {
939             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
940         }
941         return true;
942     }
943
944     private MockA1Client getA1Client(String ricId) throws ServiceException {
945         return a1ClientFactory.getOrCreateA1Client(ricId);
946     }
947
948     private PolicyType createPolicyType(String policyTypeName) {
949         return PolicyType.builder() //
950                 .id(policyTypeName) //
951                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
952                 .build();
953     }
954
955     private PolicyType addPolicyType(String policyTypeName, String ricId) {
956         PolicyType type = createPolicyType(policyTypeName);
957         policyTypes.put(type);
958         addRic(ricId).addSupportedPolicyType(type);
959         return type;
960     }
961
962     private Ric addRic(String ricId) {
963         return addRic(ricId, null);
964     }
965
966     private RicConfig ricConfig(String ricId, String managedElement) {
967         List<String> mes = new ArrayList<>();
968         if (managedElement != null) {
969             mes.add(managedElement);
970         }
971         return ImmutableRicConfig.builder() //
972                 .ricId(ricId) //
973                 .baseUrl(ricId) //
974                 .managedElementIds(mes) //
975                 .controllerName("") //
976                 .build();
977     }
978
979     private Ric addRic(String ricId, String managedElement) {
980         if (rics.get(ricId) != null) {
981             return rics.get(ricId);
982         }
983
984         RicConfig conf = ricConfig(ricId, managedElement);
985         Ric ric = new Ric(conf);
986         ric.setState(Ric.RicState.AVAILABLE);
987         this.rics.put(ric);
988         return ric;
989     }
990
991 }