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