87589f229d1964d27c77092f8e71334bcf40b7ed
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32
33 import java.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.exceptions.ServiceException;
51 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
52 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
62 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
63 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
64 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
65 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.boot.test.context.SpringBootTest;
70 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
71 import org.springframework.boot.test.context.TestConfiguration;
72 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
73 import org.springframework.boot.web.server.LocalServerPort;
74 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
75 import org.springframework.context.ApplicationContext;
76 import org.springframework.context.annotation.Bean;
77 import org.springframework.http.HttpStatus;
78 import org.springframework.http.MediaType;
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 class ApplicationTest {
94     private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
95
96     @Autowired
97     ApplicationContext context;
98
99     @Autowired
100     private Rics rics;
101
102     @Autowired
103     private Policies policies;
104
105     @Autowired
106     private PolicyTypes policyTypes;
107
108     @Autowired
109     MockA1ClientFactory a1ClientFactory;
110
111     @Autowired
112     RicSupervision supervision;
113
114     @Autowired
115     ApplicationConfig applicationConfig;
116
117     @Autowired
118     Services services;
119
120     private static Gson gson = new GsonBuilder() //
121             .serializeNulls() //
122             .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_id=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_id=";
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_id=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 (Near-RT 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         waitForRicState(ric1Name, RicState.SYNCHRONIZING);
256         waitForRicState(ric1Name, RicState.AVAILABLE);
257         waitForRicState("ric2", RicState.AVAILABLE);
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 testGetRic() throws Exception {
272         String ricId = "ric1";
273         String managedElementId = "kista_1";
274         addRic(ricId, managedElementId);
275
276         String url = "/ric?managed_element_id=" + managedElementId;
277         String rsp = restClient().get(url).block();
278         RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
279         assertThat(ricInfo.ricId).isEqualTo(ricId);
280
281         url = "/ric?ric_id=" + ricId;
282         rsp = restClient().get(url).block();
283         ricInfo = gson.fromJson(rsp, RicInfo.class);
284         assertThat(ricInfo.ricId).isEqualTo(ricId);
285
286         // test GET RIC for ManagedElement that does not exist
287         url = "/ric?managed_element_id=" + "junk";
288         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
289
290         url = "/ric";
291         testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
292     }
293
294     private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
295             boolean isTransient) {
296         String url;
297         if (policyTypeName.isEmpty()) {
298             url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName;
299         } else {
300             url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName
301                     + "&policytype_id=" + policyTypeName;
302         }
303         if (isTransient) {
304             url += "&transient=true";
305         }
306         return url;
307     }
308
309     private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
310         return putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, false);
311     }
312
313     @Test
314     void testPutPolicy() throws Exception {
315         String serviceName = "service1";
316         String ricId = "ric1";
317         String policyTypeName = "type1";
318         String policyInstanceId = "instance1";
319
320         putService(serviceName);
321         addPolicyType(policyTypeName, ricId);
322
323         // PUT a transient policy
324         String url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, true);
325         final String policyBody = jsonString();
326         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
327
328         restClient().put(url, policyBody).block();
329
330         Policy policy = policies.getPolicy(policyInstanceId);
331         assertThat(policy).isNotNull();
332         assertThat(policy.id()).isEqualTo(policyInstanceId);
333         assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
334         assertThat(policy.ric().id()).isEqualTo("ric1");
335         assertThat(policy.isTransient()).isTrue();
336
337         // Put a non transient policy
338         url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
339         restClient().put(url, policyBody).block();
340         policy = policies.getPolicy(policyInstanceId);
341         assertThat(policy.isTransient()).isFalse();
342
343         url = "/policies";
344         String rsp = restClient().get(url).block();
345         assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
346
347         url = "/policy?policy_id=" + policyInstanceId;
348         rsp = restClient().get(url).block();
349         assertThat(rsp).isEqualTo(policyBody);
350
351         // Test of error codes
352         url = putPolicyUrl(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
353         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
354
355         url = putPolicyUrl(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
356         addPolicyType(policyTypeName + "XX", "otherRic");
357         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
358
359         url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
360         this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
361         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
362         this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
363     }
364
365     @Test
366     /**
367      * Test that HttpStatus and body from failing REST call to A1 is passed on to
368      * the caller.
369      *
370      * @throws ServiceException
371      */
372     void testErrorFromRic() throws ServiceException {
373         putService("service1");
374         addPolicyType("type1", "ric1");
375
376         String url = putPolicyUrl("service1", "ric1", "type1", "id1");
377         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
378         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
379         String responseBody = "Refused";
380         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
381
382         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
383                 responseBodyBytes, StandardCharsets.UTF_8, null);
384         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
385
386         // PUT Policy
387         testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
388
389         // DELETE POLICY
390         this.addPolicy("instance1", "type1", "service1", "ric1");
391         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
392         testErrorCode(restClient().delete("/policy?policy_id=instance1"), httpStatus, responseBody);
393
394         // GET STATUS
395         this.addPolicy("instance1", "type1", "service1", "ric1");
396         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
397         testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus, responseBody);
398
399         // Check that empty response body is OK
400         a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
401         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
402         testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus);
403     }
404
405     @Test
406     void testPutTypelessPolicy() throws Exception {
407         putService("service1");
408         addPolicyType("", "ric1");
409         String url = putPolicyUrl("service1", "ric1", "", "id1");
410         restClient().put(url, jsonString()).block();
411
412         String rsp = restClient().get("/policies").block();
413         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
414         assertThat(info.policies).hasSize(1);
415         PolicyInfo policyInfo = info.policies.iterator().next();
416         assertThat(policyInfo.policyId).isEqualTo("id1");
417         assertThat(policyInfo.policyTypeId).isEmpty();
418     }
419
420     @Test
421     void testRefuseToUpdatePolicy() throws Exception {
422         // Test that only the json can be changed for a already created policy
423         // In this case service is attempted to be changed
424         this.addRic("ric1");
425         this.addRic("ricXXX");
426         this.addPolicy("instance1", "type1", "service1", "ric1");
427         this.addPolicy("instance2", "type1", "service1", "ricXXX");
428
429         // Try change ric1 -> ricXXX
430         String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
431         testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
432     }
433
434     @Test
435     void testGetPolicy() throws Exception {
436         String url = "/policy?policy_id=id";
437         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
438         {
439             String rsp = restClient().get(url).block();
440             assertThat(rsp).isEqualTo(policy.json());
441         }
442         {
443             policies.remove(policy);
444             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
445         }
446     }
447
448     @Test
449     void testDeletePolicy() throws Exception {
450         addPolicy("id", "typeName", "service1", "ric1");
451         assertThat(policies.size()).isEqualTo(1);
452
453         String url = "/policy?policy_id=id";
454         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
455
456         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
457         assertThat(policies.size()).isZero();
458
459         // Delete a non existing policy
460         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
461     }
462
463     @Test
464     void testGetPolicySchemas() throws Exception {
465         addPolicyType("type1", "ric1");
466         addPolicyType("type2", "ric2");
467
468         waitForRicState("ric1", RicState.AVAILABLE);
469         waitForRicState("ric2", RicState.AVAILABLE);
470
471         String url = "/policy-schemas";
472         String rsp = this.restClient().get(url).block();
473         assertThat(rsp).contains("type1") //
474                 .contains("{\"title\":\"type2\"}");
475
476         PolicySchemaList info = parseSchemas(rsp);
477         assertThat(info.schemas).hasSize(2);
478
479         url = "/policy-schemas?ric_id=ric1";
480         rsp = restClient().get(url).block();
481         assertThat(rsp).contains("type1");
482         info = parseSchemas(rsp);
483         assertThat(info.schemas).hasSize(1);
484
485         // Schema for type
486         url = "/policy-schemas?policytype_id=type1";
487         rsp = restClient().get(url).block();
488         assertThat(rsp).contains("type1") //
489                 .contains("title");
490
491         // Both type and ric specified
492         url = "/policy-schemas?ric_id=ric1&policytype_id=type1";
493         rsp = restClient().get(url).block();
494         PolicySchemaList list = gson.fromJson(rsp, PolicySchemaList.class);
495         assertThat(list.schemas).hasSize(1);
496
497         url = "/policy-schemas?ric_id=ric1&policytype_id=type2";
498         rsp = restClient().get(url).block();
499         list = gson.fromJson(rsp, PolicySchemaList.class);
500         assertThat(list.schemas).isEmpty();
501
502         // Get schema for non existing RIC
503         url = "/policy-schemas?ric_id=ric1XXX";
504         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
505
506         // Get non existing schema
507         url = "/policy-schemas?policytype_id=type1XX";
508         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
509     }
510
511     String createPolicyTypesJson(String... types) {
512         List<String> list = new ArrayList<>();
513         Collections.addAll(list, types);
514         PolicyTypeIdList ids = new PolicyTypeIdList(list);
515         return gson.toJson(ids);
516     }
517
518     @Test
519     void testGetPolicyTypes() throws Exception {
520         addPolicyType("type1", "ric1");
521         addPolicyType("type2", "ric2");
522
523         String url = "/policy-types";
524         String rsp = restClient().get(url).block();
525         String expResp = createPolicyTypesJson("type2", "type1");
526         assertThat(rsp).isEqualTo(expResp);
527
528         url = "/policy-types?ric_id=ric1";
529         rsp = restClient().get(url).block();
530         expResp = createPolicyTypesJson("type1");
531         assertThat(rsp).isEqualTo(expResp);
532
533         // Get policy types for non existing RIC
534         url = "/policy-types?ric_id=ric1XXX";
535         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
536     }
537
538     @Test
539     void testGetPolicies() throws Exception {
540         addPolicy("id1", "type1", "service1");
541
542         String url = "/policies";
543         String rsp = restClient().get(url).block();
544         logger.info(rsp);
545         PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
546         assertThat(info.policies).hasSize(1);
547         PolicyInfo policyInfo = info.policies.iterator().next();
548         assert (policyInfo.validate());
549         assertThat(policyInfo.policyId).isEqualTo("id1");
550         assertThat(policyInfo.policyTypeId).isEqualTo("type1");
551         assertThat(policyInfo.serviceId).isEqualTo("service1");
552     }
553
554     @Test
555     void testGetPoliciesFilter() throws Exception {
556         addPolicy("id1", "type1", "service1");
557         addPolicy("id2", "type1", "service2");
558         addPolicy("id3", "type2", "service1");
559
560         String url = "/policies?policytype_id=type1";
561         String rsp = restClient().get(url).block();
562         logger.info(rsp);
563         assertThat(rsp).contains("id1") //
564                 .contains("id2") //
565                 .doesNotContain("id3");
566
567         url = "/policies?policytype_id=type1&service_id=service2";
568         rsp = restClient().get(url).block();
569         logger.info(rsp);
570         assertThat(rsp).doesNotContain("id1") //
571                 .contains("id2") //
572                 .doesNotContain("id3");
573
574         // Test get policies for non existing type
575         url = "/policies?policytype_id=type1XXX";
576         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
577
578         // Test get policies for non existing RIC
579         url = "/policies?ric_id=XXX";
580         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
581     }
582
583     @Test
584     void testGetPolicyIdsFilter() throws Exception {
585         addPolicy("id1", "type1", "service1", "ric1");
586         addPolicy("id2", "type1", "service2", "ric1");
587         addPolicy("id3", "type2", "service1", "ric1");
588
589         String url = "/policy-ids?policytype_id=type1";
590         String rsp = restClient().get(url).block();
591         logger.info(rsp);
592         assertThat(rsp).contains("id1") //
593                 .contains("id2") //
594                 .doesNotContain("id3");
595
596         url = "/policy-ids?policytype_id=type1&service_id=service1&ric=ric1";
597         rsp = restClient().get(url).block();
598         PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
599         assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
600
601         // Test get policy ids for non existing type
602         url = "/policy-ids?policytype_id=type1XXX";
603         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
604
605         // Test get policy ids for non existing RIC
606         url = "/policy-ids?ric_id=XXX";
607         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
608     }
609
610     @Test
611     void testPutAndGetService() throws Exception {
612         // PUT
613         String serviceName = "name";
614         putService(serviceName, 0, HttpStatus.CREATED);
615         putService(serviceName, 0, HttpStatus.OK);
616
617         // GET one service
618         String url = "/services?service_id=name";
619         String rsp = restClient().get(url).block();
620         ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
621         assertThat(info.statusList).hasSize(1);
622         ServiceStatus status = info.statusList.iterator().next();
623         assertThat(status.keepAliveIntervalSeconds).isZero();
624         assertThat(status.serviceId).isEqualTo(serviceName);
625
626         // GET (all)
627         url = "/services";
628         rsp = restClient().get(url).block();
629         assertThat(rsp).as("Response contains service name").contains(serviceName);
630         logger.info(rsp);
631
632         // Keep alive
633         url = "/services/keepalive?service_id=name";
634         ResponseEntity<?> entity = restClient().putForEntity(url).block();
635         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
636
637         // DELETE service
638         assertThat(services.size()).isEqualTo(1);
639         url = "/services?service_id=name";
640         restClient().delete(url).block();
641         assertThat(services.size()).isZero();
642
643         // Keep alive, no registered service
644         testErrorCode(restClient().put("/services/keepalive?service_id=name", ""), HttpStatus.NOT_FOUND);
645
646         // PUT service with bad payload
647         testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
648         testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
649         testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
650                 false);
651         testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
652                 HttpStatus.BAD_REQUEST, false);
653
654         // GET non existing service
655         testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
656     }
657
658     @Test
659     void testServiceSupervision() throws Exception {
660         putService("service1", 1, HttpStatus.CREATED);
661         addPolicyType("type1", "ric1");
662
663         String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
664         final String policyBody = jsonString();
665         restClient().put(url, policyBody).block();
666
667         assertThat(policies.size()).isEqualTo(1);
668         assertThat(services.size()).isEqualTo(1);
669
670         // Timeout after ~1 second
671         await().untilAsserted(() -> assertThat(policies.size()).isZero());
672         assertThat(services.size()).isZero();
673     }
674
675     @Test
676     void testGetPolicyStatus() throws Exception {
677         addPolicy("id", "typeName", "service1", "ric1");
678         assertThat(policies.size()).isEqualTo(1);
679
680         String url = "/policy-status?policy_id=id";
681         String rsp = restClient().get(url).block();
682         assertThat(rsp).isEqualTo("OK");
683
684         // GET non existing policy status
685         url = "/policy-status?policy_id=XXX";
686         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
687     }
688
689     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
690         addRic(ric);
691         Policy policy = ImmutablePolicy.builder() //
692                 .id(id) //
693                 .json(jsonString()) //
694                 .ownerServiceId(service) //
695                 .ric(rics.getRic(ric)) //
696                 .type(addPolicyType(typeName, ric)) //
697                 .lastModified(Instant.now()) //
698                 .isTransient(false) //
699                 .build();
700         policies.put(policy);
701         return policy;
702     }
703
704     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
705         return addPolicy(id, typeName, service, "ric");
706     }
707
708     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
709         return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
710     }
711
712     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
713         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
714
715         String json = gson.toJson(service);
716         return json;
717     }
718
719     private void putService(String name) {
720         putService(name, 0, null);
721     }
722
723     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
724         String url = "/services";
725         String body = createServiceJson(name, keepAliveIntervalSeconds);
726         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
727         if (expectedStatus != null) {
728             assertEquals(expectedStatus, resp.getStatusCode(), "");
729         }
730     }
731
732     private String baseUrl() {
733         return "https://localhost:" + port + Consts.V2_API_ROOT;
734     }
735
736     private String jsonString() {
737         return "{\"servingCellNrcgi\":\"1\"}";
738     }
739
740     @Test
741     void testConcurrency() throws Exception {
742         final Instant startTime = Instant.now();
743         List<Thread> threads = new ArrayList<>();
744         List<ConcurrencyTestRunnable> tests = new ArrayList<>();
745         a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
746         addRic("ric");
747         addPolicyType("type1", "ric");
748         addPolicyType("type2", "ric");
749
750         for (int i = 0; i < 10; ++i) {
751             AsyncRestClient restClient = restClient();
752             ConcurrencyTestRunnable test =
753                     new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
754             Thread thread = new Thread(test, "TestThread_" + i);
755             thread.start();
756             threads.add(thread);
757             tests.add(test);
758         }
759         for (Thread t : threads) {
760             t.join();
761         }
762         for (ConcurrencyTestRunnable test : tests) {
763             assertThat(test.isFailed()).isFalse();
764         }
765         assertThat(policies.size()).isZero();
766         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
767     }
768
769     private AsyncRestClient restClient(boolean useTrustValidation) {
770         WebClientConfig config = this.applicationConfig.getWebClientConfig();
771         config = ImmutableWebClientConfig.builder() //
772                 .keyStoreType(config.keyStoreType()) //
773                 .keyStorePassword(config.keyStorePassword()) //
774                 .keyStore(config.keyStore()) //
775                 .keyPassword(config.keyPassword()) //
776                 .isTrustStoreUsed(useTrustValidation) //
777                 .trustStore(config.trustStore()) //
778                 .trustStorePassword(config.trustStorePassword()) //
779                 .build();
780
781         return new AsyncRestClient(baseUrl(), config);
782     }
783
784     private AsyncRestClient restClient() {
785         return restClient(false);
786     }
787
788     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
789         testErrorCode(request, expStatus, "", true);
790     }
791
792     private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
793         testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
794     }
795
796     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
797         testErrorCode(request, expStatus, responseContains, true);
798     }
799
800     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
801             boolean expectApplicationProblemJsonMediaType) {
802         StepVerifier.create(request) //
803                 .expectSubscription() //
804                 .expectErrorMatches(
805                         t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
806                 .verify();
807     }
808
809     private void waitForRicState(String ricId, RicState state) throws ServiceException {
810         Ric ric = rics.getRic(ricId);
811         await().untilAsserted(() -> state.equals(ric.getState()));
812     }
813
814     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
815             boolean expectApplicationProblemJsonMediaType) {
816         assertTrue(throwable instanceof WebClientResponseException);
817         WebClientResponseException responseException = (WebClientResponseException) throwable;
818         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
819         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
820         if (expectApplicationProblemJsonMediaType) {
821             assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
822         }
823         return true;
824     }
825
826     private MockA1Client getA1Client(String ricId) throws ServiceException {
827         return a1ClientFactory.getOrCreateA1Client(ricId);
828     }
829
830     private PolicyType createPolicyType(String policyTypeName) {
831         return ImmutablePolicyType.builder() //
832                 .id(policyTypeName) //
833                 .schema("{\"title\":\"" + policyTypeName + "\"}") //
834                 .build();
835     }
836
837     private PolicyType addPolicyType(String policyTypeName, String ricId) {
838         PolicyType type = createPolicyType(policyTypeName);
839         policyTypes.put(type);
840         addRic(ricId).addSupportedPolicyType(type);
841         return type;
842     }
843
844     private Ric addRic(String ricId) {
845         return addRic(ricId, null);
846     }
847
848     private Ric addRic(String ricId, String managedElement) {
849         if (rics.get(ricId) != null) {
850             return rics.get(ricId);
851         }
852         List<String> mes = new ArrayList<>();
853         if (managedElement != null) {
854             mes.add(managedElement);
855         }
856         RicConfig conf = ImmutableRicConfig.builder() //
857                 .ricId(ricId) //
858                 .baseUrl(ricId) //
859                 .managedElementIds(mes) //
860                 .controllerName("") //
861                 .build();
862         Ric ric = new Ric(conf);
863         ric.setState(Ric.RicState.AVAILABLE);
864         this.rics.put(ric);
865         return ric;
866     }
867
868     private static PolicySchemaList parseSchemas(String jsonString) {
869         return gson.fromJson(jsonString, PolicySchemaList.class);
870     }
871 }