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.Mockito.mock;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.util.ArrayList;
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.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
40 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
42 import reactor.core.publisher.Flux;
43 import reactor.core.publisher.Mono;
44 import reactor.test.StepVerifier;
46 @ExtendWith(MockitoExtension.class)
47 class StdA1ClientV2Test {
49 private static final String RIC_URL = "RicUrl";
51 private static final String RIC_BASE_URL = "RicBaseUrl/A1-P/v2";
53 private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
54 private static final String POLICIES = "/policies";
55 private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
56 private static final String POLICY_TYPE_1_ID = "type1";
57 private static final String POLICY_TYPE_2_ID = "type2";
58 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
59 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
60 private static final String POLICY_1_ID = "policy1";
61 private static final String POLICY_2_ID = "policy2";
62 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
64 StdA1ClientVersion2 clientUnderTest;
66 AsyncRestClient asyncRestClientMock;
70 RicConfig ricConfig = ImmutableRicConfig.builder() //
72 .baseUrl("RicBaseUrl") //
73 .managedElementIds(new ArrayList<>()) //
74 .controllerName("") //
76 asyncRestClientMock = mock(AsyncRestClient.class);
77 clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock);
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 void testGetPolicyIdentities() {
93 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
94 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
95 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
96 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
97 .thenReturn(policyIdsType2Resp);
99 List<String> returned = clientUnderTest.getPolicyIdentities().block();
101 assertEquals(2, returned.size(), "");
102 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
103 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
104 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
108 void testGetValidPolicyType() {
109 String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
110 Mono<String> policyTypeResp = Mono.just(policyType);
112 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
114 String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block();
115 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
116 assertThat(response).contains("title");
120 void testGetInValidPolicyTypeJson() {
121 String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
122 Mono<String> policyTypeResp = Mono.just(policyType);
124 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
126 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
127 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
128 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
132 void testGetPolicyTypeWithoutCreateSchema() {
133 Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
135 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
137 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
138 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
139 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
143 void testPutPolicy() {
144 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
147 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
149 verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID,
154 void testDeletePolicy() {
155 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
157 Mono<String> returnedMono = clientUnderTest
158 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
159 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
160 StepVerifier.create(returnedMono).expectComplete().verify();
164 void testDeleteAllPolicies() {
165 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
166 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
167 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
168 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
169 .thenReturn(policyIdsType2Resp);
170 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
172 Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
173 StepVerifier.create(returnedFlux).expectComplete().verify();
174 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
175 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
176 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
177 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
178 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);