Merge "Make assertions of log messages better"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / ApplicationTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.awaitility.Awaitility.await;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
29
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonParser;
35
36 import java.nio.charset.StandardCharsets;
37 import java.time.Duration;
38 import java.time.Instant;
39 import java.util.ArrayList;
40 import java.util.List;
41
42 import org.junit.jupiter.api.AfterEach;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.oransc.policyagent.clients.AsyncRestClient;
47 import org.oransc.policyagent.configuration.ApplicationConfig;
48 import org.oransc.policyagent.configuration.ImmutableRicConfig;
49 import org.oransc.policyagent.configuration.ImmutableWebClientConfig;
50 import org.oransc.policyagent.configuration.RicConfig;
51 import org.oransc.policyagent.configuration.WebClientConfig;
52 import org.oransc.policyagent.controllers.PolicyInfo;
53 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
54 import org.oransc.policyagent.controllers.ServiceStatus;
55 import org.oransc.policyagent.exceptions.ServiceException;
56 import org.oransc.policyagent.repository.ImmutablePolicy;
57 import org.oransc.policyagent.repository.ImmutablePolicyType;
58 import org.oransc.policyagent.repository.Lock.LockType;
59 import org.oransc.policyagent.repository.Policies;
60 import org.oransc.policyagent.repository.Policy;
61 import org.oransc.policyagent.repository.PolicyType;
62 import org.oransc.policyagent.repository.PolicyTypes;
63 import org.oransc.policyagent.repository.Ric;
64 import org.oransc.policyagent.repository.Ric.RicState;
65 import org.oransc.policyagent.repository.Rics;
66 import org.oransc.policyagent.repository.Services;
67 import org.oransc.policyagent.tasks.RicSupervision;
68 import org.oransc.policyagent.tasks.ServiceSupervision;
69 import org.oransc.policyagent.utils.MockA1Client;
70 import org.oransc.policyagent.utils.MockA1ClientFactory;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.boot.test.context.SpringBootTest;
75 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
76 import org.springframework.boot.test.context.TestConfiguration;
77 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
78 import org.springframework.boot.web.server.LocalServerPort;
79 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
80 import org.springframework.context.ApplicationContext;
81 import org.springframework.context.annotation.Bean;
82 import org.springframework.http.HttpStatus;
83 import org.springframework.http.ResponseEntity;
84 import org.springframework.test.context.junit.jupiter.SpringExtension;
85 import org.springframework.web.reactive.function.client.WebClientResponseException;
86
87 import reactor.core.publisher.Mono;
88 import reactor.test.StepVerifier;
89 import reactor.util.annotation.Nullable;
90
91 @ExtendWith(SpringExtension.class)
92 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
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()).isEqualTo(0);
200             assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
201         }
202     }
203
204     @Test
205     void testGetRics() throws Exception {
206         addRic("ric1");
207         this.addPolicyType("type1", "ric1");
208         String url = "/rics?policyType=type1";
209         String rsp = restClient().get(url).block();
210         assertThat(rsp).contains("ric1");
211
212         // nameless type for ORAN A1 1.1
213         addRic("ric2");
214         this.addPolicyType("", "ric2");
215         url = "/rics?policyType=";
216
217         // This tests also validation of trusted certs restClient(true)
218         rsp = restClient(true).get(url).block();
219         assertThat(rsp).contains("ric2");
220         assertThat(rsp).doesNotContain("ric1");
221         assertThat(rsp).contains("AVAILABLE");
222
223         // All RICs
224         rsp = restClient().get("/rics").block();
225         assertThat(rsp).contains("ric2");
226         assertThat(rsp).contains("ric1");
227
228         // Non existing policy type
229         url = "/rics?policyType=XXXX";
230         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
231     }
232
233     @Test
234     void testSynchronization() throws Exception {
235         // Two polictypes will be put in the NearRT RICs
236         PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
237         nearRtRicPolicyTypes.put(createPolicyType("typeName"));
238         nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
239         this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
240
241         // One type and one instance added to the agent storage
242         final String ric1Name = "ric1";
243         Ric ric1 = addRic(ric1Name);
244         Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
245         Ric ric2 = addRic("ric2");
246
247         getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
248         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
249
250         String policyId = "policyId";
251         Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
252         supervision.checkAllRics(); // The created policy should be put in the RIC
253
254         // Wait until synch is completed
255         await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
256         await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
257         await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
258
259         Policies ricPolicies = getA1Client(ric1Name).getPolicies();
260         assertThat(ricPolicies.size()).isEqualTo(1);
261         Policy ricPolicy = ricPolicies.get(policyId);
262         assertThat(ricPolicy.json()).isEqualTo(policy.json());
263
264         // Both types should be in the agent storage after the synch
265         assertThat(ric1.getSupportedPolicyTypes().size()).isEqualTo(2);
266         assertThat(ric2.getSupportedPolicyTypes().size()).isEqualTo(2);
267     }
268
269     @Test
270     void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
271         String ricName = "ric1";
272         String managedElementId = "kista_1";
273         addRic(ricName, managedElementId);
274
275         String url = "/ric?managedElementId=" + managedElementId;
276         String rsp = restClient().get(url).block();
277         assertThat(rsp).isEqualTo(ricName);
278
279         // test GET RIC for ManagedElement that does not exist
280         url = "/ric?managedElementId=" + "junk";
281         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
282     }
283
284     private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
285         boolean isTransient) {
286         String url;
287         if (policyTypeName.isEmpty()) {
288             url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
289         } else {
290             url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
291                 + policyTypeName;
292         }
293         if (isTransient) {
294             url += "&transient=true";
295         }
296         return url;
297     }
298
299     private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
300         return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
301     }
302
303     @Test
304     void testPutPolicy() throws Exception {
305         String serviceName = "service1";
306         String ricName = "ric1";
307         String policyTypeName = "type1";
308         String policyInstanceId = "instance1";
309
310         putService(serviceName);
311         addPolicyType(policyTypeName, ricName);
312
313         // PUT a transient policy
314         String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
315         final String policyBody = jsonString();
316         this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
317
318         restClient().put(url, policyBody).block();
319
320         Policy policy = policies.getPolicy(policyInstanceId);
321         assertThat(policy).isNotNull();
322         assertThat(policy.id()).isEqualTo(policyInstanceId);
323         assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
324         assertThat(policy.ric().name()).isEqualTo("ric1");
325         assertThat(policy.isTransient()).isEqualTo(true);
326
327         // Put a non transient policy
328         url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
329         restClient().put(url, policyBody).block();
330         policy = policies.getPolicy(policyInstanceId);
331         assertThat(policy.isTransient()).isEqualTo(false);
332
333         url = "/policies";
334         String rsp = restClient().get(url).block();
335         assertThat(rsp.contains(policyInstanceId)).as("Response contains policy instance ID.").isTrue();
336
337         url = "/policy?id=" + policyInstanceId;
338         rsp = restClient().get(url).block();
339         assertThat(rsp).isEqualTo(policyBody);
340
341         // Test of error codes
342         url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
343         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
344
345         url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
346         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
347
348         url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
349         this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
350         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
351         this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
352     }
353
354     @Test
355     /**
356      * Test that HttpStatus and body from failing REST call to A1 is passed on to
357      * the caller.
358      *
359      * @throws ServiceException
360      */
361     void testErrorFromRIC() throws ServiceException {
362         putService("service1");
363         addPolicyType("type1", "ric1");
364
365         String url = putPolicyUrl("service1", "ric1", "type1", "id1");
366         MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
367         HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
368         String responseBody = "Refused";
369         byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
370
371         WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
372             responseBodyBytes, StandardCharsets.UTF_8, null);
373         doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
374
375         // PUT Policy
376         testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
377
378         // DELETE POLICY
379         this.addPolicy("instance1", "type1", "service1", "ric1");
380         doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
381         testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
382
383         // GET STATUS
384         this.addPolicy("instance1", "type1", "service1", "ric1");
385         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
386         testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
387
388         // Check that empty response body is OK
389         a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
390         doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
391         testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
392     }
393
394     @Test
395     void testPutTypelessPolicy() throws Exception {
396         putService("service1");
397         addPolicyType("", "ric1");
398         String url = putPolicyUrl("service1", "ric1", "", "id1");
399         restClient().put(url, jsonString()).block();
400
401         String rsp = restClient().get("/policies").block();
402         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
403         assertThat(info.size()).isEqualTo(1);
404         PolicyInfo policyInfo = info.get(0);
405         assertThat(policyInfo.id).isEqualTo("id1");
406         assertThat(policyInfo.type).isEqualTo("");
407     }
408
409     @Test
410     void testRefuseToUpdatePolicy() throws Exception {
411         // Test that only the json can be changed for a already created policy
412         // In this case service is attempted to be changed
413         this.addRic("ric1");
414         this.addRic("ricXXX");
415         this.addPolicy("instance1", "type1", "service1", "ric1");
416         this.addPolicy("instance2", "type1", "service1", "ricXXX");
417
418         // Try change ric1 -> ricXXX
419         String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
420         testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
421     }
422
423     @Test
424     void testGetPolicy() throws Exception {
425         String url = "/policy?id=id";
426         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
427         {
428             String rsp = restClient().get(url).block();
429             assertThat(rsp).isEqualTo(policy.json());
430         }
431         {
432             policies.remove(policy);
433             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
434         }
435     }
436
437     @Test
438     void testDeletePolicy() throws Exception {
439         addPolicy("id", "typeName", "service1", "ric1");
440         assertThat(policies.size()).isEqualTo(1);
441
442         String url = "/policy?id=id";
443         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
444
445         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
446         assertThat(policies.size()).isEqualTo(0);
447
448         // Delete a non existing policy
449         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
450     }
451
452     @Test
453     void testGetPolicySchemas() throws Exception {
454         addPolicyType("type1", "ric1");
455         addPolicyType("type2", "ric2");
456
457         String url = "/policy_schemas";
458         String rsp = this.restClient().get(url).block();
459         assertThat(rsp).contains("type1");
460         assertThat(rsp).contains("[{\"title\":\"type2\"}");
461
462         List<String> info = parseSchemas(rsp);
463         assertThat(info.size()).isEqualTo(2);
464
465         url = "/policy_schemas?ric=ric1";
466         rsp = restClient().get(url).block();
467         assertThat(rsp).contains("type1");
468         info = parseSchemas(rsp);
469         assertThat(info.size()).isEqualTo(1);
470
471         // Get schema for non existing RIC
472         url = "/policy_schemas?ric=ric1XXX";
473         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
474     }
475
476     @Test
477     void testGetPolicySchema() throws Exception {
478         addPolicyType("type1", "ric1");
479         addPolicyType("type2", "ric2");
480
481         String url = "/policy_schema?id=type1";
482         String rsp = restClient().get(url).block();
483         logger.info(rsp);
484         assertThat(rsp).contains("type1");
485         assertThat(rsp).contains("title");
486
487         // Get non existing schema
488         url = "/policy_schema?id=type1XX";
489         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
490     }
491
492     @Test
493     void testGetPolicyTypes() throws Exception {
494         addPolicyType("type1", "ric1");
495         addPolicyType("type2", "ric2");
496
497         String url = "/policy_types";
498         String rsp = restClient().get(url).block();
499         assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
500
501         url = "/policy_types?ric=ric1";
502         rsp = restClient().get(url).block();
503         assertThat(rsp).isEqualTo("[\"type1\"]");
504
505         // Get policy types for non existing RIC
506         url = "/policy_types?ric=ric1XXX";
507         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
508     }
509
510     @Test
511     void testGetPolicies() throws Exception {
512         addPolicy("id1", "type1", "service1");
513
514         String url = "/policies";
515         String rsp = restClient().get(url).block();
516         logger.info(rsp);
517         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
518         assertThat(info).size().isEqualTo(1);
519         PolicyInfo policyInfo = info.get(0);
520         assert (policyInfo.validate());
521         assertThat(policyInfo.id).isEqualTo("id1");
522         assertThat(policyInfo.type).isEqualTo("type1");
523         assertThat(policyInfo.service).isEqualTo("service1");
524     }
525
526     @Test
527     void testGetPoliciesFilter() throws Exception {
528         addPolicy("id1", "type1", "service1");
529         addPolicy("id2", "type1", "service2");
530         addPolicy("id3", "type2", "service1");
531
532         String url = "/policies?type=type1";
533         String rsp = restClient().get(url).block();
534         logger.info(rsp);
535         assertThat(rsp).contains("id1");
536         assertThat(rsp).contains("id2");
537         assertThat(rsp.contains("id3")).isFalse();
538
539         url = "/policies?type=type1&service=service2";
540         rsp = restClient().get(url).block();
541         logger.info(rsp);
542         assertThat(rsp.contains("id1")).isFalse();
543         assertThat(rsp).contains("id2");
544         assertThat(rsp.contains("id3")).isFalse();
545
546         // Test get policies for non existing type
547         url = "/policies?type=type1XXX";
548         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
549
550         // Test get policies for non existing RIC
551         url = "/policies?ric=XXX";
552         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
553     }
554
555     @Test
556     void testGetPolicyIdsFilter() throws Exception {
557         addPolicy("id1", "type1", "service1", "ric1");
558         addPolicy("id2", "type1", "service2", "ric1");
559         addPolicy("id3", "type2", "service1", "ric1");
560
561         String url = "/policy_ids?type=type1";
562         String rsp = restClient().get(url).block();
563         logger.info(rsp);
564         assertThat(rsp).contains("id1");
565         assertThat(rsp).contains("id2");
566         assertThat(rsp.contains("id3")).isFalse();
567
568         url = "/policy_ids?type=type1&service=service1&ric=ric1";
569         rsp = restClient().get(url).block();
570         assertThat(rsp).isEqualTo("[\"id1\"]");
571
572         // Test get policy ids for non existing type
573         url = "/policy_ids?type=type1XXX";
574         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
575
576         // Test get policy ids for non existing RIC
577         url = "/policy_ids?ric=XXX";
578         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
579     }
580
581     @Test
582     void testPutAndGetService() throws Exception {
583         // PUT
584         String serviceName = "name";
585         putService(serviceName, 0, HttpStatus.CREATED);
586         putService(serviceName, 0, HttpStatus.OK);
587
588         // GET one service
589         String url = "/services?name=name";
590         String rsp = restClient().get(url).block();
591         List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
592         assertThat(info.size()).isEqualTo(1);
593         ServiceStatus status = info.iterator().next();
594         assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
595         assertThat(status.serviceName).isEqualTo(serviceName);
596
597         // GET (all)
598         url = "/services";
599         rsp = restClient().get(url).block();
600         assertThat(rsp.contains(serviceName)).as("Response contains service name").isTrue();
601         logger.info(rsp);
602
603         // Keep alive
604         url = "/services/keepalive?name=name";
605         ResponseEntity<String> entity = restClient().putForEntity(url).block();
606         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
607
608         // DELETE service
609         assertThat(services.size()).isEqualTo(1);
610         url = "/services?name=name";
611         restClient().delete(url).block();
612         assertThat(services.size()).isEqualTo(0);
613
614         // Keep alive, no registered service
615         testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
616
617         // PUT servive with bad payload
618         testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
619         testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
620         testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
621         testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
622             HttpStatus.BAD_REQUEST);
623
624         // GET non existing service
625         testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
626     }
627
628     @Test
629     void testServiceSupervision() throws Exception {
630         putService("service1", 1, HttpStatus.CREATED);
631         addPolicyType("type1", "ric1");
632
633         String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
634         final String policyBody = jsonString();
635         restClient().put(url, policyBody).block();
636
637         assertThat(policies.size()).isEqualTo(1);
638         assertThat(services.size()).isEqualTo(1);
639
640         // Timeout after ~1 second
641         await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
642         assertThat(services.size()).isEqualTo(0);
643     }
644
645     @Test
646     void testGetPolicyStatus() throws Exception {
647         addPolicy("id", "typeName", "service1", "ric1");
648         assertThat(policies.size()).isEqualTo(1);
649
650         String url = "/policy_status?id=id";
651         String rsp = restClient().get(url).block();
652         assertThat(rsp).isEqualTo("OK");
653
654         // GET non existing policy status
655         url = "/policy_status?id=XXX";
656         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
657     }
658
659     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
660         addRic(ric);
661         Policy policy = ImmutablePolicy.builder() //
662             .id(id) //
663             .json(jsonString()) //
664             .ownerServiceName(service) //
665             .ric(rics.getRic(ric)) //
666             .type(addPolicyType(typeName, ric)) //
667             .lastModified("lastModified") //
668             .isTransient(false) //
669             .build();
670         policies.put(policy);
671         return policy;
672     }
673
674     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
675         return addPolicy(id, typeName, service, "ric");
676     }
677
678     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
679         return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
680     }
681
682     private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
683         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
684
685         String json = gson.toJson(service);
686         return json;
687     }
688
689     private void putService(String name) {
690         putService(name, 0, null);
691     }
692
693     private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
694         String url = "/service";
695         String body = createServiceJson(name, keepAliveIntervalSeconds);
696         ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
697         if (expectedStatus != null) {
698             assertEquals(expectedStatus, resp.getStatusCode(), "");
699         }
700     }
701
702     private String baseUrl() {
703         return "https://localhost:" + port;
704     }
705
706     private String jsonString() {
707         return "{\"servingCellNrcgi\":\"1\"}";
708     }
709
710     @Test
711     void testConcurrency() throws Exception {
712         final Instant startTime = Instant.now();
713         List<Thread> threads = new ArrayList<>();
714         a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
715         addRic("ric");
716         addPolicyType("type1", "ric");
717         addPolicyType("type2", "ric");
718
719         for (int i = 0; i < 10; ++i) {
720             Thread thread =
721                 new Thread(new ConcurrencyTestRunnable(baseUrl(), supervision, a1ClientFactory, rics, policyTypes),
722                     "TestThread_" + i);
723             thread.start();
724             threads.add(thread);
725         }
726         for (Thread t : threads) {
727             t.join();
728         }
729         assertThat(policies.size()).isEqualTo(0);
730         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
731     }
732
733     private AsyncRestClient restClient(boolean useTrustValidation) {
734         WebClientConfig config = this.applicationConfig.getWebClientConfig();
735         config = ImmutableWebClientConfig.builder() //
736             .isTrustStoreUsed(useTrustValidation) //
737             .trustStore(config.trustStore()) //
738             .trustStorePassword(config.trustStorePassword()) //
739             .build();
740
741         return new AsyncRestClient(baseUrl(), config);
742     }
743
744     private AsyncRestClient restClient() {
745         return restClient(false);
746     }
747
748     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
749         testErrorCode(request, expStatus, "");
750     }
751
752     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
753         StepVerifier.create(request) //
754             .expectSubscription() //
755             .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
756             .verify();
757     }
758
759     private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
760         assertTrue(throwable instanceof WebClientResponseException);
761         WebClientResponseException responseException = (WebClientResponseException) throwable;
762         assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
763         assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
764         return true;
765     }
766
767     private MockA1Client getA1Client(String ricName) throws ServiceException {
768         return a1ClientFactory.getOrCreateA1Client(ricName);
769     }
770
771     private PolicyType createPolicyType(String policyTypeName) {
772         return ImmutablePolicyType.builder() //
773             .name(policyTypeName) //
774             .schema("{\"title\":\"" + policyTypeName + "\"}") //
775             .build();
776     }
777
778     private PolicyType addPolicyType(String policyTypeName, String ricName) {
779         PolicyType type = createPolicyType(policyTypeName);
780         policyTypes.put(type);
781         addRic(ricName).addSupportedPolicyType(type);
782         return type;
783     }
784
785     private Ric addRic(String ricName) {
786         return addRic(ricName, null);
787     }
788
789     private Ric addRic(String ricName, String managedElement) {
790         if (rics.get(ricName) != null) {
791             return rics.get(ricName);
792         }
793         List<String> mes = new ArrayList<>();
794         if (managedElement != null) {
795             mes.add(managedElement);
796         }
797         RicConfig conf = ImmutableRicConfig.builder() //
798             .name(ricName) //
799             .baseUrl(ricName) //
800             .managedElementIds(mes) //
801             .controllerName("") //
802             .build();
803         Ric ric = new Ric(conf);
804         ric.setState(Ric.RicState.AVAILABLE);
805         this.rics.put(ric);
806         return ric;
807     }
808
809     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
810         List<T> result = new ArrayList<>();
811         JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
812         for (JsonElement jsonElement : jsonArr) {
813             T json = gson.fromJson(jsonElement.toString(), clazz);
814             result.add(json);
815         }
816         return result;
817     }
818
819     private static List<String> parseSchemas(String jsonString) {
820         JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
821         List<String> result = new ArrayList<>();
822         for (JsonElement schemaObject : arrayOfSchema) {
823             result.add(schemaObject.toString());
824         }
825         return result;
826     }
827 }