2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 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.clients;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import org.json.JSONException;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
42 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
44 import reactor.core.publisher.Flux;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
48 @ExtendWith(MockitoExtension.class)
49 class StdA1ClientV2Test {
51 private static final String RIC_URL = "https://ric.com";
53 private static final String RIC_BASE_URL = RIC_URL + "/A1-P/v2";
55 private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
56 private static final String POLICIES = "/policies";
57 private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
58 private static final String POLICY_TYPE_1_ID = "type1";
59 private static final String POLICY_TYPE_2_ID = "type2";
60 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
61 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
62 private static final String POLICY_1_ID = "policy1";
63 private static final String POLICY_2_ID = "policy2";
64 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
66 StdA1ClientVersion2 clientUnderTest;
68 AsyncRestClient asyncRestClientMock;
72 RicConfig ricConfig = ImmutableRicConfig.builder() //
75 .managedElementIds(new ArrayList<>()) //
76 .controllerName("") //
78 asyncRestClientMock = mock(AsyncRestClient.class);
79 clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock);
83 void testGetPolicyTypeIdentities() {
84 List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
85 Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
86 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
88 Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
89 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
90 StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
94 void testGetPolicyIdentities() {
95 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
96 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
97 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
98 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
99 .thenReturn(policyIdsType2Resp);
101 List<String> returned = clientUnderTest.getPolicyIdentities().block();
103 assertEquals(2, returned.size(), "");
104 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
105 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
106 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
110 void testGetValidPolicyType() {
111 String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
112 Mono<String> policyTypeResp = Mono.just(policyType);
114 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
116 String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block();
117 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
118 assertThat(response).contains("title");
122 void testGetInValidPolicyTypeJson() {
123 String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
124 Mono<String> policyTypeResp = Mono.just(policyType);
126 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
128 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
129 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
130 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
134 void testGetPolicyTypeWithoutCreateSchema() {
135 Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
137 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
139 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
140 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
141 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
145 void testPutPolicy() {
146 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
149 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
151 ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
152 verify(asyncRestClientMock).put(urlCaptor.capture(), eq(POLICY_JSON_VALID));
153 String actualUrl = urlCaptor.getValue();
154 String expUrl = POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID;
155 assertThat(actualUrl).contains(expUrl);
159 void testDeletePolicy() {
160 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
162 Mono<String> returnedMono = clientUnderTest
163 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
164 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
165 StepVerifier.create(returnedMono).expectComplete().verify();
169 void testDeleteAllPolicies() {
170 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
171 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
172 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
173 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
174 .thenReturn(policyIdsType2Resp);
175 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
177 Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
178 StepVerifier.create(returnedFlux).expectComplete().verify();
179 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
180 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
181 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
182 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
183 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);