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