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.v2;
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;
33 import java.nio.charset.StandardCharsets;
34 import java.time.Duration;
35 import java.time.Instant;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.onap.ccsdk.oran.a1policymanagementservice.clients.AsyncRestClient;
45 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ApplicationConfig;
46 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
47 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableWebClientConfig;
48 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
49 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.WebClientConfig;
50 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
51 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
52 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
53 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
54 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
55 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
56 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
57 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
58 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
59 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
60 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
62 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
63 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
64 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
65 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68 import org.springframework.beans.factory.annotation.Autowired;
69 import org.springframework.boot.test.context.SpringBootTest;
70 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
71 import org.springframework.boot.test.context.TestConfiguration;
72 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
73 import org.springframework.boot.web.server.LocalServerPort;
74 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
75 import org.springframework.context.ApplicationContext;
76 import org.springframework.context.annotation.Bean;
77 import org.springframework.http.HttpStatus;
78 import org.springframework.http.MediaType;
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 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()).isZero();
200 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
205 void testGetRics() throws Exception {
207 this.addPolicyType("type1", "ric1");
208 String url = "/rics?policytype_id=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_id=";
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_id=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 (Near-RT 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 waitForRicState(ric1Name, RicState.SYNCHRONIZING);
256 waitForRicState(ric1Name, RicState.AVAILABLE);
257 waitForRicState("ric2", RicState.AVAILABLE);
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 testGetRic() throws Exception {
272 String ricId = "ric1";
273 String managedElementId = "kista_1";
274 addRic(ricId, managedElementId);
276 String url = "/ric?managed_element_id=" + managedElementId;
277 String rsp = restClient().get(url).block();
278 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
279 assertThat(ricInfo.ricId).isEqualTo(ricId);
281 url = "/ric?ric_id=" + ricId;
282 rsp = restClient().get(url).block();
283 ricInfo = gson.fromJson(rsp, RicInfo.class);
284 assertThat(ricInfo.ricId).isEqualTo(ricId);
286 // test GET RIC for ManagedElement that does not exist
287 url = "/ric?managed_element_id=" + "junk";
288 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
291 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
294 private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
295 boolean isTransient) {
297 if (policyTypeName.isEmpty()) {
298 url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName;
300 url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName
301 + "&policytype_id=" + policyTypeName;
304 url += "&transient=true";
309 private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
310 return putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, false);
314 void testPutPolicy() throws Exception {
315 String serviceName = "service1";
316 String ricId = "ric1";
317 String policyTypeName = "type1";
318 String policyInstanceId = "instance1";
320 putService(serviceName);
321 addPolicyType(policyTypeName, ricId);
323 // PUT a transient policy
324 String url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, true);
325 final String policyBody = jsonString();
326 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
328 restClient().put(url, policyBody).block();
330 Policy policy = policies.getPolicy(policyInstanceId);
331 assertThat(policy).isNotNull();
332 assertThat(policy.id()).isEqualTo(policyInstanceId);
333 assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
334 assertThat(policy.ric().id()).isEqualTo("ric1");
335 assertThat(policy.isTransient()).isTrue();
337 // Put a non transient policy
338 url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
339 restClient().put(url, policyBody).block();
340 policy = policies.getPolicy(policyInstanceId);
341 assertThat(policy.isTransient()).isFalse();
344 String rsp = restClient().get(url).block();
345 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
347 url = "/policy?policy_id=" + policyInstanceId;
348 rsp = restClient().get(url).block();
349 assertThat(rsp).isEqualTo(policyBody);
351 // Test of error codes
352 url = putPolicyUrl(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
353 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
355 url = putPolicyUrl(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
356 addPolicyType(policyTypeName + "XX", "otherRic");
357 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
359 url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
360 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
361 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
362 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
367 * Test that HttpStatus and body from failing REST call to A1 is passed on to
370 * @throws ServiceException
372 void testErrorFromRic() throws ServiceException {
373 putService("service1");
374 addPolicyType("type1", "ric1");
376 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
377 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
378 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
379 String responseBody = "Refused";
380 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
382 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
383 responseBodyBytes, StandardCharsets.UTF_8, null);
384 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
387 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
390 this.addPolicy("instance1", "type1", "service1", "ric1");
391 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
392 testErrorCode(restClient().delete("/policy?policy_id=instance1"), httpStatus, responseBody);
395 this.addPolicy("instance1", "type1", "service1", "ric1");
396 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
397 testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus, responseBody);
399 // Check that empty response body is OK
400 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
401 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
402 testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus);
406 void testPutTypelessPolicy() throws Exception {
407 putService("service1");
408 addPolicyType("", "ric1");
409 String url = putPolicyUrl("service1", "ric1", "", "id1");
410 restClient().put(url, jsonString()).block();
412 String rsp = restClient().get("/policies").block();
413 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
414 assertThat(info.policies).hasSize(1);
415 PolicyInfo policyInfo = info.policies.iterator().next();
416 assertThat(policyInfo.policyId).isEqualTo("id1");
417 assertThat(policyInfo.policyTypeId).isEmpty();
421 void testRefuseToUpdatePolicy() throws Exception {
422 // Test that only the json can be changed for a already created policy
423 // In this case service is attempted to be changed
425 this.addRic("ricXXX");
426 this.addPolicy("instance1", "type1", "service1", "ric1");
427 this.addPolicy("instance2", "type1", "service1", "ricXXX");
429 // Try change ric1 -> ricXXX
430 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
431 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
435 void testGetPolicy() throws Exception {
436 String url = "/policy?policy_id=id";
437 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
439 String rsp = restClient().get(url).block();
440 assertThat(rsp).isEqualTo(policy.json());
443 policies.remove(policy);
444 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
449 void testDeletePolicy() throws Exception {
450 addPolicy("id", "typeName", "service1", "ric1");
451 assertThat(policies.size()).isEqualTo(1);
453 String url = "/policy?policy_id=id";
454 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
456 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
457 assertThat(policies.size()).isZero();
459 // Delete a non existing policy
460 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
464 void testGetPolicySchemas() throws Exception {
465 addPolicyType("type1", "ric1");
466 addPolicyType("type2", "ric2");
468 waitForRicState("ric1", RicState.AVAILABLE);
469 waitForRicState("ric2", RicState.AVAILABLE);
471 String url = "/policy-schemas";
472 String rsp = this.restClient().get(url).block();
473 assertThat(rsp).contains("type1") //
474 .contains("{\"title\":\"type2\"}");
476 PolicySchemaList info = parseSchemas(rsp);
477 assertThat(info.schemas).hasSize(2);
479 url = "/policy-schemas?ric_id=ric1";
480 rsp = restClient().get(url).block();
481 assertThat(rsp).contains("type1");
482 info = parseSchemas(rsp);
483 assertThat(info.schemas).hasSize(1);
486 url = "/policy-schemas?policytype_id=type1";
487 rsp = restClient().get(url).block();
488 assertThat(rsp).contains("type1") //
491 // Both type and ric specified
492 url = "/policy-schemas?ric_id=ric1&policytype_id=type1";
493 rsp = restClient().get(url).block();
494 PolicySchemaList list = gson.fromJson(rsp, PolicySchemaList.class);
495 assertThat(list.schemas).hasSize(1);
497 url = "/policy-schemas?ric_id=ric1&policytype_id=type2";
498 rsp = restClient().get(url).block();
499 list = gson.fromJson(rsp, PolicySchemaList.class);
500 assertThat(list.schemas).isEmpty();
502 // Get schema for non existing RIC
503 url = "/policy-schemas?ric_id=ric1XXX";
504 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
506 // Get non existing schema
507 url = "/policy-schemas?policytype_id=type1XX";
508 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
511 String createPolicyTypesJson(String... types) {
512 List<String> list = new ArrayList<>();
513 Collections.addAll(list, types);
514 PolicyTypeIdList ids = new PolicyTypeIdList(list);
515 return gson.toJson(ids);
519 void testGetPolicyTypes() throws Exception {
520 addPolicyType("type1", "ric1");
521 addPolicyType("type2", "ric2");
523 String url = "/policy-types";
524 String rsp = restClient().get(url).block();
525 String expResp = createPolicyTypesJson("type2", "type1");
526 assertThat(rsp).isEqualTo(expResp);
528 url = "/policy-types?ric_id=ric1";
529 rsp = restClient().get(url).block();
530 expResp = createPolicyTypesJson("type1");
531 assertThat(rsp).isEqualTo(expResp);
533 // Get policy types for non existing RIC
534 url = "/policy-types?ric_id=ric1XXX";
535 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
539 void testGetPolicies() throws Exception {
540 addPolicy("id1", "type1", "service1");
542 String url = "/policies";
543 String rsp = restClient().get(url).block();
545 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
546 assertThat(info.policies).hasSize(1);
547 PolicyInfo policyInfo = info.policies.iterator().next();
548 assert (policyInfo.validate());
549 assertThat(policyInfo.policyId).isEqualTo("id1");
550 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
551 assertThat(policyInfo.serviceId).isEqualTo("service1");
555 void testGetPoliciesFilter() throws Exception {
556 addPolicy("id1", "type1", "service1");
557 addPolicy("id2", "type1", "service2");
558 addPolicy("id3", "type2", "service1");
560 String url = "/policies?policytype_id=type1";
561 String rsp = restClient().get(url).block();
563 assertThat(rsp).contains("id1") //
565 .doesNotContain("id3");
567 url = "/policies?policytype_id=type1&service_id=service2";
568 rsp = restClient().get(url).block();
570 assertThat(rsp).doesNotContain("id1") //
572 .doesNotContain("id3");
574 // Test get policies for non existing type
575 url = "/policies?policytype_id=type1XXX";
576 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
578 // Test get policies for non existing RIC
579 url = "/policies?ric_id=XXX";
580 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
584 void testGetPolicyIdsFilter() throws Exception {
585 addPolicy("id1", "type1", "service1", "ric1");
586 addPolicy("id2", "type1", "service2", "ric1");
587 addPolicy("id3", "type2", "service1", "ric1");
589 String url = "/policy-ids?policytype_id=type1";
590 String rsp = restClient().get(url).block();
592 assertThat(rsp).contains("id1") //
594 .doesNotContain("id3");
596 url = "/policy-ids?policytype_id=type1&service_id=service1&ric=ric1";
597 rsp = restClient().get(url).block();
598 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
599 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
601 // Test get policy ids for non existing type
602 url = "/policy-ids?policytype_id=type1XXX";
603 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
605 // Test get policy ids for non existing RIC
606 url = "/policy-ids?ric_id=XXX";
607 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
611 void testPutAndGetService() throws Exception {
613 String serviceName = "name";
614 putService(serviceName, 0, HttpStatus.CREATED);
615 putService(serviceName, 0, HttpStatus.OK);
618 String url = "/services?service_id=name";
619 String rsp = restClient().get(url).block();
620 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
621 assertThat(info.statusList).hasSize(1);
622 ServiceStatus status = info.statusList.iterator().next();
623 assertThat(status.keepAliveIntervalSeconds).isZero();
624 assertThat(status.serviceId).isEqualTo(serviceName);
628 rsp = restClient().get(url).block();
629 assertThat(rsp).as("Response contains service name").contains(serviceName);
633 url = "/services/keepalive?service_id=name";
634 ResponseEntity<?> entity = restClient().putForEntity(url).block();
635 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
638 assertThat(services.size()).isEqualTo(1);
639 url = "/services?service_id=name";
640 restClient().delete(url).block();
641 assertThat(services.size()).isZero();
643 // Keep alive, no registered service
644 testErrorCode(restClient().put("/services/keepalive?service_id=name", ""), HttpStatus.NOT_FOUND);
646 // PUT service with bad payload
647 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
648 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
649 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
651 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
652 HttpStatus.BAD_REQUEST, false);
654 // GET non existing service
655 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
659 void testServiceSupervision() throws Exception {
660 putService("service1", 1, HttpStatus.CREATED);
661 addPolicyType("type1", "ric1");
663 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
664 final String policyBody = jsonString();
665 restClient().put(url, policyBody).block();
667 assertThat(policies.size()).isEqualTo(1);
668 assertThat(services.size()).isEqualTo(1);
670 // Timeout after ~1 second
671 await().untilAsserted(() -> assertThat(policies.size()).isZero());
672 assertThat(services.size()).isZero();
676 void testGetPolicyStatus() throws Exception {
677 addPolicy("id", "typeName", "service1", "ric1");
678 assertThat(policies.size()).isEqualTo(1);
680 String url = "/policy-status?policy_id=id";
681 String rsp = restClient().get(url).block();
682 assertThat(rsp).isEqualTo("OK");
684 // GET non existing policy status
685 url = "/policy-status?policy_id=XXX";
686 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
689 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
691 Policy policy = ImmutablePolicy.builder() //
693 .json(jsonString()) //
694 .ownerServiceId(service) //
695 .ric(rics.getRic(ric)) //
696 .type(addPolicyType(typeName, ric)) //
697 .lastModified(Instant.now()) //
698 .isTransient(false) //
700 policies.put(policy);
704 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
705 return addPolicy(id, typeName, service, "ric");
708 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
709 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
712 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
713 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
715 String json = gson.toJson(service);
719 private void putService(String name) {
720 putService(name, 0, null);
723 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
724 String url = "/services";
725 String body = createServiceJson(name, keepAliveIntervalSeconds);
726 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
727 if (expectedStatus != null) {
728 assertEquals(expectedStatus, resp.getStatusCode(), "");
732 private String baseUrl() {
733 return "https://localhost:" + port + Consts.V2_API_ROOT;
736 private String jsonString() {
737 return "{\"servingCellNrcgi\":\"1\"}";
741 void testConcurrency() throws Exception {
742 final Instant startTime = Instant.now();
743 List<Thread> threads = new ArrayList<>();
744 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
745 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
747 addPolicyType("type1", "ric");
748 addPolicyType("type2", "ric");
750 for (int i = 0; i < 10; ++i) {
751 AsyncRestClient restClient = restClient();
752 ConcurrencyTestRunnable test =
753 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
754 Thread thread = new Thread(test, "TestThread_" + i);
759 for (Thread t : threads) {
762 for (ConcurrencyTestRunnable test : tests) {
763 assertThat(test.isFailed()).isFalse();
765 assertThat(policies.size()).isZero();
766 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
769 private AsyncRestClient restClient(boolean useTrustValidation) {
770 WebClientConfig config = this.applicationConfig.getWebClientConfig();
771 config = ImmutableWebClientConfig.builder() //
772 .keyStoreType(config.keyStoreType()) //
773 .keyStorePassword(config.keyStorePassword()) //
774 .keyStore(config.keyStore()) //
775 .keyPassword(config.keyPassword()) //
776 .isTrustStoreUsed(useTrustValidation) //
777 .trustStore(config.trustStore()) //
778 .trustStorePassword(config.trustStorePassword()) //
781 return new AsyncRestClient(baseUrl(), config);
784 private AsyncRestClient restClient() {
785 return restClient(false);
788 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
789 testErrorCode(request, expStatus, "", true);
792 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
793 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
796 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
797 testErrorCode(request, expStatus, responseContains, true);
800 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
801 boolean expectApplicationProblemJsonMediaType) {
802 StepVerifier.create(request) //
803 .expectSubscription() //
805 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
809 private void waitForRicState(String ricId, RicState state) throws ServiceException {
810 Ric ric = rics.getRic(ricId);
811 await().untilAsserted(() -> state.equals(ric.getState()));
814 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
815 boolean expectApplicationProblemJsonMediaType) {
816 assertTrue(throwable instanceof WebClientResponseException);
817 WebClientResponseException responseException = (WebClientResponseException) throwable;
818 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
819 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
820 if (expectApplicationProblemJsonMediaType) {
821 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
826 private MockA1Client getA1Client(String ricId) throws ServiceException {
827 return a1ClientFactory.getOrCreateA1Client(ricId);
830 private PolicyType createPolicyType(String policyTypeName) {
831 return ImmutablePolicyType.builder() //
832 .id(policyTypeName) //
833 .schema("{\"title\":\"" + policyTypeName + "\"}") //
837 private PolicyType addPolicyType(String policyTypeName, String ricId) {
838 PolicyType type = createPolicyType(policyTypeName);
839 policyTypes.put(type);
840 addRic(ricId).addSupportedPolicyType(type);
844 private Ric addRic(String ricId) {
845 return addRic(ricId, null);
848 private Ric addRic(String ricId, String managedElement) {
849 if (rics.get(ricId) != null) {
850 return rics.get(ricId);
852 List<String> mes = new ArrayList<>();
853 if (managedElement != null) {
854 mes.add(managedElement);
856 RicConfig conf = ImmutableRicConfig.builder() //
859 .managedElementIds(mes) //
860 .controllerName("") //
862 Ric ric = new Ric(conf);
863 ric.setState(Ric.RicState.AVAILABLE);
868 private static PolicySchemaList parseSchemas(String jsonString) {
869 return gson.fromJson(jsonString, PolicySchemaList.class);