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