c903d12c55dcb5e68be4a788021c0bc2293bbf0b
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / service / RemoteWebServiceCallServiceImplTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 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.portalapp.portal.service;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpServletResponse;
48
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.onap.portalapp.portal.domain.EPApp;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.service.RemoteWebServiceCallServiceImpl;
59 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
60 import org.onap.portalsdk.core.service.DataAccessService;
61 import org.onap.portalsdk.core.util.SystemProperties;
62 import org.powermock.api.mockito.PowerMockito;
63 import org.powermock.core.classloader.annotations.PrepareForTest;
64 import org.powermock.modules.junit4.PowerMockRunner;
65
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({ CipherUtil.class , SystemProperties.class})
68 public class RemoteWebServiceCallServiceImplTest {
69         
70
71         @InjectMocks
72         RemoteWebServiceCallServiceImpl remoteWebServiceCallServiceImpl = new RemoteWebServiceCallServiceImpl();
73
74         @Mock
75         AppsCacheService appCacheService;
76         
77         @Mock
78         DataAccessService dataAccessService;
79
80         @Before
81         public void setup() {
82                 MockitoAnnotations.initMocks(this);
83         }
84
85         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
86
87         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
88         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
89
90         NullPointerException nullPointerException = new NullPointerException();
91         
92         @Test
93         public void verifyRESTCredentialTest() throws Exception
94         {
95                 PowerMockito.mockStatic(CipherUtil.class);
96                 PowerMockito.mockStatic(SystemProperties.class);
97                 String criteria= " where ueb_key = 'requestUebKey'";
98                 List<EPApp> appList = new ArrayList<>();
99                 EPApp app = new EPApp();
100                 app.setAppPassword("password");
101                 appList.add(app);
102                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
103                 String secretKey = null;
104                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
105                 Mockito.when(CipherUtil.decryptPKC("password",
106                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
107                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","requestPassword"));
108         }
109         
110         @Test
111         public void verifyRESTCredentialExceptionTest() throws Exception
112         {
113                 PowerMockito.mockStatic(CipherUtil.class);
114                 PowerMockito.mockStatic(SystemProperties.class);
115                 String criteria= " where ueb_key = 'requestUebKey'";
116                 List<EPApp> appList = new ArrayList<>();
117                 EPApp app = new EPApp();
118                 app.setAppPassword("password");
119                 app.setUsername("requestAppName");
120                 appList.add(app);
121                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
122                 String secretKey = null;
123                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
124                 Mockito.when(CipherUtil.decryptPKC("password",
125                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
126                 assertTrue(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
127         }
128         
129         @Test
130         public void verifyRESTCredentialIfAppNullTest() throws Exception
131         {
132                 PowerMockito.mockStatic(CipherUtil.class);
133                 PowerMockito.mockStatic(SystemProperties.class);
134                 String criteria= " where ueb_key = 'requestUebKey'";
135                 List<EPApp> appList = new ArrayList<>();
136                 EPApp app = new EPApp();
137                 app.setAppPassword("password");
138                 app.setUsername("requestAppName");
139                 appList.add(app);
140                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
141                 String secretKey = null;
142                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
143                 Mockito.when(CipherUtil.decryptPKC("password",
144                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
145                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
146         }
147         
148         @Test
149         public void verifyAppKeyCredentialIfKeyIsNullTest() throws Exception
150         {
151                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential(null));
152         }
153         
154         @Test
155         public void verifyAppKeyCredentialTest() throws Exception
156         {
157                 PowerMockito.mockStatic(CipherUtil.class);
158                 PowerMockito.mockStatic(SystemProperties.class);
159                 StringBuffer criteria = new  StringBuffer("where ueb_key = 'requestUebKey'");
160 //              String criteria= " where ueb_key = 'requestUebKey'";
161                 List<EPApp> appList = new ArrayList<>();
162                 EPApp app = new EPApp();
163                 app.setAppPassword("password");
164                 app.setUsername("requestAppName");
165                 appList.add(app);
166                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
167                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
168         }
169         
170         @Test
171         public void verifyAppKeyCredentialSuccessTest() throws Exception
172         {
173                 PowerMockito.mockStatic(CipherUtil.class);
174                 PowerMockito.mockStatic(SystemProperties.class);
175                 String criteria= " where ueb_key = 'test'";
176                 List<EPApp> appList = new ArrayList<>();
177                 EPApp app = new EPApp();
178                 app.setAppPassword("password");
179                 app.setUsername("requestAppName");
180                 appList.add(app);
181                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
182                 assertTrue(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
183         }
184 }