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.Test;
44 import org.junit.jupiter.api.extension.ExtendWith;
45 import org.onap.ccsdk.oran.a1policymanagementservice.clients.A1ClientFactory;
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.Lock.LockType;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
63 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
64 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
65 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
66 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
67 import org.slf4j.Logger;
68 import org.slf4j.LoggerFactory;
69 import org.springframework.beans.factory.annotation.Autowired;
70 import org.springframework.boot.test.context.SpringBootTest;
71 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
72 import org.springframework.boot.test.context.TestConfiguration;
73 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
74 import org.springframework.boot.web.server.LocalServerPort;
75 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
76 import org.springframework.context.ApplicationContext;
77 import org.springframework.context.annotation.Bean;
78 import org.springframework.http.HttpStatus;
79 import org.springframework.http.ResponseEntity;
80 import org.springframework.test.context.TestPropertySource;
81 import org.springframework.test.context.junit.jupiter.SpringExtension;
82 import org.springframework.web.reactive.function.client.WebClientResponseException;
84 import reactor.core.publisher.Mono;
85 import reactor.test.StepVerifier;
86 import reactor.util.annotation.Nullable;
88 @ExtendWith(SpringExtension.class)
89 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
90 @TestPropertySource(properties = { //
91 "server.ssl.key-store=./config/keystore.jks", //
92 "app.webclient.trust-store=./config/truststore.jks", //
93 "app.vardata-directory=./target"})
94 class ApplicationTest {
95 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
98 ApplicationContext context;
104 private Policies policies;
107 private PolicyTypes policyTypes;
110 MockA1ClientFactory a1ClientFactory;
113 RicSupervision supervision;
116 ApplicationConfig applicationConfig;
121 private static Gson gson = new GsonBuilder().create();
123 public static class MockApplicationConfig extends ApplicationConfig {
125 public String getLocalConfigurationFilePath() {
126 return ""; // No config file loaded for the test
132 * Overrides the BeanFactory.
135 static class TestBeanFactory {
138 public ApplicationConfig getApplicationConfig() {
139 return new MockApplicationConfig();
143 MockA1ClientFactory getA1ClientFactory(@Autowired ApplicationConfig appConfig, @Autowired PolicyTypes types) {
144 return new MockA1ClientFactory(appConfig, types);
148 public ServiceSupervision getServiceSupervision(@Autowired Services services,
149 @Autowired A1ClientFactory a1ClientFactory, @Autowired Policies policies) {
150 Duration checkInterval = Duration.ofMillis(1);
151 return new ServiceSupervision(services, policies, a1ClientFactory, checkInterval);
155 public ServletWebServerFactory servletContainer() {
156 return new TomcatServletWebServerFactory();
170 a1ClientFactory.reset();
174 void verifyNoRicLocks() {
175 for (Ric ric : this.rics.getRics()) {
176 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
177 ric.getLock().unlockBlocking();
178 assertThat(ric.getLock().getLockCounter()).isZero();
179 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
184 void testGetRics() throws Exception {
186 this.addPolicyType("type1", "ric1");
187 String url = "/rics?policyType=type1";
188 String rsp = restClient().get(url).block();
189 assertThat(rsp).contains("ric1");
191 // nameless type for ORAN A1 1.1
193 this.addPolicyType("", "ric2");
194 url = "/rics?policyType=";
196 // This tests also validation of trusted certs restClient(true)
197 rsp = restClient(true).get(url).block();
198 assertThat(rsp).contains("ric2") //
199 .doesNotContain("ric1") //
200 .contains("AVAILABLE");
203 rsp = restClient().get("/rics").block();
204 assertThat(rsp).contains("ric2") //
207 // Non existing policy type
208 url = "/rics?policyType=XXXX";
209 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
213 void testSynchronization() throws Exception {
214 // Two polictypes will be put in the NearRT RICs
215 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes(this.applicationConfig);
216 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
217 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
218 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
220 // One type and one instance added to the Policy Management Service's storage
221 final String ric1Name = "ric1";
222 Ric ric1 = addRic(ric1Name);
223 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
224 Ric ric2 = addRic("ric2");
226 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
227 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
229 String policyId = "policyId";
230 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
231 supervision.checkAllRics(); // The created policy should be put in the RIC
233 // Wait until synch is completed
234 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
235 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
236 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
238 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
239 assertThat(ricPolicies.size()).isEqualTo(1);
240 Policy ricPolicy = ricPolicies.get(policyId);
241 assertThat(ricPolicy.getJson()).isEqualTo(policy.getJson());
243 // Both types should be in the Policy Management Service's storage after the
245 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
246 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
250 void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
251 String ricName = "ric1";
252 String managedElementId = "kista_1";
253 addRic(ricName, managedElementId);
255 String url = "/ric?managedElementId=" + managedElementId;
256 String rsp = restClient().get(url).block();
257 assertThat(rsp).isEqualTo(ricName);
259 // test GET RIC for ManagedElement that does not exist
260 url = "/ric?managedElementId=" + "junk";
261 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
264 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId,
265 boolean isTransient) {
267 if (policyTypeName.isEmpty()) {
268 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
270 url = "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
274 url += "&transient=true";
279 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
280 return putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, false);
284 void testPutPolicy() throws Exception {
285 String serviceName = "service1";
286 String ricName = "ric1";
287 String policyTypeName = "type1";
288 String policyInstanceId = "instance1";
290 putService(serviceName);
291 addPolicyType(policyTypeName, ricName);
293 // PUT a transient policy
294 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId, true);
295 final String policyBody = jsonString();
296 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
298 restClient().put(url, policyBody).block();
300 Policy policy = policies.getPolicy(policyInstanceId);
301 assertThat(policy).isNotNull();
302 assertThat(policy.getId()).isEqualTo(policyInstanceId);
303 assertThat(policy.getOwnerServiceId()).isEqualTo(serviceName);
304 assertThat(policy.getRic().id()).isEqualTo("ric1");
305 assertThat(policy.isTransient()).isTrue();
307 // Put a non transient policy
308 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
309 restClient().put(url, policyBody).block();
310 policy = policies.getPolicy(policyInstanceId);
311 assertThat(policy.isTransient()).isFalse();
314 String rsp = restClient().get(url).block();
315 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
317 url = "/policy?id=" + policyInstanceId;
318 rsp = restClient().get(url).block();
319 assertThat(rsp).isEqualTo(policyBody);
321 // Test of error codes
322 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
323 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
325 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
326 addPolicyType(policyTypeName + "XX", "otherRic");
327 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
329 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
330 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
331 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
332 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
337 * Test that HttpStatus and body from failing REST call to A1 is passed on to
340 * @throws ServiceException
342 void testErrorFromRic() throws ServiceException {
343 putService("service1");
344 addPolicyType("type1", "ric1");
346 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
347 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
348 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
349 String responseBody = "Refused";
350 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
352 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
353 responseBodyBytes, StandardCharsets.UTF_8, null);
354 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
357 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
360 this.addPolicy("instance1", "type1", "service1", "ric1");
361 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
362 testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
365 this.addPolicy("instance1", "type1", "service1", "ric1");
366 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
367 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
369 // Check that empty response body is OK
370 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
371 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
372 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
376 void testPutTypelessPolicy() throws Exception {
377 putService("service1");
378 addPolicyType("", "ric1");
379 String url = putPolicyUrl("service1", "ric1", "", "id1");
380 restClient().put(url, jsonString()).block();
382 String rsp = restClient().get("/policies").block();
383 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
384 assertThat(info).hasSize(1);
385 PolicyInfo policyInfo = info.get(0);
386 assertThat(policyInfo.id).isEqualTo("id1");
387 assertThat(policyInfo.type).isEmpty();
391 void testRefuseToUpdatePolicy() throws Exception {
392 // Test that only the json can be changed for a already created policy
393 // In this case service is attempted to be changed
395 this.addRic("ricXXX");
396 this.addPolicy("instance1", "type1", "service1", "ric1");
397 this.addPolicy("instance2", "type1", "service1", "ricXXX");
399 // Try change ric1 -> ricXXX
400 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
401 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
405 void testGetPolicy() throws Exception {
406 String url = "/policy?id=id";
407 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
409 String rsp = restClient().get(url).block();
410 assertThat(rsp).isEqualTo(policy.getJson());
413 policies.remove(policy);
414 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
419 void testDeletePolicy() throws Exception {
420 addPolicy("id", "typeName", "service1", "ric1");
421 assertThat(policies.size()).isEqualTo(1);
423 String url = "/policy?id=id";
424 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
426 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
427 assertThat(policies.size()).isZero();
429 // Delete a non existing policy
430 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
434 void testGetPolicySchemas() throws Exception {
435 addPolicyType("type1", "ric1");
436 addPolicyType("type2", "ric2");
438 String url = "/policy_schemas";
439 String rsp = this.restClient().get(url).block();
440 assertThat(rsp).contains("type1") //
441 .contains("{\"title\":\"type2\"}");
443 List<String> info = parseSchemas(rsp);
444 assertThat(info).hasSize(2);
446 url = "/policy_schemas?ric=ric1";
447 rsp = restClient().get(url).block();
448 assertThat(rsp).contains("type1");
449 info = parseSchemas(rsp);
450 assertThat(info).hasSize(1);
452 // Get schema for non existing RIC
453 url = "/policy_schemas?ric=ric1XXX";
454 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
458 void testGetPolicySchema() throws Exception {
459 addPolicyType("type1", "ric1");
460 addPolicyType("type2", "ric2");
462 String url = "/policy_schema?id=type1";
463 String rsp = restClient().get(url).block();
465 assertThat(rsp).contains("type1") //
468 // Get non existing schema
469 url = "/policy_schema?id=type1XX";
470 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
474 void testGetPolicyTypes() throws Exception {
475 addPolicyType("type1", "ric1");
476 addPolicyType("type2", "ric2");
478 String url = "/policy_types";
479 String rsp = restClient().get(url).block();
480 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
482 url = "/policy_types?ric=ric1";
483 rsp = restClient().get(url).block();
484 assertThat(rsp).isEqualTo("[\"type1\"]");
486 // Get policy types for non existing RIC
487 url = "/policy_types?ric=ric1XXX";
488 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
492 void testGetPolicies() throws Exception {
493 addPolicy("id1", "type1", "service1");
495 String url = "/policies";
496 String rsp = restClient().get(url).block();
498 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
499 assertThat(info).hasSize(1);
500 PolicyInfo policyInfo = info.get(0);
501 assert (policyInfo.validate());
502 assertThat(policyInfo.id).isEqualTo("id1");
503 assertThat(policyInfo.type).isEqualTo("type1");
504 assertThat(policyInfo.service).isEqualTo("service1");
508 void testGetPoliciesFilter() throws Exception {
509 addPolicy("id1", "type1", "service1");
510 addPolicy("id2", "type1", "service2");
511 addPolicy("id3", "type2", "service1");
513 String url = "/policies?type=type1";
514 String rsp = restClient().get(url).block();
516 assertThat(rsp).contains("id1") //
518 .doesNotContain("id3");
520 url = "/policies?type=type1&service=service2";
521 rsp = restClient().get(url).block();
523 assertThat(rsp).doesNotContain("id1") //
525 .doesNotContain("id3");
527 // Test get policies for non existing type
528 url = "/policies?type=type1XXX";
529 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
531 // Test get policies for non existing RIC
532 url = "/policies?ric=XXX";
533 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
537 void testGetPolicyIdsFilter() throws Exception {
538 addPolicy("id1", "type1", "service1", "ric1");
539 addPolicy("id2", "type1", "service2", "ric1");
540 addPolicy("id3", "type2", "service1", "ric1");
542 String url = "/policy_ids?type=type1";
543 String rsp = restClient().get(url).block();
545 assertThat(rsp).contains("id1") //
547 .doesNotContain("id3");
549 url = "/policy_ids?type=type1&service=service1&ric=ric1";
550 rsp = restClient().get(url).block();
551 assertThat(rsp).isEqualTo("[\"id1\"]");
553 // Test get policy ids for non existing type
554 url = "/policy_ids?type=type1XXX";
555 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
557 // Test get policy ids for non existing RIC
558 url = "/policy_ids?ric=XXX";
559 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
563 void testPutAndGetService() throws Exception {
565 String serviceName = "name";
566 putService(serviceName, 0, HttpStatus.CREATED);
567 putService(serviceName, 0, HttpStatus.OK);
570 String url = "/services?name=name";
571 String rsp = restClient().get(url).block();
572 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
573 assertThat(info).hasSize(1);
574 ServiceStatus status = info.iterator().next();
575 assertThat(status.keepAliveIntervalSeconds).isZero();
576 assertThat(status.serviceName).isEqualTo(serviceName);
580 rsp = restClient().get(url).block();
581 assertThat(rsp).as("Response contains service name").contains(serviceName);
585 url = "/services/keepalive?name=name";
586 ResponseEntity<String> entity = restClient().putForEntity(url).block();
587 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
590 assertThat(services.size()).isEqualTo(1);
591 url = "/services?name=name";
592 restClient().delete(url).block();
593 assertThat(services.size()).isZero();
595 // Keep alive, no registered service
596 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
598 // PUT servive with bad payload
599 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
600 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
601 testErrorCode(restClient().put("/service", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST);
602 testErrorCode(restClient().put("/service", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
603 HttpStatus.BAD_REQUEST);
605 // GET non existing service
606 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
610 void testServiceSupervision() throws Exception {
611 putService("service1", 1, HttpStatus.CREATED);
612 addPolicyType("type1", "ric1");
614 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
615 final String policyBody = jsonString();
616 restClient().put(url, policyBody).block();
618 assertThat(policies.size()).isEqualTo(1);
619 assertThat(services.size()).isEqualTo(1);
621 // Timeout after ~1 second
622 await().untilAsserted(() -> assertThat(policies.size()).isZero());
623 assertThat(services.size()).isZero();
627 void testGetPolicyStatus() throws Exception {
628 addPolicy("id", "typeName", "service1", "ric1");
629 assertThat(policies.size()).isEqualTo(1);
631 String url = "/policy_status?id=id";
632 String rsp = restClient().get(url).block();
633 assertThat(rsp).isEqualTo("OK");
635 // GET non existing policy status
636 url = "/policy_status?id=XXX";
637 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
640 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
642 Policy policy = Policy.builder() //
644 .json(jsonString()) //
645 .ownerServiceId(service) //
646 .ric(rics.getRic(ric)) //
647 .type(addPolicyType(typeName, ric)) //
648 .lastModified(Instant.now()) //
649 .isTransient(false) //
650 .statusNotificationUri("/policy_status?id=XXX") //
652 policies.put(policy);
656 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
657 return addPolicy(id, typeName, service, "ric");
660 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
661 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
664 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
665 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
667 String json = gson.toJson(service);
671 private void putService(String name) {
672 putService(name, 0, null);
675 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
676 String url = "/service";
677 String body = createServiceJson(name, keepAliveIntervalSeconds);
678 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
679 if (expectedStatus != null) {
680 assertEquals(expectedStatus, resp.getStatusCode(), "");
684 private String baseUrl() {
685 return "https://localhost:" + port;
688 private String jsonString() {
689 return "{\"servingCellNrcgi\":\"1\"}";
692 private AsyncRestClient restClient(boolean useTrustValidation) {
693 WebClientConfig config = this.applicationConfig.getWebClientConfig();
694 config = ImmutableWebClientConfig.builder() //
695 .keyStoreType(config.keyStoreType()) //
696 .keyStorePassword(config.keyStorePassword()) //
697 .keyStore(config.keyStore()) //
698 .keyPassword(config.keyPassword()) //
699 .isTrustStoreUsed(useTrustValidation) //
700 .trustStore(config.trustStore()) //
701 .trustStorePassword(config.trustStorePassword()) //
702 .httpProxyConfig(config.httpProxyConfig()) //
705 AsyncRestClientFactory f = new AsyncRestClientFactory(config);
706 return f.createRestClientNoHttpProxy(baseUrl());
709 private AsyncRestClient restClient() {
710 return restClient(false);
713 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
714 testErrorCode(request, expStatus, "");
717 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
718 StepVerifier.create(request) //
719 .expectSubscription() //
720 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
724 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains) {
725 assertTrue(throwable instanceof WebClientResponseException);
726 WebClientResponseException responseException = (WebClientResponseException) throwable;
727 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
728 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
732 private MockA1Client getA1Client(String ricName) throws ServiceException {
733 return a1ClientFactory.getOrCreateA1Client(ricName);
736 private PolicyType createPolicyType(String policyTypeName) {
737 return PolicyType.builder() //
738 .id(policyTypeName) //
739 .schema("{\"title\":\"" + policyTypeName + "\"}") //
743 private PolicyType addPolicyType(String policyTypeName, String ricName) {
744 PolicyType type = createPolicyType(policyTypeName);
745 policyTypes.put(type);
746 addRic(ricName).addSupportedPolicyType(type);
750 private Ric addRic(String ricName) {
751 return addRic(ricName, null);
754 private Ric addRic(String ricName, String managedElement) {
755 if (rics.get(ricName) != null) {
756 return rics.get(ricName);
758 List<String> mes = new ArrayList<>();
759 if (managedElement != null) {
760 mes.add(managedElement);
762 RicConfig conf = ImmutableRicConfig.builder() //
765 .managedElementIds(mes) //
766 .controllerName("") //
768 Ric ric = new Ric(conf);
769 ric.setState(Ric.RicState.AVAILABLE);
774 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
775 List<T> result = new ArrayList<>();
776 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
777 for (JsonElement jsonElement : jsonArr) {
778 T json = gson.fromJson(jsonElement.toString(), clazz);
784 private static List<String> parseSchemas(String jsonString) {
785 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
786 List<String> result = new ArrayList<>();
787 for (JsonElement schemaObject : arrayOfSchema) {
788 result.add(schemaObject.toString());