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.Assert.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.reflect.TypeToken;
32 import java.util.List;
33 import java.util.Vector;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.oransc.policyagent.clients.A1Client;
38 import org.oransc.policyagent.configuration.ApplicationConfig;
39 import org.oransc.policyagent.configuration.ImmutableRicConfig;
40 import org.oransc.policyagent.configuration.RicConfig;
41 import org.oransc.policyagent.controllers.ImmutableServiceRegistrationInfo;
42 import org.oransc.policyagent.controllers.ImmutableServiceStatus;
43 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
44 import org.oransc.policyagent.controllers.ServiceStatus;
45 import org.oransc.policyagent.exceptions.ServiceException;
46 import org.oransc.policyagent.repository.ImmutablePolicy;
47 import org.oransc.policyagent.repository.ImmutablePolicyType;
48 import org.oransc.policyagent.repository.Policies;
49 import org.oransc.policyagent.repository.Policy;
50 import org.oransc.policyagent.repository.PolicyType;
51 import org.oransc.policyagent.repository.PolicyTypes;
52 import org.oransc.policyagent.repository.Ric;
53 import org.oransc.policyagent.repository.Rics;
54 import org.oransc.policyagent.utils.MockA1Client;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.boot.test.context.SpringBootTest;
57 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
58 import org.springframework.boot.test.context.TestConfiguration;
59 import org.springframework.boot.web.server.LocalServerPort;
60 import org.springframework.context.ApplicationContext;
61 import org.springframework.context.annotation.Bean;
62 import org.springframework.http.HttpStatus;
63 import org.springframework.http.ResponseEntity;
64 import org.springframework.test.context.junit.jupiter.SpringExtension;
65 import org.springframework.web.client.RestTemplate;
67 @ExtendWith(SpringExtension.class)
68 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
69 public class ApplicationTest {
71 ApplicationContext context;
77 private Policies policies;
80 private PolicyTypes policyTypes;
82 private static Gson gson = new GsonBuilder() //
86 public static class MockApplicationConfig extends ApplicationConfig {
88 protected String getLocalConfigurationFilePath() {
89 URL url = MockApplicationConfig.class.getClassLoader().getResource("test_application_configuration.json");
95 * Overrides the BeanFactory.
98 static class TestBeanFactory {
99 private final Rics rics = new Rics();
100 private final Policies policies = new Policies();
101 private final PolicyTypes policyTypes = new PolicyTypes();
104 public ApplicationConfig getApplicationConfig() {
105 return new MockApplicationConfig();
109 A1Client getA1Client() {
110 return new MockA1Client(this.policyTypes);
114 public Policies getPolicies() {
115 return this.policies;
119 public PolicyTypes getPolicyTypes() {
120 return this.policyTypes;
124 public Rics getRics() {
132 private final RestTemplate restTemplate = new RestTemplate();
134 private void reset() {
138 assertThat(policies.size()).isEqualTo(0);
142 public void testGetRics() throws Exception {
145 String url = baseUrl() + "/rics";
146 String rsp = this.restTemplate.getForObject(url, String.class);
147 System.out.println(rsp);
148 assertThat(rsp).contains("kista_1");
150 url = baseUrl() + "/rics?policyType=ANR";
151 rsp = this.restTemplate.getForObject(url, String.class);
152 assertThat(rsp).isEqualTo("[]");
156 public void testGetRic() throws Exception {
158 String url = baseUrl() + "/ric?managedElementId=kista_1";
159 String rsp = this.restTemplate.getForObject(url, String.class);
160 System.out.println(rsp);
161 assertThat(rsp).isEqualTo("ric1");
165 public void testPutPolicy() throws Exception {
166 putService("service1");
168 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
170 addPolicyType("type1", "ric1");
171 this.rics.getRic("ric1").setState(Ric.RicState.IDLE);
173 this.restTemplate.put(url, json);
175 Policy policy = policies.getPolicy("instance1");
177 assertThat(policy).isNotNull();
178 assertThat(policy.id()).isEqualTo("instance1");
179 assertThat(policy.ownerServiceName()).isEqualTo("service1");
181 url = baseUrl() + "/policies";
182 String rsp = this.restTemplate.getForObject(url, String.class);
183 System.out.println(rsp);
186 private PolicyType addPolicyType(String policyTypeName, String ricName) {
187 PolicyType type = ImmutablePolicyType.builder() //
188 .name(policyTypeName) //
189 .schema("{\"title\":\"" + policyTypeName + "\"}") //
192 policyTypes.put(type);
193 addRic(ricName).addSupportedPolicyType(type);
197 private Ric addRic(String ricName) {
198 if (rics.get(ricName) != null) {
199 return rics.get(ricName);
201 Vector<String> mes = new Vector<>();
202 RicConfig conf = ImmutableRicConfig.builder() //
204 .baseUrl("baseUrl") //
205 .managedElementIds(mes) //
207 Ric ric = new Ric(conf);
212 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
214 Policy p = ImmutablePolicy.builder().id(id) //
216 .ownerServiceName(service) //
217 .ric(rics.getRic(ric)) //
218 .type(addPolicyType(typeName, ric)) //
219 .lastModified("lastModified").build();
224 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
225 return addPolicy(id, typeName, service, "ric");
228 private String baseUrl() {
229 return "http://localhost:" + port;
233 public void testGetPolicy() throws Exception {
234 String url = baseUrl() + "/policy?instance=id";
235 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
237 String rsp = this.restTemplate.getForObject(url, String.class);
238 assertThat(rsp).isEqualTo(policy.json());
241 policies.remove(policy);
242 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
243 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
248 public void testDeletePolicy() throws Exception {
250 String url = baseUrl() + "/policy?instance=id";
251 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
252 policy.ric().setState(Ric.RicState.IDLE);
253 assertThat(policies.size()).isEqualTo(1);
255 this.restTemplate.delete(url);
257 assertThat(policies.size()).isEqualTo(0);
260 private static <T> List<T> parseList(String json, Class<T> clazz) {
264 return gson.fromJson(json, new TypeToken<T>() {}.getType());
269 public void testGetPolicySchemas() throws Exception {
271 addPolicyType("type1", "ric1");
272 addPolicyType("type2", "ric2");
274 String url = baseUrl() + "/policy_schemas";
275 String rsp = this.restTemplate.getForObject(url, String.class);
276 System.out.println("*** " + rsp);
277 assertThat(rsp).contains("type1");
278 assertThat(rsp).contains("type2");
279 assertThat(rsp).contains("title");
281 List<String> info = parseList(rsp, String.class);
282 assertEquals(2, info.size());
284 url = baseUrl() + "/policy_schemas?ric=ric1";
285 rsp = this.restTemplate.getForObject(url, String.class);
286 assertThat(rsp).contains("type1");
287 info = parseList(rsp, String.class);
288 assertEquals(1, info.size());
292 public void testGetPolicySchema() throws Exception {
294 addPolicyType("type1", "ric1");
295 addPolicyType("type2", "ric2");
297 String url = baseUrl() + "/policy_schema?id=type1";
298 String rsp = this.restTemplate.getForObject(url, String.class);
299 System.out.println(rsp);
300 assertThat(rsp).contains("type1");
301 assertThat(rsp).contains("title");
305 public void testGetPolicyTypes() throws Exception {
307 addPolicyType("type1", "ric1");
308 addPolicyType("type2", "ric2");
310 String url = baseUrl() + "/policy_types";
311 String rsp = this.restTemplate.getForObject(url, String.class);
312 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
314 url = baseUrl() + "/policy_types?ric=ric1";
315 rsp = this.restTemplate.getForObject(url, String.class);
316 assertThat(rsp).isEqualTo("[\"type1\"]");
320 public void testGetPolicies() throws Exception {
321 String url = baseUrl() + "/policies";
322 addPolicy("id1", "type1", "service1");
323 addPolicy("id2", "type2", "service2");
325 String rsp = this.restTemplate.getForObject(url, String.class);
326 System.out.println(rsp);
327 assertThat(rsp).contains("id1");
328 assertThat(rsp).contains("id2");
332 public void testGetPoliciesFilter() throws Exception {
333 addPolicy("id1", "type1", "service1");
334 addPolicy("id2", "type1", "service2");
335 addPolicy("id3", "type2", "service1");
337 String url = baseUrl() + "/policies?type=type1";
338 String rsp = this.restTemplate.getForObject(url, String.class);
339 System.out.println(rsp);
340 assertThat(rsp).contains("id1");
341 assertThat(rsp).contains("id2");
342 assertFalse(rsp.contains("id3"));
344 url = baseUrl() + "/policies?type=type1&service=service2";
345 rsp = this.restTemplate.getForObject(url, String.class);
346 System.out.println(rsp);
347 assertFalse(rsp.contains("id1"));
348 assertThat(rsp).contains("id2");
349 assertFalse(rsp.contains("id3"));
352 private String createServiceJson(String name) {
353 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
354 .keepAliveInterval(1) //
356 .callbackUrl("callbackUrl") //
358 String json = gson.toJson(service);
362 private void putService(String name) {
363 String url = baseUrl() + "/service";
364 this.restTemplate.put(url, createServiceJson(name));
368 public void testPutAndGetService() throws Exception {
371 String url = baseUrl() + "/service?name=name";
372 String rsp = this.restTemplate.getForObject(url, String.class);
373 ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
374 assertThat(status.keepAliveInterval() == 1);
375 assertThat(status.name().equals("name"));
377 url = baseUrl() + "/services";
378 rsp = this.restTemplate.getForObject(url, String.class);
379 assertThat(rsp.contains("name"));
380 System.out.println(rsp);
382 url = baseUrl() + "/service/ping";
383 this.restTemplate.put(url, "name");