4b6d17b0476e9e690ce617efe176fc232ae47606
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020 Nordix Foundation. All rights reserved.
6  * ======================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.onap.ccsdk.oran.a1policymanagementservice.clients;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.mockito.ArgumentMatchers.anyString;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.json.JSONException;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.junit.jupiter.api.extension.ExtendWith;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.junit.jupiter.MockitoExtension;
41 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.ImmutableRicConfig;
42 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
43
44 import reactor.core.publisher.Flux;
45 import reactor.core.publisher.Mono;
46 import reactor.test.StepVerifier;
47
48 @ExtendWith(MockitoExtension.class)
49 class StdA1ClientV2Test {
50
51     private static final String RIC_URL = "https://ric.com";
52
53     private static final String RIC_BASE_URL = RIC_URL + "/A1-P/v2";
54
55     private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
56     private static final String POLICIES = "/policies";
57     private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
58     private static final String POLICY_TYPE_1_ID = "type1";
59     private static final String POLICY_TYPE_2_ID = "type2";
60     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
61     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
62     private static final String POLICY_1_ID = "policy1";
63     private static final String POLICY_2_ID = "policy2";
64     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
65
66     StdA1ClientVersion2 clientUnderTest;
67
68     AsyncRestClient asyncRestClientMock;
69
70     @BeforeEach
71     void init() {
72         RicConfig ricConfig = ImmutableRicConfig.builder() //
73                 .ricId("name") //
74                 .baseUrl(RIC_URL) //
75                 .managedElementIds(new ArrayList<>()) //
76                 .controllerName("") //
77                 .customAdapterClass("") //
78                 .build();
79         asyncRestClientMock = mock(AsyncRestClient.class);
80         clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock);
81     }
82
83     @Test
84     void testGetPolicyTypeIdentities() {
85         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
86         Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
87         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
88
89         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
90         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
91         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
92     }
93
94     @Test
95     void testGetPolicyIdentities() {
96         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
97         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
98         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
99         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
100                 .thenReturn(policyIdsType2Resp);
101
102         List<String> returned = clientUnderTest.getPolicyIdentities().block();
103
104         assertEquals(2, returned.size(), "");
105         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
106         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
107         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
108     }
109
110     @Test
111     void testGetValidPolicyType() {
112         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
113         Mono<String> policyTypeResp = Mono.just(policyType);
114
115         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
116
117         String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block();
118         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
119         assertThat(response).contains("title");
120     }
121
122     @Test
123     void testGetInValidPolicyTypeJson() {
124         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
125         Mono<String> policyTypeResp = Mono.just(policyType);
126
127         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
128
129         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
130         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
131         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
132     }
133
134     @Test
135     void testGetPolicyTypeWithoutCreateSchema() {
136         Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
137
138         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
139
140         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
141         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
142         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
143     }
144
145     @Test
146     void testPutPolicy() {
147         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
148
149         clientUnderTest
150                 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
151                 .block();
152         ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
153         verify(asyncRestClientMock).put(urlCaptor.capture(), eq(POLICY_JSON_VALID));
154         String actualUrl = urlCaptor.getValue();
155         String expUrl = POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID;
156         assertThat(actualUrl).contains(expUrl);
157     }
158
159     @Test
160     void testDeletePolicy() {
161         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
162
163         Mono<String> returnedMono = clientUnderTest
164                 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
165         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
166         StepVerifier.create(returnedMono).expectComplete().verify();
167     }
168
169     @Test
170     void testDeleteAllPolicies() {
171         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
172         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
173         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
174         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
175                 .thenReturn(policyIdsType2Resp);
176         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
177
178         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
179         StepVerifier.create(returnedFlux).expectComplete().verify();
180         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
181         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
182         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
183         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
184         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);
185     }
186 }