4270ded6902c3ce13ebc35e2a11fb3f660d7b02c
[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.junit.Assert.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
26
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.reflect.TypeToken;
30
31 import java.util.List;
32 import java.util.Vector;
33
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.oransc.policyagent.configuration.ApplicationConfig;
37 import org.oransc.policyagent.configuration.ImmutableRicConfig;
38 import org.oransc.policyagent.configuration.RicConfig;
39 import org.oransc.policyagent.controllers.ImmutableServiceRegistrationInfo;
40 import org.oransc.policyagent.controllers.ImmutableServiceStatus;
41 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
42 import org.oransc.policyagent.controllers.ServiceStatus;
43 import org.oransc.policyagent.exceptions.ServiceException;
44 import org.oransc.policyagent.repository.ImmutablePolicy;
45 import org.oransc.policyagent.repository.ImmutablePolicyType;
46 import org.oransc.policyagent.repository.Policies;
47 import org.oransc.policyagent.repository.Policy;
48 import org.oransc.policyagent.repository.PolicyType;
49 import org.oransc.policyagent.repository.PolicyTypes;
50 import org.oransc.policyagent.repository.Ric;
51 import org.oransc.policyagent.repository.Rics;
52 import org.oransc.policyagent.tasks.RepositorySupervision;
53 import org.oransc.policyagent.utils.MockA1Client;
54 import org.oransc.policyagent.utils.MockA1ClientFactory;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
58 import org.springframework.boot.test.context.TestConfiguration;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.context.ApplicationContext;
61 import org.springframework.context.annotation.Bean;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.test.context.junit.jupiter.SpringExtension;
65 import org.springframework.web.client.RestTemplate;
66
67 @ExtendWith(SpringExtension.class)
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
69 public class ApplicationTest {
70     @Autowired
71     ApplicationContext context;
72
73     @Autowired
74     private Rics rics;
75
76     @Autowired
77     private Policies policies;
78
79     @Autowired
80     private PolicyTypes policyTypes;
81
82     @Autowired
83     MockA1ClientFactory a1ClientFactory;
84
85     @Autowired
86     RepositorySupervision supervision;
87
88     private static Gson gson = new GsonBuilder() //
89         .serializeNulls() //
90         .create(); //
91
92     public static class MockApplicationConfig extends ApplicationConfig {
93         @Override
94         public String getLocalConfigurationFilePath() {
95             return ""; // No config file loaded for the test
96         }
97     }
98
99     /**
100      * Overrides the BeanFactory.
101      */
102     @TestConfiguration
103     static class TestBeanFactory {
104         private final PolicyTypes policyTypes = new PolicyTypes();
105
106         @Bean
107         public ApplicationConfig getApplicationConfig() {
108             return new MockApplicationConfig();
109         }
110
111         @Bean
112         MockA1ClientFactory getA1ClientFactory() {
113             return new MockA1ClientFactory(this.policyTypes);
114         }
115
116         @Bean
117         public Policies getPolicies() {
118             return new Policies();
119         }
120
121         @Bean
122         public PolicyTypes getPolicyTypes() {
123             return this.policyTypes;
124         }
125
126         @Bean
127         public Rics getRics() {
128             return new Rics();
129         }
130     }
131
132     @LocalServerPort
133     private int port;
134
135     private final RestTemplate restTemplate = new RestTemplate();
136
137     private void reset() {
138         rics.clear();
139         policies.clear();
140         policyTypes.clear();
141         assertThat(policies.size()).isEqualTo(0);
142     }
143
144     @Test
145     public void testGetRics() throws Exception {
146         reset();
147         addRic("kista_1");
148         String url = baseUrl() + "/rics";
149         String rsp = this.restTemplate.getForObject(url, String.class);
150         System.out.println(rsp);
151         assertThat(rsp).contains("kista_1");
152
153         url = baseUrl() + "/rics?policyType=ANR";
154         rsp = this.restTemplate.getForObject(url, String.class);
155         assertThat(rsp).isEqualTo("[]");
156     }
157
158     @Test
159     public void testRecovery() throws Exception {
160         reset();
161         Policy policy = addPolicy("policyId", "typeName", "service", "ric"); // This should be created in the RIC
162         Policy policy2 = addPolicy("policyId2", "typeName", "service", "ric");
163
164         getA1Client("ric").putPolicy(policy2); // put it in the RIC
165         policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
166
167         supervision.checkAllRics(); // The created policy should be put in the RIC
168         Policies ricPolicies = getA1Client("ric").getPolicies();
169         assertThat(ricPolicies.size()).isEqualTo(1);
170         Policy ricPolicy = ricPolicies.get("policyId");
171         assertThat(ricPolicy.json()).isEqualTo(policy.json());
172     }
173
174     MockA1Client getA1Client(String ricName) throws ServiceException {
175         return a1ClientFactory.getOrCreateA1Client(ricName);
176     }
177
178     @Test
179     public void testGetRic() throws Exception {
180         reset();
181         Ric ric = addRic("ric1");
182         ric.addManagedElement("kista_1");
183         String url = baseUrl() + "/ric?managedElementId=kista_1";
184
185         String rsp = this.restTemplate.getForObject(url, String.class);
186         System.out.println(rsp);
187
188         assertThat(rsp).isEqualTo("ric1");
189     }
190
191     @Test
192     public void testPutPolicy() throws Exception {
193         putService("service1");
194
195         String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
196         String json = "{}";
197         addPolicyType("type1", "ric1");
198         this.rics.getRic("ric1").setState(Ric.RicState.IDLE);
199
200         this.restTemplate.put(url, json);
201
202         Policy policy = policies.getPolicy("instance1");
203
204         assertThat(policy).isNotNull();
205         assertThat(policy.id()).isEqualTo("instance1");
206         assertThat(policy.ownerServiceName()).isEqualTo("service1");
207
208         url = baseUrl() + "/policies";
209         String rsp = this.restTemplate.getForObject(url, String.class);
210         System.out.println(rsp);
211     }
212
213     private PolicyType addPolicyType(String policyTypeName, String ricName) {
214         PolicyType type = ImmutablePolicyType.builder() //
215             .name(policyTypeName) //
216             .schema("{\"title\":\"" + policyTypeName + "\"}") //
217             .build();
218
219         policyTypes.put(type);
220         addRic(ricName).addSupportedPolicyType(type);
221         return type;
222     }
223
224     private Ric addRic(String ricName) {
225         if (rics.get(ricName) != null) {
226             return rics.get(ricName);
227         }
228         Vector<String> mes = new Vector<>();
229         RicConfig conf = ImmutableRicConfig.builder() //
230             .name(ricName) //
231             .baseUrl(ricName) //
232             .managedElementIds(mes) //
233             .build();
234         Ric ric = new Ric(conf);
235         this.rics.put(ric);
236         return ric;
237     }
238
239     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
240         addRic(ric);
241         Policy p = ImmutablePolicy.builder().id(id) //
242             .json("{}") //
243             .ownerServiceName(service) //
244             .ric(rics.getRic(ric)) //
245             .type(addPolicyType(typeName, ric)) //
246             .lastModified("lastModified").build();
247         policies.put(p);
248         return p;
249     }
250
251     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
252         return addPolicy(id, typeName, service, "ric");
253     }
254
255     private String baseUrl() {
256         return "http://localhost:" + port;
257     }
258
259     @Test
260     public void testGetPolicy() throws Exception {
261         String url = baseUrl() + "/policy?instance=id";
262         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
263         {
264             String rsp = this.restTemplate.getForObject(url, String.class);
265             assertThat(rsp).isEqualTo(policy.json());
266         }
267         {
268             policies.remove(policy);
269             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
270             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
271         }
272     }
273
274     @Test
275     public void testDeletePolicy() throws Exception {
276         reset();
277         String url = baseUrl() + "/policy?instance=id";
278         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
279         policy.ric().setState(Ric.RicState.IDLE);
280         assertThat(policies.size()).isEqualTo(1);
281
282         this.restTemplate.delete(url);
283
284         assertThat(policies.size()).isEqualTo(0);
285     }
286
287     private static <T> List<T> parseList(String json, Class<T> clazz) {
288         if (null == json) {
289             return null;
290         }
291         return gson.fromJson(json, new TypeToken<T>() {}.getType());
292
293     }
294
295     @Test
296     public void testGetPolicySchemas() throws Exception {
297         reset();
298         addPolicyType("type1", "ric1");
299         addPolicyType("type2", "ric2");
300
301         String url = baseUrl() + "/policy_schemas";
302         String rsp = this.restTemplate.getForObject(url, String.class);
303         System.out.println("*** " + rsp);
304         assertThat(rsp).contains("type1");
305         assertThat(rsp).contains("type2");
306         assertThat(rsp).contains("title");
307
308         List<String> info = parseList(rsp, String.class);
309         assertEquals(2, info.size());
310
311         url = baseUrl() + "/policy_schemas?ric=ric1";
312         rsp = this.restTemplate.getForObject(url, String.class);
313         assertThat(rsp).contains("type1");
314         info = parseList(rsp, String.class);
315         assertEquals(1, info.size());
316     }
317
318     @Test
319     public void testGetPolicySchema() throws Exception {
320         reset();
321         addPolicyType("type1", "ric1");
322         addPolicyType("type2", "ric2");
323
324         String url = baseUrl() + "/policy_schema?id=type1";
325         String rsp = this.restTemplate.getForObject(url, String.class);
326         System.out.println(rsp);
327         assertThat(rsp).contains("type1");
328         assertThat(rsp).contains("title");
329     }
330
331     @Test
332     public void testGetPolicyTypes() throws Exception {
333         reset();
334         addPolicyType("type1", "ric1");
335         addPolicyType("type2", "ric2");
336
337         String url = baseUrl() + "/policy_types";
338         String rsp = this.restTemplate.getForObject(url, String.class);
339         assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
340
341         url = baseUrl() + "/policy_types?ric=ric1";
342         rsp = this.restTemplate.getForObject(url, String.class);
343         assertThat(rsp).isEqualTo("[\"type1\"]");
344     }
345
346     @Test
347     public void testGetPolicies() throws Exception {
348         String url = baseUrl() + "/policies";
349         addPolicy("id1", "type1", "service1");
350         addPolicy("id2", "type2", "service2");
351
352         String rsp = this.restTemplate.getForObject(url, String.class);
353         System.out.println(rsp);
354         assertThat(rsp).contains("id1");
355         assertThat(rsp).contains("id2");
356     }
357
358     @Test
359     public void testGetPoliciesFilter() throws Exception {
360         addPolicy("id1", "type1", "service1");
361         addPolicy("id2", "type1", "service2");
362         addPolicy("id3", "type2", "service1");
363
364         String url = baseUrl() + "/policies?type=type1";
365         String rsp = this.restTemplate.getForObject(url, String.class);
366         System.out.println(rsp);
367         assertThat(rsp).contains("id1");
368         assertThat(rsp).contains("id2");
369         assertFalse(rsp.contains("id3"));
370
371         url = baseUrl() + "/policies?type=type1&service=service2";
372         rsp = this.restTemplate.getForObject(url, String.class);
373         System.out.println(rsp);
374         assertFalse(rsp.contains("id1"));
375         assertThat(rsp).contains("id2");
376         assertFalse(rsp.contains("id3"));
377     }
378
379     private String createServiceJson(String name) {
380         ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
381             .keepAliveInterval(1) //
382             .name(name) //
383             .callbackUrl("callbackUrl") //
384             .build();
385         String json = gson.toJson(service);
386         return json;
387     }
388
389     private void putService(String name) {
390         String url = baseUrl() + "/service";
391         this.restTemplate.put(url, createServiceJson(name));
392     }
393
394     @Test
395     public void testPutAndGetService() throws Exception {
396         putService("name");
397
398         String url = baseUrl() + "/service?name=name";
399         String rsp = this.restTemplate.getForObject(url, String.class);
400         ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
401         assertThat(status.keepAliveInterval() == 1);
402         assertThat(status.name().equals("name"));
403
404         url = baseUrl() + "/services";
405         rsp = this.restTemplate.getForObject(url, String.class);
406         assertThat(rsp.contains("name"));
407         System.out.println(rsp);
408
409         url = baseUrl() + "/service/ping";
410         this.restTemplate.put(url, "name");
411     }
412
413 }