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.ArgumentMatchers.anyString;
24 import static org.mockito.Mockito.doReturn;
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.Arrays;
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;
36 import org.oransc.policyagent.repository.Policy;
38 import reactor.core.publisher.Flux;
39 import reactor.core.publisher.Mono;
40 import reactor.test.StepVerifier;
42 @ExtendWith(MockitoExtension.class)
43 public class StdA1ClientTest {
44 private static final String RIC_URL = "RicUrl";
45 private static final String POLICYTYPES_IDENTITIES_URL = "/policytypes";
46 private static final String POLICIES_IDENTITIES_URL = "/policies";
47 private static final String POLICYTYPES_URL = "/policytypes/";
48 private static final String POLICIES_URL = "/policies/";
50 private static final String POLICY_TYPE_1_NAME = "type1";
51 private static final String POLICY_TYPE_2_NAME = "type2";
52 private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
53 private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
54 private static final String POLICY_1_ID = "policy1";
55 private static final String POLICY_2_ID = "policy2";
56 private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
57 private static final String POLICY_JSON_INVALID = "\"policyId\":\"policy1\"}";
58 private static final String POLICY_TYPE = "typeName";
60 StdA1Client clientUnderTest;
62 AsyncRestClient asyncRestClientMock;
66 asyncRestClientMock = mock(AsyncRestClient.class);
67 clientUnderTest = new StdA1Client(A1ClientHelper.createRic(RIC_URL).getConfig(), asyncRestClientMock);
71 public void testGetPolicyTypeIdentities() {
72 Mono<String> policyTypeIds = Mono.just(Arrays.asList(POLICY_TYPE_1_NAME, POLICY_TYPE_2_NAME).toString());
73 when(asyncRestClientMock.get(POLICYTYPES_IDENTITIES_URL)).thenReturn(policyTypeIds);
75 Mono<?> policyTypeIdsFlux = clientUnderTest.getPolicyTypeIdentities();
76 verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
77 StepVerifier.create(policyTypeIdsFlux).expectNextCount(1).expectComplete().verify();
81 public void testGetPolicyIdentities() {
82 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
83 when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
85 Mono<?> policyIdsFlux = clientUnderTest.getPolicyIdentities();
86 verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
87 StepVerifier.create(policyIdsFlux).expectNextCount(1).expectComplete().verify();
91 public void testGetValidPolicyType() {
92 Mono<?> policyTypeResp =
93 Mono.just("{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + ", \"statusSchema\": {} }");
95 doReturn(policyTypeResp).when(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
97 Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
98 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
99 StepVerifier.create(policyTypeMono).expectNext(POLICY_TYPE_SCHEMA_VALID).expectComplete().verify();
103 public void testGetInvalidPolicyType() {
104 when(asyncRestClientMock.get(POLICYTYPES_URL + POLICY_TYPE_1_NAME))
105 .thenReturn(Mono.just(POLICY_TYPE_SCHEMA_INVALID));
107 Mono<String> policyTypeMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME);
108 verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_NAME);
109 StepVerifier.create(policyTypeMono).expectErrorMatches(throwable -> throwable instanceof JSONException)
114 public void testPutPolicyValidResponse() {
115 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
117 Mono<String> policyMono = clientUnderTest
118 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
119 verify(asyncRestClientMock).put(POLICIES_URL + POLICY_1_ID + "?policyTypeId=" + POLICY_TYPE, POLICY_JSON_VALID);
120 StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
124 public void testPutPolicyInvalidResponse() {
125 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
127 Mono<String> policyMono = clientUnderTest
128 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
129 StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
133 public void testDeletePolicy() {
134 when(asyncRestClientMock.delete(POLICIES_URL + POLICY_1_ID)).thenReturn(Mono.empty());
136 Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
137 Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
138 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
139 StepVerifier.create(responseMono).expectComplete().verify();
143 public void testDeleteAllPolicies() {
144 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
145 when(asyncRestClientMock.get(POLICIES_IDENTITIES_URL)).thenReturn(policyIds);
146 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
148 Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
149 StepVerifier.create(responseFlux).expectComplete().verify();
150 verify(asyncRestClientMock).get(POLICIES_IDENTITIES_URL);
151 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_1_ID);
152 verify(asyncRestClientMock).delete(POLICIES_URL + POLICY_2_ID);