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