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;
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.ServiceRegistrationInfo;
42 import org.oransc.policyagent.controllers.ServiceStatus;
43 import org.oransc.policyagent.exceptions.ServiceException;
44 import org.oransc.policyagent.repository.ImmutablePolicy;
45 import org.oransc.policyagent.repository.ImmutablePolicyType;
46 import org.oransc.policyagent.repository.Policies;
47 import org.oransc.policyagent.repository.Policy;
48 import org.oransc.policyagent.repository.PolicyType;
49 import org.oransc.policyagent.repository.PolicyTypes;
50 import org.oransc.policyagent.repository.Ric;
51 import org.oransc.policyagent.repository.Rics;
52 import org.oransc.policyagent.tasks.RepositorySupervision;
53 import org.oransc.policyagent.utils.MockA1Client;
54 import org.oransc.policyagent.utils.MockA1ClientFactory;
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;
83 MockA1ClientFactory a1ClientFactory;
86 RepositorySupervision supervision;
88 private static Gson gson = new GsonBuilder() //
92 public static class MockApplicationConfig extends ApplicationConfig {
94 public String getLocalConfigurationFilePath() {
95 return ""; // No config file loaded for the test
100 * Overrides the BeanFactory.
103 static class TestBeanFactory {
104 private final PolicyTypes policyTypes = new PolicyTypes();
107 public ApplicationConfig getApplicationConfig() {
108 return new MockApplicationConfig();
112 MockA1ClientFactory getA1ClientFactory() {
113 return new MockA1ClientFactory(this.policyTypes);
117 public Policies getPolicies() {
118 return new Policies();
122 public PolicyTypes getPolicyTypes() {
123 return this.policyTypes;
127 public Rics getRics() {
135 private final RestTemplate restTemplate = new RestTemplate();
137 private void reset() {
141 assertThat(policies.size()).isEqualTo(0);
145 public void testGetRics() throws Exception {
148 String url = baseUrl() + "/rics";
149 String rsp = this.restTemplate.getForObject(url, String.class);
150 System.out.println(rsp);
151 assertThat(rsp).contains("kista_1");
153 url = baseUrl() + "/rics?policyType=ANR";
154 rsp = this.restTemplate.getForObject(url, String.class);
155 assertThat(rsp).isEqualTo("[]");
159 public void testRecovery() throws Exception {
161 Policy policy = addPolicy("policyId", "typeName", "service", "ric"); // This should be created in the RIC
162 Policy policy2 = addPolicy("policyId2", "typeName", "service", "ric");
164 getA1Client("ric").putPolicy(policy2); // put it in the RIC
165 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
167 supervision.checkAllRics(); // The created policy should be put in the RIC
168 Policies ricPolicies = getA1Client("ric").getPolicies();
169 assertThat(ricPolicies.size()).isEqualTo(1);
170 Policy ricPolicy = ricPolicies.get("policyId");
171 assertThat(ricPolicy.json()).isEqualTo(policy.json());
174 MockA1Client getA1Client(String ricName) throws ServiceException {
175 return a1ClientFactory.getOrCreateA1Client(ricName);
179 public void testGetRic() throws Exception {
181 Ric ric = addRic("ric1");
182 ric.addManagedElement("kista_1");
183 String url = baseUrl() + "/ric?managedElementId=kista_1";
185 String rsp = this.restTemplate.getForObject(url, String.class);
186 System.out.println(rsp);
188 assertThat(rsp).isEqualTo("ric1");
192 public void testPutPolicy() throws Exception {
193 putService("service1");
195 String url = baseUrl() + "/policy?type=type1&instance=instance1&ric=ric1&service=service1";
197 addPolicyType("type1", "ric1");
198 this.rics.getRic("ric1").setState(Ric.RicState.IDLE);
200 this.restTemplate.put(url, json);
202 Policy policy = policies.getPolicy("instance1");
204 assertThat(policy).isNotNull();
205 assertThat(policy.id()).isEqualTo("instance1");
206 assertThat(policy.ownerServiceName()).isEqualTo("service1");
208 url = baseUrl() + "/policies";
209 String rsp = this.restTemplate.getForObject(url, String.class);
210 System.out.println(rsp);
213 private PolicyType addPolicyType(String policyTypeName, String ricName) {
214 PolicyType type = ImmutablePolicyType.builder() //
215 .name(policyTypeName) //
216 .schema("{\"title\":\"" + policyTypeName + "\"}") //
219 policyTypes.put(type);
220 addRic(ricName).addSupportedPolicyType(type);
224 private Ric addRic(String ricName) {
225 if (rics.get(ricName) != null) {
226 return rics.get(ricName);
228 Vector<String> mes = new Vector<>();
229 RicConfig conf = ImmutableRicConfig.builder() //
232 .managedElementIds(mes) //
234 Ric ric = new Ric(conf);
239 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
241 Policy p = ImmutablePolicy.builder().id(id) //
243 .ownerServiceName(service) //
244 .ric(rics.getRic(ric)) //
245 .type(addPolicyType(typeName, ric)) //
246 .lastModified("lastModified").build();
251 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
252 return addPolicy(id, typeName, service, "ric");
255 private String baseUrl() {
256 return "http://localhost:" + port;
260 public void testGetPolicy() throws Exception {
261 String url = baseUrl() + "/policy?instance=id";
262 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
264 String rsp = this.restTemplate.getForObject(url, String.class);
265 assertThat(rsp).isEqualTo(policy.json());
268 policies.remove(policy);
269 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
270 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
275 public void testDeletePolicy() throws Exception {
277 String url = baseUrl() + "/policy?instance=id";
278 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
279 policy.ric().setState(Ric.RicState.IDLE);
280 assertThat(policies.size()).isEqualTo(1);
282 this.restTemplate.delete(url);
284 assertThat(policies.size()).isEqualTo(0);
287 private static <T> List<T> parseList(String json, Class<T> clazz) {
291 return gson.fromJson(json, new TypeToken<T>() {}.getType());
296 public void testGetPolicySchemas() throws Exception {
298 addPolicyType("type1", "ric1");
299 addPolicyType("type2", "ric2");
301 String url = baseUrl() + "/policy_schemas";
302 String rsp = this.restTemplate.getForObject(url, String.class);
303 System.out.println("*** " + rsp);
304 assertThat(rsp).contains("type1");
305 assertThat(rsp).contains("type2");
306 assertThat(rsp).contains("title");
308 List<String> info = parseList(rsp, String.class);
309 assertEquals(2, info.size());
311 url = baseUrl() + "/policy_schemas?ric=ric1";
312 rsp = this.restTemplate.getForObject(url, String.class);
313 assertThat(rsp).contains("type1");
314 info = parseList(rsp, String.class);
315 assertEquals(1, info.size());
319 public void testGetPolicySchema() throws Exception {
321 addPolicyType("type1", "ric1");
322 addPolicyType("type2", "ric2");
324 String url = baseUrl() + "/policy_schema?id=type1";
325 String rsp = this.restTemplate.getForObject(url, String.class);
326 System.out.println(rsp);
327 assertThat(rsp).contains("type1");
328 assertThat(rsp).contains("title");
332 public void testGetPolicyTypes() throws Exception {
334 addPolicyType("type1", "ric1");
335 addPolicyType("type2", "ric2");
337 String url = baseUrl() + "/policy_types";
338 String rsp = this.restTemplate.getForObject(url, String.class);
339 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
341 url = baseUrl() + "/policy_types?ric=ric1";
342 rsp = this.restTemplate.getForObject(url, String.class);
343 assertThat(rsp).isEqualTo("[\"type1\"]");
347 public void testGetPolicies() throws Exception {
348 String url = baseUrl() + "/policies";
349 addPolicy("id1", "type1", "service1");
350 addPolicy("id2", "type2", "service2");
352 String rsp = this.restTemplate.getForObject(url, String.class);
353 System.out.println(rsp);
354 assertThat(rsp).contains("id1");
355 assertThat(rsp).contains("id2");
359 public void testGetPoliciesFilter() throws Exception {
360 addPolicy("id1", "type1", "service1");
361 addPolicy("id2", "type1", "service2");
362 addPolicy("id3", "type2", "service1");
364 String url = baseUrl() + "/policies?type=type1";
365 String rsp = this.restTemplate.getForObject(url, String.class);
366 System.out.println(rsp);
367 assertThat(rsp).contains("id1");
368 assertThat(rsp).contains("id2");
369 assertFalse(rsp.contains("id3"));
371 url = baseUrl() + "/policies?type=type1&service=service2";
372 rsp = this.restTemplate.getForObject(url, String.class);
373 System.out.println(rsp);
374 assertFalse(rsp.contains("id1"));
375 assertThat(rsp).contains("id2");
376 assertFalse(rsp.contains("id3"));
379 private String createServiceJson(String name) {
380 ServiceRegistrationInfo service = ImmutableServiceRegistrationInfo.builder() //
381 .keepAliveInterval(1) //
383 .callbackUrl("callbackUrl") //
385 String json = gson.toJson(service);
389 private void putService(String name) {
390 String url = baseUrl() + "/service";
391 this.restTemplate.put(url, createServiceJson(name));
395 public void testPutAndGetService() throws Exception {
398 String url = baseUrl() + "/service?name=name";
399 String rsp = this.restTemplate.getForObject(url, String.class);
400 ServiceStatus status = gson.fromJson(rsp, ImmutableServiceStatus.class);
401 assertThat(status.keepAliveInterval() == 1);
402 assertThat(status.name().equals("name"));
404 url = baseUrl() + "/services";
405 rsp = this.restTemplate.getForObject(url, String.class);
406 assertThat(rsp.contains("name"));
407 System.out.println(rsp);
409 url = baseUrl() + "/service/ping";
410 this.restTemplate.put(url, "name");