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