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