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