Spring Aspect logging
[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 testPutAndGetService() throws Exception {
501         // PUT
502         putService("name", 0);
503
504         // GET one service
505         String url = "/services?name=name";
506         String rsp = restClient().get(url).block();
507         List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
508         assertThat(info.size()).isEqualTo(1);
509         ServiceStatus status = info.iterator().next();
510         assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
511         assertThat(status.serviceName).isEqualTo("name");
512
513         // GET (all)
514         url = "/services";
515         rsp = restClient().get(url).block();
516         assertThat(rsp.contains("name")).isTrue();
517         logger.info(rsp);
518
519         // Keep alive
520         url = "/services/keepalive?name=name";
521         ResponseEntity<String> entity = restClient().postForEntity(url, null).block();
522         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
523
524         // DELETE service
525         assertThat(services.size()).isEqualTo(1);
526         url = "/services?name=name";
527         restClient().delete(url).block();
528         assertThat(services.size()).isEqualTo(0);
529
530         // Keep alive, no registerred service
531         testErrorCode(restClient().post("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
532
533         // PUT servive with crap payload
534         testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
535         testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
536
537         // GET non existing servive
538         testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
539     }
540
541     @Test
542     public void testServiceSupervision() throws Exception {
543         putService("service1", 1);
544         addPolicyType("type1", "ric1");
545
546         String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
547         final String policyBody = jsonString();
548         restClient().put(url, policyBody).block();
549
550         assertThat(policies.size()).isEqualTo(1);
551         assertThat(services.size()).isEqualTo(1);
552
553         // Timeout after ~1 second
554         await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
555         assertThat(services.size()).isEqualTo(0);
556     }
557
558     @Test
559     public void testGetPolicyStatus() throws Exception {
560         addPolicy("id", "typeName", "service1", "ric1");
561         assertThat(policies.size()).isEqualTo(1);
562
563         String url = "/policy_status?instance=id";
564         String rsp = restClient().get(url).block();
565         assertThat(rsp.equals("OK")).isTrue();
566
567         // GET non existing policy status
568         url = "/policy_status?instance=XXX";
569         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
570     }
571
572     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
573         addRic(ric);
574         Policy p = ImmutablePolicy.builder().id(id) //
575             .json(jsonString()) //
576             .ownerServiceName(service) //
577             .ric(rics.getRic(ric)) //
578             .type(addPolicyType(typeName, ric)) //
579             .lastModified("lastModified").build();
580         policies.put(p);
581         return p;
582     }
583
584     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
585         return addPolicy(id, typeName, service, "ric");
586     }
587
588     private String createServiceJson(String name, long keepAliveIntervalSeconds) {
589         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, "callbackUrl");
590
591         String json = gson.toJson(service);
592         return json;
593     }
594
595     private void putService(String name) {
596         putService(name, 0);
597     }
598
599     private void putService(String name, long keepAliveIntervalSeconds) {
600         String url = "/service";
601         String body = createServiceJson(name, keepAliveIntervalSeconds);
602         restClient().put(url, body).block();
603     }
604
605     private String baseUrl() {
606         return "http://localhost:" + port;
607     }
608
609     private String jsonString() {
610         return "{\n  \"servingCellNrcgi\": \"1\"\n }";
611     }
612
613     private static class ConcurrencyTestRunnable implements Runnable {
614         private final RestTemplate restTemplate = new RestTemplate();
615         private final String baseUrl;
616         static AtomicInteger nextCount = new AtomicInteger(0);
617         private final int count;
618         private final RicSupervision supervision;
619
620         ConcurrencyTestRunnable(String baseUrl, RicSupervision supervision) {
621             this.baseUrl = baseUrl;
622             this.count = nextCount.incrementAndGet();
623             this.supervision = supervision;
624         }
625
626         @Override
627         public void run() {
628             for (int i = 0; i < 100; ++i) {
629                 if (i % 10 == 0) {
630                     this.supervision.checkAllRics();
631                 }
632                 String name = "policy:" + count + ":" + i;
633                 putPolicy(name);
634                 deletePolicy(name);
635             }
636         }
637
638         private void putPolicy(String name) {
639             String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
640             restTemplate.put(putUrl, createJsonHttpEntity("{}"));
641         }
642
643         private void deletePolicy(String name) {
644             String deleteUrl = baseUrl + "/policy?instance=" + name;
645             restTemplate.delete(deleteUrl);
646         }
647     }
648
649     @Test
650     public void testConcurrency() throws Exception {
651         final Instant startTime = Instant.now();
652         List<Thread> threads = new ArrayList<>();
653         addRic("ric1");
654         addPolicyType("type1", "ric1");
655
656         for (int i = 0; i < 100; ++i) {
657             Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
658             t.start();
659             threads.add(t);
660         }
661         for (Thread t : threads) {
662             t.join();
663         }
664         assertThat(policies.size()).isEqualTo(0);
665         logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
666     }
667
668     private AsyncRestClient restClient() {
669         return new AsyncRestClient(baseUrl());
670     }
671
672     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
673         testErrorCode(request, expStatus, "");
674     }
675
676     private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
677         StepVerifier.create(request) //
678             .expectSubscription() //
679             .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
680             .verify();
681     }
682
683     private boolean checkWebClientError(Throwable t, HttpStatus expStatus, String responseContains) {
684         assertTrue(t instanceof WebClientResponseException);
685         WebClientResponseException e = (WebClientResponseException) t;
686         assertThat(e.getStatusCode()).isEqualTo(expStatus);
687         assertThat(e.getResponseBodyAsString()).contains(responseContains);
688         return true;
689     }
690
691     private MockA1Client getA1Client(String ricName) throws ServiceException {
692         return a1ClientFactory.getOrCreateA1Client(ricName);
693     }
694
695     private PolicyType addPolicyType(String policyTypeName, String ricName) {
696         PolicyType type = ImmutablePolicyType.builder() //
697             .name(policyTypeName) //
698             .schema("{\"title\":\"" + policyTypeName + "\"}") //
699             .build();
700
701         policyTypes.put(type);
702         addRic(ricName).addSupportedPolicyType(type);
703         return type;
704     }
705
706     private Ric addRic(String ricName) {
707         return addRic(ricName, null);
708     }
709
710     private Ric addRic(String ricName, String managedElement) {
711         if (rics.get(ricName) != null) {
712             return rics.get(ricName);
713         }
714         List<String> mes = new ArrayList<>();
715         if (managedElement != null) {
716             mes.add(managedElement);
717         }
718         RicConfig conf = ImmutableRicConfig.builder() //
719             .name(ricName) //
720             .baseUrl(ricName) //
721             .managedElementIds(mes) //
722             .build();
723         Ric ric = new Ric(conf);
724         ric.setState(Ric.RicState.IDLE);
725         this.rics.put(ric);
726         return ric;
727     }
728
729     private static HttpEntity<String> createJsonHttpEntity(String content) {
730         HttpHeaders headers = new HttpHeaders();
731         headers.setContentType(MediaType.APPLICATION_JSON);
732         return new HttpEntity<String>(content, headers);
733     }
734
735     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
736         List<T> result = new ArrayList<>();
737         JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
738         for (JsonElement jsonElement : jsonArr) {
739             T o = gson.fromJson(jsonElement.toString(), clazz);
740             result.add(o);
741         }
742         return result;
743     }
744
745     private static List<String> parseSchemas(String jsonString) {
746         JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
747         List<String> result = new ArrayList<>();
748         for (JsonElement schemaObject : arrayOfSchema) {
749             result.add(schemaObject.toString());
750         }
751         return result;
752     }
753 }