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.mockito.Mockito.doReturn;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import java.util.Arrays;
29 import org.json.JSONException;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.junit.jupiter.api.extension.ExtendWith;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.Spy;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.mockito.junit.jupiter.MockitoExtension;
39 import reactor.core.publisher.Flux;
40 import reactor.core.publisher.Mono;
41 import reactor.test.StepVerifier;
43 @ExtendWith(MockitoExtension.class)
44 @RunWith(MockitoJUnitRunner.class)
45 public class A1ClientImplTest {
46 private static final String RIC_URL = "RicUrl";
47 private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes/identities";
48 private static final String POLICIES_IDENTITIES_URL = "/policies/identities";
49 private static final String POLICYTYPES_URL = "/policytypes/";
50 private static final String POLICIES_URL = "/policies/";
52 private static final String POLICY_TYPE_1_NAME = "type1";
53 private static final String POLICY_TYPE_2_NAME = "type2";
54 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
55 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
56 private static final String POLICY_1_ID = "policy1";
57 private static final String POLICY_2_ID = "policy2";
58 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
59 private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
62 A1ClientImpl a1Client;
65 AsyncRestClient asyncRestClientMock;
69 doReturn(asyncRestClientMock).when(a1Client).createClient(RIC_URL);
73 public void testGetPolicyTypeIdentities() {
74 Mono<String> policyTypeIds = Mono.just(Arrays.toString(new String[] {POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME}));
75 when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
77 Flux<String> policyTypeIdsFlux = a1Client.getPolicyTypeIdentities(RIC_URL);
78 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
79 StepVerifier.create(policyTypeIdsFlux).expectNext(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).expectComplete()
84 public void testGetPolicyIdentities() {
85 Mono<String> policyIds = Mono.just(Arrays.toString(new String[] {POLICY_1_ID, POLICY_2_ID}));
86 when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
88 Flux<String> policyIdsFlux = a1Client.getPolicyIdentities(RIC_URL);
89 verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
90 StepVerifier.create(policyIdsFlux).expectNext(POLICY_1_ID, POLICY_2_ID).expectComplete().verify();
94 public void testGetValidPolicyType() {
95 when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
96 .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_VALID));
98 Mono<String> policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME);
99 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
100 StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
104 public void testGetInvalidPolicyType() {
105 when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
106 .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
108 Mono<String> policyTypeMono = a1Client.getPolicyType(RIC_URL, POLICY_TYPE_1_NAME);
109 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
110 StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
115 public void testPutPolicyValidResponse() {
116 when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID))
117 .thenReturn(Mono.just(POLICY_JSON_VALID));
119 Mono<String> policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID);
120 verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
121 StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
125 public void testPutPolicyInvalidResponse() {
126 when(asyncRestClientMock.put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID))
127 .thenReturn(Mono.just(POLICY_JSON_INVALID));
129 Mono<String> policyMono = a1Client.putPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID);
130 verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID, POLICY_JSON_VALID);
131 StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
135 public void testDeletePolicy() {
136 when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
138 Mono<Void> responseMono = a1Client.deletePolicy(RIC_URL, POLICY_1_ID);
139 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
140 StepVerifier.create(responseMono).expectComplete().verify();