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