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.configuration.ApplicationConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
49 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
50 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
51 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
52 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
64 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
65 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
66 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
67 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.boot.test.context.SpringBootTest;
72 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
73 import org.springframework.boot.test.context.TestConfiguration;
74 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
75 import org.springframework.boot.web.server.LocalServerPort;
76 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
77 import org.springframework.context.ApplicationContext;
78 import org.springframework.context.annotation.Bean;
79 import org.springframework.http.HttpStatus;
80 import org.springframework.http.ResponseEntity;
81 import org.springframework.test.context.TestPropertySource;
82 import org.springframework.test.context.junit.jupiter.SpringExtension;
83 import org.springframework.web.reactive.function.client.WebClientResponseException;
85 import reactor.core.publisher.Mono;
86 import reactor.test.StepVerifier;
87 import reactor.util.annotation.Nullable;
89 @ExtendWith(SpringExtension.class)
90 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
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() //
126 public static class MockApplicationConfig extends ApplicationConfig {
128 public String getLocalConfigurationFilePath() {
129 return ""; // No config file loaded for the test
134 * Overrides the BeanFactory.
137 static class TestBeanFactory {
138 private final PolicyTypes policyTypes = new PolicyTypes();
139 private final Services services = new Services();
140 private final Policies policies = new Policies();
141 MockA1ClientFactory a1ClientFactory = null;
144 public ApplicationConfig getApplicationConfig() {
145 return new MockApplicationConfig();
149 MockA1ClientFactory getA1ClientFactory() {
150 if (a1ClientFactory == null) {
151 this.a1ClientFactory = new MockA1ClientFactory(this.policyTypes);
153 return this.a1ClientFactory;
157 public PolicyTypes getPolicyTypes() {
158 return this.policyTypes;
162 Policies getPolicies() {
163 return this.policies;
167 Services getServices() {
168 return this.services;
172 public ServiceSupervision getServiceSupervision() {
173 Duration checkInterval = Duration.ofMillis(1);
174 return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
178 public ServletWebServerFactory servletContainer() {
179 return new TomcatServletWebServerFactory();
193 a1ClientFactory.reset();
197 void verifyNoRicLocks() {
198 for (Ric ric : this.rics.getRics()) {
199 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
200 ric.getLock().unlockBlocking();
201 assertThat(ric.getLock().getLockCounter()).isZero();
202 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
207 void testGetRics() throws Exception {
209 this.addPolicyType("type1", "ric1");
210 String url = "/rics?policyType=type1";
211 String rsp = restClient().get(url).block();
212 assertThat(rsp).contains("ric1");
214 // nameless type for ORAN A1 1.1
216 this.addPolicyType("", "ric2");
217 url = "/rics?policyType=";
219 // This tests also validation of trusted certs restClient(true)
220 rsp = restClient(true).get(url).block();
221 assertThat(rsp).contains("ric2") //
222 .doesNotContain("ric1") //
223 .contains("AVAILABLE");
226 rsp = restClient().get("/rics").block();
227 assertThat(rsp).contains("ric2") //
230 // Non existing policy type
231 url = "/rics?policyType=XXXX";
232 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
236 void testSynchronization() throws Exception {
237 // Two polictypes will be put in the NearRT RICs
238 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
239 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
240 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
241 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
243 // One type and one instance added to the Policy Management Service's storage
244 final String ric1Name = "ric1";
245 Ric ric1 = addRic(ric1Name);
246 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
247 Ric ric2 = addRic("ric2");
249 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
250 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
252 String policyId = "policyId";
253 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
254 supervision.checkAllRics(); // The created policy should be put in the RIC
256 // Wait until synch is completed
257 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
258 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
259 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
261 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
262 assertThat(ricPolicies.size()).isEqualTo(1);
263 Policy ricPolicy = ricPolicies.get(policyId);
264 assertThat(ricPolicy.json()).isEqualTo(policy.json());
266 // Both types should be in the Policy Management Service's storage after the
268 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
269 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
273 void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
274 String ricName = "ric1";
275 String managedElementId = "kista_1";
276 addRic(ricName, managedElementId);
278 String url = "/ric?managedElementId=" + managedElementId;
279 String rsp = restClient().get(url).block();
280 assertThat(rsp).isEqualTo(ricName);
282 // test GET RIC for ManagedElement that does not exist
283 url = "/ric?managedElementId=" + "junk";
284 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
287 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
288 boolean isTransient) {
290 if (policyTypeName.isEmpty()) {
291 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
293 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
297 url += "&transient=true";
302 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
303 return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
307 void testPutPolicy() throws Exception {
308 String serviceName = "service1";
309 String ricName = "ric1";
310 String policyTypeName = "type1";
311 String policyInstanceId = "instance1";
313 putService(serviceName);
314 addPolicyType(policyTypeName, ricName);
316 // PUT a transient policy
317 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
318 final String policyBody = jsonString();
319 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
321 restClient().put(url, policyBody).block();
323 Policy policy = policies.getPolicy(policyInstanceId);
324 assertThat(policy).isNotNull();
325 assertThat(policy.id()).isEqualTo(policyInstanceId);
326 assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
327 assertThat(policy.ric().id()).isEqualTo("ric1");
328 assertThat(policy.isTransient()).isTrue();
330 // Put a non transient policy
331 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
332 restClient().put(url, policyBody).block();
333 policy = policies.getPolicy(policyInstanceId);
334 assertThat(policy.isTransient()).isFalse();
337 String rsp = restClient().get(url).block();
338 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
340 url = "/policy?id=" + policyInstanceId;
341 rsp = restClient().get(url).block();
342 assertThat(rsp).isEqualTo(policyBody);
344 // Test of error codes
345 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
346 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
348 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
349 addPolicyType(policyTypeName + "XX", "otherRic");
350 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
352 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
353 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
354 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
355 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
360 * Test that HttpStatus and body from failing REST call to A1 is passed on to
363 * @throws ServiceException
365 void testErrorFromRic() throws ServiceException {
366 putService("service1");
367 addPolicyType("type1", "ric1");
369 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
370 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
371 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
372 String responseBody = "Refused";
373 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
375 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
376 responseBodyBytes, StandardCharsets.UTF_8, null);
377 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
380 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
383 this.addPolicy("instance1", "type1", "service1", "ric1");
384 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
385 testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
388 this.addPolicy("instance1", "type1", "service1", "ric1");
389 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
390 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
392 // Check that empty response body is OK
393 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
394 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
395 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
399 void testPutTypelessPolicy() throws Exception {
400 putService("service1");
401 addPolicyType("", "ric1");
402 String url = putPolicyUrl("service1", "ric1", "", "id1");
403 restClient().put(url, jsonString()).block();
405 String rsp = restClient().get("/policies").block();
406 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
407 assertThat(info).hasSize(1);
408 PolicyInfo policyInfo = info.get(0);
409 assertThat(policyInfo.id).isEqualTo("id1");
410 assertThat(policyInfo.type).isEmpty();
414 void testRefuseToUpdatePolicy() throws Exception {
415 // Test that only the json can be changed for a already created policy
416 // In this case service is attempted to be changed
418 this.addRic("ricXXX");
419 this.addPolicy("instance1", "type1", "service1", "ric1");
420 this.addPolicy("instance2", "type1", "service1", "ricXXX");
422 // Try change ric1 -> ricXXX
423 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
424 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
428 void testGetPolicy() throws Exception {
429 String url = "/policy?id=id";
430 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
432 String rsp = restClient().get(url).block();
433 assertThat(rsp).isEqualTo(policy.json());
436 policies.remove(policy);
437 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
442 void testDeletePolicy() throws Exception {
443 addPolicy("id", "typeName", "service1", "ric1");
444 assertThat(policies.size()).isEqualTo(1);
446 String url = "/policy?id=id";
447 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
449 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
450 assertThat(policies.size()).isZero();
452 // Delete a non existing policy
453 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
457 void testGetPolicySchemas() throws Exception {
458 addPolicyType("type1", "ric1");
459 addPolicyType("type2", "ric2");
461 String url = "/policy_schemas";
462 String rsp = this.restClient().get(url).block();
463 assertThat(rsp).contains("type1") //
464 .contains("[{\"title\":\"type2\"}");
466 List<String> info = parseSchemas(rsp);
467 assertThat(info).hasSize(2);
469 url = "/policy_schemas?ric=ric1";
470 rsp = restClient().get(url).block();
471 assertThat(rsp).contains("type1");
472 info = parseSchemas(rsp);
473 assertThat(info).hasSize(1);
475 // Get schema for non existing RIC
476 url = "/policy_schemas?ric=ric1XXX";
477 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
481 void testGetPolicySchema() throws Exception {
482 addPolicyType("type1", "ric1");
483 addPolicyType("type2", "ric2");
485 String url = "/policy_schema?id=type1";
486 String rsp = restClient().get(url).block();
488 assertThat(rsp).contains("type1") //
491 // Get non existing schema
492 url = "/policy_schema?id=type1XX";
493 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
497 void testGetPolicyTypes() throws Exception {
498 addPolicyType("type1", "ric1");
499 addPolicyType("type2", "ric2");
501 String url = "/policy_types";
502 String rsp = restClient().get(url).block();
503 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
505 url = "/policy_types?ric=ric1";
506 rsp = restClient().get(url).block();
507 assertThat(rsp).isEqualTo("[\"type1\"]");
509 // Get policy types for non existing RIC
510 url = "/policy_types?ric=ric1XXX";
511 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
515 void testGetPolicies() throws Exception {
516 addPolicy("id1", "type1", "service1");
518 String url = "/policies";
519 String rsp = restClient().get(url).block();
521 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
522 assertThat(info).hasSize(1);
523 PolicyInfo policyInfo = info.get(0);
524 assert (policyInfo.validate());
525 assertThat(policyInfo.id).isEqualTo("id1");
526 assertThat(policyInfo.type).isEqualTo("type1");
527 assertThat(policyInfo.service).isEqualTo("service1");
531 void testGetPoliciesFilter() throws Exception {
532 addPolicy("id1", "type1", "service1");
533 addPolicy("id2", "type1", "service2");
534 addPolicy("id3", "type2", "service1");
536 String url = "/policies?type=type1";
537 String rsp = restClient().get(url).block();
539 assertThat(rsp).contains("id1") //
541 .doesNotContain("id3");
543 url = "/policies?type=type1&service=service2";
544 rsp = restClient().get(url).block();
546 assertThat(rsp).doesNotContain("id1") //
548 .doesNotContain("id3");
550 // Test get policies for non existing type
551 url = "/policies?type=type1XXX";
552 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
554 // Test get policies for non existing RIC
555 url = "/policies?ric=XXX";
556 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
560 void testGetPolicyIdsFilter() throws Exception {
561 addPolicy("id1", "type1", "service1", "ric1");
562 addPolicy("id2", "type1", "service2", "ric1");
563 addPolicy("id3", "type2", "service1", "ric1");
565 String url = "/policy_ids?type=type1";
566 String rsp = restClient().get(url).block();
568 assertThat(rsp).contains("id1") //
570 .doesNotContain("id3");
572 url = "/policy_ids?type=type1&service=service1&ric=ric1";
573 rsp = restClient().get(url).block();
574 assertThat(rsp).isEqualTo("[\"id1\"]");
576 // Test get policy ids for non existing type
577 url = "/policy_ids?type=type1XXX";
578 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
580 // Test get policy ids for non existing RIC
581 url = "/policy_ids?ric=XXX";
582 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
586 void testPutAndGetService() throws Exception {
588 String serviceName = "name";
589 putService(serviceName, 0, HttpStatus.CREATED);
590 putService(serviceName, 0, HttpStatus.OK);
593 String url = "/services?name=name";
594 String rsp = restClient().get(url).block();
595 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
596 assertThat(info).hasSize(1);
597 ServiceStatus status = info.iterator().next();
598 assertThat(status.keepAliveIntervalSeconds).isZero();
599 assertThat(status.serviceName).isEqualTo(serviceName);
603 rsp = restClient().get(url).block();
604 assertThat(rsp).as("Response contains service name").contains(serviceName);
608 url = "/services/keepalive?name=name";
609 ResponseEntity<String> entity = restClient().putForEntity(url).block();
610 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
613 assertThat(services.size()).isEqualTo(1);
614 url = "/services?name=name";
615 restClient().delete(url).block();
616 assertThat(services.size()).isZero();
618 // Keep alive, no registered service
619 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
621 // PUT servive with bad payload
622 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
623 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
624 testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
625 testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
626 HttpStatus.BAD_REQUEST);
628 // GET non existing service
629 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
633 void testServiceSupervision() throws Exception {
634 putService("service1", 1, HttpStatus.CREATED);
635 addPolicyType("type1", "ric1");
637 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
638 final String policyBody = jsonString();
639 restClient().put(url, policyBody).block();
641 assertThat(policies.size()).isEqualTo(1);
642 assertThat(services.size()).isEqualTo(1);
644 // Timeout after ~1 second
645 await().untilAsserted(() -> assertThat(policies.size()).isZero());
646 assertThat(services.size()).isZero();
650 void testGetPolicyStatus() throws Exception {
651 addPolicy("id", "typeName", "service1", "ric1");
652 assertThat(policies.size()).isEqualTo(1);
654 String url = "/policy_status?id=id";
655 String rsp = restClient().get(url).block();
656 assertThat(rsp).isEqualTo("OK");
658 // GET non existing policy status
659 url = "/policy_status?id=XXX";
660 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
663 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
665 Policy policy = ImmutablePolicy.builder() //
667 .json(jsonString()) //
668 .ownerServiceId(service) //
669 .ric(rics.getRic(ric)) //
670 .type(addPolicyType(typeName, ric)) //
671 .lastModified(Instant.now()) //
672 .isTransient(false) //
674 policies.put(policy);
678 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
679 return addPolicy(id, typeName, service, "ric");
682 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
683 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
686 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
687 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
689 String json = gson.toJson(service);
693 private void putService(String name) {
694 putService(name, 0, null);
697 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
698 String url = "/service";
699 String body = createServiceJson(name, keepAliveIntervalSeconds);
700 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
701 if (expectedStatus != null) {
702 assertEquals(expectedStatus, resp.getStatusCode(), "");
706 private String baseUrl() {
707 return "https://localhost:" + port;
710 private String jsonString() {
711 return "{\"servingCellNrcgi\":\"1\"}";
714 private AsyncRestClient restClient(boolean useTrustValidation) {
715 WebClientConfig config = this.applicationConfig.getWebClientConfig();
716 config = ImmutableWebClientConfig.builder() //
717 .keyStoreType(config.keyStoreType()) //
718 .keyStorePassword(config.keyStorePassword()) //
719 .keyStore(config.keyStore()) //
720 .keyPassword(config.keyPassword()) //
721 .isTrustStoreUsed(useTrustValidation) //
722 .trustStore(config.trustStore()) //
723 .trustStorePassword(config.trustStorePassword()) //
726 return new AsyncRestClient(baseUrl(), config);
729 private AsyncRestClient restClient() {
730 return restClient(false);
733 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
734 testErrorCode(request, expStatus, "");
737 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
738 StepVerifier.create(request) //
739 .expectSubscription() //
740 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
744 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
745 assertTrue(throwable instanceof WebClientResponseException);
746 WebClientResponseException responseException = (WebClientResponseException) throwable;
747 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
748 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
752 private MockA1Client getA1Client(String ricName) throws ServiceException {
753 return a1ClientFactory.getOrCreateA1Client(ricName);
756 private PolicyType createPolicyType(String policyTypeName) {
757 return ImmutablePolicyType.builder() //
758 .id(policyTypeName) //
759 .schema("{\"title\":\"" + policyTypeName + "\"}") //
763 private PolicyType addPolicyType(String policyTypeName, String ricName) {
764 PolicyType type = createPolicyType(policyTypeName);
765 policyTypes.put(type);
766 addRic(ricName).addSupportedPolicyType(type);
770 private Ric addRic(String ricName) {
771 return addRic(ricName, null);
774 private Ric addRic(String ricName, String managedElement) {
775 if (rics.get(ricName) != null) {
776 return rics.get(ricName);
778 List<String> mes = new ArrayList<>();
779 if (managedElement != null) {
780 mes.add(managedElement);
782 RicConfig conf = ImmutableRicConfig.builder() //
785 .managedElementIds(mes) //
786 .controllerName("") //
788 Ric ric = new Ric(conf);
789 ric.setState(Ric.RicState.AVAILABLE);
794 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
795 List<T> result = new ArrayList<>();
796 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
797 for (JsonElement jsonElement : jsonArr) {
798 T json = gson.fromJson(jsonElement.toString(), clazz);
804 private static List<String> parseSchemas(String jsonString) {
805 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
806 List<String> result = new ArrayList<>();
807 for (JsonElement schemaObject : arrayOfSchema) {
808 result.add(schemaObject.toString());