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===================================
20 package org.oransc.policyagent;
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertFalse;
25 import com.google.gson.Gson;
26 import com.google.gson.GsonBuilder;
27 import com.google.gson.reflect.TypeToken;
30 import java.util.List;
31 import java.util.Vector;
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;
64 @RunWith(SpringRunner.class)
65 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
66 public class ApplicationTest {
72 private Policies policies;
75 private PolicyTypes policyTypes;
77 private static Gson gson = new GsonBuilder() //
81 public static class MockApplicationConfig extends ApplicationConfig {
83 public void initialize() {
84 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
85 loadConfigurationFromFile(url.getFile());
90 * overrides the BeanFactory
93 static class TestBeanFactory {
96 public ApplicationConfig getApplicationConfig() {
97 return new MockApplicationConfig();
104 private final RestTemplate restTemplate = new RestTemplate();
107 public void reset() {
111 assertThat(policies.size()).isEqualTo(0);
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");
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");
128 // managedElmentId -> nodeName
131 public void testPutPolicy() throws Exception {
132 putService("service1");
134 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
136 addPolicyType("type1");
138 this.restTemplate.put(url, json);
140 Policy policy = policies.get("instance1");
142 assertThat(policy).isNotNull();
143 assertThat(policy.id()).isEqualTo("instance1");
144 assertThat(policy.ownerServiceName()).isEqualTo("service1");
146 url = baseUrl() + "/policies";
147 String rsp = this.restTemplate.getForObject(url, String.class);
148 System.out.println(rsp);
151 private PolicyType addPolicyType(String name) {
152 PolicyType type = ImmutablePolicyType.builder() //
157 policyTypes.put(type);
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);
169 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
171 Policy p = ImmutablePolicy.builder().id(id) //
173 .ownerServiceName(service) //
174 .ric(rics.getRic(ric)) //
175 .type(addPolicyType(typeName)) //
176 .lastModified("lastModified").build();
181 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
182 return addPolicy(id, typeName, service, "ric");
185 private String baseUrl() {
186 return "http://localhost:" + port;
190 public void testGetPolicy() throws Exception {
191 String url = baseUrl() + "/policy?instance=id";
192 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
194 String rsp = this.restTemplate.getForObject(url, String.class);
195 assertThat(rsp).isEqualTo(policy.json());
198 policies.remove(policy);
199 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
200 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
205 public void testDeletePolicy() throws Exception {
207 String url = baseUrl() + "/policy?instance=id";
208 addPolicy("id", "typeName", "service1", "ric1");
209 assertThat(policies.size()).isEqualTo(1);
211 this.restTemplate.delete(url);
213 assertThat(policies.size()).isEqualTo(0);
216 public static <T> List<T> parseList(String json, Class<T> clazz) {
220 return gson.fromJson(json, new TypeToken<T>() {}.getType());
224 public void testGetPolicyTypes() throws Exception {
225 String url = baseUrl() + "/policy_types";
227 addPolicy("id1", "type1", "service1");
228 addPolicy("id2", "type2", "service2");
230 String rsp = this.restTemplate.getForObject(url, String.class);
231 System.out.println(rsp);
232 assertThat(rsp).contains("type1");
233 assertThat(rsp).contains("type2");
235 List<PolicyTypeInfo> info = parseList(rsp, PolicyTypeInfo.class);
236 System.out.println(info.size());
241 public void testGetPolicies() throws Exception {
242 String url = baseUrl() + "/policies";
243 addPolicy("id1", "type1", "service1");
244 addPolicy("id2", "type2", "service2");
246 String rsp = this.restTemplate.getForObject(url, String.class);
247 System.out.println(rsp);
248 assertThat(rsp).contains("id1");
249 assertThat(rsp).contains("id2");
253 public void testGetPoliciesFilter() throws Exception {
254 addPolicy("id1", "type1", "service1");
255 addPolicy("id2", "type1", "service2");
256 addPolicy("id3", "type2", "service1");
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"));
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"));
273 private String createServiceJson(String name) {
274 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
275 .keepAliveInterval(1) //
278 String json = gson.toJson(service);
282 private void putService(String name) {
283 String url = baseUrl() + "/service";
284 this.restTemplate.put(url, createServiceJson(name));
288 public void testPutAndGetService() throws Exception {
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"));
297 url = baseUrl() + "/services";
298 rsp = this.restTemplate.getForObject(url, String.class);
299 assertThat(rsp.contains("name"));
300 System.out.println(rsp);
302 url = baseUrl() + "/service/ping";
303 this.restTemplate.put(url, "name");