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.annotation.Bean;
59 import org.springframework.http.HttpStatus;
60 import org.springframework.http.ResponseEntity;
61 import org.springframework.test.context.junit.jupiter.SpringExtension;
62 import org.springframework.web.client.RestTemplate;
64 @ExtendWith(SpringExtension.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();
106 private void reset() {
110 assertThat(policies.size()).isEqualTo(0);
114 public void testGetRics() throws Exception {
115 String url = baseUrl() + "/rics";
116 String rsp = this.restTemplate.getForObject(url, String.class);
117 assertThat(rsp).contains("kista_1");
121 public void testGetRic() throws Exception {
122 String url = baseUrl() + "/ric?managedElementId=kista_1";
123 String rsp = this.restTemplate.getForObject(url, String.class);
124 assertThat(rsp).isEqualTo("ric1");
127 // managedElmentId -> nodeName
130 public void testPutPolicy() throws Exception {
131 putService("service1");
133 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
135 addPolicyType("type1");
137 this.restTemplate.put(url, json);
139 Policy policy = policies.get("instance1");
141 assertThat(policy).isNotNull();
142 assertThat(policy.id()).isEqualTo("instance1");
143 assertThat(policy.ownerServiceName()).isEqualTo("service1");
145 url = baseUrl() + "/policies";
146 String rsp = this.restTemplate.getForObject(url, String.class);
147 System.out.println(rsp);
150 private PolicyType addPolicyType(String name) {
151 PolicyType type = ImmutablePolicyType.builder() //
156 policyTypes.put(type);
160 private Ric addRic(String name) {
161 Vector<String> mes = new Vector<>();
162 RicConfig conf = ImmutableRicConfig.builder().name(name).baseUrl("baseUrl").managedElementIds(mes).build();
163 Ric ric = new Ric(conf);
168 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
170 Policy p = ImmutablePolicy.builder().id(id) //
172 .ownerServiceName(service) //
173 .ric(rics.getRic(ric)) //
174 .type(addPolicyType(typeName)) //
175 .lastModified("lastModified").build();
180 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
181 return addPolicy(id, typeName, service, "ric");
184 private String baseUrl() {
185 return "http://localhost:" + port;
189 public void testGetPolicy() throws Exception {
190 String url = baseUrl() + "/policy?instance=id";
191 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
193 String rsp = this.restTemplate.getForObject(url, String.class);
194 assertThat(rsp).isEqualTo(policy.json());
197 policies.remove(policy);
198 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
199 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
204 public void testDeletePolicy() throws Exception {
206 String url = baseUrl() + "/policy?instance=id";
207 addPolicy("id", "typeName", "service1", "ric1");
208 assertThat(policies.size()).isEqualTo(1);
210 this.restTemplate.delete(url);
212 assertThat(policies.size()).isEqualTo(0);
215 private static <T> List<T> parseList(String json, Class<T> clazz) {
219 return gson.fromJson(json, new TypeToken<T>() {}.getType());
223 public void testGetPolicyTypes() throws Exception {
225 addPolicy("id1", "type1", "service1");
226 addPolicy("id2", "type2", "service2");
228 String url = baseUrl() + "/policy_types";
229 String rsp = this.restTemplate.getForObject(url, String.class);
230 System.out.println(rsp);
231 assertThat(rsp).contains("type1");
232 assertThat(rsp).contains("type2");
234 List<PolicyTypeInfo> info = parseList(rsp, PolicyTypeInfo.class);
235 System.out.println(info.size());
240 public void testGetPolicies() throws Exception {
241 String url = baseUrl() + "/policies";
242 addPolicy("id1", "type1", "service1");
243 addPolicy("id2", "type2", "service2");
245 String rsp = this.restTemplate.getForObject(url, String.class);
246 System.out.println(rsp);
247 assertThat(rsp).contains("id1");
248 assertThat(rsp).contains("id2");
252 public void testGetPoliciesFilter() throws Exception {
253 addPolicy("id1", "type1", "service1");
254 addPolicy("id2", "type1", "service2");
255 addPolicy("id3", "type2", "service1");
257 String url = baseUrl() + "/policies?type=type1";
258 String rsp = this.restTemplate.getForObject(url, String.class);
259 System.out.println(rsp);
260 assertThat(rsp).contains("id1");
261 assertThat(rsp).contains("id2");
262 assertFalse(rsp.contains("id3"));
264 url = baseUrl() + "/policies?type=type1&service=service2";
265 rsp = this.restTemplate.getForObject(url, String.class);
266 System.out.println(rsp);
267 assertFalse(rsp.contains("id1"));
268 assertThat(rsp).contains("id2");
269 assertFalse(rsp.contains("id3"));
272 private String createServiceJson(String name) {
273 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
274 .keepAliveInterval(1) //
277 String json = gson.toJson(service);
281 private void putService(String name) {
282 String url = baseUrl() + "/service";
283 this.restTemplate.put(url, createServiceJson(name));
287 public void testPutAndGetService() throws Exception {
290 String url = baseUrl() + "/service?name=name";
291 String rsp = this.restTemplate.getForObject(url, String.class);
292 ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
293 assertThat(status.keepAliveInterval() == 1);
294 assertThat(status.name().equals("name"));
296 url = baseUrl() + "/services";
297 rsp = this.restTemplate.getForObject(url, String.class);
298 assertThat(rsp.contains("name"));
299 System.out.println(rsp);
301 url = baseUrl() + "/service/ping";
302 this.restTemplate.put(url, "name");