2f2cce02a00a71fd5e890f55c1c1ef107bb95150
[ccsdk/oran.git] /
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
4  * ======================================================================
5  * Copyright (C) 2020-2023 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.junit.jupiter.api.Assertions.assertEquals;
24 import static org.mockito.ArgumentMatchers.anyString;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import java.util.Arrays;
30 import java.util.List;
31
32 import org.json.JSONException;
33 import org.json.JSONObject;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.DisplayName;
36 import org.junit.jupiter.api.Test;
37 import org.junit.jupiter.api.extension.ExtendWith;
38 import org.mockito.junit.jupiter.MockitoExtension;
39 import org.onap.ccsdk.oran.a1policymanagementservice.configuration.RicConfig;
40
41 import reactor.core.publisher.Flux;
42 import reactor.core.publisher.Mono;
43 import reactor.test.StepVerifier;
44
45 @ExtendWith(MockitoExtension.class)
46 class OscA1ClientTest {
47
48     private static final String RIC_URL = "RicUrl";
49
50     private static final String RIC_BASE_URL = "RicBaseUrl/a1-p";
51
52     private static final String POLICYTYPES_IDENTITIES_URL = RIC_BASE_URL + "/policytypes";
53     private static final String POLICIES = "/policies";
54     private static final String POLICYTYPES_URL = RIC_BASE_URL + "/policytypes/";
55     private static final String POLICY_TYPE_1_ID = "type1";
56     private static final String POLICY_TYPE_2_ID = "type2";
57     private static final String POLICY_TYPE_SCHEMA_VALID = "{\"type\":\"type1\"}";
58     private static final String POLICY_TYPE_SCHEMA_INVALID = "\"type\":\"type1\"}";
59     private static final String POLICY_1_ID = "policy1";
60     private static final String POLICY_2_ID = "policy2";
61     private static final String POLICY_JSON_VALID = "{\"policyId\":\"policy1\"}";
62
63     OscA1Client clientUnderTest;
64
65     AsyncRestClient asyncRestClientMock;
66
67     @BeforeEach
68     void init() {
69         RicConfig ricConfig = RicConfig.builder() //
70                 .ricId("name") //
71                 .baseUrl("RicBaseUrl") //
72                 .build();
73         asyncRestClientMock = mock(AsyncRestClient.class);
74         clientUnderTest = new OscA1Client(ricConfig, asyncRestClientMock);
75     }
76
77     @Test
78     @DisplayName("test Get Policy Type Identities")
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     @DisplayName("test Get Policy Identities")
91     void testGetPolicyIdentities() {
92         Mono<String> policyTypeIdsResp = Mono.just(Arrays.asList(POLICY_TYPE_1_ID, POLICY_TYPE_2_ID).toString());
93         Mono<String> policyIdsType1Resp = Mono.just(Arrays.asList(POLICY_1_ID).toString());
94         Mono<String> policyIdsType2Resp = Mono.just(Arrays.asList(POLICY_2_ID).toString());
95         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeIdsResp).thenReturn(policyIdsType1Resp)
96                 .thenReturn(policyIdsType2Resp);
97
98         List<String> returned = clientUnderTest.getPolicyIdentities().block();
99
100         assertEquals(2, returned.size(), "");
101         verify(asyncRestClientMock).get(POLICYTYPES_IDENTITIES_URL);
102         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES);
103         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_2_ID + POLICIES);
104     }
105
106     @Test
107     @DisplayName("test Get Valid PolicyType")
108     void testGetValidPolicyType() {
109         String policyType = "{\"create_schema\": " + POLICY_TYPE_SCHEMA_VALID + "}";
110         Mono<String> policyTypeResp = Mono.just(policyType);
111
112         when(asyncRestClientMock.get(anyString())).thenReturn(policyTypeResp);
113
114         Mono<String> returnedMono = clientUnderTest.getPolicyTypeSchema(POLICY_TYPE_1_ID);
115         verify(asyncRestClientMock).get(POLICYTYPES_URL + POLICY_TYPE_1_ID);
116         StepVerifier.create(returnedMono).expectNext(getCreateSchema(policyType, POLICY_TYPE_1_ID)).expectComplete()
117                 .verify();
118     }
119
120     @Test
121     @DisplayName("test Get In Valid Policy Type Json")
122     void testGetInValidPolicyTypeJson() {
123         String policyType = "{\"create_schema\": " + 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     @DisplayName("test Get Policy Type Without CreateS chema")
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     @DisplayName("test Put Policy")
147     void testPutPolicy() {
148         when(asyncRestClientMock.put(anyString(), anyString())).thenReturn(Mono.empty());
149
150         clientUnderTest
151                 .putPolicy(A1ClientHelper.createPolicy(RIC_URL, POLICY_1_ID, POLICY_JSON_VALID, POLICY_TYPE_1_ID))
152                 .block();
153         verify(asyncRestClientMock).put(POLICYTYPES_URL + POLICY_TYPE_1_ID + POLICIES + "/" + POLICY_1_ID,
154                 POLICY_JSON_VALID);
155     }
156
157     @Test
158     @DisplayName("test Delete Policy")
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     @DisplayName("test Delete All Policies")
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
187     private String getCreateSchema(String policyType, String policyTypeId) {
188         JSONObject obj = new JSONObject(policyType);
189         JSONObject schemaObj = obj.getJSONObject("create_schema");
190         schemaObj.put("title", policyTypeId);
191         return schemaObj.toString();
192     }
193 }