REST error codes
[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
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonParser;
32
33 import java.time.Duration;
34 import java.time.Instant;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.atomic.AtomicInteger;
38
39 import org.junit.jupiter.api.AfterEach;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.oransc.policyagent.clients.AsyncRestClient;
44 import org.oransc.policyagent.configuration.ApplicationConfig;
45 import org.oransc.policyagent.configuration.ImmutableRicConfig;
46 import org.oransc.policyagent.configuration.RicConfig;
47 import org.oransc.policyagent.controllers.PolicyInfo;
48 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
49 import org.oransc.policyagent.controllers.ServiceStatus;
50 import org.oransc.policyagent.exceptions.ServiceException;
51 import org.oransc.policyagent.repository.ImmutablePolicy;
52 import org.oransc.policyagent.repository.ImmutablePolicyType;
53 import org.oransc.policyagent.repository.Lock.LockType;
54 import org.oransc.policyagent.repository.Policies;
55 import org.oransc.policyagent.repository.Policy;
56 import org.oransc.policyagent.repository.PolicyType;
57 import org.oransc.policyagent.repository.PolicyTypes;
58 import org.oransc.policyagent.repository.Ric;
59 import org.oransc.policyagent.repository.Ric.RicState;
60 import org.oransc.policyagent.repository.Rics;
61 import org.oransc.policyagent.repository.Services;
62 import org.oransc.policyagent.tasks.RepositorySupervision;
63 import org.oransc.policyagent.utils.MockA1Client;
64 import org.oransc.policyagent.utils.MockA1ClientFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
68 import org.springframework.boot.test.context.TestConfiguration;
69 import org.springframework.boot.web.server.LocalServerPort;
70 import org.springframework.context.ApplicationContext;
71 import org.springframework.context.annotation.Bean;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpHeaders;
74 import org.springframework.http.HttpStatus;
75 import org.springframework.http.MediaType;
76 import org.springframework.http.ResponseEntity;
77 import org.springframework.test.context.junit.jupiter.SpringExtension;
78 import org.springframework.web.client.RestTemplate;
79 import org.springframework.web.reactive.function.client.WebClientResponseException;
80
81 import reactor.core.publisher.Mono;
82 import reactor.test.StepVerifier;
83
84 @ExtendWith(SpringExtension.class)
85 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
86 public class ApplicationTest {
87     @Autowired
88     ApplicationContext context;
89
90     @Autowired
91     private Rics rics;
92
93     @Autowired
94     private Policies policies;
95
96     @Autowired
97     private PolicyTypes policyTypes;
98
99     @Autowired
100     MockA1ClientFactory a1ClientFactory;
101
102     @Autowired
103     RepositorySupervision supervision;
104
105     @Autowired
106     Services services;
107
108     private static Gson gson = new GsonBuilder() //
109         .serializeNulls() //
110         .create(); //
111
112     public static class MockApplicationConfig extends ApplicationConfig {
113         @Override
114         public String getLocalConfigurationFilePath() {
115             return ""; // No config file loaded for the test
116         }
117     }
118
119     /**
120      * Overrides the BeanFactory.
121      */
122     @TestConfiguration
123     static class TestBeanFactory {
124         private final PolicyTypes policyTypes = new PolicyTypes();
125
126         @Bean
127         public ApplicationConfig getApplicationConfig() {
128             return new MockApplicationConfig();
129         }
130
131         @Bean
132         MockA1ClientFactory getA1ClientFactory() {
133             return new MockA1ClientFactory(this.policyTypes);
134         }
135
136         @Bean
137         public PolicyTypes getPolicyTypes() {
138             return this.policyTypes;
139         }
140     }
141
142     @LocalServerPort
143     private int port;
144
145     @BeforeEach
146     public void reset() {
147         rics.clear();
148         policies.clear();
149         policyTypes.clear();
150         services.clear();
151     }
152
153     @AfterEach
154     public void verifyNoRicLocks() {
155         for (Ric ric : this.rics.getRics()) {
156             ric.getLock().lockBlocking(LockType.EXCLUSIVE);
157             ric.getLock().unlockBlocking();
158             assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
159             assertThat(ric.getState()).isEqualTo(Ric.RicState.IDLE);
160         }
161     }
162
163     @Test
164     public void testGetRics() throws Exception {
165         addRic("kista_1");
166         this.addPolicyType("type1", "kista_1");
167         String url = "/rics?policyType=type1";
168         String rsp = restClient().get(url).block();
169         assertThat(rsp).contains("kista_1");
170
171         // Non existing policy type
172         url = "/rics?policyType=XXXX";
173         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
174     }
175
176     @Test
177     public void testRecovery() throws Exception {
178         addRic("ric").setState(Ric.RicState.UNDEFINED);
179         String ricName = "ric";
180         Policy policy2 = addPolicy("policyId2", "typeName", "service", ricName);
181
182         getA1Client(ricName).putPolicy(policy2); // put it in the RIC
183         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
184
185         String policyId = "policyId";
186         Policy policy = addPolicy(policyId, "typeName", "service", ricName); // This should be created in the RIC
187         supervision.checkAllRics(); // The created policy should be put in the RIC
188         await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ricName).getState()));
189         await().untilAsserted(() -> RicState.IDLE.equals(rics.getRic(ricName).getState()));
190
191         Policies ricPolicies = getA1Client(ricName).getPolicies();
192         assertThat(ricPolicies.size()).isEqualTo(1);
193         Policy ricPolicy = ricPolicies.get(policyId);
194         assertThat(ricPolicy.json()).isEqualTo(policy.json());
195     }
196
197     @Test
198     public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
199         String ricName = "ric1";
200         String managedElementId = "kista_1";
201         addRic(ricName, managedElementId);
202
203         String url = "/ric?managedElementId=" + managedElementId;
204         String rsp = restClient().get(url).block();
205         assertThat(rsp).isEqualTo(ricName);
206
207         // test GET RIC for ManagedElement that does not exist
208         url = "/ric?managedElementId=" + "junk";
209         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
210     }
211
212     private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
213         String url = "/policy?type=" + policyTypeName + "&instance=" + policyInstanceId + "&ric=" + ricName
214             + "&service=" + serviceName;
215         return url;
216     }
217
218     @Test
219     public void testPutPolicy() throws Exception {
220         String serviceName = "service1";
221         String ricName = "ric1";
222         String policyTypeName = "type1";
223         String policyInstanceId = "instance1";
224
225         putService(serviceName);
226         addPolicyType(policyTypeName, ricName);
227
228         String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
229         final String policyBody = jsonString();
230         this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
231
232         restClient().put(url, policyBody).block();
233
234         Policy policy = policies.getPolicy(policyInstanceId);
235         assertThat(policy).isNotNull();
236         assertThat(policy.id()).isEqualTo(policyInstanceId);
237         assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
238         assertThat(policy.ric().name()).isEqualTo("ric1");
239
240         url = "/policies";
241         String rsp = restClient().get(url).block();
242         assertThat(rsp.contains(policyInstanceId)).isTrue();
243
244         // Test of error codes
245         url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
246         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
247
248         url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
249         testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
250
251         url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
252         this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
253         testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
254         this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
255     }
256
257     @Test
258     public void testRefuseToUpdatePolicy() throws Exception {
259         // Test that only the json can be changed for a already created policy
260         // In this case service is attempted to be changed
261         this.addRic("ric1");
262         this.addRic("ricXXX");
263         this.addPolicy("instance1", "type1", "service1", "ric1");
264
265         // Try change ric1 -> ricXXX
266         String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
267         testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.METHOD_NOT_ALLOWED);
268     }
269
270     @Test
271     public void testGetPolicy() throws Exception {
272         String url = "/policy?instance=id";
273         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
274         {
275             String rsp = restClient().get(url).block();
276             assertThat(rsp).isEqualTo(policy.json());
277         }
278         {
279             policies.remove(policy);
280             testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
281         }
282     }
283
284     @Test
285     public void testDeletePolicy() throws Exception {
286         addPolicy("id", "typeName", "service1", "ric1");
287         assertThat(policies.size()).isEqualTo(1);
288
289         String url = "/policy?instance=id";
290         ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
291
292         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
293         assertThat(policies.size()).isEqualTo(0);
294
295         // Delete a non existing policy
296         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
297     }
298
299     @Test
300     public void testGetPolicySchemas() throws Exception {
301         addPolicyType("type1", "ric1");
302         addPolicyType("type2", "ric2");
303
304         String url = "/policy_schemas";
305         String rsp = this.restClient().get(url).block();
306         assertThat(rsp).contains("type1");
307         assertThat(rsp).contains("[{\"title\":\"type2\"}");
308
309         List<String> info = parseSchemas(rsp);
310         assertThat(info.size()).isEqualTo(2);
311
312         url = "/policy_schemas?ric=ric1";
313         rsp = restClient().get(url).block();
314         assertThat(rsp).contains("type1");
315         info = parseSchemas(rsp);
316         assertThat(info.size()).isEqualTo(1);
317
318         // Get schema for non existing RIC
319         url = "/policy_schemas?ric=ric1XXX";
320         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
321     }
322
323     @Test
324     public void testGetPolicySchema() throws Exception {
325         addPolicyType("type1", "ric1");
326         addPolicyType("type2", "ric2");
327
328         String url = "/policy_schema?id=type1";
329         String rsp = restClient().get(url).block();
330         System.out.println(rsp);
331         assertThat(rsp).contains("type1");
332         assertThat(rsp).contains("title");
333
334         // Get non existing schema
335         url = "/policy_schema?id=type1XX";
336         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
337     }
338
339     @Test
340     public void testGetPolicyTypes() throws Exception {
341         addPolicyType("type1", "ric1");
342         addPolicyType("type2", "ric2");
343
344         String url = "/policy_types";
345         String rsp = restClient().get(url).block();
346         assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
347
348         url = "/policy_types?ric=ric1";
349         rsp = restClient().get(url).block();
350         assertThat(rsp).isEqualTo("[\"type1\"]");
351
352         // Get policy types for non existing RIC
353         url = "/policy_types?ric=ric1XXX";
354         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
355     }
356
357     @Test
358     public void testGetPolicies() throws Exception {
359         reset();
360         addPolicy("id1", "type1", "service1");
361
362         String url = "/policies";
363         String rsp = restClient().get(url).block();
364         System.out.println(rsp);
365         List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
366         assertThat(info).size().isEqualTo(1);
367         PolicyInfo policyInfo = info.get(0);
368         assert (policyInfo.validate());
369         assertThat(policyInfo.id).isEqualTo("id1");
370         assertThat(policyInfo.type).isEqualTo("type1");
371         assertThat(policyInfo.service).isEqualTo("service1");
372     }
373
374     @Test
375     public void testGetPoliciesFilter() throws Exception {
376         addPolicy("id1", "type1", "service1");
377         addPolicy("id2", "type1", "service2");
378         addPolicy("id3", "type2", "service1");
379
380         String url = "/policies?type=type1";
381         String rsp = restClient().get(url).block();
382         System.out.println(rsp);
383         assertThat(rsp).contains("id1");
384         assertThat(rsp).contains("id2");
385         assertThat(rsp.contains("id3")).isFalse();
386
387         url = "/policies?type=type1&service=service2";
388         rsp = restClient().get(url).block();
389         System.out.println(rsp);
390         assertThat(rsp.contains("id1")).isFalse();
391         assertThat(rsp).contains("id2");
392         assertThat(rsp.contains("id3")).isFalse();
393
394         // Test get policies for non existing type
395         url = "/policies?type=type1XXX";
396         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
397
398         // Test get policies for non existing RIC
399         url = "/policies?ric=XXX";
400         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
401     }
402
403     @Test
404     public void testPutAndGetService() throws Exception {
405         // PUT
406         putService("name");
407
408         // GET one service
409         String url = "/services?name=name";
410         String rsp = restClient().get(url).block();
411         List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
412         assertThat(info.size()).isEqualTo(1);
413         ServiceStatus status = info.iterator().next();
414         assertThat(status.keepAliveIntervalSeconds).isEqualTo(1);
415         assertThat(status.serviceName).isEqualTo("name");
416
417         // GET (all)
418         url = "/services";
419         rsp = restClient().get(url).block();
420         assertThat(rsp.contains("name")).isTrue();
421         System.out.println(rsp);
422
423         // Keep alive
424         url = "/services/keepalive?name=name";
425         ResponseEntity<String> entity = restClient().postForEntity(url, null).block();
426         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
427
428         // DELETE service
429         assertThat(services.size()).isEqualTo(1);
430         url = "/services?name=name";
431         restClient().delete(url).block();
432         assertThat(services.size()).isEqualTo(0);
433
434         // Keep alive, no registerred service
435         testErrorCode(restClient().post("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
436
437         // PUT servive with crap payload
438         testErrorCode(restClient().put("/service", "junk"), HttpStatus.BAD_REQUEST);
439
440         // GET non existing servive
441         testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
442     }
443
444     @Test
445     public void testGetPolicyStatus() throws Exception {
446         addPolicy("id", "typeName", "service1", "ric1");
447         assertThat(policies.size()).isEqualTo(1);
448
449         String url = "/policy_status?instance=id";
450         String rsp = restClient().get(url).block();
451         assertThat(rsp.equals("OK")).isTrue();
452
453         // GET non existing policy status
454         url = "/policy_status?instance=XXX";
455         testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
456     }
457
458     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
459         addRic(ric);
460         Policy p = ImmutablePolicy.builder().id(id) //
461             .json(jsonString()) //
462             .ownerServiceName(service) //
463             .ric(rics.getRic(ric)) //
464             .type(addPolicyType(typeName, ric)) //
465             .lastModified("lastModified").build();
466         policies.put(p);
467         return p;
468     }
469
470     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
471         return addPolicy(id, typeName, service, "ric");
472     }
473
474     private String createServiceJson(String name) {
475         ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, 1, "callbackUrl");
476
477         String json = gson.toJson(service);
478         return json;
479     }
480
481     private void putService(String name) {
482         String url = "/service";
483         restClient().put(url, createServiceJson(name)).block();
484     }
485
486     private String baseUrl() {
487         return "http://localhost:" + port;
488     }
489
490     private String jsonString() {
491         return "{\n  \"servingCellNrcgi\": \"1\"\n }";
492     }
493
494     private static class ConcurrencyTestRunnable implements Runnable {
495         private final RestTemplate restTemplate = new RestTemplate();
496         private final String baseUrl;
497         static AtomicInteger nextCount = new AtomicInteger(0);
498         private final int count;
499         private final RepositorySupervision supervision;
500
501         ConcurrencyTestRunnable(String baseUrl, RepositorySupervision supervision) {
502             this.baseUrl = baseUrl;
503             this.count = nextCount.incrementAndGet();
504             this.supervision = supervision;
505         }
506
507         @Override
508         public void run() {
509             for (int i = 0; i < 100; ++i) {
510                 if (i % 10 == 0) {
511                     this.supervision.checkAllRics();
512                 }
513                 String name = "policy:" + count + ":" + i;
514                 putPolicy(name);
515                 deletePolicy(name);
516             }
517         }
518
519         private void putPolicy(String name) {
520             String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
521             restTemplate.put(putUrl, createJsonHttpEntity("{}"));
522         }
523
524         private void deletePolicy(String name) {
525             String deleteUrl = baseUrl + "/policy?instance=" + name;
526             restTemplate.delete(deleteUrl);
527         }
528     }
529
530     @Test
531     public void testConcurrency() throws Exception {
532         final Instant startTime = Instant.now();
533         List<Thread> threads = new ArrayList<>();
534         addRic("ric1");
535         addPolicyType("type1", "ric1");
536
537         for (int i = 0; i < 100; ++i) {
538             Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
539             t.start();
540             threads.add(t);
541         }
542         for (Thread t : threads) {
543             t.join();
544         }
545         assertThat(policies.size()).isEqualTo(0);
546         System.out.println("Concurrency test took " + Duration.between(startTime, Instant.now()));
547     }
548
549     private AsyncRestClient restClient() {
550         return new AsyncRestClient(baseUrl());
551     }
552
553     private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
554         StepVerifier.create(request) //
555             .expectSubscription() //
556             .expectErrorMatches(t -> checkWebClientError(t, expStatus)) //
557             .verify();
558     }
559
560     private boolean checkWebClientError(Throwable t, HttpStatus expStatus) {
561         assertTrue(t instanceof WebClientResponseException);
562         WebClientResponseException e = (WebClientResponseException) t;
563         assertThat(e.getStatusCode()).isEqualTo(expStatus);
564         return true;
565     }
566
567     private MockA1Client getA1Client(String ricName) throws ServiceException {
568         return a1ClientFactory.getOrCreateA1Client(ricName);
569     }
570
571     private PolicyType addPolicyType(String policyTypeName, String ricName) {
572         PolicyType type = ImmutablePolicyType.builder() //
573             .name(policyTypeName) //
574             .schema("{\"title\":\"" + policyTypeName + "\"}") //
575             .build();
576
577         policyTypes.put(type);
578         addRic(ricName).addSupportedPolicyType(type);
579         return type;
580     }
581
582     private Ric addRic(String ricName) {
583         return addRic(ricName, null);
584     }
585
586     private Ric addRic(String ricName, String managedElement) {
587         if (rics.get(ricName) != null) {
588             return rics.get(ricName);
589         }
590         List<String> mes = new ArrayList<>();
591         if (managedElement != null) {
592             mes.add(managedElement);
593         }
594         RicConfig conf = ImmutableRicConfig.builder() //
595             .name(ricName) //
596             .baseUrl(ricName) //
597             .managedElementIds(mes) //
598             .build();
599         Ric ric = new Ric(conf);
600         ric.setState(Ric.RicState.IDLE);
601         this.rics.put(ric);
602         return ric;
603     }
604
605     private static HttpEntity<String> createJsonHttpEntity(String content) {
606         HttpHeaders headers = new HttpHeaders();
607         headers.setContentType(MediaType.APPLICATION_JSON);
608         return new HttpEntity<String>(content, headers);
609     }
610
611     private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
612         List<T> result = new ArrayList<>();
613         JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
614         for (JsonElement jsonElement : jsonArr) {
615             T o = gson.fromJson(jsonElement.toString(), clazz);
616             result.add(o);
617         }
618         return result;
619     }
620
621     private static List<String> parseSchemas(String jsonString) {
622         JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
623         List<String> result = new ArrayList<>();
624         for (JsonElement schemaObject : arrayOfSchema) {
625             result.add(schemaObject.toString());
626         }
627         return result;
628     }
629 }