97d0b5044000205ee3b1674c8df23c06e37c9768
[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.v1;
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 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonParser;
35
36 import java.nio.charset.StandardCharsets;
37 import java.time.Duration;
38 import java.time.Instant;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 import org.junit.jupiter.api.AfterEach;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
47 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
48 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
49 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
50 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
51 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
52 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
53 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
65 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
66 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
67 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
68 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71 import org.springframework.beans.factory.annotation.Autowired;
72 import org.springframework.boot.test.context.SpringBootTest;
73 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
74 import org.springframework.boot.test.context.TestConfiguration;
75 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
76 import org.springframework.boot.web.server.LocalServerPort;
77 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
78 import org.springframework.context.ApplicationContext;
79 import org.springframework.context.annotation.Bean;
80 import org.springframework.http.HttpStatus;
81 import org.springframework.http.ResponseEntity;
82 import org.springframework.test.context.TestPropertySource;
83 import org.springframework.test.context.junit.jupiter.SpringExtension;
84 import org.springframework.web.reactive.function.client.WebClientResponseException;
85
86 import reactor.core.publisher.Mono;
87 import reactor.test.StepVerifier;
88 import reactor.util.annotation.Nullable;
89
90 @ExtendWith(SpringExtension.class)
91 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
92 @TestPropertySource(properties = { //
93         "server.ssl.key-store=./config/keystore.jks", //
94         "app.webclient.trust-store=./config/truststore.jks"})
95 class ApplicationTest {
96     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
97
98     @Autowired
99     ApplicationContext context;
100
101     @Autowired
102     private Rics rics;
103
104     @Autowired
105     private Policies policies;
106
107     @Autowired
108     private PolicyTypes policyTypes;
109
110     @Autowired
111     MockA1ClientFactory a1ClientFactory;
112
113     @Autowired
114     RicSupervision supervision;
115
116     @Autowired
117     ApplicationConfig applicationConfig;
118
119     @Autowired
120     Services services;
121
122     private static Gson gson = new GsonBuilder() //
123             .serializeNulls() //
124             .create(); //
125
126     public static class MockApplicationConfig extends ApplicationConfig {
127         @Override
128         public String getLocalConfigurationFilePath() {
129             return ""; // No config file loaded for the test
130         }
131     }
132
133     /**
134      * Overrides the BeanFactory.
135      */
136     @TestConfiguration
137     static class TestBeanFactory {
138         private final PolicyTypes policyTypes = new PolicyTypes();
139         private final Services services = new Services();
140         private final Policies policies = new Policies();
141         MockA1ClientFactory a1ClientFactory = null;
142
143         @Bean
144         public ApplicationConfig getApplicationConfig() {
145             return new MockApplicationConfig();
146         }
147
148         @Bean
149         MockA1ClientFactory getA1ClientFactory() {
150             if (a1ClientFactory == null) {
151                 this.a1ClientFactory = new MockA1ClientFactory(this.policyTypes);
152             }
153             return this.a1ClientFactory;
154         }
155
156         @Bean
157         public PolicyTypes getPolicyTypes() {
158             return this.policyTypes;
159         }
160
161         @Bean
162         Policies getPolicies() {
163             return this.policies;
164         }
165
166         @Bean
167         Services getServices() {
168             return this.services;
169         }
170
171         @Bean
172         public ServiceSupervision getServiceSupervision() {
173             Duration checkInterval = Duration.ofMillis(1);
174             return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
175         }
176
177         @Bean
178         public ServletWebServerFactory servletContainer() {
179             return new TomcatServletWebServerFactory();
180         }
181
182     }
183
184     @LocalServerPort
185     private int port;
186
187     @BeforeEach
188     void reset() {
189         rics.clear();
190         policies.clear();
191         policyTypes.clear();
192         services.clear();
193         a1ClientFactory.reset();
194     }
195
196     @AfterEach
197     void verifyNoRicLocks() {
198         for (Ric ric : this.rics.getRics()) {
199             ric.getLock().lockBlocking(LockType.EXCLUSIVE);
200             ric.getLock().unlockBlocking();
201             assertThat(ric.getLock().getLockCounter()).isZero();
202             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
203         }
204     }
205
206     @Test
207     void testGetRics() throws Exception {
208         addRic("ric1");
209         this.addPolicyType("type1", "ric1");
210         String url = "/rics?policyType=type1";
211         String rsp = restClient().get(url).block();
212         assertThat(rsp).contains("ric1");
213
214         // nameless type for ORAN A1 1.1
215         addRic("ric2");
216         this.addPolicyType("", "ric2");
217         url = "/rics?policyType=";
218
219         // This tests also validation of trusted certs restClient(true)
220         rsp = restClient(true).get(url).block();
221         assertThat(rsp).contains("ric2") //
222                 .doesNotContain("ric1") //
223                 .contains("AVAILABLE");
224
225         // All RICs
226         rsp = restClient().get("/rics").block();
227         assertThat(rsp).contains("ric2") //
228                 .contains("ric1");
229
230         // Non existing policy type
231         url = "/rics?policyType=XXXX";
232         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
233     }
234
235     @Test
236     void testSynchronization() throws Exception {
237         // Two polictypes will be put in the NearRT RICs
238         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
239         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
240         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
241         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
242
243         // One type and one instance added to the Policy Management Service's storage
244         final String ric1Name = "ric1";
245         Ric ric1 = addRic(ric1Name);
246         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
247         Ric ric2 = addRic("ric2");
248
249         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
250         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
251
252         String policyId = "policyId";
253         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
254         supervision.checkAllRics(); // The created policy should be put in the RIC
255
256         // Wait until synch is completed
257         await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
258         await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
259         await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
260
261         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
262         assertThat(ricPolicies.size()).isEqualTo(1);
263         Policy ricPolicy = ricPolicies.get(policyId);
264         assertThat(ricPolicy.json()).isEqualTo(policy.json());
265
266         // Both types should be in the Policy Management Service's storage after the
267         // synch
268         assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
269         assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
270     }
271
272     @Test
273     void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
274         String ricName = "ric1";
275         String managedElementId = "kista_1";
276         addRic(ricName, managedElementId);
277
278         String url = "/ric?managedElementId=" + managedElementId;
279         String rsp = restClient().get(url).block();
280         assertThat(rsp).isEqualTo(ricName);
281
282         // test GET RIC for ManagedElement that does not exist
283         url = "/ric?managedElementId=" + "junk";
284         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
285     }
286
287     private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
288             boolean isTransient) {
289         String url;
290         if (policyTypeName.isEmpty()) {
291             url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
292         } else {
293             url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
294                     + policyTypeName;
295         }
296         if (isTransient) {
297             url += "&transient=true";
298         }
299         return url;
300     }
301
302     private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
303         return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
304     }
305
306     @Test
307     void testPutPolicy() throws Exception {
308         String serviceName = "service1";
309         String ricName = "ric1";
310         String policyTypeName = "type1";
311         String policyInstanceId = "instance1";
312
313         putService(serviceName);
314         addPolicyType(policyTypeName, ricName);
315
316         // PUT a transient policy
317         String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
318         final String policyBody = jsonString();
319         this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
320
321         restClient().put(url, policyBody).block();
322
323         Policy policy = policies.getPolicy(policyInstanceId);
324         assertThat(policy).isNotNull();
325         assertThat(policy.id()).isEqualTo(policyInstanceId);
326         assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
327         assertThat(policy.ric().id()).isEqualTo("ric1");
328         assertThat(policy.isTransient()).isTrue();
329
330         // Put a non transient policy
331         url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
332         restClient().put(url, policyBody).block();
333         policy = policies.getPolicy(policyInstanceId);
334         assertThat(policy.isTransient()).isFalse();
335
336         url = "/policies";
337         String rsp = restClient().get(url).block();
338         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
339
340         url = "/policy?id=" + policyInstanceId;
341         rsp = restClient().get(url).block();
342         assertThat(rsp).isEqualTo(policyBody);
343
344         // Test of error codes
345         url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
346         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
347
348         url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
349         addPolicyType(policyTypeName + "XX", "otherRic");
350         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
351
352         url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
353         this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
354         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
355         this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
356     }
357
358     @Test
359     /**
360      * Test that HttpStatus and body from failing REST call to A1 is passed on to
361      * the caller.
362      *
363      * @throws ServiceException
364      */
365     void testErrorFromRic() throws ServiceException {
366         putService("service1");
367         addPolicyType("type1", "ric1");
368
369         String url = putPolicyUrl("service1", "ric1", "type1", "id1");
370         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
371         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
372         String responseBody = "Refused";
373         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
374
375         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
376                 responseBodyBytes, StandardCharsets.UTF_8, null);
377         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
378
379         // PUT Policy
380         testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
381
382         // DELETE POLICY
383         this.addPolicy("instance1", "type1", "service1", "ric1");
384         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
385         testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
386
387         // GET STATUS
388         this.addPolicy("instance1", "type1", "service1", "ric1");
389         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
390         testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
391
392         // Check that empty response body is OK
393         a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
394         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
395         testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
396     }
397
398     @Test
399     void testPutTypelessPolicy() throws Exception {
400         putService("service1");
401         addPolicyType("", "ric1");
402         String url = putPolicyUrl("service1", "ric1", "", "id1");
403         restClient().put(url, jsonString()).block();
404
405         String rsp = restClient().get("/policies").block();
406         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
407         assertThat(info).hasSize(1);
408         PolicyInfo policyInfo = info.get(0);
409         assertThat(policyInfo.id).isEqualTo("id1");
410         assertThat(policyInfo.type).isEmpty();
411     }
412
413     @Test
414     void testRefuseToUpdatePolicy() throws Exception {
415         // Test that only the json can be changed for a already created policy
416         // In this case service is attempted to be changed
417         this.addRic("ric1");
418         this.addRic("ricXXX");
419         this.addPolicy("instance1", "type1", "service1", "ric1");
420         this.addPolicy("instance2", "type1", "service1", "ricXXX");
421
422         // Try change ric1 -> ricXXX
423         String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
424         testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
425     }
426
427     @Test
428     void testGetPolicy() throws Exception {
429         String url = "/policy?id=id";
430         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
431         {
432             String rsp = restClient().get(url).block();
433             assertThat(rsp).isEqualTo(policy.json());
434         }
435         {
436             policies.remove(policy);
437             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
438         }
439     }
440
441     @Test
442     void testDeletePolicy() throws Exception {
443         addPolicy("id", "typeName", "service1", "ric1");
444         assertThat(policies.size()).isEqualTo(1);
445
446         String url = "/policy?id=id";
447         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
448
449         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
450         assertThat(policies.size()).isZero();
451
452         // Delete a non existing policy
453         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
454     }
455
456     @Test
457     void testGetPolicySchemas() throws Exception {
458         addPolicyType("type1", "ric1");
459         addPolicyType("type2", "ric2");
460
461         String url = "/policy_schemas";
462         String rsp = this.restClient().get(url).block();
463         assertThat(rsp).contains("type1") //
464                 .contains("{\"title\":\"type2\"}");
465
466         List<String> info = parseSchemas(rsp);
467         assertThat(info).hasSize(2);
468
469         url = "/policy_schemas?ric=ric1";
470         rsp = restClient().get(url).block();
471         assertThat(rsp).contains("type1");
472         info = parseSchemas(rsp);
473         assertThat(info).hasSize(1);
474
475         // Get schema for non existing RIC
476         url = "/policy_schemas?ric=ric1XXX";
477         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
478     }
479
480     @Test
481     void testGetPolicySchema() throws Exception {
482         addPolicyType("type1", "ric1");
483         addPolicyType("type2", "ric2");
484
485         String url = "/policy_schema?id=type1";
486         String rsp = restClient().get(url).block();
487         logger.info(rsp);
488         assertThat(rsp).contains("type1") //
489                 .contains("title");
490
491         // Get non existing schema
492         url = "/policy_schema?id=type1XX";
493         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
494     }
495
496     @Test
497     void testGetPolicyTypes() throws Exception {
498         addPolicyType("type1", "ric1");
499         addPolicyType("type2", "ric2");
500
501         String url = "/policy_types";
502         String rsp = restClient().get(url).block();
503         assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
504
505         url = "/policy_types?ric=ric1";
506         rsp = restClient().get(url).block();
507         assertThat(rsp).isEqualTo("[\"type1\"]");
508
509         // Get policy types for non existing RIC
510         url = "/policy_types?ric=ric1XXX";
511         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
512     }
513
514     @Test
515     void testGetPolicies() throws Exception {
516         addPolicy("id1", "type1", "service1");
517
518         String url = "/policies";
519         String rsp = restClient().get(url).block();
520         logger.info(rsp);
521         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
522         assertThat(info).hasSize(1);
523         PolicyInfo policyInfo = info.get(0);
524         assert (policyInfo.validate());
525         assertThat(policyInfo.id).isEqualTo("id1");
526         assertThat(policyInfo.type).isEqualTo("type1");
527         assertThat(policyInfo.service).isEqualTo("service1");
528     }
529
530     @Test
531     void testGetPoliciesFilter() throws Exception {
532         addPolicy("id1", "type1", "service1");
533         addPolicy("id2", "type1", "service2");
534         addPolicy("id3", "type2", "service1");
535
536         String url = "/policies?type=type1";
537         String rsp = restClient().get(url).block();
538         logger.info(rsp);
539         assertThat(rsp).contains("id1") //
540                 .contains("id2") //
541                 .doesNotContain("id3");
542
543         url = "/policies?type=type1&service=service2";
544         rsp = restClient().get(url).block();
545         logger.info(rsp);
546         assertThat(rsp).doesNotContain("id1") //
547                 .contains("id2") //
548                 .doesNotContain("id3");
549
550         // Test get policies for non existing type
551         url = "/policies?type=type1XXX";
552         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
553
554         // Test get policies for non existing RIC
555         url = "/policies?ric=XXX";
556         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
557     }
558
559     @Test
560     void testGetPolicyIdsFilter() throws Exception {
561         addPolicy("id1", "type1", "service1", "ric1");
562         addPolicy("id2", "type1", "service2", "ric1");
563         addPolicy("id3", "type2", "service1", "ric1");
564
565         String url = "/policy_ids?type=type1";
566         String rsp = restClient().get(url).block();
567         logger.info(rsp);
568         assertThat(rsp).contains("id1") //
569                 .contains("id2") //
570                 .doesNotContain("id3");
571
572         url = "/policy_ids?type=type1&service=service1&ric=ric1";
573         rsp = restClient().get(url).block();
574         assertThat(rsp).isEqualTo("[\"id1\"]");
575
576         // Test get policy ids for non existing type
577         url = "/policy_ids?type=type1XXX";
578         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
579
580         // Test get policy ids for non existing RIC
581         url = "/policy_ids?ric=XXX";
582         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
583     }
584
585     @Test
586     void testPutAndGetService() throws Exception {
587         // PUT
588         String serviceName = "name";
589         putService(serviceName, 0, HttpStatus.CREATED);
590         putService(serviceName, 0, HttpStatus.OK);
591
592         // GET one service
593         String url = "/services?name=name";
594         String rsp = restClient().get(url).block();
595         List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
596         assertThat(info).hasSize(1);
597         ServiceStatus status = info.iterator().next();
598         assertThat(status.keepAliveIntervalSeconds).isZero();
599         assertThat(status.serviceName).isEqualTo(serviceName);
600
601         // GET (all)
602         url = "/services";
603         rsp = restClient().get(url).block();
604         assertThat(rsp).as("Response contains service name").contains(serviceName);
605         logger.info(rsp);
606
607         // Keep alive
608         url = "/services/keepalive?name=name";
609         ResponseEntity<String> entity = restClient().putForEntity(url).block();
610         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
611
612         // DELETE service
613         assertThat(services.size()).isEqualTo(1);
614         url = "/services?name=name";
615         restClient().delete(url).block();
616         assertThat(services.size()).isZero();
617
618         // Keep alive, no registered service
619         testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
620
621         // PUT servive with bad payload
622         testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
623         testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
624         testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
625         testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
626                 HttpStatus.BAD_REQUEST);
627
628         // GET non existing service
629         testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
630     }
631
632     @Test
633     void testServiceSupervision() throws Exception {
634         putService("service1", 1, HttpStatus.CREATED);
635         addPolicyType("type1", "ric1");
636
637         String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
638         final String policyBody = jsonString();
639         restClient().put(url, policyBody).block();
640
641         assertThat(policies.size()).isEqualTo(1);
642         assertThat(services.size()).isEqualTo(1);
643
644         // Timeout after ~1 second
645         await().untilAsserted(() -> assertThat(policies.size()).isZero());
646         assertThat(services.size()).isZero();
647     }
648
649     @Test
650     void testGetPolicyStatus() throws Exception {
651         addPolicy("id", "typeName", "service1", "ric1");
652         assertThat(policies.size()).isEqualTo(1);
653
654         String url = "/policy_status?id=id";
655         String rsp = restClient().get(url).block();
656         assertThat(rsp).isEqualTo("OK");
657
658         // GET non existing policy status
659         url = "/policy_status?id=XXX";
660         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
661     }
662
663     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
664         addRic(ric);
665         Policy policy = ImmutablePolicy.builder() //
666                 .id(id) //
667                 .json(jsonString()) //
668                 .ownerServiceId(service) //
669                 .ric(rics.getRic(ric)) //
670                 .type(addPolicyType(typeName, ric)) //
671                 .lastModified(Instant.now()) //
672                 .isTransient(false) //
673                 .build();
674         policies.put(policy);
675         return policy;
676     }
677
678     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
679         return addPolicy(id, typeName, service, "ric");
680     }
681
682     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
683         return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
684     }
685
686     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
687         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
688
689         String json = gson.toJson(service);
690         return json;
691     }
692
693     private void putService(String name) {
694         putService(name, 0, null);
695     }
696
697     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
698         String url = "/service";
699         String body = createServiceJson(name, keepAliveIntervalSeconds);
700         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
701         if (expectedStatus != null) {
702             assertEquals(expectedStatus, resp.getStatusCode(), "");
703         }
704     }
705
706     private String baseUrl() {
707         return "https://localhost:" + port;
708     }
709
710     private String jsonString() {
711         return "{\"servingCellNrcgi\":\"1\"}";
712     }
713
714     private AsyncRestClient restClient(boolean useTrustValidation) {
715         WebClientConfig config = this.applicationConfig.getWebClientConfig();
716         config = ImmutableWebClientConfig.builder() //
717                 .keyStoreType(config.keyStoreType()) //
718                 .keyStorePassword(config.keyStorePassword()) //
719                 .keyStore(config.keyStore()) //
720                 .keyPassword(config.keyPassword()) //
721                 .isTrustStoreUsed(useTrustValidation) //
722                 .trustStore(config.trustStore()) //
723                 .trustStorePassword(config.trustStorePassword()) //
724                 .build();
725
726         AsyncRestClientFactory f = new AsyncRestClientFactory(config);
727         return f.createRestClient(baseUrl());
728     }
729
730     private AsyncRestClient restClient() {
731         return restClient(false);
732     }
733
734     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
735         testErrorCode(request, expStatus, "");
736     }
737
738     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
739         StepVerifier.create(request) //
740                 .expectSubscription() //
741                 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
742                 .verify();
743     }
744
745     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
746         assertTrue(throwable instanceof WebClientResponseException);
747         WebClientResponseException responseException = (WebClientResponseException) throwable;
748         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
749         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
750         return true;
751     }
752
753     private MockA1Client getA1Client(String ricName) throws ServiceException {
754         return a1ClientFactory.getOrCreateA1Client(ricName);
755     }
756
757     private PolicyType createPolicyType(String policyTypeName) {
758         return ImmutablePolicyType.builder() //
759                 .id(policyTypeName) //
760                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
761                 .build();
762     }
763
764     private PolicyType addPolicyType(String policyTypeName, String ricName) {
765         PolicyType type = createPolicyType(policyTypeName);
766         policyTypes.put(type);
767         addRic(ricName).addSupportedPolicyType(type);
768         return type;
769     }
770
771     private Ric addRic(String ricName) {
772         return addRic(ricName, null);
773     }
774
775     private Ric addRic(String ricName, String managedElement) {
776         if (rics.get(ricName) != null) {
777             return rics.get(ricName);
778         }
779         List<String> mes = new ArrayList<>();
780         if (managedElement != null) {
781             mes.add(managedElement);
782         }
783         RicConfig conf = ImmutableRicConfig.builder() //
784                 .ricId(ricName) //
785                 .baseUrl(ricName) //
786                 .managedElementIds(mes) //
787                 .controllerName("") //
788                 .build();
789         Ric ric = new Ric(conf);
790         ric.setState(Ric.RicState.AVAILABLE);
791         this.rics.put(ric);
792         return ric;
793     }
794
795     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
796         List<T> result = new ArrayList<>();
797         JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
798         for (JsonElement jsonElement : jsonArr) {
799             T json = gson.fromJson(jsonElement.toString(), clazz);
800             result.add(json);
801         }
802         return result;
803     }
804
805     private static List<String> parseSchemas(String jsonString) {
806         JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
807         List<String> result = new ArrayList<>();
808         for (JsonElement schemaObject : arrayOfSchema) {
809             result.add(schemaObject.toString());
810         }
811         return result;
812     }
813 }