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