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.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.doReturn;
30 import com.google.gson.Gson;
31 import com.google.gson.GsonBuilder;
32 import com.google.gson.JsonArray;
33 import com.google.gson.JsonElement;
34 import com.google.gson.JsonParser;
36 import java.nio.charset.StandardCharsets;
37 import java.time.Duration;
38 import java.time.Instant;
39 import java.util.ArrayList;
40 import java.util.List;
42 import org.junit.jupiter.api.AfterEach;
43 import org.junit.jupiter.api.BeforeEach;
44 import org.junit.jupiter.api.Test;
45 import org.junit.jupiter.api.extension.ExtendWith;
46 import org.oransc.policyagent.clients.AsyncRestClient;
47 import org.oransc.policyagent.configuration.ApplicationConfig;
48 import org.oransc.policyagent.configuration.ImmutableRicConfig;
49 import org.oransc.policyagent.configuration.ImmutableWebClientConfig;
50 import org.oransc.policyagent.configuration.RicConfig;
51 import org.oransc.policyagent.configuration.WebClientConfig;
52 import org.oransc.policyagent.controllers.PolicyInfo;
53 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
54 import org.oransc.policyagent.controllers.ServiceStatus;
55 import org.oransc.policyagent.exceptions.ServiceException;
56 import org.oransc.policyagent.repository.ImmutablePolicy;
57 import org.oransc.policyagent.repository.ImmutablePolicyType;
58 import org.oransc.policyagent.repository.Lock.LockType;
59 import org.oransc.policyagent.repository.Policies;
60 import org.oransc.policyagent.repository.Policy;
61 import org.oransc.policyagent.repository.PolicyType;
62 import org.oransc.policyagent.repository.PolicyTypes;
63 import org.oransc.policyagent.repository.Ric;
64 import org.oransc.policyagent.repository.Ric.RicState;
65 import org.oransc.policyagent.repository.Rics;
66 import org.oransc.policyagent.repository.Services;
67 import org.oransc.policyagent.tasks.RicSupervision;
68 import org.oransc.policyagent.tasks.ServiceSupervision;
69 import org.oransc.policyagent.utils.MockA1Client;
70 import org.oransc.policyagent.utils.MockA1ClientFactory;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.springframework.beans.factory.annotation.Autowired;
74 import org.springframework.boot.test.context.SpringBootTest;
75 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
76 import org.springframework.boot.test.context.TestConfiguration;
77 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
78 import org.springframework.boot.web.server.LocalServerPort;
79 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
80 import org.springframework.context.ApplicationContext;
81 import org.springframework.context.annotation.Bean;
82 import org.springframework.http.HttpStatus;
83 import org.springframework.http.ResponseEntity;
84 import org.springframework.test.context.junit.jupiter.SpringExtension;
85 import org.springframework.web.reactive.function.client.WebClientResponseException;
87 import reactor.core.publisher.Mono;
88 import reactor.test.StepVerifier;
89 import reactor.util.annotation.Nullable;
91 @ExtendWith(SpringExtension.class)
92 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
93 public class ApplicationTest {
94 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
97 ApplicationContext context;
103 private Policies policies;
106 private PolicyTypes policyTypes;
109 MockA1ClientFactory a1ClientFactory;
112 RicSupervision supervision;
115 ApplicationConfig applicationConfig;
120 private static Gson gson = new GsonBuilder() //
124 public static class MockApplicationConfig extends ApplicationConfig {
126 public String getLocalConfigurationFilePath() {
127 return ""; // No config file loaded for the test
132 * Overrides the BeanFactory.
135 static class TestBeanFactory {
136 private final PolicyTypes policyTypes = new PolicyTypes();
137 private final Services services = new Services();
138 private final Policies policies = new Policies();
139 MockA1ClientFactory a1ClientFactory = null;
142 public ApplicationConfig getApplicationConfig() {
143 return new MockApplicationConfig();
147 MockA1ClientFactory getA1ClientFactory() {
148 if (a1ClientFactory == null) {
149 this.a1ClientFactory = new MockA1ClientFactory(this.policyTypes);
151 return this.a1ClientFactory;
155 public PolicyTypes getPolicyTypes() {
156 return this.policyTypes;
160 Policies getPolicies() {
161 return this.policies;
165 Services getServices() {
166 return this.services;
170 public ServiceSupervision getServiceSupervision() {
171 Duration checkInterval = Duration.ofMillis(1);
172 return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
176 public ServletWebServerFactory servletContainer() {
177 return new TomcatServletWebServerFactory();
186 public void reset() {
191 a1ClientFactory.reset();
195 public void verifyNoRicLocks() {
196 for (Ric ric : this.rics.getRics()) {
197 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
198 ric.getLock().unlockBlocking();
199 assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
200 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
205 public void testGetRics() throws Exception {
207 this.addPolicyType("type1", "ric1");
208 String url = "/rics?policyType=type1";
209 String rsp = restClient().get(url).block();
210 assertThat(rsp).contains("ric1");
212 // nameless type for ORAN A1 1.1
214 this.addPolicyType("", "ric2");
215 url = "/rics?policyType=";
217 // This tests also validation of trusted certs restClient(true)
218 rsp = restClient(true).get(url).block();
219 assertThat(rsp).contains("ric2");
220 assertThat(rsp).doesNotContain("ric1");
221 assertThat(rsp).contains("AVAILABLE");
224 rsp = restClient().get("/rics").block();
225 assertThat(rsp).contains("ric2");
226 assertThat(rsp).contains("ric1");
228 // Non existing policy type
229 url = "/rics?policyType=XXXX";
230 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
234 public void testSynchronization() throws Exception {
235 // Two polictypes will be put in the NearRT RICs
236 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
237 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
238 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
239 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
241 // One type and one instance added to the agent storage
242 final String ric1Name = "ric1";
243 Ric ric1 = addRic(ric1Name);
244 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
245 Ric ric2 = addRic("ric2");
247 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
248 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
250 String policyId = "policyId";
251 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
252 supervision.checkAllRics(); // The created policy should be put in the RIC
254 // Wait until synch is completed
255 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
256 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
257 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
259 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
260 assertThat(ricPolicies.size()).isEqualTo(1);
261 Policy ricPolicy = ricPolicies.get(policyId);
262 assertThat(ricPolicy.json()).isEqualTo(policy.json());
264 // Both types should be in the agent storage after the synch
265 assertThat(ric1.getSupportedPolicyTypes().size()).isEqualTo(2);
266 assertThat(ric2.getSupportedPolicyTypes().size()).isEqualTo(2);
270 public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
271 String ricName = "ric1";
272 String managedElementId = "kista_1";
273 addRic(ricName, managedElementId);
275 String url = "/ric?managedElementId=" + managedElementId;
276 String rsp = restClient().get(url).block();
277 assertThat(rsp).isEqualTo(ricName);
279 // test GET RIC for ManagedElement that does not exist
280 url = "/ric?managedElementId=" + "junk";
281 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
284 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
285 boolean isTransient) {
287 if (policyTypeName.isEmpty()) {
288 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
290 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
294 url += "&transient=true";
299 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
300 return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
304 public void testPutPolicy() throws Exception {
305 String serviceName = "service1";
306 String ricName = "ric1";
307 String policyTypeName = "type1";
308 String policyInstanceId = "instance1";
310 putService(serviceName);
311 addPolicyType(policyTypeName, ricName);
313 // PUT a transient policy
314 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
315 final String policyBody = jsonString();
316 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
318 restClient().put(url, policyBody).block();
320 Policy policy = policies.getPolicy(policyInstanceId);
321 assertThat(policy).isNotNull();
322 assertThat(policy.id()).isEqualTo(policyInstanceId);
323 assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
324 assertThat(policy.ric().name()).isEqualTo("ric1");
325 assertThat(policy.isTransient()).isEqualTo(true);
327 // Put a non transient policy
328 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
329 restClient().put(url, policyBody).block();
330 policy = policies.getPolicy(policyInstanceId);
331 assertThat(policy.isTransient()).isEqualTo(false);
334 String rsp = restClient().get(url).block();
335 assertThat(rsp.contains(policyInstanceId)).isTrue();
337 url = "/policy?id=" + policyInstanceId;
338 rsp = restClient().get(url).block();
339 assertThat(rsp).isEqualTo(policyBody);
341 // Test of error codes
342 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
343 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
345 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
346 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
348 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
349 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
350 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
351 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
356 * Test that HttpStatus and body from failing REST call to A1 is passed on to
359 * @throws ServiceException
361 public void testErrorFromRIC() throws ServiceException {
362 putService("service1");
363 addPolicyType("type1", "ric1");
365 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
366 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
367 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
368 String responseBody = "Refused";
369 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
371 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
372 responseBodyBytes, StandardCharsets.UTF_8, null);
373 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
376 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
379 this.addPolicy("instance1", "type1", "service1", "ric1");
380 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
381 testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
384 this.addPolicy("instance1", "type1", "service1", "ric1");
385 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
386 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
388 // Check that empty response body is OK
389 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
390 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
391 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
395 public void testPutTypelessPolicy() throws Exception {
396 putService("service1");
397 addPolicyType("", "ric1");
398 String url = putPolicyUrl("service1", "ric1", "", "id1");
399 restClient().put(url, jsonString()).block();
401 String rsp = restClient().get("/policies").block();
402 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
403 assertThat(info).size().isEqualTo(1);
404 PolicyInfo policyInfo = info.get(0);
405 assertThat(policyInfo.id.equals("id1")).isTrue();
406 assertThat(policyInfo.type.equals("")).isTrue();
410 public void testRefuseToUpdatePolicy() throws Exception {
411 // Test that only the json can be changed for a already created policy
412 // In this case service is attempted to be changed
414 this.addRic("ricXXX");
415 this.addPolicy("instance1", "type1", "service1", "ric1");
416 this.addPolicy("instance2", "type1", "service1", "ricXXX");
418 // Try change ric1 -> ricXXX
419 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
420 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
424 public void testGetPolicy() throws Exception {
425 String url = "/policy?id=id";
426 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
428 String rsp = restClient().get(url).block();
429 assertThat(rsp).isEqualTo(policy.json());
432 policies.remove(policy);
433 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
438 public void testDeletePolicy() throws Exception {
439 addPolicy("id", "typeName", "service1", "ric1");
440 assertThat(policies.size()).isEqualTo(1);
442 String url = "/policy?id=id";
443 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
445 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
446 assertThat(policies.size()).isEqualTo(0);
448 // Delete a non existing policy
449 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
453 public void testGetPolicySchemas() throws Exception {
454 addPolicyType("type1", "ric1");
455 addPolicyType("type2", "ric2");
457 String url = "/policy_schemas";
458 String rsp = this.restClient().get(url).block();
459 assertThat(rsp).contains("type1");
460 assertThat(rsp).contains("[{\"title\":\"type2\"}");
462 List<String> info = parseSchemas(rsp);
463 assertThat(info.size()).isEqualTo(2);
465 url = "/policy_schemas?ric=ric1";
466 rsp = restClient().get(url).block();
467 assertThat(rsp).contains("type1");
468 info = parseSchemas(rsp);
469 assertThat(info.size()).isEqualTo(1);
471 // Get schema for non existing RIC
472 url = "/policy_schemas?ric=ric1XXX";
473 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
477 public void testGetPolicySchema() throws Exception {
478 addPolicyType("type1", "ric1");
479 addPolicyType("type2", "ric2");
481 String url = "/policy_schema?id=type1";
482 String rsp = restClient().get(url).block();
484 assertThat(rsp).contains("type1");
485 assertThat(rsp).contains("title");
487 // Get non existing schema
488 url = "/policy_schema?id=type1XX";
489 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
493 public void testGetPolicyTypes() throws Exception {
494 addPolicyType("type1", "ric1");
495 addPolicyType("type2", "ric2");
497 String url = "/policy_types";
498 String rsp = restClient().get(url).block();
499 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
501 url = "/policy_types?ric=ric1";
502 rsp = restClient().get(url).block();
503 assertThat(rsp).isEqualTo("[\"type1\"]");
505 // Get policy types for non existing RIC
506 url = "/policy_types?ric=ric1XXX";
507 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
511 public void testGetPolicies() throws Exception {
512 addPolicy("id1", "type1", "service1");
514 String url = "/policies";
515 String rsp = restClient().get(url).block();
517 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
518 assertThat(info).size().isEqualTo(1);
519 PolicyInfo policyInfo = info.get(0);
520 assert (policyInfo.validate());
521 assertThat(policyInfo.id).isEqualTo("id1");
522 assertThat(policyInfo.type).isEqualTo("type1");
523 assertThat(policyInfo.service).isEqualTo("service1");
527 public void testGetPoliciesFilter() throws Exception {
528 addPolicy("id1", "type1", "service1");
529 addPolicy("id2", "type1", "service2");
530 addPolicy("id3", "type2", "service1");
532 String url = "/policies?type=type1";
533 String rsp = restClient().get(url).block();
535 assertThat(rsp).contains("id1");
536 assertThat(rsp).contains("id2");
537 assertThat(rsp.contains("id3")).isFalse();
539 url = "/policies?type=type1&service=service2";
540 rsp = restClient().get(url).block();
542 assertThat(rsp.contains("id1")).isFalse();
543 assertThat(rsp).contains("id2");
544 assertThat(rsp.contains("id3")).isFalse();
546 // Test get policies for non existing type
547 url = "/policies?type=type1XXX";
548 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
550 // Test get policies for non existing RIC
551 url = "/policies?ric=XXX";
552 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
556 public void testGetPolicyIdsFilter() throws Exception {
557 addPolicy("id1", "type1", "service1", "ric1");
558 addPolicy("id2", "type1", "service2", "ric1");
559 addPolicy("id3", "type2", "service1", "ric1");
561 String url = "/policy_ids?type=type1";
562 String rsp = restClient().get(url).block();
564 assertThat(rsp).contains("id1");
565 assertThat(rsp).contains("id2");
566 assertThat(rsp.contains("id3")).isFalse();
568 url = "/policy_ids?type=type1&service=service1&ric=ric1";
569 rsp = restClient().get(url).block();
570 assertThat(rsp).isEqualTo("[\"id1\"]");
572 // Test get policy ids for non existing type
573 url = "/policy_ids?type=type1XXX";
574 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
576 // Test get policy ids for non existing RIC
577 url = "/policy_ids?ric=XXX";
578 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
582 public void testPutAndGetService() throws Exception {
584 putService("name", 0, HttpStatus.CREATED);
585 putService("name", 0, HttpStatus.OK);
588 String url = "/services?name=name";
589 String rsp = restClient().get(url).block();
590 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
591 assertThat(info.size()).isEqualTo(1);
592 ServiceStatus status = info.iterator().next();
593 assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
594 assertThat(status.serviceName).isEqualTo("name");
598 rsp = restClient().get(url).block();
599 assertThat(rsp.contains("name")).isTrue();
603 url = "/services/keepalive?name=name";
604 ResponseEntity<String> entity = restClient().putForEntity(url).block();
605 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
608 assertThat(services.size()).isEqualTo(1);
609 url = "/services?name=name";
610 restClient().delete(url).block();
611 assertThat(services.size()).isEqualTo(0);
613 // Keep alive, no registerred service
614 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
616 // PUT servive with bad payload
617 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
618 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
619 testErrorCode(restClient().put("/service", createServiceJson("name", -123)), HttpStatus.BAD_REQUEST);
620 testErrorCode(restClient().put("/service", createServiceJson("name", 0, "missing.portandprotocol.com")),
621 HttpStatus.BAD_REQUEST);
623 // GET non existing servive
624 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
628 public void testServiceSupervision() throws Exception {
629 putService("service1", 1, HttpStatus.CREATED);
630 addPolicyType("type1", "ric1");
632 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
633 final String policyBody = jsonString();
634 restClient().put(url, policyBody).block();
636 assertThat(policies.size()).isEqualTo(1);
637 assertThat(services.size()).isEqualTo(1);
639 // Timeout after ~1 second
640 await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
641 assertThat(services.size()).isEqualTo(0);
645 public void testGetPolicyStatus() throws Exception {
646 addPolicy("id", "typeName", "service1", "ric1");
647 assertThat(policies.size()).isEqualTo(1);
649 String url = "/policy_status?id=id";
650 String rsp = restClient().get(url).block();
651 assertThat(rsp.equals("OK")).isTrue();
653 // GET non existing policy status
654 url = "/policy_status?id=XXX";
655 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
658 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
660 Policy p = ImmutablePolicy.builder() //
662 .json(jsonString()) //
663 .ownerServiceName(service) //
664 .ric(rics.getRic(ric)) //
665 .type(addPolicyType(typeName, ric)) //
666 .lastModified("lastModified") //
667 .isTransient(false) //
673 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
674 return addPolicy(id, typeName, service, "ric");
677 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
678 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
681 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
682 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
684 String json = gson.toJson(service);
688 private void putService(String name) {
689 putService(name, 0, null);
692 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
693 String url = "/service";
694 String body = createServiceJson(name, keepAliveIntervalSeconds);
695 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
696 if (expectedStatus != null) {
697 assertEquals(expectedStatus, resp.getStatusCode(), "");
701 private String baseUrl() {
702 return "https://localhost:" + port;
705 private String jsonString() {
706 return "{\"servingCellNrcgi\":\"1\"}";
710 public void testConcurrency() throws Exception {
711 final Instant startTime = Instant.now();
712 List<Thread> threads = new ArrayList<>();
713 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
715 addPolicyType("type1", "ric");
716 addPolicyType("type2", "ric");
718 for (int i = 0; i < 10; ++i) {
720 new Thread(new ConcurrencyTestRunnable(baseUrl(), supervision, a1ClientFactory, rics, policyTypes),
725 for (Thread t : threads) {
728 assertThat(policies.size()).isEqualTo(0);
729 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
732 private AsyncRestClient restClient(boolean useTrustValidation) {
733 WebClientConfig config = this.applicationConfig.getWebClientConfig();
734 config = ImmutableWebClientConfig.builder() //
735 .isTrustStoreUsed(useTrustValidation) //
736 .trustStore(config.trustStore()) //
737 .trustStorePassword(config.trustStorePassword()) //
740 return new AsyncRestClient(baseUrl(), config);
743 private AsyncRestClient restClient() {
744 return restClient(false);
747 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
748 testErrorCode(request, expStatus, "");
751 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
752 StepVerifier.create(request) //
753 .expectSubscription() //
754 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
758 private boolean checkWebClientError(Throwable t, HttpStatus expStatus, String responseContains) {
759 assertTrue(t instanceof WebClientResponseException);
760 WebClientResponseException e = (WebClientResponseException) t;
761 assertThat(e.getStatusCode()).isEqualTo(expStatus);
762 assertThat(e.getResponseBodyAsString()).contains(responseContains);
766 private MockA1Client getA1Client(String ricName) throws ServiceException {
767 return a1ClientFactory.getOrCreateA1Client(ricName);
770 private PolicyType createPolicyType(String policyTypeName) {
771 return ImmutablePolicyType.builder() //
772 .name(policyTypeName) //
773 .schema("{\"title\":\"" + policyTypeName + "\"}") //
777 private PolicyType addPolicyType(String policyTypeName, String ricName) {
778 PolicyType type = createPolicyType(policyTypeName);
779 policyTypes.put(type);
780 addRic(ricName).addSupportedPolicyType(type);
784 private Ric addRic(String ricName) {
785 return addRic(ricName, null);
788 private Ric addRic(String ricName, String managedElement) {
789 if (rics.get(ricName) != null) {
790 return rics.get(ricName);
792 List<String> mes = new ArrayList<>();
793 if (managedElement != null) {
794 mes.add(managedElement);
796 RicConfig conf = ImmutableRicConfig.builder() //
799 .managedElementIds(mes) //
800 .controllerName("") //
802 Ric ric = new Ric(conf);
803 ric.setState(Ric.RicState.AVAILABLE);
808 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
809 List<T> result = new ArrayList<>();
810 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
811 for (JsonElement jsonElement : jsonArr) {
812 T o = gson.fromJson(jsonElement.toString(), clazz);
818 private static List<String> parseSchemas(String jsonString) {
819 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
820 List<String> result = new ArrayList<>();
821 for (JsonElement schemaObject : arrayOfSchema) {
822 result.add(schemaObject.toString());