2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.policyagent;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.reflect.TypeToken;
31 import java.util.List;
32 import java.util.Vector;
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;
65 @ExtendWith(SpringExtension.class)
66 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
67 public class ApplicationTest {
69 ApplicationContext context;
75 private Policies policies;
78 private PolicyTypes policyTypes;
80 private static Gson gson = new GsonBuilder() //
84 public static class MockApplicationConfig extends ApplicationConfig {
86 public void initialize() {
87 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
88 loadConfigurationFromFile(url.getFile());
93 * Overrides the BeanFactory.
96 static class TestBeanFactory {
99 public ApplicationConfig getApplicationConfig() {
100 return new MockApplicationConfig();
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()));
117 private final RestTemplate restTemplate = new RestTemplate();
119 private void reset() {
123 assertThat(policies.size()).isEqualTo(0);
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");
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("[]");
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");
146 // managedElmentId -> nodeName
149 public void testPutPolicy() throws Exception {
150 putService("service1");
152 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
154 addPolicyType("type1");
156 this.restTemplate.put(url, json);
158 Policy policy = policies.get("instance1");
160 assertThat(policy).isNotNull();
161 assertThat(policy.id()).isEqualTo("instance1");
162 assertThat(policy.ownerServiceName()).isEqualTo("service1");
164 url = baseUrl() + "/policies";
165 String rsp = this.restTemplate.getForObject(url, String.class);
166 System.out.println(rsp);
169 private PolicyType addPolicyType(String name) {
170 PolicyType type = ImmutablePolicyType.builder() //
174 policyTypes.put(type);
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);
186 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
188 Policy p = ImmutablePolicy.builder().id(id) //
190 .ownerServiceName(service) //
191 .ric(rics.getRic(ric)) //
192 .type(addPolicyType(typeName)) //
193 .lastModified("lastModified").build();
198 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
199 return addPolicy(id, typeName, service, "ric");
202 private String baseUrl() {
203 return "http://localhost:" + port;
207 public void testGetPolicy() throws Exception {
208 String url = baseUrl() + "/policy?instance=id";
209 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
211 String rsp = this.restTemplate.getForObject(url, String.class);
212 assertThat(rsp).isEqualTo(policy.json());
215 policies.remove(policy);
216 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
217 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
222 public void testDeletePolicy() throws Exception {
224 String url = baseUrl() + "/policy?instance=id";
225 addPolicy("id", "typeName", "service1", "ric1");
226 assertThat(policies.size()).isEqualTo(1);
228 this.restTemplate.delete(url);
230 assertThat(policies.size()).isEqualTo(0);
233 private static <T> List<T> parseList(String json, Class<T> clazz) {
237 return gson.fromJson(json, new TypeToken<T>() {}.getType());
241 public void testGetPolicyTypes() throws Exception {
243 addPolicy("id1", "type1", "service1");
244 addPolicy("id2", "type2", "service2");
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");
252 List<PolicyTypeInfo> info = parseList(rsp, PolicyTypeInfo.class);
253 System.out.println(info.size());
258 public void testGetPolicies() throws Exception {
259 String url = baseUrl() + "/policies";
260 addPolicy("id1", "type1", "service1");
261 addPolicy("id2", "type2", "service2");
263 String rsp = this.restTemplate.getForObject(url, String.class);
264 System.out.println(rsp);
265 assertThat(rsp).contains("id1");
266 assertThat(rsp).contains("id2");
270 public void testGetPoliciesFilter() throws Exception {
271 addPolicy("id1", "type1", "service1");
272 addPolicy("id2", "type1", "service2");
273 addPolicy("id3", "type2", "service1");
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"));
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"));
290 private String createServiceJson(String name) {
291 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
292 .keepAliveInterval(1) //
295 String json = gson.toJson(service);
299 private void putService(String name) {
300 String url = baseUrl() + "/service";
301 this.restTemplate.put(url, createServiceJson(name));
305 public void testPutAndGetService() throws Exception {
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"));
314 url = baseUrl() + "/services";
315 rsp = this.restTemplate.getForObject(url, String.class);
316 assertThat(rsp.contains("name"));
317 System.out.println(rsp);
319 url = baseUrl() + "/service/ping";
320 this.restTemplate.put(url, "name");