de55882bc168e6b895eba1a5a3387992ad8b629f
[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                 .build();
78         asyncRestClientMock = mock(AsyncRestClient.class);
79         clientUnderTest = new StdA1ClientVersion2(ricConfig, asyncRestClientMock);
80     }
81
82     @Test
83     void testGetPolicyTypeIdentities() {
84         List<String> policyTypeIds = Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID);
85         Mono<String> policyTypeIdsResp = Mono.just(policyTypeIds.toString());
86         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp);
87
88         Mono<List<String>> returnedMono = clientUnderTest.getPolicyTypeIdentities();
89         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
90         StepVerifier.create(returnedMono).expectNext(policyTypeIds).expectComplete().verify();
91     }
92
93     @Test
94     void testGetPolicyIdentities() {
95         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
96         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
97         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
98         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
99                 .thenReturn(policyIdsType2Resp);
100
101         List<String> returned = clientUnderTest.getPolicyIdentities().block();
102
103         assertEquals(2, returned.size(), "");
104         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
105         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
106         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
107     }
108
109     @Test
110     void testGetValidPolicyType() {
111         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
112         Mono<String> policyTypeResp = Mono.just(policyType);
113
114         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
115
116         String response = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID).block();
117         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
118         assertThat(response).contains("title");
119     }
120
121     @Test
122     void testGetInValidPolicyTypeJson() {
123         String policyType = "{\"policySchema\": " + POLICY_TYPE_SCHEMA_INVALID + "}";
124         Mono<String> policyTypeResp = Mono.just(policyType);
125
126         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
127
128         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
129         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
130         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof JSONException).verify();
131     }
132
133     @Test
134     void testGetPolicyTypeWithoutCreateSchema() {
135         Mono<String> policyTypeResp = Mono.just(POLICY_TYPE_SCHEMA_VALID);
136
137         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
138
139         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
140         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
141         StepVerifier.create(returnedMono).expectErrorMatches(throwable -> throwable instanceof Exception).verify();
142     }
143
144     @Test
145     void testPutPolicy() {
146         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
147
148         clientUnderTest
149                 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
150                 .block();
151         ArgumentCaptor<String> urlCaptor = ArgumentCaptor.forClass(String.class);
152         verify(asyncRestClientMock).put(urlCaptor.capture(), eq(POLICY_JSON_VALID));
153         String actualUrl = urlCaptor.getValue();
154         String expUrl = POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID;
155         assertThat(actualUrl).contains(expUrl);
156     }
157
158     @Test
159     void testDeletePolicy() {
160         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
161
162         Mono<String> returnedMono = clientUnderTest
163                 .deletePolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID));
164         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
165         StepVerifier.create(returnedMono).expectComplete().verify();
166     }
167
168     @Test
169     void testDeleteAllPolicies() {
170         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
171         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
172         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
173         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
174                 .thenReturn(policyIdsType2Resp);
175         when(asyncRestClientMock.delete(anyString())).thenReturn(Mono.empty());
176
177         Flux<String> returnedFlux = clientUnderTest.deleteAllPolicies();
178         StepVerifier.create(returnedFlux).expectComplete().verify();
179         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
180         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
181         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID);
182         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
183         verify(asyncRestClientMock).delete(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES + "/" + POLICY_2_ID);
184     }
185 }