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;
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.controllers.v2.Consts;
51 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyIdList;
52 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyInfo;
53 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyInfoList;
54 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicySchemaList;
55 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.PolicyTypeIdList;
56 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.RicInfo;
57 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ServiceRegistrationInfo;
58 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ServiceStatus;
59 import org.onap.ccsdk.oran.a1policymanagementservice.controllers.v2.ServiceStatusList;
60 import org.onap.ccsdk.oran.a1policymanagementservice.exceptions.ServiceException;
61 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicy;
62 import org.onap.ccsdk.oran.a1policymanagementservice.repository.ImmutablePolicyType;
63 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Lock.LockType;
64 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policies;
65 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Policy;
66 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyType;
67 import org.onap.ccsdk.oran.a1policymanagementservice.repository.PolicyTypes;
68 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric;
69 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Ric.RicState;
70 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Rics;
71 import org.onap.ccsdk.oran.a1policymanagementservice.repository.Services;
72 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.RicSupervision;
73 import org.onap.ccsdk.oran.a1policymanagementservice.tasks.ServiceSupervision;
74 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1Client;
75 import org.onap.ccsdk.oran.a1policymanagementservice.utils.MockA1ClientFactory;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.springframework.beans.factory.annotation.Autowired;
79 import org.springframework.boot.test.context.SpringBootTest;
80 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
81 import org.springframework.boot.test.context.TestConfiguration;
82 import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
83 import org.springframework.boot.web.server.LocalServerPort;
84 import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
85 import org.springframework.context.ApplicationContext;
86 import org.springframework.context.annotation.Bean;
87 import org.springframework.http.HttpStatus;
88 import org.springframework.http.MediaType;
89 import org.springframework.http.ResponseEntity;
90 import org.springframework.test.context.TestPropertySource;
91 import org.springframework.test.context.junit.jupiter.SpringExtension;
92 import org.springframework.web.reactive.function.client.WebClientResponseException;
94 import reactor.core.publisher.Mono;
95 import reactor.test.StepVerifier;
96 import reactor.util.annotation.Nullable;
98 @ExtendWith(SpringExtension.class)
99 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
102 "server.ssl.key-store=./config/keystore.jks", //
103 "app.webclient.trust-store=./config/truststore.jks"})
104 class ApplicationTestV2 {
105 private static final Logger logger = LoggerFactory.getLogger(ApplicationTestV2.class);
108 ApplicationContext context;
114 private Policies policies;
117 private PolicyTypes policyTypes;
120 MockA1ClientFactory a1ClientFactory;
123 RicSupervision supervision;
126 ApplicationConfig applicationConfig;
131 private static Gson gson = new GsonBuilder() //
135 public static class MockApplicationConfig extends ApplicationConfig {
137 public String getLocalConfigurationFilePath() {
138 return ""; // No config file loaded for the test
143 * Overrides the BeanFactory.
146 static class TestBeanFactory {
147 private final PolicyTypes policyTypes = new PolicyTypes();
148 private final Services services = new Services();
149 private final Policies policies = new Policies();
150 MockA1ClientFactory a1ClientFactory = null;
153 public ApplicationConfig getApplicationConfig() {
154 return new MockApplicationConfig();
158 MockA1ClientFactory getA1ClientFactory() {
159 if (a1ClientFactory == null) {
160 this.a1ClientFactory = new MockA1ClientFactory(this.policyTypes);
162 return this.a1ClientFactory;
166 public PolicyTypes getPolicyTypes() {
167 return this.policyTypes;
171 Policies getPolicies() {
172 return this.policies;
176 Services getServices() {
177 return this.services;
181 public ServiceSupervision getServiceSupervision() {
182 Duration checkInterval = Duration.ofMillis(1);
183 return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
187 public ServletWebServerFactory servletContainer() {
188 return new TomcatServletWebServerFactory();
202 a1ClientFactory.reset();
206 void verifyNoRicLocks() {
207 for (Ric ric : this.rics.getRics()) {
208 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
209 ric.getLock().unlockBlocking();
210 assertThat(ric.getLock().getLockCounter()).isZero();
211 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
216 void testGetRics() throws Exception {
218 this.addPolicyType("type1", "ric1");
219 String url = "/rics?policytype_id=type1";
220 String rsp = restClient().get(url).block();
221 assertThat(rsp).contains("ric1");
223 // nameless type for ORAN A1 1.1
225 this.addPolicyType("", "ric2");
226 url = "/rics?policytype_id=";
228 // This tests also validation of trusted certs restClient(true)
229 rsp = restClient(true).get(url).block();
230 assertThat(rsp).contains("ric2") //
231 .doesNotContain("ric1") //
232 .contains("AVAILABLE");
235 rsp = restClient().get("/rics").block();
236 assertThat(rsp).contains("ric2") //
239 // Non existing policy type
240 url = "/rics?policytype_id=XXXX";
241 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
245 void testSynchronization() throws Exception {
246 // Two polictypes will be put in the NearRT RICs
247 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
248 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
249 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
250 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
252 // One type and one instance added to the Policy Management Service's storage
253 final String ric1Name = "ric1";
254 Ric ric1 = addRic(ric1Name);
255 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
256 Ric ric2 = addRic("ric2");
258 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC (Near-RT RIC)
259 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
261 String policyId = "policyId";
262 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
263 supervision.checkAllRics(); // The created policy should be put in the RIC
265 // Wait until synch is completed
266 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
267 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
268 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
270 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
271 assertThat(ricPolicies.size()).isEqualTo(1);
272 Policy ricPolicy = ricPolicies.get(policyId);
273 assertThat(ricPolicy.json()).isEqualTo(policy.json());
275 // Both types should be in the Policy Management Service's storage after the
277 assertThat(ric1.getSupportedPolicyTypes()).hasSize(2);
278 assertThat(ric2.getSupportedPolicyTypes()).hasSize(2);
282 void testGetRic() throws Exception {
283 String ricId = "ric1";
284 String managedElementId = "kista_1";
285 addRic(ricId, managedElementId);
287 String url = "/ric?managed_element_id=" + managedElementId;
288 String rsp = restClient().get(url).block();
289 RicInfo ricInfo = gson.fromJson(rsp, RicInfo.class);
290 assertThat(ricInfo.ricId).isEqualTo(ricId);
292 url = "/ric?ric_id=" + ricId;
293 rsp = restClient().get(url).block();
294 ricInfo = gson.fromJson(rsp, RicInfo.class);
295 assertThat(ricInfo.ricId).isEqualTo(ricId);
297 // test GET RIC for ManagedElement that does not exist
298 url = "/ric?managed_element_id=" + "junk";
299 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
302 testErrorCode(restClient().get(url), HttpStatus.BAD_REQUEST);
305 private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId,
306 boolean isTransient) {
308 if (policyTypeName.isEmpty()) {
309 url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName;
311 url = "/policy?policy_id=" + policyInstanceId + "&ric_id=" + ricId + "&service_id=" + serviceName
312 + "&policytype_id=" + policyTypeName;
315 url += "&transient=true";
320 private String putPolicyUrl(String serviceName, String ricId, String policyTypeName, String policyInstanceId) {
321 return putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, false);
325 void testPutPolicy() throws Exception {
326 String serviceName = "service1";
327 String ricId = "ric1";
328 String policyTypeName = "type1";
329 String policyInstanceId = "instance1";
331 putService(serviceName);
332 addPolicyType(policyTypeName, ricId);
334 // PUT a transient policy
335 String url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId, true);
336 final String policyBody = jsonString();
337 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
339 restClient().put(url, policyBody).block();
341 Policy policy = policies.getPolicy(policyInstanceId);
342 assertThat(policy).isNotNull();
343 assertThat(policy.id()).isEqualTo(policyInstanceId);
344 assertThat(policy.ownerServiceId()).isEqualTo(serviceName);
345 assertThat(policy.ric().id()).isEqualTo("ric1");
346 assertThat(policy.isTransient()).isTrue();
348 // Put a non transient policy
349 url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
350 restClient().put(url, policyBody).block();
351 policy = policies.getPolicy(policyInstanceId);
352 assertThat(policy.isTransient()).isFalse();
355 String rsp = restClient().get(url).block();
356 assertThat(rsp).as("Response contains policy instance ID.").contains(policyInstanceId);
358 url = "/policy?policy_id=" + policyInstanceId;
359 rsp = restClient().get(url).block();
360 assertThat(rsp).isEqualTo(policyBody);
362 // Test of error codes
363 url = putPolicyUrl(serviceName, ricId + "XX", policyTypeName, policyInstanceId);
364 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
366 url = putPolicyUrl(serviceName, ricId, policyTypeName + "XX", policyInstanceId);
367 addPolicyType(policyTypeName + "XX", "otherRic");
368 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
370 url = putPolicyUrl(serviceName, ricId, policyTypeName, policyInstanceId);
371 this.rics.getRic(ricId).setState(Ric.RicState.SYNCHRONIZING);
372 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
373 this.rics.getRic(ricId).setState(Ric.RicState.AVAILABLE);
378 * Test that HttpStatus and body from failing REST call to A1 is passed on to
381 * @throws ServiceException
383 void testErrorFromRic() throws ServiceException {
384 putService("service1");
385 addPolicyType("type1", "ric1");
387 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
388 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
389 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
390 String responseBody = "Refused";
391 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
393 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
394 responseBodyBytes, StandardCharsets.UTF_8, null);
395 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
398 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
401 this.addPolicy("instance1", "type1", "service1", "ric1");
402 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
403 testErrorCode(restClient().delete("/policy?policy_id=instance1"), httpStatus, responseBody);
406 this.addPolicy("instance1", "type1", "service1", "ric1");
407 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
408 testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus, responseBody);
410 // Check that empty response body is OK
411 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
412 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
413 testErrorCode(restClient().get("/policy-status?policy_id=instance1"), httpStatus);
417 void testPutTypelessPolicy() throws Exception {
418 putService("service1");
419 addPolicyType("", "ric1");
420 String url = putPolicyUrl("service1", "ric1", "", "id1");
421 restClient().put(url, jsonString()).block();
423 String rsp = restClient().get("/policies").block();
424 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
425 assertThat(info.policies).hasSize(1);
426 PolicyInfo policyInfo = info.policies.iterator().next();
427 assertThat(policyInfo.policyId).isEqualTo("id1");
428 assertThat(policyInfo.policyTypeId).isEmpty();
432 void testRefuseToUpdatePolicy() throws Exception {
433 // Test that only the json can be changed for a already created policy
434 // In this case service is attempted to be changed
436 this.addRic("ricXXX");
437 this.addPolicy("instance1", "type1", "service1", "ric1");
438 this.addPolicy("instance2", "type1", "service1", "ricXXX");
440 // Try change ric1 -> ricXXX
441 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
442 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
446 void testGetPolicy() throws Exception {
447 String url = "/policy?policy_id=id";
448 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
450 String rsp = restClient().get(url).block();
451 assertThat(rsp).isEqualTo(policy.json());
454 policies.remove(policy);
455 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
460 void testDeletePolicy() throws Exception {
461 addPolicy("id", "typeName", "service1", "ric1");
462 assertThat(policies.size()).isEqualTo(1);
464 String url = "/policy?policy_id=id";
465 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
467 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
468 assertThat(policies.size()).isZero();
470 // Delete a non existing policy
471 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
475 void testGetPolicySchemas() throws Exception {
476 addPolicyType("type1", "ric1");
477 addPolicyType("type2", "ric2");
479 String url = "/policy-schemas";
480 String rsp = this.restClient().get(url).block();
481 assertThat(rsp).contains("type1") //
482 .contains("[{\"title\":\"type2\"}");
484 PolicySchemaList info = parseSchemas(rsp);
485 assertThat(info.schemas).hasSize(2);
487 url = "/policy-schemas?ric_id=ric1";
488 rsp = restClient().get(url).block();
489 assertThat(rsp).contains("type1");
490 info = parseSchemas(rsp);
491 assertThat(info.schemas).hasSize(1);
494 url = "/policy-schemas?policytype_id=type1";
495 rsp = restClient().get(url).block();
496 assertThat(rsp).contains("type1") //
499 // Both type and ric specified
500 url = "/policy-schemas?ric_id=ric1&policytype_id=type1";
501 rsp = restClient().get(url).block();
502 PolicySchemaList list = gson.fromJson(rsp, PolicySchemaList.class);
503 assertThat(list.schemas).hasSize(1);
505 url = "/policy-schemas?ric_id=ric1&policytype_id=type2";
506 rsp = restClient().get(url).block();
507 list = gson.fromJson(rsp, PolicySchemaList.class);
508 assertThat(list.schemas).isEmpty();
510 // Get schema for non existing RIC
511 url = "/policy-schemas?ric_id=ric1XXX";
512 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
514 // Get non existing schema
515 url = "/policy-schemas?policytype_id=type1XX";
516 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
519 String createPolicyTypesJson(String... types) {
520 List<String> list = new ArrayList<>();
521 Collections.addAll(list, types);
522 PolicyTypeIdList ids = new PolicyTypeIdList(list);
523 return gson.toJson(ids);
527 void testGetPolicyTypes() throws Exception {
528 addPolicyType("type1", "ric1");
529 addPolicyType("type2", "ric2");
531 String url = "/policy-types";
532 String rsp = restClient().get(url).block();
533 String expResp = createPolicyTypesJson("type2", "type1");
534 assertThat(rsp).isEqualTo(expResp);
536 url = "/policy-types?ric_id=ric1";
537 rsp = restClient().get(url).block();
538 expResp = createPolicyTypesJson("type1");
539 assertThat(rsp).isEqualTo(expResp);
541 // Get policy types for non existing RIC
542 url = "/policy-types?ric_id=ric1XXX";
543 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
547 void testGetPolicies() throws Exception {
548 addPolicy("id1", "type1", "service1");
550 String url = "/policies";
551 String rsp = restClient().get(url).block();
553 PolicyInfoList info = gson.fromJson(rsp, PolicyInfoList.class);
554 assertThat(info.policies).hasSize(1);
555 PolicyInfo policyInfo = info.policies.iterator().next();
556 assert (policyInfo.validate());
557 assertThat(policyInfo.policyId).isEqualTo("id1");
558 assertThat(policyInfo.policyTypeId).isEqualTo("type1");
559 assertThat(policyInfo.serviceId).isEqualTo("service1");
563 void testGetPoliciesFilter() throws Exception {
564 addPolicy("id1", "type1", "service1");
565 addPolicy("id2", "type1", "service2");
566 addPolicy("id3", "type2", "service1");
568 String url = "/policies?policytype_id=type1";
569 String rsp = restClient().get(url).block();
571 assertThat(rsp).contains("id1") //
573 .doesNotContain("id3");
575 url = "/policies?policytype_id=type1&service_id=service2";
576 rsp = restClient().get(url).block();
578 assertThat(rsp).doesNotContain("id1") //
580 .doesNotContain("id3");
582 // Test get policies for non existing type
583 url = "/policies?policytype_id=type1XXX";
584 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
586 // Test get policies for non existing RIC
587 url = "/policies?ric_id=XXX";
588 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
592 void testGetPolicyIdsFilter() throws Exception {
593 addPolicy("id1", "type1", "service1", "ric1");
594 addPolicy("id2", "type1", "service2", "ric1");
595 addPolicy("id3", "type2", "service1", "ric1");
597 String url = "/policy-ids?policytype_id=type1";
598 String rsp = restClient().get(url).block();
600 assertThat(rsp).contains("id1") //
602 .doesNotContain("id3");
604 url = "/policy-ids?policytype_id=type1&service_id=service1&ric=ric1";
605 rsp = restClient().get(url).block();
606 PolicyIdList respList = gson.fromJson(rsp, PolicyIdList.class);
607 assertThat(respList.policyIds.iterator().next()).isEqualTo("id1");
609 // Test get policy ids for non existing type
610 url = "/policy-ids?policytype_id=type1XXX";
611 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
613 // Test get policy ids for non existing RIC
614 url = "/policy-ids?ric_id=XXX";
615 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
619 void testPutAndGetService() throws Exception {
621 String serviceName = "name";
622 putService(serviceName, 0, HttpStatus.CREATED);
623 putService(serviceName, 0, HttpStatus.OK);
626 String url = "/services?service_id=name";
627 String rsp = restClient().get(url).block();
628 ServiceStatusList info = gson.fromJson(rsp, ServiceStatusList.class);
629 assertThat(info.statusList).hasSize(1);
630 ServiceStatus status = info.statusList.iterator().next();
631 assertThat(status.keepAliveIntervalSeconds).isZero();
632 assertThat(status.serviceId).isEqualTo(serviceName);
636 rsp = restClient().get(url).block();
637 assertThat(rsp).as("Response contains service name").contains(serviceName);
641 url = "/services/keepalive?service_id=name";
642 ResponseEntity<?> entity = restClient().putForEntity(url).block();
643 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
646 assertThat(services.size()).isEqualTo(1);
647 url = "/services?service_id=name";
648 restClient().delete(url).block();
649 assertThat(services.size()).isZero();
651 // Keep alive, no registered service
652 testErrorCode(restClient().put("/services/keepalive?service_id=name", ""), HttpStatus.NOT_FOUND);
654 // PUT service with bad payload
655 testErrorCode(restClient().put("/services", "crap"), HttpStatus.BAD_REQUEST, false);
656 testErrorCode(restClient().put("/services", "{}"), HttpStatus.BAD_REQUEST, false);
657 testErrorCode(restClient().put("/services", createServiceJson(serviceName, -123)), HttpStatus.BAD_REQUEST,
659 testErrorCode(restClient().put("/services", createServiceJson(serviceName, 0, "missing.portandprotocol.com")),
660 HttpStatus.BAD_REQUEST, false);
662 // GET non existing service
663 testErrorCode(restClient().get("/services?service_id=XXX"), HttpStatus.NOT_FOUND);
667 void testServiceSupervision() throws Exception {
668 putService("service1", 1, HttpStatus.CREATED);
669 addPolicyType("type1", "ric1");
671 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
672 final String policyBody = jsonString();
673 restClient().put(url, policyBody).block();
675 assertThat(policies.size()).isEqualTo(1);
676 assertThat(services.size()).isEqualTo(1);
678 // Timeout after ~1 second
679 await().untilAsserted(() -> assertThat(policies.size()).isZero());
680 assertThat(services.size()).isZero();
684 void testGetPolicyStatus() throws Exception {
685 addPolicy("id", "typeName", "service1", "ric1");
686 assertThat(policies.size()).isEqualTo(1);
688 String url = "/policy-status?policy_id=id";
689 String rsp = restClient().get(url).block();
690 assertThat(rsp).isEqualTo("OK");
692 // GET non existing policy status
693 url = "/policy-status?policy_id=XXX";
694 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
697 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
699 Policy policy = ImmutablePolicy.builder() //
701 .json(jsonString()) //
702 .ownerServiceId(service) //
703 .ric(rics.getRic(ric)) //
704 .type(addPolicyType(typeName, ric)) //
705 .lastModified(Instant.now()) //
706 .isTransient(false) //
708 policies.put(policy);
712 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
713 return addPolicy(id, typeName, service, "ric");
716 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
717 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
720 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
721 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
723 String json = gson.toJson(service);
727 private void putService(String name) {
728 putService(name, 0, null);
731 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
732 String url = "/services";
733 String body = createServiceJson(name, keepAliveIntervalSeconds);
734 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
735 if (expectedStatus != null) {
736 assertEquals(expectedStatus, resp.getStatusCode(), "");
740 private String baseUrl() {
741 return "https://localhost:" + port + Consts.V2_API_ROOT;
744 private String jsonString() {
745 return "{\"servingCellNrcgi\":\"1\"}";
749 void testConcurrency() throws Exception {
750 final Instant startTime = Instant.now();
751 List<Thread> threads = new ArrayList<>();
752 List<ConcurrencyTestRunnable> tests = new ArrayList<>();
753 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
755 addPolicyType("type1", "ric");
756 addPolicyType("type2", "ric");
758 for (int i = 0; i < 10; ++i) {
759 AsyncRestClient restClient = restClient();
760 ConcurrencyTestRunnable test =
761 new ConcurrencyTestRunnable(restClient, supervision, a1ClientFactory, rics, policyTypes);
762 Thread thread = new Thread(test, "TestThread_" + i);
767 for (Thread t : threads) {
770 for (ConcurrencyTestRunnable test : tests) {
771 assertThat(test.isFailed()).isFalse();
773 assertThat(policies.size()).isZero();
774 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
777 private AsyncRestClient restClient(boolean useTrustValidation) {
778 WebClientConfig config = this.applicationConfig.getWebClientConfig();
779 config = ImmutableWebClientConfig.builder() //
780 .keyStoreType(config.keyStoreType()) //
781 .keyStorePassword(config.keyStorePassword()) //
782 .keyStore(config.keyStore()) //
783 .keyPassword(config.keyPassword()) //
784 .isTrustStoreUsed(useTrustValidation) //
785 .trustStore(config.trustStore()) //
786 .trustStorePassword(config.trustStorePassword()) //
789 return new AsyncRestClient(baseUrl(), config);
792 private AsyncRestClient restClient() {
793 return restClient(false);
796 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
797 testErrorCode(request, expStatus, "", true);
800 private void testErrorCode(Mono<?> request, HttpStatus expStatus, boolean expectApplicationProblemJsonMediaType) {
801 testErrorCode(request, expStatus, "", expectApplicationProblemJsonMediaType);
804 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
805 testErrorCode(request, expStatus, responseContains, true);
808 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains,
809 boolean expectApplicationProblemJsonMediaType) {
810 StepVerifier.create(request) //
811 .expectSubscription() //
813 t -> checkWebClientError(t, expStatus, responseContains, expectApplicationProblemJsonMediaType)) //
817 private boolean checkWebClientError(Throwable throwable, HttpStatus expStatus, String responseContains,
818 boolean expectApplicationProblemJsonMediaType) {
819 assertTrue(throwable instanceof WebClientResponseException);
820 WebClientResponseException responseException = (WebClientResponseException) throwable;
821 assertThat(responseException.getStatusCode()).isEqualTo(expStatus);
822 assertThat(responseException.getResponseBodyAsString()).contains(responseContains);
823 if (expectApplicationProblemJsonMediaType) {
824 assertThat(responseException.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_PROBLEM_JSON);
829 private MockA1Client getA1Client(String ricId) throws ServiceException {
830 return a1ClientFactory.getOrCreateA1Client(ricId);
833 private PolicyType createPolicyType(String policyTypeName) {
834 return ImmutablePolicyType.builder() //
835 .id(policyTypeName) //
836 .schema("{\"title\":\"" + policyTypeName + "\"}") //
840 private PolicyType addPolicyType(String policyTypeName, String ricId) {
841 PolicyType type = createPolicyType(policyTypeName);
842 policyTypes.put(type);
843 addRic(ricId).addSupportedPolicyType(type);
847 private Ric addRic(String ricId) {
848 return addRic(ricId, null);
851 private Ric addRic(String ricId, String managedElement) {
852 if (rics.get(ricId) != null) {
853 return rics.get(ricId);
855 List<String> mes = new ArrayList<>();
856 if (managedElement != null) {
857 mes.add(managedElement);
859 RicConfig conf = ImmutableRicConfig.builder() //
862 .managedElementIds(mes) //
863 .controllerName("") //
865 Ric ric = new Ric(conf);
866 ric.setState(Ric.RicState.AVAILABLE);
871 private static PolicySchemaList parseSchemas(String jsonString) {
872 return gson.fromJson(jsonString, PolicySchemaList.class);