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