06fe521d6f41cb1b939985827a9581616ea2c2ca
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalsdk.core.restful.client;
39
40 import java.util.List;
41
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;
63
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({PortalApiProperties.class, CipherUtil.class, HttpClients.class, EntityUtils.class})
66 public class SharedContextRestClientTest {
67
68         @InjectMocks
69         private SharedContextRestClient sharedContextRestClient;
70         
71         @Mock
72         private AppService appService;
73         
74         @Test(expected = IllegalArgumentException.class)
75         public void getContextValueExceptionTest() throws Exception {
76                 String contextId= "\123";
77                 String key = "key";
78                 sharedContextRestClient.getContextValue(contextId, key);
79         }
80         
81         @Test
82         public void getContextValueTest() throws Exception {
83                 String contextId= "\123";
84                 String key = "key";
85                 
86                 PowerMockito.mockStatic(PortalApiProperties.class);
87                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
88                 
89                 App app = new App();
90                 app.setUsername("User");
91                 String password = "Password";
92                 app.setAppPassword(password);
93                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
94                 
95                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
96                 
97                 PowerMockito.mockStatic(CipherUtil.class);
98                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
99                 
100                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
101                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
102                 
103                 PowerMockito.mockStatic(HttpClients.class);
104                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
105                 
106                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
107                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
108                 Mockito.when(response.getEntity()).thenReturn(entity);
109                 
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);
117         }
118         
119         @Test
120         public void getUserContextTest() throws Exception {
121                 String contextId = "234";
122                 
123                 PowerMockito.mockStatic(PortalApiProperties.class);
124                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
125                 
126                 App app = new App();
127                 app.setUsername("User");
128                 String password = "Password";
129                 app.setAppPassword(password);
130                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
131                 
132                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
133                 
134                 PowerMockito.mockStatic(CipherUtil.class);
135                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
136                 
137                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
138                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
139                 
140                 PowerMockito.mockStatic(HttpClients.class);
141                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
142                 
143                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
144                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
145                 Mockito.when(response.getEntity()).thenReturn(entity);
146                 
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);
152                 
153                 List<SharedContext>     contextList = sharedContextRestClient.getUserContext(contextId);
154                 Assert.assertNotNull(contextList);
155         }
156         
157         @Test
158         public void checkSharedContextTest() throws Exception {
159                 String contextId ="Context";
160                 String key = "Key";
161                 
162                 App app = new App();
163                 app.setUsername("User");
164                 String password = "Password";
165                 app.setAppPassword(password);
166                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
167                 
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");
171                 
172                 PowerMockito.mockStatic(CipherUtil.class);
173                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
174                 
175                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
176                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
177                 
178                 PowerMockito.mockStatic(HttpClients.class);
179                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
180                 
181                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
182                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
183                 Mockito.when(response.getEntity()).thenReturn(entity);
184                 
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);
192         }
193         
194         @Test
195         public void removeSharedContextTest() throws Exception {
196                 String contextId ="Context";
197                 String key = "Key";
198                 
199                 App app = new App();
200                 app.setUsername("User");
201                 String password = "Password";
202                 app.setAppPassword(password);
203                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
204                 
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");
208                 
209                 PowerMockito.mockStatic(CipherUtil.class);
210                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
211                 
212                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
213                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
214                 
215                 PowerMockito.mockStatic(HttpClients.class);
216                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
217                 
218                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
219                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
220                 Mockito.when(response.getEntity()).thenReturn(entity);
221                 
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);
229         }
230         
231         @Test
232         public void clearSharedContextTest() throws Exception {
233                 String contextId ="Context";
234                 
235                 App app = new App();
236                 app.setUsername("User");
237                 String password = "Password";
238                 app.setAppPassword(password);
239                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
240                 
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");
244                 
245                 PowerMockito.mockStatic(CipherUtil.class);
246                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
247                 
248                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
249                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
250                 
251                 PowerMockito.mockStatic(HttpClients.class);
252                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
253                 
254                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
255                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
256                 Mockito.when(response.getEntity()).thenReturn(entity);
257                 
258                 PowerMockito.mockStatic(EntityUtils.class);
259                 int number = 123;
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);
266         }
267         
268         @Test
269         public void setSharedContextTest() throws Exception {
270                 String contextId ="Context";
271                 String key ="Key";
272                 String value = "Value";
273                 
274                 App app = new App();
275                 app.setUsername("User");
276                 String password = "Password";
277                 app.setAppPassword(password);
278                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
279                 
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");
283                 
284                 PowerMockito.mockStatic(CipherUtil.class);
285                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
286                 
287                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
288                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
289                 
290                 PowerMockito.mockStatic(HttpClients.class);
291                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
292                 
293                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
294                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
295                 Mockito.when(response.getEntity()).thenReturn(entity);
296                 
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);
304         }
305         
306 }