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.mockito.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import java.util.Arrays;
29 import java.util.List;
31 import org.json.JSONException;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34 import org.junit.jupiter.api.extension.ExtendWith;
35 import org.mockito.junit.jupiter.MockitoExtension;
37 import reactor.core.publisher.Flux;
38 import reactor.core.publisher.Mono;
39 import reactor.test.StepVerifier;
41 @ExtendWith(MockitoExtension.class)
42 public class OscA1ClientTest {
43 private static final String RIC_URL = "RicUrl";
44 private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes";
45 private static final String POLICIES_IDENTITIES_URL = "/policies";
46 private static final String POLICYTYPES_URL = "/policytypes/";
47 private static final String POLICIES_URL = "/policies/";
49 private static final String POLICY_TYPE_1_ID = "type1";
50 private static final String POLICY_TYPE_2_ID = "type2";
51 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
52 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
53 private static final String POLICY_1_ID = "policy1";
54 private static final String POLICY_2_ID = "policy2";
55 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
57 OscA1Client clientUnderTest;
59 AsyncRestClient asyncRestClientMock;
63 asyncRestClientMock = mock(AsyncRestClient.class);
64 clientUnderTest = new OscA1Client(asyncRestClientMock);
68 public void testGetPolicyTypeIdentities() {
69 List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
70 Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
71 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
73 Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
74 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
75 StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
79 public void testGetPolicyIdentities() {
80 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
81 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
82 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
83 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
84 .thenReturn(policyIdsType2Resp);
86 Mono<List<String>> returnedMono = clientUnderTest.getPolicyIdentities();
87 StepVerifier.create(returnedMono).expectNext(Arrays.asList(POLICY_1_ID, POLICY_2_ID)).expectComplete().verify();
88 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
89 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL);
90 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL);
94 public void testGetValidPolicyType() {
95 String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
96 Mono<String> policyTypeResp = Mono.just(policyType);
98 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
100 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
101 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
102 StepVerifier.create(returnedMono).expectNext(A1ClientHelper.getCreateSchema(policyType, POLICY_TYPE_1_ID))
103 .expectComplete().verify();
107 public void testGetInValidPolicyTypeJson() {
108 String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
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).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
119 public void testGetPolicyTypeWithoutCreateSchema() {
120 Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
122 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
124 Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
125 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
126 StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
130 public void testPutPolicy() {
131 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
133 Mono<String> returnedMono = clientUnderTest
134 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
135 verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID,
137 StepVerifier.create(returnedMono).expectComplete().verify();
141 public void testDeletePolicy() {
142 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
144 Mono<String> returnedMono = clientUnderTest
145 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
146 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID);
147 StepVerifier.create(returnedMono).expectComplete().verify();
151 public void testDeleteAllPolicies() {
152 Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
153 Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
154 Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
155 when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
156 .thenReturn(policyIdsType2Resp);
157 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
159 Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
160 StepVerifier.create(returnedFlux).expectComplete().verify();
161 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
162 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_IDENTITIES_URL);
163 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES_URL + POLICY_1_ID);
164 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_IDENTITIES_URL);
165 verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES_URL + POLICY_2_ID);