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 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();
191 a1ClientFactory.reset();
195 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 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 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 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 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)).as("Response contains policy instance ID.").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 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 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).isEqualTo("id1");
406 assertThat(policyInfo.type).isEqualTo("");
410 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 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 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 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 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 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 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 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 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 void testPutAndGetService() throws Exception {
584 String serviceName = "name";
585 putService(serviceName, 0, HttpStatus.CREATED);
586 putService(serviceName, 0, HttpStatus.OK);
589 String url = "/services?name=name";
590 String rsp = restClient().get(url).block();
591 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
592 assertThat(info.size()).isEqualTo(1);
593 ServiceStatus status = info.iterator().next();
594 assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
595 assertThat(status.serviceName).isEqualTo(serviceName);
599 rsp = restClient().get(url).block();
600 assertThat(rsp.contains(serviceName)).as("Response contains service name").isTrue();
604 url = "/services/keepalive?name=name";
605 ResponseEntity<String> entity = restClient().putForEntity(url).block();
606 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
609 assertThat(services.size()).isEqualTo(1);
610 url = "/services?name=name";
611 restClient().delete(url).block();
612 assertThat(services.size()).isEqualTo(0);
614 // Keep alive, no registered service
615 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
617 // PUT servive with bad payload
618 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
619 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
620 testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
621 testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
622 HttpStatus.BAD_REQUEST);
624 // GET non existing service
625 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
629 void testServiceSupervision() throws Exception {
630 putService("service1", 1, HttpStatus.CREATED);
631 addPolicyType("type1", "ric1");
633 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
634 final String policyBody = jsonString();
635 restClient().put(url, policyBody).block();
637 assertThat(policies.size()).isEqualTo(1);
638 assertThat(services.size()).isEqualTo(1);
640 // Timeout after ~1 second
641 await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
642 assertThat(services.size()).isEqualTo(0);
646 void testGetPolicyStatus() throws Exception {
647 addPolicy("id", "typeName", "service1", "ric1");
648 assertThat(policies.size()).isEqualTo(1);
650 String url = "/policy_status?id=id";
651 String rsp = restClient().get(url).block();
652 assertThat(rsp).isEqualTo("OK");
654 // GET non existing policy status
655 url = "/policy_status?id=XXX";
656 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
659 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
661 Policy policy = ImmutablePolicy.builder() //
663 .json(jsonString()) //
664 .ownerServiceName(service) //
665 .ric(rics.getRic(ric)) //
666 .type(addPolicyType(typeName, ric)) //
667 .lastModified("lastModified") //
668 .isTransient(false) //
670 policies.put(policy);
674 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
675 return addPolicy(id, typeName, service, "ric");
678 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
679 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
682 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
683 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
685 String json = gson.toJson(service);
689 private void putService(String name) {
690 putService(name, 0, null);
693 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
694 String url = "/service";
695 String body = createServiceJson(name, keepAliveIntervalSeconds);
696 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
697 if (expectedStatus != null) {
698 assertEquals(expectedStatus, resp.getStatusCode(), "");
702 private String baseUrl() {
703 return "https://localhost:" + port;
706 private String jsonString() {
707 return "{\"servingCellNrcgi\":\"1\"}";
711 void testConcurrency() throws Exception {
712 final Instant startTime = Instant.now();
713 List<Thread> threads = new ArrayList<>();
714 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
716 addPolicyType("type1", "ric");
717 addPolicyType("type2", "ric");
719 for (int i = 0; i < 10; ++i) {
721 new Thread(new ConcurrencyTestRunnable(baseUrl(), supervision, a1ClientFactory, rics, policyTypes),
726 for (Thread t : threads) {
729 assertThat(policies.size()).isEqualTo(0);
730 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
733 private AsyncRestClient restClient(boolean useTrustValidation) {
734 WebClientConfig config = this.applicationConfig.getWebClientConfig();
735 config = ImmutableWebClientConfig.builder() //
736 .isTrustStoreUsed(useTrustValidation) //
737 .trustStore(config.trustStore()) //
738 .trustStorePassword(config.trustStorePassword()) //
741 return new AsyncRestClient(baseUrl(), config);
744 private AsyncRestClient restClient() {
745 return restClient(false);
748 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
749 testErrorCode(request, expStatus, "");
752 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
753 StepVerifier.create(request) //
754 .expectSubscription() //
755 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
759 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
760 assertTrue(throwable instanceof WebClientResponseException);
761 WebClientResponseException responseException = (WebClientResponseException) throwable;
762 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
763 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
767 private MockA1Client getA1Client(String ricName) throws ServiceException {
768 return a1ClientFactory.getOrCreateA1Client(ricName);
771 private PolicyType createPolicyType(String policyTypeName) {
772 return ImmutablePolicyType.builder() //
773 .name(policyTypeName) //
774 .schema("{\"title\":\"" + policyTypeName + "\"}") //
778 private PolicyType addPolicyType(String policyTypeName, String ricName) {
779 PolicyType type = createPolicyType(policyTypeName);
780 policyTypes.put(type);
781 addRic(ricName).addSupportedPolicyType(type);
785 private Ric addRic(String ricName) {
786 return addRic(ricName, null);
789 private Ric addRic(String ricName, String managedElement) {
790 if (rics.get(ricName) != null) {
791 return rics.get(ricName);
793 List<String> mes = new ArrayList<>();
794 if (managedElement != null) {
795 mes.add(managedElement);
797 RicConfig conf = ImmutableRicConfig.builder() //
800 .managedElementIds(mes) //
801 .controllerName("") //
803 Ric ric = new Ric(conf);
804 ric.setState(Ric.RicState.AVAILABLE);
809 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
810 List<T> result = new ArrayList<>();
811 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
812 for (JsonElement jsonElement : jsonArr) {
813 T json = gson.fromJson(jsonElement.toString(), clazz);
819 private static List<String> parseSchemas(String jsonString) {
820 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
821 List<String> result = new ArrayList<>();
822 for (JsonElement schemaObject : arrayOfSchema) {
823 result.add(schemaObject.toString());