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;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonParser;
33 import java.time.Duration;
34 import java.time.Instant;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.atomic.AtomicInteger;
39 import org.junit.jupiter.api.AfterEach;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.junit.jupiter.api.extension.ExtendWith;
43 import org.oransc.policyagent.clients.AsyncRestClient;
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.HttpStatus;
75 import org.springframework.http.MediaType;
76 import org.springframework.http.ResponseEntity;
77 import org.springframework.test.context.junit.jupiter.SpringExtension;
78 import org.springframework.web.client.RestTemplate;
79 import org.springframework.web.reactive.function.client.WebClientResponseException;
81 import reactor.core.publisher.Mono;
82 import reactor.test.StepVerifier;
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;
146 public void reset() {
154 public void verifyNoRicLocks() {
155 for (Ric ric : this.rics.getRics()) {
156 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
157 ric.getLock().unlockBlocking();
158 assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
159 assertThat(ric.getState()).isEqualTo(Ric.RicState.IDLE);
164 public void testGetRics() throws Exception {
166 this.addPolicyType("type1", "kista_1");
167 String url = "/rics?policyType=type1";
168 String rsp = restClient().get(url).block();
169 assertThat(rsp).contains("kista_1");
171 // Non existing policy type
172 url = "/rics?policyType=XXXX";
173 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
177 public void testRecovery() throws Exception {
178 addRic("ric").setState(Ric.RicState.UNDEFINED);
179 String ricName = "ric";
180 Policy policy2 = addPolicy("policyId2", "typeName", "service", ricName);
182 getA1Client(ricName).putPolicy(policy2); // put it in the RIC
183 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
185 String policyId = "policyId";
186 Policy policy = addPolicy(policyId, "typeName", "service", ricName); // This should be created in the RIC
187 supervision.checkAllRics(); // The created policy should be put in the RIC
188 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ricName).getState()));
189 await().untilAsserted(() -> RicState.IDLE.equals(rics.getRic(ricName).getState()));
191 Policies ricPolicies = getA1Client(ricName).getPolicies();
192 assertThat(ricPolicies.size()).isEqualTo(1);
193 Policy ricPolicy = ricPolicies.get(policyId);
194 assertThat(ricPolicy.json()).isEqualTo(policy.json());
198 public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
199 String ricName = "ric1";
200 String managedElementId = "kista_1";
201 addRic(ricName, managedElementId);
203 String url = "/ric?managedElementId=" + managedElementId;
204 String rsp = restClient().get(url).block();
205 assertThat(rsp).isEqualTo(ricName);
207 // test GET RIC for ManagedElement that does not exist
208 url = "/ric?managedElementId=" + "junk";
209 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
212 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
213 String url = "/policy?type=" + policyTypeName + "&instance=" + policyInstanceId + "&ric=" + ricName
214 + "&service=" + serviceName;
219 public void testPutPolicy() throws Exception {
220 String serviceName = "service1";
221 String ricName = "ric1";
222 String policyTypeName = "type1";
223 String policyInstanceId = "instance1";
225 putService(serviceName);
226 addPolicyType(policyTypeName, ricName);
228 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
229 final String policyBody = jsonString();
230 this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
232 restClient().put(url, policyBody).block();
234 Policy policy = policies.getPolicy(policyInstanceId);
235 assertThat(policy).isNotNull();
236 assertThat(policy.id()).isEqualTo(policyInstanceId);
237 assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
238 assertThat(policy.ric().name()).isEqualTo("ric1");
241 String rsp = restClient().get(url).block();
242 assertThat(rsp.contains(policyInstanceId)).isTrue();
244 // Test of error codes
245 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
246 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
248 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
249 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
251 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
252 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
253 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
254 this.rics.getRic(ricName).setState(Ric.RicState.IDLE);
258 public void testRefuseToUpdatePolicy() throws Exception {
259 // Test that only the json can be changed for a already created policy
260 // In this case service is attempted to be changed
262 this.addRic("ricXXX");
263 this.addPolicy("instance1", "type1", "service1", "ric1");
265 // Try change ric1 -> ricXXX
266 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
267 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.METHOD_NOT_ALLOWED);
271 public void testGetPolicy() throws Exception {
272 String url = "/policy?instance=id";
273 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
275 String rsp = restClient().get(url).block();
276 assertThat(rsp).isEqualTo(policy.json());
279 policies.remove(policy);
280 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
285 public void testDeletePolicy() throws Exception {
286 addPolicy("id", "typeName", "service1", "ric1");
287 assertThat(policies.size()).isEqualTo(1);
289 String url = "/policy?instance=id";
290 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
292 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
293 assertThat(policies.size()).isEqualTo(0);
295 // Delete a non existing policy
296 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
300 public void testGetPolicySchemas() throws Exception {
301 addPolicyType("type1", "ric1");
302 addPolicyType("type2", "ric2");
304 String url = "/policy_schemas";
305 String rsp = this.restClient().get(url).block();
306 assertThat(rsp).contains("type1");
307 assertThat(rsp).contains("[{\"title\":\"type2\"}");
309 List<String> info = parseSchemas(rsp);
310 assertThat(info.size()).isEqualTo(2);
312 url = "/policy_schemas?ric=ric1";
313 rsp = restClient().get(url).block();
314 assertThat(rsp).contains("type1");
315 info = parseSchemas(rsp);
316 assertThat(info.size()).isEqualTo(1);
318 // Get schema for non existing RIC
319 url = "/policy_schemas?ric=ric1XXX";
320 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
324 public void testGetPolicySchema() throws Exception {
325 addPolicyType("type1", "ric1");
326 addPolicyType("type2", "ric2");
328 String url = "/policy_schema?id=type1";
329 String rsp = restClient().get(url).block();
330 System.out.println(rsp);
331 assertThat(rsp).contains("type1");
332 assertThat(rsp).contains("title");
334 // Get non existing schema
335 url = "/policy_schema?id=type1XX";
336 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
340 public void testGetPolicyTypes() throws Exception {
341 addPolicyType("type1", "ric1");
342 addPolicyType("type2", "ric2");
344 String url = "/policy_types";
345 String rsp = restClient().get(url).block();
346 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
348 url = "/policy_types?ric=ric1";
349 rsp = restClient().get(url).block();
350 assertThat(rsp).isEqualTo("[\"type1\"]");
352 // Get policy types for non existing RIC
353 url = "/policy_types?ric=ric1XXX";
354 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
358 public void testGetPolicies() throws Exception {
360 addPolicy("id1", "type1", "service1");
362 String url = "/policies";
363 String rsp = restClient().get(url).block();
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 = "/policies?type=type1";
381 String rsp = restClient().get(url).block();
382 System.out.println(rsp);
383 assertThat(rsp).contains("id1");
384 assertThat(rsp).contains("id2");
385 assertThat(rsp.contains("id3")).isFalse();
387 url = "/policies?type=type1&service=service2";
388 rsp = restClient().get(url).block();
389 System.out.println(rsp);
390 assertThat(rsp.contains("id1")).isFalse();
391 assertThat(rsp).contains("id2");
392 assertThat(rsp.contains("id3")).isFalse();
394 // Test get policies for non existing type
395 url = "/policies?type=type1XXX";
396 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
398 // Test get policies for non existing RIC
399 url = "/policies?ric=XXX";
400 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
404 public void testPutAndGetService() throws Exception {
409 String url = "/services?name=name";
410 String rsp = restClient().get(url).block();
411 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
412 assertThat(info.size()).isEqualTo(1);
413 ServiceStatus status = info.iterator().next();
414 assertThat(status.keepAliveIntervalSeconds).isEqualTo(1);
415 assertThat(status.serviceName).isEqualTo("name");
419 rsp = restClient().get(url).block();
420 assertThat(rsp.contains("name")).isTrue();
421 System.out.println(rsp);
424 url = "/services/keepalive?name=name";
425 ResponseEntity<String> entity = restClient().postForEntity(url, null).block();
426 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
429 assertThat(services.size()).isEqualTo(1);
430 url = "/services?name=name";
431 restClient().delete(url).block();
432 assertThat(services.size()).isEqualTo(0);
434 // Keep alive, no registerred service
435 testErrorCode(restClient().post("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
437 // PUT servive with crap payload
438 testErrorCode(restClient().put("/service", "junk"), HttpStatus.BAD_REQUEST);
440 // GET non existing servive
441 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
445 public void testGetPolicyStatus() throws Exception {
446 addPolicy("id", "typeName", "service1", "ric1");
447 assertThat(policies.size()).isEqualTo(1);
449 String url = "/policy_status?instance=id";
450 String rsp = restClient().get(url).block();
451 assertThat(rsp.equals("OK")).isTrue();
453 // GET non existing policy status
454 url = "/policy_status?instance=XXX";
455 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
458 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
460 Policy p = ImmutablePolicy.builder().id(id) //
461 .json(jsonString()) //
462 .ownerServiceName(service) //
463 .ric(rics.getRic(ric)) //
464 .type(addPolicyType(typeName, ric)) //
465 .lastModified("lastModified").build();
470 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
471 return addPolicy(id, typeName, service, "ric");
474 private String createServiceJson(String name) {
475 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, 1, "callbackUrl");
477 String json = gson.toJson(service);
481 private void putService(String name) {
482 String url = "/service";
483 restClient().put(url, createServiceJson(name)).block();
486 private String baseUrl() {
487 return "http://localhost:" + port;
490 private String jsonString() {
491 return "{\n \"servingCellNrcgi\": \"1\"\n }";
494 private static class ConcurrencyTestRunnable implements Runnable {
495 private final RestTemplate restTemplate = new RestTemplate();
496 private final String baseUrl;
497 static AtomicInteger nextCount = new AtomicInteger(0);
498 private final int count;
499 private final RepositorySupervision supervision;
501 ConcurrencyTestRunnable(String baseUrl, RepositorySupervision supervision) {
502 this.baseUrl = baseUrl;
503 this.count = nextCount.incrementAndGet();
504 this.supervision = supervision;
509 for (int i = 0; i < 100; ++i) {
511 this.supervision.checkAllRics();
513 String name = "policy:" + count + ":" + i;
519 private void putPolicy(String name) {
520 String putUrl = baseUrl + "/policy?type=type1&instance=" + name + "&ric=ric1&service=service1";
521 restTemplate.put(putUrl, createJsonHttpEntity("{}"));
524 private void deletePolicy(String name) {
525 String deleteUrl = baseUrl + "/policy?instance=" + name;
526 restTemplate.delete(deleteUrl);
531 public void testConcurrency() throws Exception {
532 final Instant startTime = Instant.now();
533 List<Thread> threads = new ArrayList<>();
535 addPolicyType("type1", "ric1");
537 for (int i = 0; i < 100; ++i) {
538 Thread t = new Thread(new ConcurrencyTestRunnable(baseUrl(), this.supervision), "TestThread_" + i);
542 for (Thread t : threads) {
545 assertThat(policies.size()).isEqualTo(0);
546 System.out.println("Concurrency test took " + Duration.between(startTime, Instant.now()));
549 private AsyncRestClient restClient() {
550 return new AsyncRestClient(baseUrl());
553 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
554 StepVerifier.create(request) //
555 .expectSubscription() //
556 .expectErrorMatches(t -> checkWebClientError(t, expStatus)) //
560 private boolean checkWebClientError(Throwable t, HttpStatus expStatus) {
561 assertTrue(t instanceof WebClientResponseException);
562 WebClientResponseException e = (WebClientResponseException) t;
563 assertThat(e.getStatusCode()).isEqualTo(expStatus);
567 private MockA1Client getA1Client(String ricName) throws ServiceException {
568 return a1ClientFactory.getOrCreateA1Client(ricName);
571 private PolicyType addPolicyType(String policyTypeName, String ricName) {
572 PolicyType type = ImmutablePolicyType.builder() //
573 .name(policyTypeName) //
574 .schema("{\"title\":\"" + policyTypeName + "\"}") //
577 policyTypes.put(type);
578 addRic(ricName).addSupportedPolicyType(type);
582 private Ric addRic(String ricName) {
583 return addRic(ricName, null);
586 private Ric addRic(String ricName, String managedElement) {
587 if (rics.get(ricName) != null) {
588 return rics.get(ricName);
590 List<String> mes = new ArrayList<>();
591 if (managedElement != null) {
592 mes.add(managedElement);
594 RicConfig conf = ImmutableRicConfig.builder() //
597 .managedElementIds(mes) //
599 Ric ric = new Ric(conf);
600 ric.setState(Ric.RicState.IDLE);
605 private static HttpEntity<String> createJsonHttpEntity(String content) {
606 HttpHeaders headers = new HttpHeaders();
607 headers.setContentType(MediaType.APPLICATION_JSON);
608 return new HttpEntity<String>(content, headers);
611 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
612 List<T> result = new ArrayList<>();
613 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
614 for (JsonElement jsonElement : jsonArr) {
615 T o = gson.fromJson(jsonElement.toString(), clazz);
621 private static List<String> parseSchemas(String jsonString) {
622 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
623 List<String> result = new ArrayList<>();
624 for (JsonElement schemaObject : arrayOfSchema) {
625 result.add(schemaObject.toString());