d38525b31f044d00d1eb3c59a912f877e8968cba
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / service / RemoteWebServiceCallServiceImplTest.java
1 package org.openecomp.portalapp.portal.service;
2
3 import static org.junit.Assert.*;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.openecomp.portalsdk.core.util.SystemProperties;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.InjectMocks;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.MockitoAnnotations;
19 import org.openecomp.portalapp.portal.domain.EPApp;
20 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
21 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
22 import org.openecomp.portalapp.service.RemoteWebServiceCallServiceImpl;
23 import org.openecomp.portalsdk.core.onboarding.util.CipherUtil;
24 import org.openecomp.portalsdk.core.service.DataAccessService;
25 import org.powermock.api.mockito.PowerMockito;
26 import org.powermock.core.classloader.annotations.PrepareForTest;
27 import org.powermock.modules.junit4.PowerMockRunner;
28
29 @RunWith(PowerMockRunner.class)
30 @PrepareForTest({ CipherUtil.class , SystemProperties.class})
31 public class RemoteWebServiceCallServiceImplTest {
32         
33
34         @InjectMocks
35         RemoteWebServiceCallServiceImpl remoteWebServiceCallServiceImpl = new RemoteWebServiceCallServiceImpl();
36
37         @Mock
38         AppsCacheService appCacheService;
39         
40         @Mock
41         DataAccessService dataAccessService;
42         
43
44         @Before
45         public void setup() {
46                 MockitoAnnotations.initMocks(this);
47         }
48
49         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
50
51         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
52         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
53
54         NullPointerException nullPointerException = new NullPointerException();
55         
56         @Test
57         public void verifyRESTCredentialTest() throws Exception
58         {
59                 PowerMockito.mockStatic(CipherUtil.class);
60                 PowerMockito.mockStatic(SystemProperties.class);
61                 String criteria= " where ueb_key = 'requestUebKey'";
62                 List<EPApp> appList = new ArrayList<>();
63                 EPApp app = new EPApp();
64                 app.setAppPassword("password");
65                 appList.add(app);
66                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
67                 String secretKey = null;
68                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
69                 Mockito.when(CipherUtil.decrypt("password",
70                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
71                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","requestPassword"));
72         }
73         
74         @Test
75         public void verifyRESTCredentialExceptionTest() throws Exception
76         {
77                 PowerMockito.mockStatic(CipherUtil.class);
78                 PowerMockito.mockStatic(SystemProperties.class);
79                 String criteria= " where ueb_key = 'requestUebKey'";
80                 List<EPApp> appList = new ArrayList<>();
81                 EPApp app = new EPApp();
82                 app.setAppPassword("password");
83                 app.setUsername("requestAppName");
84                 appList.add(app);
85                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
86                 String secretKey = null;
87                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
88                 Mockito.when(CipherUtil.decrypt("password",
89                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
90                 assertTrue(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
91         }
92         
93         @Test
94         public void verifyRESTCredentialIfAppNullTest() throws Exception
95         {
96                 PowerMockito.mockStatic(CipherUtil.class);
97                 PowerMockito.mockStatic(SystemProperties.class);
98                 String criteria= " where ueb_key = 'requestUebKey'";
99                 List<EPApp> appList = new ArrayList<>();
100                 EPApp app = new EPApp();
101                 app.setAppPassword("password");
102                 app.setUsername("requestAppName");
103                 appList.add(app);
104                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
105                 String secretKey = null;
106                 Mockito.when(SystemProperties.getProperty(SystemProperties.Decryption_Key)).thenReturn(secretKey);
107                 Mockito.when(CipherUtil.decrypt("password",
108                                 secretKey == null ? null : secretKey)).thenReturn("pwd");
109                 assertFalse(remoteWebServiceCallServiceImpl.verifyRESTCredential(secretKey,"requestUebKey","requestAppName","pwd"));
110         }
111         
112         @Test
113         public void verifyAppKeyCredentialIfKeyIsNullTest() throws Exception
114         {
115                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential(null));
116         }
117         
118         @Test
119         public void verifyAppKeyCredentialTest() throws Exception
120         {
121                 PowerMockito.mockStatic(CipherUtil.class);
122                 PowerMockito.mockStatic(SystemProperties.class);
123                 StringBuffer criteria = new  StringBuffer("where ueb_key = 'requestUebKey'");
124 //              String criteria= " where ueb_key = 'requestUebKey'";
125                 List<EPApp> appList = new ArrayList<>();
126                 EPApp app = new EPApp();
127                 app.setAppPassword("password");
128                 app.setUsername("requestAppName");
129                 appList.add(app);
130                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(null);
131                 assertFalse(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
132         }
133         
134         @Test
135         public void verifyAppKeyCredentialSuccessTest() throws Exception
136         {
137                 PowerMockito.mockStatic(CipherUtil.class);
138                 PowerMockito.mockStatic(SystemProperties.class);
139                 String criteria= " where ueb_key = 'test'";
140                 List<EPApp> appList = new ArrayList<>();
141                 EPApp app = new EPApp();
142                 app.setAppPassword("password");
143                 app.setUsername("requestAppName");
144                 appList.add(app);
145                 Mockito.when(dataAccessService.getList(EPApp.class, criteria.toString(), null, null)).thenReturn(appList);
146                 assertTrue(remoteWebServiceCallServiceImpl.verifyAppKeyCredential("test"));
147         }
148 }