2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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.oransc.policyagent;
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.oransc.policyagent.clients.AsyncRestClient;
47 import org.oransc.policyagent.configuration.ApplicationConfig;
48 import org.oransc.policyagent.configuration.ImmutableRicConfig;
49 import org.oransc.policyagent.configuration.RicConfig;
50 import org.oransc.policyagent.controllers.PolicyInfo;
51 import org.oransc.policyagent.controllers.ServiceRegistrationInfo;
52 import org.oransc.policyagent.controllers.ServiceStatus;
53 import org.oransc.policyagent.exceptions.ServiceException;
54 import org.oransc.policyagent.repository.ImmutablePolicy;
55 import org.oransc.policyagent.repository.ImmutablePolicyType;
56 import org.oransc.policyagent.repository.Lock.LockType;
57 import org.oransc.policyagent.repository.Policies;
58 import org.oransc.policyagent.repository.Policy;
59 import org.oransc.policyagent.repository.PolicyType;
60 import org.oransc.policyagent.repository.PolicyTypes;
61 import org.oransc.policyagent.repository.Ric;
62 import org.oransc.policyagent.repository.Ric.RicState;
63 import org.oransc.policyagent.repository.Rics;
64 import org.oransc.policyagent.repository.Services;
65 import org.oransc.policyagent.tasks.RicSupervision;
66 import org.oransc.policyagent.tasks.ServiceSupervision;
67 import org.oransc.policyagent.utils.MockA1Client;
68 import org.oransc.policyagent.utils.MockA1ClientFactory;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71 import org.springframework.beans.factory.annotation.Autowired;
72 import org.springframework.boot.test.context.SpringBootTest;
73 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
74 import org.springframework.boot.test.context.TestConfiguration;
75 import org.springframework.boot.web.server.LocalServerPort;
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.junit.jupiter.SpringExtension;
81 import org.springframework.web.reactive.function.client.WebClientResponseException;
83 import reactor.core.publisher.Mono;
84 import reactor.test.StepVerifier;
85 import reactor.util.annotation.Nullable;
87 @ExtendWith(SpringExtension.class)
88 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
89 public class ApplicationTest {
90 private static final Logger logger = LoggerFactory.getLogger(ApplicationTest.class);
93 ApplicationContext context;
99 private Policies policies;
102 private PolicyTypes policyTypes;
105 MockA1ClientFactory a1ClientFactory;
108 RicSupervision supervision;
113 private static Gson gson = new GsonBuilder() //
117 public static class MockApplicationConfig extends ApplicationConfig {
119 public String getLocalConfigurationFilePath() {
120 return ""; // No config file loaded for the test
125 * Overrides the BeanFactory.
128 static class TestBeanFactory {
129 private final PolicyTypes policyTypes = new PolicyTypes();
130 private final Services services = new Services();
131 private final Policies policies = new Policies();
132 MockA1ClientFactory a1ClientFactory = null;
135 public ApplicationConfig getApplicationConfig() {
136 return new MockApplicationConfig();
140 MockA1ClientFactory getA1ClientFactory() {
141 if (a1ClientFactory == null) {
142 this.a1ClientFactory = new MockA1ClientFactory(this.policyTypes);
144 return this.a1ClientFactory;
148 public PolicyTypes getPolicyTypes() {
149 return this.policyTypes;
153 Policies getPolicies() {
154 return this.policies;
158 Services getServices() {
159 return this.services;
163 public ServiceSupervision getServiceSupervision() {
164 Duration checkInterval = Duration.ofMillis(1);
165 return new ServiceSupervision(this.services, this.policies, this.getA1ClientFactory(), checkInterval);
173 public void reset() {
178 a1ClientFactory.reset();
182 public void verifyNoRicLocks() {
183 for (Ric ric : this.rics.getRics()) {
184 ric.getLock().lockBlocking(LockType.EXCLUSIVE);
185 ric.getLock().unlockBlocking();
186 assertThat(ric.getLock().getLockCounter()).isEqualTo(0);
187 assertThat(ric.getState()).isEqualTo(Ric.RicState.AVAILABLE);
192 public void testGetRics() throws Exception {
194 this.addPolicyType("type1", "ric1");
195 String url = "/rics?policyType=type1";
196 String rsp = restClient().get(url).block();
197 assertThat(rsp).contains("ric1");
199 // nameless type for ORAN A1 1.1
201 this.addPolicyType("", "ric2");
202 url = "/rics?policyType=";
203 rsp = restClient().get(url).block();
204 assertThat(rsp).contains("ric2");
205 assertThat(rsp).doesNotContain("ric1");
206 assertThat(rsp).contains("AVAILABLE");
209 rsp = restClient().get("/rics").block();
210 assertThat(rsp).contains("ric2");
211 assertThat(rsp).contains("ric1");
213 // Non existing policy type
214 url = "/rics?policyType=XXXX";
215 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
219 public void testSynchronization() throws Exception {
220 // Two polictypes will be put in the NearRT RICs
221 PolicyTypes nearRtRicPolicyTypes = new PolicyTypes();
222 nearRtRicPolicyTypes.put(createPolicyType("typeName"));
223 nearRtRicPolicyTypes.put(createPolicyType("typeName2"));
224 this.a1ClientFactory.setPolicyTypes(nearRtRicPolicyTypes);
226 // One type and one instance added to the agent storage
227 final String ric1Name = "ric1";
228 Ric ric1 = addRic(ric1Name);
229 Policy policy2 = addPolicy("policyId2", "typeName", "service", ric1Name);
230 Ric ric2 = addRic("ric2");
232 getA1Client(ric1Name).putPolicy(policy2); // put it in the RIC
233 policies.remove(policy2); // Remove it from the repo -> should be deleted in the RIC
235 String policyId = "policyId";
236 Policy policy = addPolicy(policyId, "typeName", "service", ric1Name); // This should be created in the RIC
237 supervision.checkAllRics(); // The created policy should be put in the RIC
239 // Wait until synch is completed
240 await().untilAsserted(() -> RicState.SYNCHRONIZING.equals(rics.getRic(ric1Name).getState()));
241 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic(ric1Name).getState()));
242 await().untilAsserted(() -> RicState.AVAILABLE.equals(rics.getRic("ric2").getState()));
244 Policies ricPolicies = getA1Client(ric1Name).getPolicies();
245 assertThat(ricPolicies.size()).isEqualTo(1);
246 Policy ricPolicy = ricPolicies.get(policyId);
247 assertThat(ricPolicy.json()).isEqualTo(policy.json());
249 // Both types should be in the agent storage after the synch
250 assertThat(ric1.getSupportedPolicyTypes().size()).isEqualTo(2);
251 assertThat(ric2.getSupportedPolicyTypes().size()).isEqualTo(2);
255 public void testGetRicForManagedElement_thenReturnCorrectRic() throws Exception {
256 String ricName = "ric1";
257 String managedElementId = "kista_1";
258 addRic(ricName, managedElementId);
260 String url = "/ric?managedElementId=" + managedElementId;
261 String rsp = restClient().get(url).block();
262 assertThat(rsp).isEqualTo(ricName);
264 // test GET RIC for ManagedElement that does not exist
265 url = "/ric?managedElementId=" + "junk";
266 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
269 private String putPolicyUrl(String serviceName, String ricName, String policyTypeName, String policyInstanceId) {
270 if (policyTypeName.isEmpty()) {
271 return "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName;
273 return "/policy?id=" + policyInstanceId + "&ric=" + ricName + "&service=" + serviceName + "&type="
279 public void testPutPolicy() throws Exception {
280 String serviceName = "service1";
281 String ricName = "ric1";
282 String policyTypeName = "type1";
283 String policyInstanceId = "instance1";
285 putService(serviceName);
286 addPolicyType(policyTypeName, ricName);
288 String url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
289 final String policyBody = jsonString();
290 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
292 restClient().put(url, policyBody).block();
294 Policy policy = policies.getPolicy(policyInstanceId);
295 assertThat(policy).isNotNull();
296 assertThat(policy.id()).isEqualTo(policyInstanceId);
297 assertThat(policy.ownerServiceName()).isEqualTo(serviceName);
298 assertThat(policy.ric().name()).isEqualTo("ric1");
301 String rsp = restClient().get(url).block();
302 assertThat(rsp.contains(policyInstanceId)).isTrue();
304 // Test of error codes
305 url = putPolicyUrl(serviceName, ricName + "XX", policyTypeName, policyInstanceId);
306 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
308 url = putPolicyUrl(serviceName, ricName, policyTypeName + "XX", policyInstanceId);
309 testErrorCode(restClient().put(url, policyBody), HttpStatus.NOT_FOUND);
311 url = putPolicyUrl(serviceName, ricName, policyTypeName, policyInstanceId);
312 this.rics.getRic(ricName).setState(Ric.RicState.SYNCHRONIZING);
313 testErrorCode(restClient().put(url, policyBody), HttpStatus.LOCKED);
314 this.rics.getRic(ricName).setState(Ric.RicState.AVAILABLE);
319 * Test that HttpStatus and body from failing REST call to A1 is passed on to
322 * @throws ServiceException
324 public void testErrorFromRIC() throws ServiceException {
325 putService("service1");
326 addPolicyType("type1", "ric1");
328 String url = putPolicyUrl("service1", "ric1", "type1", "id1");
329 MockA1Client a1Client = a1ClientFactory.getOrCreateA1Client("ric1");
330 HttpStatus httpStatus = HttpStatus.INTERNAL_SERVER_ERROR;
331 String responseBody = "Refused";
332 byte[] responseBodyBytes = responseBody.getBytes(StandardCharsets.UTF_8);
334 WebClientResponseException a1Exception = new WebClientResponseException(httpStatus.value(), "statusText", null,
335 responseBodyBytes, StandardCharsets.UTF_8, null);
336 doReturn(Mono.error(a1Exception)).when(a1Client).putPolicy(any());
339 testErrorCode(restClient().put(url, "{}"), httpStatus, responseBody);
342 this.addPolicy("instance1", "type1", "service1", "ric1");
343 doReturn(Mono.error(a1Exception)).when(a1Client).deletePolicy(any());
344 testErrorCode(restClient().delete("/policy?id=instance1"), httpStatus, responseBody);
347 this.addPolicy("instance1", "type1", "service1", "ric1");
348 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
349 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus, responseBody);
351 // Check that empty response body is OK
352 a1Exception = new WebClientResponseException(httpStatus.value(), "", null, null, null, null);
353 doReturn(Mono.error(a1Exception)).when(a1Client).getPolicyStatus(any());
354 testErrorCode(restClient().get("/policy_status?id=instance1"), httpStatus);
358 public void testPutTypelessPolicy() throws Exception {
359 putService("service1");
360 addPolicyType("", "ric1");
361 String url = putPolicyUrl("service1", "ric1", "", "id1");
362 restClient().put(url, jsonString()).block();
364 String rsp = restClient().get("/policies").block();
365 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
366 assertThat(info).size().isEqualTo(1);
367 PolicyInfo policyInfo = info.get(0);
368 assertThat(policyInfo.id.equals("id1")).isTrue();
369 assertThat(policyInfo.type.equals("")).isTrue();
373 public void testRefuseToUpdatePolicy() throws Exception {
374 // Test that only the json can be changed for a already created policy
375 // In this case service is attempted to be changed
377 this.addRic("ricXXX");
378 this.addPolicy("instance1", "type1", "service1", "ric1");
379 this.addPolicy("instance2", "type1", "service1", "ricXXX");
381 // Try change ric1 -> ricXXX
382 String urlWrongRic = putPolicyUrl("service1", "ricXXX", "type1", "instance1");
383 testErrorCode(restClient().put(urlWrongRic, jsonString()), HttpStatus.CONFLICT);
387 public void testGetPolicy() throws Exception {
388 String url = "/policy?id=id";
389 Policy policy = addPolicy("id", "typeName", "service1", "ric1");
391 String rsp = restClient().get(url).block();
392 assertThat(rsp).isEqualTo(policy.json());
395 policies.remove(policy);
396 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
401 public void testDeletePolicy() throws Exception {
402 addPolicy("id", "typeName", "service1", "ric1");
403 assertThat(policies.size()).isEqualTo(1);
405 String url = "/policy?id=id";
406 ResponseEntity<String> entity = restClient().deleteForEntity(url).block();
408 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
409 assertThat(policies.size()).isEqualTo(0);
411 // Delete a non existing policy
412 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
416 public void testGetPolicySchemas() throws Exception {
417 addPolicyType("type1", "ric1");
418 addPolicyType("type2", "ric2");
420 String url = "/policy_schemas";
421 String rsp = this.restClient().get(url).block();
422 assertThat(rsp).contains("type1");
423 assertThat(rsp).contains("[{\"title\":\"type2\"}");
425 List<String> info = parseSchemas(rsp);
426 assertThat(info.size()).isEqualTo(2);
428 url = "/policy_schemas?ric=ric1";
429 rsp = restClient().get(url).block();
430 assertThat(rsp).contains("type1");
431 info = parseSchemas(rsp);
432 assertThat(info.size()).isEqualTo(1);
434 // Get schema for non existing RIC
435 url = "/policy_schemas?ric=ric1XXX";
436 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
440 public void testGetPolicySchema() throws Exception {
441 addPolicyType("type1", "ric1");
442 addPolicyType("type2", "ric2");
444 String url = "/policy_schema?id=type1";
445 String rsp = restClient().get(url).block();
447 assertThat(rsp).contains("type1");
448 assertThat(rsp).contains("title");
450 // Get non existing schema
451 url = "/policy_schema?id=type1XX";
452 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
456 public void testGetPolicyTypes() throws Exception {
457 addPolicyType("type1", "ric1");
458 addPolicyType("type2", "ric2");
460 String url = "/policy_types";
461 String rsp = restClient().get(url).block();
462 assertThat(rsp).isEqualTo("[\"type2\",\"type1\"]");
464 url = "/policy_types?ric=ric1";
465 rsp = restClient().get(url).block();
466 assertThat(rsp).isEqualTo("[\"type1\"]");
468 // Get policy types for non existing RIC
469 url = "/policy_types?ric=ric1XXX";
470 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
474 public void testGetPolicies() throws Exception {
475 addPolicy("id1", "type1", "service1");
477 String url = "/policies";
478 String rsp = restClient().get(url).block();
480 List<PolicyInfo> info = parseList(rsp, PolicyInfo.class);
481 assertThat(info).size().isEqualTo(1);
482 PolicyInfo policyInfo = info.get(0);
483 assert (policyInfo.validate());
484 assertThat(policyInfo.id).isEqualTo("id1");
485 assertThat(policyInfo.type).isEqualTo("type1");
486 assertThat(policyInfo.service).isEqualTo("service1");
490 public void testGetPoliciesFilter() throws Exception {
491 addPolicy("id1", "type1", "service1");
492 addPolicy("id2", "type1", "service2");
493 addPolicy("id3", "type2", "service1");
495 String url = "/policies?type=type1";
496 String rsp = restClient().get(url).block();
498 assertThat(rsp).contains("id1");
499 assertThat(rsp).contains("id2");
500 assertThat(rsp.contains("id3")).isFalse();
502 url = "/policies?type=type1&service=service2";
503 rsp = restClient().get(url).block();
505 assertThat(rsp.contains("id1")).isFalse();
506 assertThat(rsp).contains("id2");
507 assertThat(rsp.contains("id3")).isFalse();
509 // Test get policies for non existing type
510 url = "/policies?type=type1XXX";
511 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
513 // Test get policies for non existing RIC
514 url = "/policies?ric=XXX";
515 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
519 public void testGetPolicyIdsFilter() throws Exception {
520 addPolicy("id1", "type1", "service1", "ric1");
521 addPolicy("id2", "type1", "service2", "ric1");
522 addPolicy("id3", "type2", "service1", "ric1");
524 String url = "/policy_ids?type=type1";
525 String rsp = restClient().get(url).block();
527 assertThat(rsp).contains("id1");
528 assertThat(rsp).contains("id2");
529 assertThat(rsp.contains("id3")).isFalse();
531 url = "/policy_ids?type=type1&service=service1&ric=ric1";
532 rsp = restClient().get(url).block();
533 assertThat(rsp).isEqualTo("[\"id1\"]");
535 // Test get policy ids for non existing type
536 url = "/policy_ids?type=type1XXX";
537 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
539 // Test get policy ids for non existing RIC
540 url = "/policy_ids?ric=XXX";
541 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
545 public void testPutAndGetService() throws Exception {
547 putService("name", 0, HttpStatus.CREATED);
548 putService("name", 0, HttpStatus.OK);
551 String url = "/services?name=name";
552 String rsp = restClient().get(url).block();
553 List<ServiceStatus> info = parseList(rsp, ServiceStatus.class);
554 assertThat(info.size()).isEqualTo(1);
555 ServiceStatus status = info.iterator().next();
556 assertThat(status.keepAliveIntervalSeconds).isEqualTo(0);
557 assertThat(status.serviceName).isEqualTo("name");
561 rsp = restClient().get(url).block();
562 assertThat(rsp.contains("name")).isTrue();
566 url = "/services/keepalive?name=name";
567 ResponseEntity<String> entity = restClient().putForEntity(url).block();
568 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
571 assertThat(services.size()).isEqualTo(1);
572 url = "/services?name=name";
573 restClient().delete(url).block();
574 assertThat(services.size()).isEqualTo(0);
576 // Keep alive, no registerred service
577 testErrorCode(restClient().put("/services/keepalive?name=name", ""), HttpStatus.NOT_FOUND);
579 // PUT servive with bad payload
580 testErrorCode(restClient().put("/service", "crap"), HttpStatus.BAD_REQUEST);
581 testErrorCode(restClient().put("/service", "{}"), HttpStatus.BAD_REQUEST);
582 testErrorCode(restClient().put("/service", createServiceJson("name", -123)), HttpStatus.BAD_REQUEST);
583 testErrorCode(restClient().put("/service", createServiceJson("name", 0, "missing.portandprotocol.com")),
584 HttpStatus.BAD_REQUEST);
586 // GET non existing servive
587 testErrorCode(restClient().get("/services?name=XXX"), HttpStatus.NOT_FOUND);
591 public void testServiceSupervision() throws Exception {
592 putService("service1", 1, HttpStatus.CREATED);
593 addPolicyType("type1", "ric1");
595 String url = putPolicyUrl("service1", "ric1", "type1", "instance1");
596 final String policyBody = jsonString();
597 restClient().put(url, policyBody).block();
599 assertThat(policies.size()).isEqualTo(1);
600 assertThat(services.size()).isEqualTo(1);
602 // Timeout after ~1 second
603 await().untilAsserted(() -> assertThat(policies.size()).isEqualTo(0));
604 assertThat(services.size()).isEqualTo(0);
608 public void testGetPolicyStatus() throws Exception {
609 addPolicy("id", "typeName", "service1", "ric1");
610 assertThat(policies.size()).isEqualTo(1);
612 String url = "/policy_status?id=id";
613 String rsp = restClient().get(url).block();
614 assertThat(rsp.equals("OK")).isTrue();
616 // GET non existing policy status
617 url = "/policy_status?id=XXX";
618 testErrorCode(restClient().get(url), HttpStatus.NOT_FOUND);
621 private Policy addPolicy(String id, String typeName, String service, String ric) throws ServiceException {
623 Policy p = ImmutablePolicy.builder().id(id) //
624 .json(jsonString()) //
625 .ownerServiceName(service) //
626 .ric(rics.getRic(ric)) //
627 .type(addPolicyType(typeName, ric)) //
628 .lastModified("lastModified").build();
633 private Policy addPolicy(String id, String typeName, String service) throws ServiceException {
634 return addPolicy(id, typeName, service, "ric");
637 private String createServiceJson(String name, long keepAliveIntervalSeconds) {
638 return createServiceJson(name, keepAliveIntervalSeconds, "https://examples.javacodegeeks.com/core-java/");
641 private String createServiceJson(String name, long keepAliveIntervalSeconds, String url) {
642 ServiceRegistrationInfo service = new ServiceRegistrationInfo(name, keepAliveIntervalSeconds, url);
644 String json = gson.toJson(service);
648 private void putService(String name) {
649 putService(name, 0, null);
652 private void putService(String name, long keepAliveIntervalSeconds, @Nullable HttpStatus expectedStatus) {
653 String url = "/service";
654 String body = createServiceJson(name, keepAliveIntervalSeconds);
655 ResponseEntity<String> resp = restClient().putForEntity(url, body).block();
656 if (expectedStatus != null) {
657 assertEquals(expectedStatus, resp.getStatusCode(), "");
661 private String baseUrl() {
662 return "https://localhost:" + port;
665 private String jsonString() {
666 return "{\n \"servingCellNrcgi\": \"1\"\n }";
670 public void testConcurrency() throws Exception {
671 final Instant startTime = Instant.now();
672 List<Thread> threads = new ArrayList<>();
673 a1ClientFactory.setResponseDelay(Duration.ofMillis(1));
675 addPolicyType("type1", "ric");
676 addPolicyType("type2", "ric");
678 for (int i = 0; i < 100; ++i) {
680 new Thread(new ConcurrencyTestRunnable(baseUrl(), supervision, a1ClientFactory, rics, policyTypes),
685 for (Thread t : threads) {
688 assertThat(policies.size()).isEqualTo(0);
689 logger.info("Concurrency test took " + Duration.between(startTime, Instant.now()));
692 private AsyncRestClient restClient() {
693 return new AsyncRestClient(baseUrl());
696 private void testErrorCode(Mono<?> request, HttpStatus expStatus) {
697 testErrorCode(request, expStatus, "");
700 private void testErrorCode(Mono<?> request, HttpStatus expStatus, String responseContains) {
701 StepVerifier.create(request) //
702 .expectSubscription() //
703 .expectErrorMatches(t -> checkWebClientError(t, expStatus, responseContains)) //
707 private boolean checkWebClientError(Throwable t, HttpStatus expStatus, String responseContains) {
708 assertTrue(t instanceof WebClientResponseException);
709 WebClientResponseException e = (WebClientResponseException) t;
710 assertThat(e.getStatusCode()).isEqualTo(expStatus);
711 assertThat(e.getResponseBodyAsString()).contains(responseContains);
715 private MockA1Client getA1Client(String ricName) throws ServiceException {
716 return a1ClientFactory.getOrCreateA1Client(ricName);
719 private PolicyType createPolicyType(String policyTypeName) {
720 return ImmutablePolicyType.builder() //
721 .name(policyTypeName) //
722 .schema("{\"title\":\"" + policyTypeName + "\"}") //
726 private PolicyType addPolicyType(String policyTypeName, String ricName) {
727 PolicyType type = createPolicyType(policyTypeName);
728 policyTypes.put(type);
729 addRic(ricName).addSupportedPolicyType(type);
733 private Ric addRic(String ricName) {
734 return addRic(ricName, null);
737 private Ric addRic(String ricName, String managedElement) {
738 if (rics.get(ricName) != null) {
739 return rics.get(ricName);
741 List<String> mes = new ArrayList<>();
742 if (managedElement != null) {
743 mes.add(managedElement);
745 RicConfig conf = ImmutableRicConfig.builder() //
748 .managedElementIds(mes) //
749 .controllerName("") //
751 Ric ric = new Ric(conf);
752 ric.setState(Ric.RicState.AVAILABLE);
757 private static <T> List<T> parseList(String jsonString, Class<T> clazz) {
758 List<T> result = new ArrayList<>();
759 JsonArray jsonArr = JsonParser.parseString(jsonString).getAsJsonArray();
760 for (JsonElement jsonElement : jsonArr) {
761 T o = gson.fromJson(jsonElement.toString(), clazz);
767 private static List<String> parseSchemas(String jsonString) {
768 JsonArray arrayOfSchema = JsonParser.parseString(jsonString).getAsJsonArray();
769 List<String> result = new ArrayList<>();
770 for (JsonElement schemaObject : arrayOfSchema) {
771 result.add(schemaObject.toString());