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