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============================================
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.scheduler.CoreRegister;
60 import org.onap.portalsdk.core.service.AppService;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({PortalApiProperties.class, CipherUtil.class, HttpClients.class, EntityUtils.class})
67 public class SharedContextRestClientTest {
70 private SharedContextRestClient sharedContextRestClient;
73 private AppService appService;
75 @Test(expected = IllegalArgumentException.class)
76 public void getContextValueExceptionTest() throws Exception {
77 String contextId= "\123";
79 sharedContextRestClient.getContextValue(contextId, key);
83 public void getContextValueTest() throws Exception {
84 String contextId= "\123";
87 PowerMockito.mockStatic(PortalApiProperties.class);
88 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
91 app.setUsername("User");
92 String password = "Password";
93 app.setAppPassword(password);
94 Mockito.when(appService.getDefaultApp()).thenReturn(app);
96 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
98 PowerMockito.mockStatic(CipherUtil.class);
99 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
101 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
102 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
104 PowerMockito.mockStatic(HttpClients.class);
105 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
107 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
108 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
109 Mockito.when(response.getEntity()).thenReturn(entity);
111 PowerMockito.mockStatic(EntityUtils.class);
112 String responseJson = " { \"response\": \"Success\", \"context_id\": \"200\"}";
113 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
114 StatusLine statusLine = Mockito.mock(StatusLine.class);
115 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
116 SharedContext context = sharedContextRestClient.getContextValue(contextId, key);
117 Assert.assertNull(context);
121 public void getUserContextTest() throws Exception {
122 String contextId = "234";
124 PowerMockito.mockStatic(PortalApiProperties.class);
125 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
128 app.setUsername("User");
129 String password = "Password";
130 app.setAppPassword(password);
131 Mockito.when(appService.getDefaultApp()).thenReturn(app);
133 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
135 PowerMockito.mockStatic(CipherUtil.class);
136 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
138 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
139 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
141 PowerMockito.mockStatic(HttpClients.class);
142 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
144 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
145 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
146 Mockito.when(response.getEntity()).thenReturn(entity);
148 PowerMockito.mockStatic(EntityUtils.class);
149 String responseJson = " [ { \"response\": \"Success\", \"context_id\": \"200\"} ]";
150 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
151 StatusLine statusLine = Mockito.mock(StatusLine.class);
152 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
154 List<SharedContext> contextList = sharedContextRestClient.getUserContext(contextId);
155 Assert.assertNotNull(contextList);
159 public void checkSharedContextTest() throws Exception {
160 String contextId ="Context";
164 app.setUsername("User");
165 String password = "Password";
166 app.setAppPassword(password);
167 Mockito.when(appService.getDefaultApp()).thenReturn(app);
169 PowerMockito.mockStatic(PortalApiProperties.class);
170 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
171 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
173 PowerMockito.mockStatic(CipherUtil.class);
174 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
176 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
177 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
179 PowerMockito.mockStatic(HttpClients.class);
180 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
182 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
183 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
184 Mockito.when(response.getEntity()).thenReturn(entity);
186 PowerMockito.mockStatic(EntityUtils.class);
187 String responseJson = " { \"response\": \"exists\", \"context_id\": \"200\"} ";
188 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
189 StatusLine statusLine = Mockito.mock(StatusLine.class);
190 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
191 boolean status = sharedContextRestClient.checkSharedContext(contextId, key);
192 Assert.assertTrue(status);
196 public void removeSharedContextTest() throws Exception {
197 String contextId ="Context";
201 app.setUsername("User");
202 String password = "Password";
203 app.setAppPassword(password);
204 Mockito.when(appService.getDefaultApp()).thenReturn(app);
206 PowerMockito.mockStatic(PortalApiProperties.class);
207 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
208 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
210 PowerMockito.mockStatic(CipherUtil.class);
211 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
213 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
214 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
216 PowerMockito.mockStatic(HttpClients.class);
217 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
219 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
220 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
221 Mockito.when(response.getEntity()).thenReturn(entity);
223 PowerMockito.mockStatic(EntityUtils.class);
224 String responseJson = " { \"response\": \"removed\", \"context_id\": \"200\"} ";
225 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
226 StatusLine statusLine = Mockito.mock(StatusLine.class);
227 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
228 boolean status = sharedContextRestClient.removeSharedContext(contextId, key);
229 Assert.assertTrue(status);
233 public void clearSharedContextTest() throws Exception {
234 String contextId ="Context";
237 app.setUsername("User");
238 String password = "Password";
239 app.setAppPassword(password);
240 Mockito.when(appService.getDefaultApp()).thenReturn(app);
242 PowerMockito.mockStatic(PortalApiProperties.class);
243 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
244 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
246 PowerMockito.mockStatic(CipherUtil.class);
247 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
249 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
250 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
252 PowerMockito.mockStatic(HttpClients.class);
253 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
255 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
256 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
257 Mockito.when(response.getEntity()).thenReturn(entity);
259 PowerMockito.mockStatic(EntityUtils.class);
261 String responseJson = " { \"response\": " + number + " , \"context_id\": \"200\"} ";
262 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
263 StatusLine statusLine = Mockito.mock(StatusLine.class);
264 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
265 int status = sharedContextRestClient.clearSharedContext(contextId);
266 Assert.assertTrue(status == number);
270 public void setSharedContextTest() throws Exception {
271 String contextId ="Context";
273 String value = "Value";
276 app.setUsername("User");
277 String password = "Password";
278 app.setAppPassword(password);
279 Mockito.when(appService.getDefaultApp()).thenReturn(app);
281 PowerMockito.mockStatic(PortalApiProperties.class);
282 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
283 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
285 PowerMockito.mockStatic(CipherUtil.class);
286 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
288 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
289 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
291 PowerMockito.mockStatic(HttpClients.class);
292 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
294 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
295 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
296 Mockito.when(response.getEntity()).thenReturn(entity);
298 PowerMockito.mockStatic(EntityUtils.class);
299 String responseJson = " { \"response\": \"replaced\", \"context_id\": \"200\"} ";
300 Mockito.when(EntityUtils.toString(entity)).thenReturn(responseJson);
301 StatusLine statusLine = Mockito.mock(StatusLine.class);
302 Mockito.when(response.getStatusLine()).thenReturn(statusLine);
303 boolean status = sharedContextRestClient.setSharedContext(contextId, key, value);
304 Assert.assertTrue(status);