2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.portalsdk.core.restful.client;
40 import java.util.List;
42 import org.apache.http.HttpEntity;
43 import org.apache.http.StatusLine;
44 import org.apache.http.client.methods.CloseableHttpResponse;
45 import org.apache.http.impl.client.CloseableHttpClient;
46 import org.apache.http.impl.client.HttpClients;
47 import org.apache.http.util.EntityUtils;
48 import org.junit.Assert;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.onap.portalsdk.core.domain.App;
55 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
56 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
57 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
58 import org.onap.portalsdk.core.restful.domain.SharedContext;
59 import org.onap.portalsdk.core.service.AppService;
60 import org.powermock.api.mockito.PowerMockito;
61 import org.powermock.core.classloader.annotations.PrepareForTest;
62 import org.powermock.modules.junit4.PowerMockRunner;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({PortalApiProperties.class, CipherUtil.class, HttpClients.class, EntityUtils.class})
66 public class SharedContextRestClientTest {
69 private SharedContextRestClient sharedContextRestClient;
72 private AppService appService;
74 @Test(expected = IllegalArgumentException.class)
75 public void getContextValueExceptionTest() throws Exception {
76 String contextId= "\123";
78 sharedContextRestClient.getContextValue(contextId, key);
82 public void getContextValueTest() throws Exception {
83 String contextId= "\123";
86 PowerMockito.mockStatic(PortalApiProperties.class);
87 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
90 app.setUsername("User");
91 String password = "Password";
92 app.setAppPassword(password);
93 Mockito.when(appService.getDefaultApp()).thenReturn(app);
95 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
97 PowerMockito.mockStatic(CipherUtil.class);
98 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
100 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
101 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
103 PowerMockito.mockStatic(HttpClients.class);
104 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
106 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
107 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
108 Mockito.when(response.getEntity()).thenReturn(entity);
110 PowerMockito.mockStatic(EntityUtils.class);
111 String responseJson = " { \"response\": \"Success\", \"context_id\": \"200\"}";
112 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
113 StatusLine statusLine = Mockito.mock(StatusLine.class);
114 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
115 SharedContext context = sharedContextRestClient.getContextValue(contextId, key);
116 Assert.assertNull(context);
120 public void getUserContextTest() throws Exception {
121 String contextId = "234";
123 PowerMockito.mockStatic(PortalApiProperties.class);
124 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
127 app.setUsername("User");
128 String password = "Password";
129 app.setAppPassword(password);
130 Mockito.when(appService.getDefaultApp()).thenReturn(app);
132 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
134 PowerMockito.mockStatic(CipherUtil.class);
135 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
137 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
138 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
140 PowerMockito.mockStatic(HttpClients.class);
141 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
143 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
144 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
145 Mockito.when(response.getEntity()).thenReturn(entity);
147 PowerMockito.mockStatic(EntityUtils.class);
148 String responseJson = " [ { \"response\": \"Success\", \"context_id\": \"200\"} ]";
149 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
150 StatusLine statusLine = Mockito.mock(StatusLine.class);
151 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
153 List<SharedContext> contextList = sharedContextRestClient.getUserContext(contextId);
154 Assert.assertNotNull(contextList);
158 public void checkSharedContextTest() throws Exception {
159 String contextId ="Context";
163 app.setUsername("User");
164 String password = "Password";
165 app.setAppPassword(password);
166 Mockito.when(appService.getDefaultApp()).thenReturn(app);
168 PowerMockito.mockStatic(PortalApiProperties.class);
169 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
170 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
172 PowerMockito.mockStatic(CipherUtil.class);
173 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
175 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
176 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
178 PowerMockito.mockStatic(HttpClients.class);
179 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
181 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
182 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
183 Mockito.when(response.getEntity()).thenReturn(entity);
185 PowerMockito.mockStatic(EntityUtils.class);
186 String responseJson = " { \"response\": \"exists\", \"context_id\": \"200\"} ";
187 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
188 StatusLine statusLine = Mockito.mock(StatusLine.class);
189 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
190 boolean status = sharedContextRestClient.checkSharedContext(contextId, key);
191 Assert.assertTrue(status);
195 public void removeSharedContextTest() throws Exception {
196 String contextId ="Context";
200 app.setUsername("User");
201 String password = "Password";
202 app.setAppPassword(password);
203 Mockito.when(appService.getDefaultApp()).thenReturn(app);
205 PowerMockito.mockStatic(PortalApiProperties.class);
206 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
207 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
209 PowerMockito.mockStatic(CipherUtil.class);
210 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
212 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
213 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
215 PowerMockito.mockStatic(HttpClients.class);
216 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
218 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
219 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
220 Mockito.when(response.getEntity()).thenReturn(entity);
222 PowerMockito.mockStatic(EntityUtils.class);
223 String responseJson = " { \"response\": \"removed\", \"context_id\": \"200\"} ";
224 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
225 StatusLine statusLine = Mockito.mock(StatusLine.class);
226 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
227 boolean status = sharedContextRestClient.removeSharedContext(contextId, key);
228 Assert.assertTrue(status);
232 public void clearSharedContextTest() throws Exception {
233 String contextId ="Context";
236 app.setUsername("User");
237 String password = "Password";
238 app.setAppPassword(password);
239 Mockito.when(appService.getDefaultApp()).thenReturn(app);
241 PowerMockito.mockStatic(PortalApiProperties.class);
242 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
243 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
245 PowerMockito.mockStatic(CipherUtil.class);
246 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
248 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
249 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
251 PowerMockito.mockStatic(HttpClients.class);
252 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
254 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
255 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
256 Mockito.when(response.getEntity()).thenReturn(entity);
258 PowerMockito.mockStatic(EntityUtils.class);
260 String responseJson = " { \"response\": " + number + " , \"context_id\": \"200\"} ";
261 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
262 StatusLine statusLine = Mockito.mock(StatusLine.class);
263 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
264 int status = sharedContextRestClient.clearSharedContext(contextId);
265 Assert.assertTrue(status == number);
269 public void setSharedContextTest() throws Exception {
270 String contextId ="Context";
272 String value = "Value";
275 app.setUsername("User");
276 String password = "Password";
277 app.setAppPassword(password);
278 Mockito.when(appService.getDefaultApp()).thenReturn(app);
280 PowerMockito.mockStatic(PortalApiProperties.class);
281 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
282 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
284 PowerMockito.mockStatic(CipherUtil.class);
285 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
287 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
288 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
290 PowerMockito.mockStatic(HttpClients.class);
291 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
293 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
294 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
295 Mockito.when(response.getEntity()).thenReturn(entity);
297 PowerMockito.mockStatic(EntityUtils.class);
298 String responseJson = " { \"response\": \"replaced\", \"context_id\": \"200\"} ";
299 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
300 StatusLine statusLine = Mockito.mock(StatusLine.class);
301 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
302 boolean status = sharedContextRestClient.setSharedContext(contextId, key, value);
303 Assert.assertTrue(status);