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.awaitility.Awaitility.await;
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonParser;
32 import java.io.IOException;
33 import java.time.Duration;
34 import java.time.Instant;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Vector;
38 import java.util.concurrent.atomic.AtomicInteger;
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.oransc.policyagent.configuration.ApplicationConfig;
45 import org.oransc.policyagent.configuration.ImmutableRicConfig;
46 import org.oransc.policyagent.configuration.RicConfig;
47 import org.oransc.policyagent.controllers.PolicyInfo;
48 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
49 import org.oransc.policyagent.controllers.ServiceStatus;
50 import org.oransc.policyagent.exceptions.ServiceException;
51 import org.oransc.policyagent.repository.ImmutablePolicy;
52 import org.oransc.policyagent.repository.ImmutablePolicyType;
53 import org.oransc.policyagent.repository.Lock.LockType;
54 import org.oransc.policyagent.repository.Policies;
55 import org.oransc.policyagent.repository.Policy;
56 import org.oransc.policyagent.repository.PolicyType;
57 import org.oransc.policyagent.repository.PolicyTypes;
58 import org.oransc.policyagent.repository.Ric;
59 import org.oransc.policyagent.repository.Ric.RicState;
60 import org.oransc.policyagent.repository.Rics;
61 import org.oransc.policyagent.repository.Services;
62 import org.oransc.policyagent.tasks.RepositorySupervision;
63 import org.oransc.policyagent.utils.MockA1Client;
64 import org.oransc.policyagent.utils.MockA1ClientFactory;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.boot.test.context.SpringBootTest;
67 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
68 import org.springframework.boot.test.context.TestConfiguration;
69 import org.springframework.boot.web.server.LocalServerPort;
70 import org.springframework.context.ApplicationContext;
71 import org.springframework.context.annotation.Bean;
72 import org.springframework.http.HttpEntity;
73 import org.springframework.http.HttpHeaders;
74 import org.springframework.http.HttpMethod;
75 import org.springframework.http.HttpStatus;
76 import org.springframework.http.HttpStatus.Series;
77 import org.springframework.http.MediaType;
78 import org.springframework.http.ResponseEntity;
79 import org.springframework.http.client.ClientHttpResponse;
80 import org.springframework.test.context.junit.jupiter.SpringExtension;
81 import org.springframework.web.client.ResponseErrorHandler;
82 import org.springframework.web.client.RestTemplate;
84 @ExtendWith(SpringExtension.class)
85 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
86 public class ApplicationTest {
88 ApplicationContext context;
94 private Policies policies;
97 private PolicyTypes policyTypes;
100 MockA1ClientFactory a1ClientFactory;
103 RepositorySupervision supervision;
108 private static Gson gson = new GsonBuilder() //
112 public static class MockApplicationConfig extends ApplicationConfig {
114 public String getLocalConfigurationFilePath() {
115 return ""; // No config file loaded for the test
120 * Overrides the BeanFactory.
123 static class TestBeanFactory {
124 private final PolicyTypes policyTypes = new PolicyTypes();
127 public ApplicationConfig getApplicationConfig() {
128 return new MockApplicationConfig();
132 MockA1ClientFactory getA1ClientFactory() {
133 return new MockA1ClientFactory(this.policyTypes);
137 public PolicyTypes getPolicyTypes() {
138 return this.policyTypes;
145 private final RestTemplate restTemplate = new RestTemplate();
147 public class RestTemplateResponseErrorHandler implements ResponseErrorHandler {
150 public boolean hasError(ClientHttpResponse httpResponse) throws IOException {
151 return (httpResponse.getStatusCode().series() == Series.CLIENT_ERROR
152 || httpResponse.getStatusCode().series() == Series.SERVER_ERROR);
156 public void handleError(ClientHttpResponse httpResponse) throws IOException {
157 System.out.println("Error " + httpResponse.toString());
161 private void setRestErrorhandler() {
162 restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
166 public void reset() {
174 public void verifyNoRicLocks() {
175 for (Ric ric : this.rics.getRics()) {
176 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
177 ric.getLock().unlockBlocking();
178 assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
179 assertThat(ric.getState()).isEqualTo(Ric.RicState.IDLE);
184 public void testGetRics() throws Exception {
186 String url = baseUrl() + "/rics";
187 String rsp = this.restTemplate.getForObject(url, String.class);
188 System.out.println(rsp);
189 assertThat(rsp).contains("kista_1");
191 url = baseUrl() + "/rics?policyType=STD_PolicyModelUnconstrained_0.2.0";
192 rsp = this.restTemplate.getForObject(url, String.class);
193 assertThat(rsp).isEqualTo("[]");
197 public void testRecovery() throws Exception {
198 addRic("ric").setState(Ric.RicState.UNDEFINED);
199 String ricName = "ric";
200 Policy policy2 = addPolicy("policyId2", "typeName", "service", ricName);
202 getA1Client(ricName).putPolicy(policy2); // put it in the RIC
203 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
205 String policyId = "policyId";
206 Policy policy = addPolicy(policyId, "typeName", "service", ricName); // This should be created in the RIC
207 supervision.checkAllRics(); // The created policy should be put in the RIC
208 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ricName).getState()));
209 await().untilAsserted(() -> RicState.IDLE.equals(rics.getRic(ricName).getState()));
211 Policies ricPolicies = getA1Client(ricName).getPolicies();
212 assertThat(ricPolicies.size()).isEqualTo(1);
213 Policy ricPolicy = ricPolicies.get(policyId);
214 assertThat(ricPolicy.json()).isEqualTo(policy.json());
218 public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
219 String ricName = "ric1";
220 Ric ric = addRic(ricName);
221 String managedElementId = "kista_1";
222 ric.addManagedElement(managedElementId);
224 String url = baseUrl() + "/ric?managedElementId=" + managedElementId;
225 String rsp = this.restTemplate.getForObject(url, String.class);
227 assertThat(rsp).isEqualTo(ricName);
231 public void testGetRicForManagedElementThatDoesNotExist() throws Exception {
232 this.setRestErrorhandler();
233 String url = baseUrl() + "/ric?managedElementId=kista_1";
234 ResponseEntity<String> entity = this.restTemplate.getForEntity(url, String.class);
235 assertThat(entity.getStatusCode().equals(HttpStatus.NOT_FOUND));
239 public void testPutPolicy() throws Exception {
240 String serviceName = "service1";
241 String ricName = "ric1";
242 String policyTypeName = "type1";
243 String policyInstanceId = "instance1";
245 putService(serviceName);
246 addPolicyType(policyTypeName, ricName);
248 String url = baseUrl() + "/policy?type=" + policyTypeName + "&instance=" + policyInstanceId + "&ric=" + ricName
249 + "&service=" + serviceName;
250 final String json = jsonString();
251 this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
253 this.restTemplate.put(url, createJsonHttpEntity(json));
255 Policy policy = policies.getPolicy(policyInstanceId);
256 assertThat(policy).isNotNull();
257 assertThat(policy.id()).isEqualTo(policyInstanceId);
258 assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
259 assertThat(policy.ric().name()).isEqualTo("ric1");
261 url = baseUrl() + "/policies";
262 String rsp = this.restTemplate.getForObject(url, String.class);
263 assertThat(rsp.contains(policyInstanceId)).isTrue();
268 public void testRefuseToUpdatePolicy() throws Exception {
269 // Test that only the json can be changed for a already created policy
270 // In this case service is attempted to be changed
272 this.addRic("ricXXX");
274 this.addPolicy("instance1", "type1", "service1", "ric1");
275 this.setRestErrorhandler();
276 String urlWrongRic = baseUrl() + "/policy?type=type1&instance=instance1&ric=ricXXX&service=service1";
277 ResponseEntity<String> entity = this.putForEntity(urlWrongRic, jsonString());
278 assertThat(entity.getStatusCode().equals(HttpStatus.METHOD_NOT_ALLOWED));
280 Policy policy = policies.getPolicy("instance1");
281 assertThat(policy.ric().name()).isEqualTo("ric1"); // Not changed
285 public void testGetPolicy() throws Exception {
286 String url = baseUrl() + "/policy?instance=id";
287 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
289 String rsp = this.restTemplate.getForObject(url, String.class);
290 assertThat(rsp).isEqualTo(policy.json());
293 policies.remove(policy);
294 ResponseEntity<String> rsp = this.restTemplate.getForEntity(url, String.class);
295 assertThat(rsp.getStatusCodeValue()).isEqualTo(HttpStatus.NO_CONTENT.value());
300 public void testDeletePolicy() throws Exception {
301 String url = baseUrl() + "/policy?instance=id";
302 addPolicy("id", "typeName", "service1", "ric1");
303 assertThat(policies.size()).isEqualTo(1);
305 this.restTemplate.delete(url);
307 assertThat(policies.size()).isEqualTo(0);
311 public void testGetPolicySchemas() throws Exception {
312 addPolicyType("type1", "ric1");
313 addPolicyType("type2", "ric2");
315 String url = baseUrl() + "/policy_schemas";
316 String rsp = this.restTemplate.getForObject(url, String.class);
317 System.out.println("*** " + rsp);
318 assertThat(rsp).contains("type1");
319 assertThat(rsp).contains("[{\"title\":\"type2\"}");
321 List<String> info = parseSchemas(rsp);
322 assertThat(info.size()).isEqualTo(2);
324 url = baseUrl() + "/policy_schemas?ric=ric1";
325 rsp = this.restTemplate.getForObject(url, String.class);
326 assertThat(rsp).contains("type1");
327 info = parseSchemas(rsp);
328 assertThat(info.size()).isEqualTo(1);
332 public void testGetPolicySchema() throws Exception {
333 addPolicyType("type1", "ric1");
334 addPolicyType("type2", "ric2");
336 String url = baseUrl() + "/policy_schema?id=type1";
337 String rsp = this.restTemplate.getForObject(url, String.class);
338 System.out.println(rsp);
339 assertThat(rsp).contains("type1");
340 assertThat(rsp).contains("title");
344 public void testGetPolicyTypes() throws Exception {
345 addPolicyType("type1", "ric1");
346 addPolicyType("type2", "ric2");
348 String url = baseUrl() + "/policy_types";
349 String rsp = this.restTemplate.getForObject(url, String.class);
350 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
352 url = baseUrl() + "/policy_types?ric=ric1";
353 rsp = this.restTemplate.getForObject(url, String.class);
354 assertThat(rsp).isEqualTo("[\"type1\"]");
358 public void testGetPolicies() throws Exception {
360 String url = baseUrl() + "/policies";
361 addPolicy("id1", "type1", "service1");
363 String rsp = this.restTemplate.getForObject(url, String.class);
364 System.out.println(rsp);
365 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
366 assertThat(info).size().isEqualTo(1);
367 PolicyInfo policyInfo = info.get(0);
368 assert (policyInfo.validate());
369 assertThat(policyInfo.id).isEqualTo("id1");
370 assertThat(policyInfo.type).isEqualTo("type1");
371 assertThat(policyInfo.service).isEqualTo("service1");
375 public void testGetPoliciesFilter() throws Exception {
376 addPolicy("id1", "type1", "service1");
377 addPolicy("id2", "type1", "service2");
378 addPolicy("id3", "type2", "service1");
380 String url = baseUrl() + "/policies?type=type1";
381 String rsp = this.restTemplate.getForObject(url, String.class);
382 System.out.println(rsp);
383 assertThat(rsp).contains("id1");
384 assertThat(rsp).contains("id2");
385 assertThat(rsp.contains("id3")).isFalse();
387 url = baseUrl() + "/policies?type=type1&service=service2";
388 rsp = this.restTemplate.getForObject(url, String.class);
389 System.out.println(rsp);
390 assertThat(rsp.contains("id1")).isFalse();
391 assertThat(rsp).contains("id2");
392 assertThat(rsp.contains("id3")).isFalse();
396 public void testPutAndGetService() throws Exception {
401 String url = baseUrl() + "/services?serviceName=name";
402 String rsp = this.restTemplate.getForObject(url, String.class);
403 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
404 assertThat(info.size()).isEqualTo(1);
405 ServiceStatus status = info.iterator().next();
406 assertThat(status.keepAliveIntervalSeconds).isEqualTo(1);
407 assertThat(status.serviceName).isEqualTo("name");
410 url = baseUrl() + "/services";
411 rsp = this.restTemplate.getForObject(url, String.class);
412 assertThat(rsp.contains("name")).isTrue();
413 System.out.println(rsp);
416 url = baseUrl() + "/services/keepalive?name=name";
417 ResponseEntity<String> entity = this.restTemplate.postForEntity(url, null, String.class);
418 assertThat(entity.getStatusCode().equals(HttpStatus.OK));
421 assertThat(services.size()).isEqualTo(1);
422 url = baseUrl() + "/services?name=name";
423 this.restTemplate.delete(url);
424 assertThat(services.size()).isEqualTo(0);
426 // Keep alive, no registerred service
427 url = baseUrl() + "/services/keepalive?name=name";
428 setRestErrorhandler();
429 entity = this.restTemplate.postForEntity(url, null, String.class);
430 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
434 public void testGetPolicyStatus() throws Exception {
435 addPolicy("id", "typeName", "service1", "ric1");
436 assertThat(policies.size()).isEqualTo(1);
438 String url = baseUrl() + "/policy_status?instance=id";
439 String rsp = this.restTemplate.getForObject(url, String.class);
440 assertThat(rsp.equals("OK")).isTrue();
443 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
445 Policy p = ImmutablePolicy.builder().id(id) //
446 .json(jsonString()) //
447 .ownerServiceName(service) //
448 .ric(rics.getRic(ric)) //
449 .type(addPolicyType(typeName, ric)) //
450 .lastModified("lastModified").build();
455 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
456 return addPolicy(id, typeName, service, "ric");
459 private String createServiceJson(String name) {
460 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, 1, "callbackUrl");
462 String json = gson.toJson(service);
466 private void putService(String name) {
467 String url = baseUrl() + "/service";
468 HttpEntity<String> entity = createJsonHttpEntity(createServiceJson(name));
469 this.restTemplate.put(url, entity);
472 private String baseUrl() {
473 return "http://localhost:" + port;
476 private String jsonString() {
477 return "{\n \"servingCellNrcgi\": \"1\"\n }";
480 private static class ConcurrencyTestRunnable implements Runnable {
481 private final RestTemplate restTemplate = new RestTemplate();
482 private final String baseUrl;
483 static AtomicInteger nextCount = new AtomicInteger(0);
484 private final int count;
485 private final RepositorySupervision supervision;
487 ConcurrencyTestRunnable(String baseUrl, RepositorySupervision supervision) {
488 this.baseUrl = baseUrl;
489 this.count = nextCount.incrementAndGet();
490 this.supervision = supervision;
494 for (int i = 0; i < 100; ++i) {
496 this.supervision.checkAllRics();
498 String name = "policy:" + count + ":" + i;
504 private void putPolicy(String name) {
505 String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
506 this.restTemplate.put(putUrl, createJsonHttpEntity("{}"));
509 private void deletePolicy(String name) {
510 String deleteUrl = baseUrl + "/policy?instance=" + name;
511 this.restTemplate.delete(deleteUrl);
516 public void testConcurrency() throws Exception {
517 final Instant startTime = Instant.now();
518 List<Thread> threads = new ArrayList<>();
520 addPolicyType("type1", "ric1");
522 for (int i = 0; i < 100; ++i) {
523 Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
527 for (Thread t : threads) {
530 assertThat(policies.size()).isEqualTo(0);
531 System.out.println("Concurrency test took " + Duration.between(startTime, Instant.now()));
534 private MockA1Client getA1Client(String ricName) throws ServiceException {
535 return a1ClientFactory.getOrCreateA1Client(ricName);
538 private PolicyType addPolicyType(String policyTypeName, String ricName) {
539 PolicyType type = ImmutablePolicyType.builder() //
540 .name(policyTypeName) //
541 .schema("{\"title\":\"" + policyTypeName + "\"}") //
544 policyTypes.put(type);
545 addRic(ricName).addSupportedPolicyType(type);
549 private Ric addRic(String ricName) {
550 if (rics.get(ricName) != null) {
551 return rics.get(ricName);
553 Vector<String> mes = new Vector<>();
554 RicConfig conf = ImmutableRicConfig.builder() //
557 .managedElementIds(mes) //
559 Ric ric = new Ric(conf);
560 ric.setState(Ric.RicState.IDLE);
565 private static HttpEntity<String> createJsonHttpEntity(String content) {
566 HttpHeaders headers = new HttpHeaders();
567 headers.setContentType(MediaType.APPLICATION_JSON);
568 return new HttpEntity<String>(content, headers);
571 private ResponseEntity<String> putForEntity(String url, String jsonBody) {
572 return restTemplate.exchange(url, HttpMethod.PUT, createJsonHttpEntity(jsonBody), String.class);
575 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
576 List<T> result = new ArrayList<>();
577 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
578 for (JsonElement jsonElement : jsonArr) {
579 T o = gson.fromJson(jsonElement.toString(), clazz);
585 private static List<String> parseSchemas(String jsonString) {
586 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
587 List<String> result = new ArrayList<>();
588 for (JsonElement schemaObject : arrayOfSchema) {
589 result.add(schemaObject.toString());