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.doReturn;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
29 import java.util.Arrays;
30 import java.util.List;
32 import org.json.JSONException;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.extension.ExtendWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.jupiter.MockitoExtension;
38 import org.oransc.policyagent.configuration.RicConfig;
39 import org.oransc.policyagent.repository.Policy;
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
45 @ExtendWith(MockitoExtension.class)
46 public class StdA1ClientTest {
47 private static final String RIC_URL = "RicUrl";
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;
61 RicConfig ricConfigMock;
65 clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock);
68 private String policiesUrl() {
69 return RIC_URL + "/A1-P/v1/policies";
72 private String policiesBaseUrl() {
73 return policiesUrl() + "/";
77 public void testGetPolicyTypeIdentities() {
78 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
79 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
80 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
84 public void testGetPolicyIdentities() {
85 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
86 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
87 when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
89 List<String> result = clientUnderTest.getPolicyIdentities().block();
90 assertEquals(2, result.size(), "");
92 verify(asyncRestClientMock).get(policiesUrl());
96 public void testGetValidPolicyType() {
97 String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
98 assertEquals("{}", policyType, "");
102 public void testPutPolicyValidResponse() {
103 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
104 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_VALID));
106 Mono<String> policyMono = clientUnderTest
107 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
109 verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON_VALID);
110 StepVerifier.create(policyMono).expectNext(POLICY_JSON_VALID).expectComplete().verify();
114 public void testPutPolicyInvalidResponse() {
115 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON_INVALID));
117 Mono<String> policyMono = clientUnderTest
118 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE));
120 StepVerifier.create(policyMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
124 public void testDeletePolicy() {
125 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
126 final String url = policiesBaseUrl() + POLICY_1_ID;
127 when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty());
129 Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE);
130 Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
131 verify(asyncRestClientMock).delete(url);
132 StepVerifier.create(responseMono).expectComplete().verify();
136 public void testDeleteAllPolicies() {
137 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
138 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
139 when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds);
140 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
142 Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
143 StepVerifier.create(responseFlux).expectComplete().verify();
144 verify(asyncRestClientMock).get(policiesUrl());
145 verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID);
146 verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID);