2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
6 * ======================================================================
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.onap.ccsdk.oran.a1policymanagementservice.controllers.v1;
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.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
47 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClientFactory;
48 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
49 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
50 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
51 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
52 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
53 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
65 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
66 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
67 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
68 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71 import org.springframework.beans.factory.annotation.Autowired;
72 import org.springframework.boot.test.context.SpringBootTest;
73 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
74 import org.springframework.boot.test.context.TestConfiguration;
75 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
76 import org.springframework.boot.web.server.LocalServerPort;
77 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
78 import org.springframework.context.ApplicationContext;
79 import org.springframework.context.annotation.Bean;
80 import org.springframework.http.HttpStatus;
81 import org.springframework.http.ResponseEntity;
82 import org.springframework.test.context.TestPropertySource;
83 import org.springframework.test.context.junit.jupiter.SpringExtension;
84 import org.springframework.web.reactive.function.client.WebClientResponseException;
86 import reactor.core.publisher.Mono;
87 import reactor.test.StepVerifier;
88 import reactor.util.annotation.Nullable;
90 @ExtendWith(SpringExtension.class)
91 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
92 @TestPropertySource(properties = { //
93 "server.ssl.key-store=./config/keystore.jks", //
94 "app.webclient.trust-store=./config/truststore.jks"})
95 class ApplicationTest {
96 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
99 ApplicationContext context;
105 private Policies policies;
108 private PolicyTypes policyTypes;
111 MockA1ClientFactory a1ClientFactory;
114 RicSupervision supervision;
117 ApplicationConfig applicationConfig;
122 private static Gson gson = new GsonBuilder().create();
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()).isZero();
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 .doesNotContain("ric1") //
221 .contains("AVAILABLE");
224 rsp = restClient().get("/rics").block();
225 assertThat(rsp).contains("ric2") //
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 Policy Management Service's 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 Policy Management Service's storage after the
266 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
267 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
271 void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
272 String ricName = "ric1";
273 String managedElementId = "kista_1";
274 addRic(ricName, managedElementId);
276 String url = "/ric?managedElementId=" + managedElementId;
277 String rsp = restClient().get(url).block();
278 assertThat(rsp).isEqualTo(ricName);
280 // test GET RIC for ManagedElement that does not exist
281 url = "/ric?managedElementId=" + "junk";
282 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
285 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
286 boolean isTransient) {
288 if (policyTypeName.isEmpty()) {
289 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
291 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
295 url += "&transient=true";
300 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
301 return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
305 void testPutPolicy() throws Exception {
306 String serviceName = "service1";
307 String ricName = "ric1";
308 String policyTypeName = "type1";
309 String policyInstanceId = "instance1";
311 putService(serviceName);
312 addPolicyType(policyTypeName, ricName);
314 // PUT a transient policy
315 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
316 final String policyBody = jsonString();
317 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
319 restClient().put(url, policyBody).block();
321 Policy policy = policies.getPolicy(policyInstanceId);
322 assertThat(policy).isNotNull();
323 assertThat(policy.id()).isEqualTo(policyInstanceId);
324 assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
325 assertThat(policy.ric().id()).isEqualTo("ric1");
326 assertThat(policy.isTransient()).isTrue();
328 // Put a non transient policy
329 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
330 restClient().put(url, policyBody).block();
331 policy = policies.getPolicy(policyInstanceId);
332 assertThat(policy.isTransient()).isFalse();
335 String rsp = restClient().get(url).block();
336 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
338 url = "/policy?id=" + policyInstanceId;
339 rsp = restClient().get(url).block();
340 assertThat(rsp).isEqualTo(policyBody);
342 // Test of error codes
343 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
344 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
346 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
347 addPolicyType(policyTypeName + "XX", "otherRic");
348 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
350 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
351 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
352 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
353 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
358 * Test that HttpStatus and body from failing REST call to A1 is passed on to
361 * @throws ServiceException
363 void testErrorFromRic() throws ServiceException {
364 putService("service1");
365 addPolicyType("type1", "ric1");
367 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
368 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
369 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
370 String responseBody = "Refused";
371 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
373 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
374 responseBodyBytes, StandardCharsets.UTF_8, null);
375 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
378 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
381 this.addPolicy("instance1", "type1", "service1", "ric1");
382 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
383 testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
386 this.addPolicy("instance1", "type1", "service1", "ric1");
387 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
388 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
390 // Check that empty response body is OK
391 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
392 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
393 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
397 void testPutTypelessPolicy() throws Exception {
398 putService("service1");
399 addPolicyType("", "ric1");
400 String url = putPolicyUrl("service1", "ric1", "", "id1");
401 restClient().put(url, jsonString()).block();
403 String rsp = restClient().get("/policies").block();
404 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
405 assertThat(info).hasSize(1);
406 PolicyInfo policyInfo = info.get(0);
407 assertThat(policyInfo.id).isEqualTo("id1");
408 assertThat(policyInfo.type).isEmpty();
412 void testRefuseToUpdatePolicy() throws Exception {
413 // Test that only the json can be changed for a already created policy
414 // In this case service is attempted to be changed
416 this.addRic("ricXXX");
417 this.addPolicy("instance1", "type1", "service1", "ric1");
418 this.addPolicy("instance2", "type1", "service1", "ricXXX");
420 // Try change ric1 -> ricXXX
421 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
422 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
426 void testGetPolicy() throws Exception {
427 String url = "/policy?id=id";
428 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
430 String rsp = restClient().get(url).block();
431 assertThat(rsp).isEqualTo(policy.json());
434 policies.remove(policy);
435 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
440 void testDeletePolicy() throws Exception {
441 addPolicy("id", "typeName", "service1", "ric1");
442 assertThat(policies.size()).isEqualTo(1);
444 String url = "/policy?id=id";
445 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
447 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
448 assertThat(policies.size()).isZero();
450 // Delete a non existing policy
451 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
455 void testGetPolicySchemas() throws Exception {
456 addPolicyType("type1", "ric1");
457 addPolicyType("type2", "ric2");
459 String url = "/policy_schemas";
460 String rsp = this.restClient().get(url).block();
461 assertThat(rsp).contains("type1") //
462 .contains("{\"title\":\"type2\"}");
464 List<String> info = parseSchemas(rsp);
465 assertThat(info).hasSize(2);
467 url = "/policy_schemas?ric=ric1";
468 rsp = restClient().get(url).block();
469 assertThat(rsp).contains("type1");
470 info = parseSchemas(rsp);
471 assertThat(info).hasSize(1);
473 // Get schema for non existing RIC
474 url = "/policy_schemas?ric=ric1XXX";
475 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
479 void testGetPolicySchema() throws Exception {
480 addPolicyType("type1", "ric1");
481 addPolicyType("type2", "ric2");
483 String url = "/policy_schema?id=type1";
484 String rsp = restClient().get(url).block();
486 assertThat(rsp).contains("type1") //
489 // Get non existing schema
490 url = "/policy_schema?id=type1XX";
491 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
495 void testGetPolicyTypes() throws Exception {
496 addPolicyType("type1", "ric1");
497 addPolicyType("type2", "ric2");
499 String url = "/policy_types";
500 String rsp = restClient().get(url).block();
501 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
503 url = "/policy_types?ric=ric1";
504 rsp = restClient().get(url).block();
505 assertThat(rsp).isEqualTo("[\"type1\"]");
507 // Get policy types for non existing RIC
508 url = "/policy_types?ric=ric1XXX";
509 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
513 void testGetPolicies() throws Exception {
514 addPolicy("id1", "type1", "service1");
516 String url = "/policies";
517 String rsp = restClient().get(url).block();
519 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
520 assertThat(info).hasSize(1);
521 PolicyInfo policyInfo = info.get(0);
522 assert (policyInfo.validate());
523 assertThat(policyInfo.id).isEqualTo("id1");
524 assertThat(policyInfo.type).isEqualTo("type1");
525 assertThat(policyInfo.service).isEqualTo("service1");
529 void testGetPoliciesFilter() throws Exception {
530 addPolicy("id1", "type1", "service1");
531 addPolicy("id2", "type1", "service2");
532 addPolicy("id3", "type2", "service1");
534 String url = "/policies?type=type1";
535 String rsp = restClient().get(url).block();
537 assertThat(rsp).contains("id1") //
539 .doesNotContain("id3");
541 url = "/policies?type=type1&service=service2";
542 rsp = restClient().get(url).block();
544 assertThat(rsp).doesNotContain("id1") //
546 .doesNotContain("id3");
548 // Test get policies for non existing type
549 url = "/policies?type=type1XXX";
550 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
552 // Test get policies for non existing RIC
553 url = "/policies?ric=XXX";
554 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
558 void testGetPolicyIdsFilter() throws Exception {
559 addPolicy("id1", "type1", "service1", "ric1");
560 addPolicy("id2", "type1", "service2", "ric1");
561 addPolicy("id3", "type2", "service1", "ric1");
563 String url = "/policy_ids?type=type1";
564 String rsp = restClient().get(url).block();
566 assertThat(rsp).contains("id1") //
568 .doesNotContain("id3");
570 url = "/policy_ids?type=type1&service=service1&ric=ric1";
571 rsp = restClient().get(url).block();
572 assertThat(rsp).isEqualTo("[\"id1\"]");
574 // Test get policy ids for non existing type
575 url = "/policy_ids?type=type1XXX";
576 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
578 // Test get policy ids for non existing RIC
579 url = "/policy_ids?ric=XXX";
580 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
584 void testPutAndGetService() throws Exception {
586 String serviceName = "name";
587 putService(serviceName, 0, HttpStatus.CREATED);
588 putService(serviceName, 0, HttpStatus.OK);
591 String url = "/services?name=name";
592 String rsp = restClient().get(url).block();
593 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
594 assertThat(info).hasSize(1);
595 ServiceStatus status = info.iterator().next();
596 assertThat(status.keepAliveIntervalSeconds).isZero();
597 assertThat(status.serviceName).isEqualTo(serviceName);
601 rsp = restClient().get(url).block();
602 assertThat(rsp).as("Response contains service name").contains(serviceName);
606 url = "/services/keepalive?name=name";
607 ResponseEntity<String> entity = restClient().putForEntity(url).block();
608 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
611 assertThat(services.size()).isEqualTo(1);
612 url = "/services?name=name";
613 restClient().delete(url).block();
614 assertThat(services.size()).isZero();
616 // Keep alive, no registered service
617 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
619 // PUT servive with bad payload
620 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
621 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
622 testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
623 testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
624 HttpStatus.BAD_REQUEST);
626 // GET non existing service
627 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
631 void testServiceSupervision() throws Exception {
632 putService("service1", 1, HttpStatus.CREATED);
633 addPolicyType("type1", "ric1");
635 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
636 final String policyBody = jsonString();
637 restClient().put(url, policyBody).block();
639 assertThat(policies.size()).isEqualTo(1);
640 assertThat(services.size()).isEqualTo(1);
642 // Timeout after ~1 second
643 await().untilAsserted(() -> assertThat(policies.size()).isZero());
644 assertThat(services.size()).isZero();
648 void testGetPolicyStatus() throws Exception {
649 addPolicy("id", "typeName", "service1", "ric1");
650 assertThat(policies.size()).isEqualTo(1);
652 String url = "/policy_status?id=id";
653 String rsp = restClient().get(url).block();
654 assertThat(rsp).isEqualTo("OK");
656 // GET non existing policy status
657 url = "/policy_status?id=XXX";
658 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
661 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
663 Policy policy = ImmutablePolicy.builder() //
665 .json(jsonString()) //
666 .ownerServiceId(service) //
667 .ric(rics.getRic(ric)) //
668 .type(addPolicyType(typeName, ric)) //
669 .lastModified(Instant.now()) //
670 .isTransient(false) //
671 .statusNotificationUri("/policy_status?id=XXX") //
673 policies.put(policy);
677 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
678 return addPolicy(id, typeName, service, "ric");
681 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
682 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
685 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
686 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
688 String json = gson.toJson(service);
692 private void putService(String name) {
693 putService(name, 0, null);
696 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
697 String url = "/service";
698 String body = createServiceJson(name, keepAliveIntervalSeconds);
699 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
700 if (expectedStatus != null) {
701 assertEquals(expectedStatus, resp.getStatusCode(), "");
705 private String baseUrl() {
706 return "https://localhost:" + port;
709 private String jsonString() {
710 return "{\"servingCellNrcgi\":\"1\"}";
713 private AsyncRestClient restClient(boolean useTrustValidation) {
714 WebClientConfig config = this.applicationConfig.getWebClientConfig();
715 config = ImmutableWebClientConfig.builder() //
716 .keyStoreType(config.keyStoreType()) //
717 .keyStorePassword(config.keyStorePassword()) //
718 .keyStore(config.keyStore()) //
719 .keyPassword(config.keyPassword()) //
720 .isTrustStoreUsed(useTrustValidation) //
721 .trustStore(config.trustStore()) //
722 .trustStorePassword(config.trustStorePassword()) //
723 .httpProxyConfig(config.httpProxyConfig()) //
726 AsyncRestClientFactory f = new AsyncRestClientFactory(config);
727 return f.createRestClientNoHttpProxy(baseUrl());
730 private AsyncRestClient restClient() {
731 return restClient(false);
734 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
735 testErrorCode(request, expStatus, "");
738 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
739 StepVerifier.create(request) //
740 .expectSubscription() //
741 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
745 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
746 assertTrue(throwable instanceof WebClientResponseException);
747 WebClientResponseException responseException = (WebClientResponseException) throwable;
748 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
749 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
753 private MockA1Client getA1Client(String ricName) throws ServiceException {
754 return a1ClientFactory.getOrCreateA1Client(ricName);
757 private PolicyType createPolicyType(String policyTypeName) {
758 return ImmutablePolicyType.builder() //
759 .id(policyTypeName) //
760 .schema("{\"title\":\"" + policyTypeName + "\"}") //
764 private PolicyType addPolicyType(String policyTypeName, String ricName) {
765 PolicyType type = createPolicyType(policyTypeName);
766 policyTypes.put(type);
767 addRic(ricName).addSupportedPolicyType(type);
771 private Ric addRic(String ricName) {
772 return addRic(ricName, null);
775 private Ric addRic(String ricName, String managedElement) {
776 if (rics.get(ricName) != null) {
777 return rics.get(ricName);
779 List<String> mes = new ArrayList<>();
780 if (managedElement != null) {
781 mes.add(managedElement);
783 RicConfig conf = ImmutableRicConfig.builder() //
786 .managedElementIds(mes) //
787 .controllerName("") //
789 Ric ric = new Ric(conf);
790 ric.setState(Ric.RicState.AVAILABLE);
795 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
796 List<T> result = new ArrayList<>();
797 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
798 for (JsonElement jsonElement : jsonArr) {
799 T json = gson.fromJson(jsonElement.toString(), clazz);
805 private static List<String> parseSchemas(String jsonString) {
806 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
807 List<String> result = new ArrayList<>();
808 for (JsonElement schemaObject : arrayOfSchema) {
809 result.add(schemaObject.toString());