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.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.configuration.RicConfig;
38 import org.oransc.policyagent.repository.Policy;
40 import reactor.core.publisher.Flux;
41 import reactor.core.publisher.Mono;
42 import reactor.test.StepVerifier;
44 @ExtendWith(MockitoExtension.class)
45 class StdA1ClientTest {
46 private static final String RIC_URL = "RicUrl";
47 private static final String POLICY_TYPE_1_NAME = "type1";
48 private static final String POLICY_1_ID = "policy1";
49 private static final String POLICY_2_ID = "policy2";
50 private static final String POLICY_JSON = "{\"policyId\":\"policy1\"}";
51 private static final String POLICY_TYPE = "typeName";
53 StdA1ClientVersion1 clientUnderTest;
56 AsyncRestClient asyncRestClientMock;
59 RicConfig ricConfigMock;
63 clientUnderTest = new StdA1ClientVersion1(asyncRestClientMock, ricConfigMock);
66 private String policiesUrl() {
67 return RIC_URL + "/A1-P/v1/policies";
70 private String policiesBaseUrl() {
71 return policiesUrl() + "/";
75 void testGetPolicyTypeIdentities() {
76 List<String> policyTypeIds = clientUnderTest.getPolicyTypeIdentities().block();
77 assertEquals(1, policyTypeIds.size(), "should hardcoded to one");
78 assertEquals("", policyTypeIds.get(0), "should hardcoded to empty");
82 void testGetPolicyIdentities() {
83 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
84 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
85 when(asyncRestClientMock.get(anyString())).thenReturn(policyIds);
87 List<String> result = clientUnderTest.getPolicyIdentities().block();
88 assertEquals(2, result.size(), "");
90 verify(asyncRestClientMock).get(policiesUrl());
94 void testGetValidPolicyType() {
95 String policyType = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_NAME).block();
96 assertEquals("{}", policyType, "");
100 void testPutPolicyValidResponse() {
101 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
102 when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.just(POLICY_JSON));
104 Mono<String> policyMono =
105 clientUnderTest.putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE));
107 verify(asyncRestClientMock).put(policiesBaseUrl() + POLICY_1_ID, POLICY_JSON);
108 StepVerifier.create(policyMono).expectNext(POLICY_JSON).expectComplete().verify();
112 void testDeletePolicy() {
113 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
114 final String url = policiesBaseUrl() + POLICY_1_ID;
115 when(asyncRestClientMock.delete(url)).thenReturn(Mono.empty());
117 Policy policy = A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON, POLICY_TYPE);
118 Mono<?> responseMono = clientUnderTest.deletePolicy(policy);
119 verify(asyncRestClientMock).delete(url);
120 StepVerifier.create(responseMono).expectComplete().verify();
124 void testDeleteAllPolicies() {
125 doReturn(RIC_URL).when(ricConfigMock).baseUrl();
126 Mono<String> policyIds = Mono.just(Arrays.asList(POLICY_1_ID, POLICY_2_ID).toString());
127 when(asyncRestClientMock.get(policiesUrl())).thenReturn(policyIds);
128 when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
130 Flux<String> responseFlux = clientUnderTest.deleteAllPolicies();
131 StepVerifier.create(responseFlux).expectComplete().verify();
132 verify(asyncRestClientMock).get(policiesUrl());
133 verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_1_ID);
134 verify(asyncRestClientMock).delete(policiesBaseUrl() + POLICY_2_ID);