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