2 * ========================LICENSE_START=================================
5 * Copyright (C) 2020 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.clients;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
33 import org.json.JSONException;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.junit.jupiter.api.extension.ExtendWith;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.oransc.policyagent.configuration.ImmutableRicConfig;
39 import org.oransc.policyagent.configuration.RicConfig;
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
45 @ExtendWith(MockitoExtension.class)
46 class OscA1ClientTest {
48 private static final String RIC_URL = "RicUrl";
50 private static final String RIC_BASE_URL = "RicBaseUrl/a1-p";
52 private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
53 private static final String POLICIES = "/policies";
54 private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
55 private static final String POLICY_TYPE_1_ID = "type1";
56 private static final String POLICY_TYPE_2_ID = "type2";
57 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
58 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
59 private static final String POLICY_1_ID = "policy1";
60 private static final String POLICY_2_ID = "policy2";
61 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
63 OscA1Client clientUnderTest;
65 AsyncRestClient asyncRestClientMock;
69 RicConfig ricConfig = ImmutableRicConfig.builder() //
71 .baseUrl("RicBaseUrl") //
72 .managedElementIds(new ArrayList<>()) //
73 .controllerName("") //
75 asyncRestClientMock = mock(AsyncRestClient.class);
76 clientUnderTest = new OscA1Client(ricConfig, asyncRestClientMock);
80 void testGetPolicyTypeIdentities() {
81 List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
82 Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
83 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
85 Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
86 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
87 StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
91 void testGetPolicyIdentities() {
92 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
93 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
94 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
95 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
96 .thenReturn(policyIdsType2Resp);
98 List<String> returned = clientUnderTest.getPolicyIdentities().block();
100 assertEquals(2, returned.size(), "");
101 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
102 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
103 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
107 void testGetValidPolicyType() {
108 String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
109 Mono<String> policyTypeResp = Mono.just(policyType);
111 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
113 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
114 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
115 StepVerifier.create(returnedMono).expectNext(A1ClientHelper.getCreateSchema(policyType, POLICY_TYPE_1_ID))
116 .expectComplete().verify();
120 void testGetInValidPolicyTypeJson() {
121 String policyType = "{\"create_schema\": " + 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)).block();
148 verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID,
153 void testDeletePolicy() {
154 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
156 Mono<String> returnedMono = clientUnderTest
157 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
158 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
159 StepVerifier.create(returnedMono).expectComplete().verify();
163 void testDeleteAllPolicies() {
164 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
165 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
166 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
167 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
168 .thenReturn(policyIdsType2Resp);
169 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
171 Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
172 StepVerifier.create(returnedFlux).expectComplete().verify();
173 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
174 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
175 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
176 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
177 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);