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