83ee76037a976d3f3bedf45a933e5dee0548168a
[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.jupiter.api.Assertions.assertFalse;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.reflect.TypeToken;
29
30 import java.net.URL;
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.PolicyTypeInfo;
42 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
43 import org.oransc.policyagent.controllers.ServiceStatus;
44 import org.oransc.policyagent.exceptions.ServiceException;
45 import org.oransc.policyagent.repository.ImmutablePolicy;
46 import org.oransc.policyagent.repository.ImmutablePolicyType;
47 import org.oransc.policyagent.repository.Policies;
48 import org.oransc.policyagent.repository.Policy;
49 import org.oransc.policyagent.repository.PolicyType;
50 import org.oransc.policyagent.repository.PolicyTypes;
51 import org.oransc.policyagent.repository.Ric;
52 import org.oransc.policyagent.repository.Rics;
53 import org.springframework.beans.factory.annotation.Autowired;
54 import org.springframework.boot.test.context.SpringBootTest;
55 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
56 import org.springframework.boot.test.context.TestConfiguration;
57 import org.springframework.boot.web.server.LocalServerPort;
58 import org.springframework.context.ApplicationContext;
59 import org.springframework.context.annotation.Bean;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.http.ResponseEntity;
62 import org.springframework.test.context.junit.jupiter.SpringExtension;
63 import org.springframework.web.client.RestTemplate;
64
65 @ExtendWith(SpringExtension.class)
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
67 public class ApplicationTest {
68     @Autowired
69     ApplicationContext context;
70
71     @Autowired
72     private Rics rics;
73
74     @Autowired
75     private Policies policies;
76
77     @Autowired
78     private PolicyTypes policyTypes;
79
80     private static Gson gson = new GsonBuilder() //
81         .serializeNulls() //
82         .create(); //
83
84     public static class MockApplicationConfig extends ApplicationConfig {
85         @Override
86         public void initialize() {
87             URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
88             loadConfigurationFromFile(url.getFile());
89         }
90     }
91
92     /**
93      * Overrides the BeanFactory.
94      */
95     @TestConfiguration
96     static class TestBeanFactory {
97
98         @Bean
99         public ApplicationConfig getApplicationConfig() {
100             return new MockApplicationConfig();
101         }
102
103         @Bean
104         public Rics getRics() {
105             Rics rics = new Rics();
106             rics.put(new Ric(ImmutableRicConfig.builder().name("kista_1").baseUrl("kista_url")
107                 .managedElementIds(new Vector<>()).build()));
108             rics.put(new Ric(ImmutableRicConfig.builder().name("ric1").baseUrl("ric_url")
109                 .managedElementIds(new Vector<>()).build()));
110             return rics;
111         }
112     }
113
114     @LocalServerPort
115     private int port;
116
117     private final RestTemplate restTemplate = new RestTemplate();
118
119     private void reset() {
120         rics.clear();
121         policies.clear();
122         policyTypes.clear();
123         assertThat(policies.size()).isEqualTo(0);
124     }
125
126     @Test
127     public void testGetRics() throws Exception {
128         String url = baseUrl() + "/rics";
129         String rsp = this.restTemplate.getForObject(url, String.class);
130         System.out.println(rsp);
131         assertThat(rsp).contains("kista_1");
132
133         url = baseUrl() + "/rics?policyType=ANR";
134         rsp = this.restTemplate.getForObject(url, String.class);
135         // TODO this should test that the correct types are retrieved from the RIC
136         assertThat(rsp).isEqualTo("[]");
137     }
138
139     @Test
140     public void testGetRic() throws Exception {
141         String url = baseUrl() + "/ric?managedElementId=kista_1";
142         String rsp = this.restTemplate.getForObject(url, String.class);
143         assertThat(rsp).isEqualTo("ric1");
144     }
145
146     // managedElmentId -> nodeName
147
148     @Test
149     public void testPutPolicy() throws Exception {
150         putService("service1");
151
152         String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
153         String json = "{}";
154         addPolicyType("type1");
155
156         this.restTemplate.put(url, json);
157
158         Policy policy = policies.get("instance1");
159
160         assertThat(policy).isNotNull();
161         assertThat(policy.id()).isEqualTo("instance1");
162         assertThat(policy.ownerServiceName()).isEqualTo("service1");
163
164         url = baseUrl() + "/policies";
165         String rsp = this.restTemplate.getForObject(url, String.class);
166         System.out.println(rsp);
167     }
168
169     private PolicyType addPolicyType(String name) {
170         PolicyType type = ImmutablePolicyType.builder() //
171             .name(name) //
172             .build();
173
174         policyTypes.put(type);
175         return type;
176     }
177
178     private Ric addRic(String name) {
179         Vector<String> mes = new Vector<>();
180         RicConfig conf = ImmutableRicConfig.builder().name(name).baseUrl("baseUrl").managedElementIds(mes).build();
181         Ric ric = new Ric(conf);
182         this.rics.put(ric);
183         return ric;
184     }
185
186     private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
187         addRic(ric);
188         Policy p = ImmutablePolicy.builder().id(id) //
189             .json("{}") //
190             .ownerServiceName(service) //
191             .ric(rics.getRic(ric)) //
192             .type(addPolicyType(typeName)) //
193             .lastModified("lastModified").build();
194         policies.put(p);
195         return p;
196     }
197
198     private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
199         return addPolicy(id, typeName, service, "ric");
200     }
201
202     private String baseUrl() {
203         return "http://localhost:" + port;
204     }
205
206     @Test
207     public void testGetPolicy() throws Exception {
208         String url = baseUrl() + "/policy?instance=id";
209         Policy policy = addPolicy("id", "typeName", "service1", "ric1");
210         {
211             String rsp = this.restTemplate.getForObject(url, String.class);
212             assertThat(rsp).isEqualTo(policy.json());
213         }
214         {
215             policies.remove(policy);
216             ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
217             assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
218         }
219     }
220
221     @Test
222     public void testDeletePolicy() throws Exception {
223         reset();
224         String url = baseUrl() + "/policy?instance=id";
225         addPolicy("id", "typeName", "service1", "ric1");
226         assertThat(policies.size()).isEqualTo(1);
227
228         this.restTemplate.delete(url);
229
230         assertThat(policies.size()).isEqualTo(0);
231     }
232
233     private static <T> List<T> parseList(String json, Class<T> clazz) {
234         if (null == json) {
235             return null;
236         }
237         return gson.fromJson(json, new TypeToken<T>() {}.getType());
238     }
239
240     @Test
241     public void testGetPolicyTypes() throws Exception {
242         reset();
243         addPolicy("id1", "type1", "service1");
244         addPolicy("id2", "type2", "service2");
245
246         String url = baseUrl() + "/policy_types";
247         String rsp = this.restTemplate.getForObject(url, String.class);
248         System.out.println(rsp);
249         assertThat(rsp).contains("type1");
250         assertThat(rsp).contains("type2");
251
252         List<PolicyTypeInfo> info = parseList(rsp, PolicyTypeInfo.class);
253         System.out.println(info.size());
254
255     }
256
257     @Test
258     public void testGetPolicies() throws Exception {
259         String url = baseUrl() + "/policies";
260         addPolicy("id1", "type1", "service1");
261         addPolicy("id2", "type2", "service2");
262
263         String rsp = this.restTemplate.getForObject(url, String.class);
264         System.out.println(rsp);
265         assertThat(rsp).contains("id1");
266         assertThat(rsp).contains("id2");
267     }
268
269     @Test
270     public void testGetPoliciesFilter() throws Exception {
271         addPolicy("id1", "type1", "service1");
272         addPolicy("id2", "type1", "service2");
273         addPolicy("id3", "type2", "service1");
274
275         String url = baseUrl() + "/policies?type=type1";
276         String rsp = this.restTemplate.getForObject(url, String.class);
277         System.out.println(rsp);
278         assertThat(rsp).contains("id1");
279         assertThat(rsp).contains("id2");
280         assertFalse(rsp.contains("id3"));
281
282         url = baseUrl() + "/policies?type=type1&service=service2";
283         rsp = this.restTemplate.getForObject(url, String.class);
284         System.out.println(rsp);
285         assertFalse(rsp.contains("id1"));
286         assertThat(rsp).contains("id2");
287         assertFalse(rsp.contains("id3"));
288     }
289
290     private String createServiceJson(String name) {
291         ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
292             .keepAliveInterval(1) //
293             .name(name) //
294             .build();
295         String json = gson.toJson(service);
296         return json;
297     }
298
299     private void putService(String name) {
300         String url = baseUrl() + "/service";
301         this.restTemplate.put(url, createServiceJson(name));
302     }
303
304     @Test
305     public void testPutAndGetService() throws Exception {
306         putService("name");
307
308         String url = baseUrl() + "/service?name=name";
309         String rsp = this.restTemplate.getForObject(url, String.class);
310         ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
311         assertThat(status.keepAliveInterval() == 1);
312         assertThat(status.name().equals("name"));
313
314         url = baseUrl() + "/services";
315         rsp = this.restTemplate.getForObject(url, String.class);
316         assertThat(rsp.contains("name"));
317         System.out.println(rsp);
318
319         url = baseUrl() + "/service/ping";
320         this.restTemplate.put(url, "name");
321     }
322
323 }