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.Arrays;
32 import java.util.List;
34 import org.json.JSONException;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.DisplayName;
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.RicConfig;
43 import reactor.core.publisher.Flux;
44 import reactor.core.publisher.Mono;
45 import reactor.test.StepVerifier;
47 @ExtendWith(MockitoExtension.class)
48 class StdA1ClientV2Test {
50 private static final String RIC_URL = "https://ric.com";
52 private static final String RIC_BASE_URL = RIC_URL + "/A1-P/v2";
54 private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
55 private static final String POLICIES = "/policies";
56 private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
57 private static final String POLICY_TYPE_1_ID = "type1";
58 private static final String POLICY_TYPE_2_ID = "type2";
59 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
60 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
61 private static final String POLICY_1_ID = "policy1";
62 private static final String POLICY_2_ID = "policy2";
63 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
65 StdA1ClientVersion2 clientUnderTest;
67 AsyncRestClient asyncRestClientMock;
71 RicConfig ricConfig = RicConfig.builder() //
75 asyncRestClientMock = mock(AsyncRestClient.class);
76 clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock);
80 @DisplayName("test Get Policy Type Identities")
81 void testGetPolicyTypeIdentities() {
82 List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
83 Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
84 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
86 Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
87 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
88 StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
92 @DisplayName("test Get Policy Identities")
93 void testGetPolicyIdentities() {
94 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
95 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
96 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
97 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
98 .thenReturn(policyIdsType2Resp);
100 List<String> returned = clientUnderTest.getPolicyIdentities().block();
102 assertEquals(2, returned.size(), "");
103 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
104 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
105 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
109 @DisplayName("test Get Valid Policy Type")
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 @DisplayName("test Get In Valid PolicyType Json")
123 void testGetInValidPolicyTypeJson() {
124 String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
125 Mono<String> policyTypeResp = Mono.just(policyType);
127 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
129 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
130 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
131 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
135 @DisplayName("test Get Policy Type Without Create Schema")
136 void testGetPolicyTypeWithoutCreateSchema() {
137 Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
139 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
141 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
142 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
143 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
147 @DisplayName("test Put Policy")
148 void testPutPolicy() {
149 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
152 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
154 ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
155 verify(asyncRestClientMock).put(urlCaptor.capture(), eq(POLICY_JSON_VALID));
156 String actualUrl = urlCaptor.getValue();
157 String expUrl = POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID;
158 assertThat(actualUrl).contains(expUrl);
162 @DisplayName("test Delete Policy")
163 void testDeletePolicy() {
164 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
166 Mono<String> returnedMono = clientUnderTest
167 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
168 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
169 StepVerifier.create(returnedMono).expectComplete().verify();
173 @DisplayName("test Delete All Policies")
174 void testDeleteAllPolicies() {
175 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
176 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
177 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
178 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
179 .thenReturn(policyIdsType2Resp);
180 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
182 Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
183 StepVerifier.create(returnedFlux).expectComplete().verify();
184 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
185 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
186 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
187 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
188 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);