2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 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.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.Mock;
36 import org.mockito.junit.jupiter.MockitoExtension;
37 import org.oransc.policyagent.repository.Policy;
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
43 @ExtendWith(MockitoExtension.class)
44 public class StdA1ClientTest {
45 private static final String RIC_URL = "RicUrl";
46 private static final String POLICIES_IDENTITIES_URL = "/policies";
47 private static final String POLICIES_URL = "/policies/";
48 private static final String POLICY_TYPE_1_NAME = "type1";
49 private static final String POLICY_1_ID = "policy1";
50 private static final String POLICY_2_ID = "policy2";
51 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
52 private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
53 private static final String POLICY_TYPE = "typeName";
55 StdA1ClientVersion1 clientUnderTest;
58 AsyncRestClient asyncRestClientMock;
62 clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock);
66 public void testGetPolicyTypeIdentities() {
67 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
68 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
69 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
73 public void testGetPolicyIdentities() {
74 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
75 when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
77 List<String> result = clientUnderTest.getPolicyIdentities().block();
78 assertEquals(2, result.size(), "");
80 verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
84 public void testGetValidPolicyType() {
85 String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
86 assertEquals("{}", policyType, "");
90 public void testPutPolicyValidResponse() {
91 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
93 Mono<String> policyMono = clientUnderTest
94 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
96 verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
97 StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
101 public void testPutPolicyInvalidResponse() {
102 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
104 Mono<String> policyMono = clientUnderTest
105 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
107 StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
111 public void testDeletePolicy() {
112 when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
114 Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
115 Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
116 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
117 StepVerifier.create(responseMono).expectComplete().verify();
121 public void testDeleteAllPolicies() {
122 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
123 when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
124 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
126 Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
127 StepVerifier.create(responseFlux).expectComplete().verify();
128 verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
129 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
130 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);