db6cde7c7169c7b7dfdf568777ba584f143e69c2
[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.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;
64
65 @RunWith(PowerMockRunner.class)
66 @PrepareForTest({PortalApiProperties.class, CipherUtil.class, HttpClients.class, EntityUtils.class})
67 public class SharedContextRestClientTest {
68
69         @InjectMocks
70         private SharedContextRestClient sharedContextRestClient;
71         
72         @Mock
73         private AppService appService;
74         
75         @Test(expected = IllegalArgumentException.class)
76         public void getContextValueExceptionTest() throws Exception {
77                 String contextId= "\123";
78                 String key = "key";
79                 sharedContextRestClient.getContextValue(contextId, key);
80         }
81         
82         @Test
83         public void getContextValueTest() throws Exception {
84                 String contextId= "\123";
85                 String key = "key";
86                 
87                 PowerMockito.mockStatic(PortalApiProperties.class);
88                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
89                 
90                 App app = new App();
91                 app.setUsername("User");
92                 String password = "Password";
93                 app.setAppPassword(password);
94                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
95                 
96                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
97                 
98                 PowerMockito.mockStatic(CipherUtil.class);
99                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
100                 
101                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
102                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
103                 
104                 PowerMockito.mockStatic(HttpClients.class);
105                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
106                 
107                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
108                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
109                 Mockito.when(response.getEntity()).thenReturn(entity);
110                 
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);
118         }
119         
120         @Test
121         public void getUserContextTest() throws Exception {
122                 String contextId = "234";
123                 
124                 PowerMockito.mockStatic(PortalApiProperties.class);
125                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REST_URL)).thenReturn("ResetURL/");
126                 
127                 App app = new App();
128                 app.setUsername("User");
129                 String password = "Password";
130                 app.setAppPassword(password);
131                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
132                 
133                 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("Key");
134                 
135                 PowerMockito.mockStatic(CipherUtil.class);
136                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
137                 
138                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
139                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
140                 
141                 PowerMockito.mockStatic(HttpClients.class);
142                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
143                 
144                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
145                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
146                 Mockito.when(response.getEntity()).thenReturn(entity);
147                 
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);
153                 
154                 List<SharedContext>     contextList = sharedContextRestClient.getUserContext(contextId);
155                 Assert.assertNotNull(contextList);
156         }
157         
158         @Test
159         public void checkSharedContextTest() throws Exception {
160                 String contextId ="Context";
161                 String key = "Key";
162                 
163                 App app = new App();
164                 app.setUsername("User");
165                 String password = "Password";
166                 app.setAppPassword(password);
167                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
168                 
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");
172                 
173                 PowerMockito.mockStatic(CipherUtil.class);
174                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
175                 
176                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
177                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
178                 
179                 PowerMockito.mockStatic(HttpClients.class);
180                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
181                 
182                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
183                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
184                 Mockito.when(response.getEntity()).thenReturn(entity);
185                 
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);
193         }
194         
195         @Test
196         public void removeSharedContextTest() throws Exception {
197                 String contextId ="Context";
198                 String key = "Key";
199                 
200                 App app = new App();
201                 app.setUsername("User");
202                 String password = "Password";
203                 app.setAppPassword(password);
204                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
205                 
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");
209                 
210                 PowerMockito.mockStatic(CipherUtil.class);
211                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
212                 
213                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
214                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
215                 
216                 PowerMockito.mockStatic(HttpClients.class);
217                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
218                 
219                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
220                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
221                 Mockito.when(response.getEntity()).thenReturn(entity);
222                 
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);
230         }
231         
232         @Test
233         public void clearSharedContextTest() throws Exception {
234                 String contextId ="Context";
235                 
236                 App app = new App();
237                 app.setUsername("User");
238                 String password = "Password";
239                 app.setAppPassword(password);
240                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
241                 
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");
245                 
246                 PowerMockito.mockStatic(CipherUtil.class);
247                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
248                 
249                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
250                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
251                 
252                 PowerMockito.mockStatic(HttpClients.class);
253                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
254                 
255                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
256                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
257                 Mockito.when(response.getEntity()).thenReturn(entity);
258                 
259                 PowerMockito.mockStatic(EntityUtils.class);
260                 int number = 123;
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);
267         }
268         
269         @Test
270         public void setSharedContextTest() throws Exception {
271                 String contextId ="Context";
272                 String key ="Key";
273                 String value = "Value";
274                 
275                 App app = new App();
276                 app.setUsername("User");
277                 String password = "Password";
278                 app.setAppPassword(password);
279                 Mockito.when(appService.getDefaultApp()).thenReturn(app);
280                 
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");
284                 
285                 PowerMockito.mockStatic(CipherUtil.class);
286                 Mockito.when(CipherUtil.decryptPKC(app.getAppPassword())).thenReturn(password);
287                 
288                 CloseableHttpClient httpClient = PowerMockito.mock(CloseableHttpClient.class);
289                 CloseableHttpResponse response = PowerMockito.mock(CloseableHttpResponse.class);
290                 
291                 PowerMockito.mockStatic(HttpClients.class);
292                 Mockito.when(HttpClients.createDefault()).thenReturn(httpClient);
293                 
294                 Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
295                 HttpEntity entity = PowerMockito.mock(HttpEntity.class);
296                 Mockito.when(response.getEntity()).thenReturn(entity);
297                 
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);
305         }
306         
307 }