Add doc folder.
[portal.git] / ecomp-portal-BE-common-test / src / main / java / org / openecomp / portalapp / portal / test / service / ExternalAccessRolesServiceImplTest.java
1 package org.openecomp.portalapp.portal.test.service;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertFalse;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.InjectMocks;
19 import org.mockito.Matchers;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.MockitoAnnotations;
23 import org.openecomp.portalapp.portal.domain.CentralRoleFunction;
24 import org.openecomp.portalapp.portal.domain.EPApp;
25 import org.openecomp.portalapp.portal.domain.EPAppRoleFunction;
26 import org.openecomp.portalapp.portal.domain.EPRole;
27 import org.openecomp.portalapp.portal.domain.EPUser;
28 import org.openecomp.portalapp.portal.service.ExternalAccessRolesServiceImpl;
29 import org.openecomp.portalapp.portal.test.core.MockEPUser;
30 import org.openecomp.portalapp.portal.transport.ExternalAccessPerms;
31 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
32 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
33 import org.openecomp.portalapp.test.framework.MockitoTestSuite;
34 import org.openecomp.portalsdk.core.domain.Role;
35 import org.openecomp.portalsdk.core.service.DataAccessService;
36 import org.openecomp.portalsdk.core.service.DataAccessServiceImpl;
37 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41 import org.springframework.http.HttpEntity;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpMethod;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.web.client.HttpClientErrorException;
47 import org.springframework.web.client.RestTemplate;
48
49 @RunWith(PowerMockRunner.class)
50 @PrepareForTest({ EcompPortalUtils.class, SystemProperties.class, EPCommonSystemProperties.class })
51 public class ExternalAccessRolesServiceImplTest {
52         @Mock
53         DataAccessService dataAccessService1 = new DataAccessServiceImpl();
54
55         @Mock
56         RestTemplate template = new RestTemplate();
57
58         @InjectMocks
59         ExternalAccessRolesServiceImpl externalAccessRolesServiceImpl = new ExternalAccessRolesServiceImpl();
60
61         @Before
62         public void setup() {
63                 MockitoAnnotations.initMocks(this);
64         }
65
66         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
67
68         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
69         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
70         NullPointerException nullPointerException = new NullPointerException();
71         MockEPUser mockUser = new MockEPUser();
72
73         public EPApp mockApp() {
74                 EPApp app = new EPApp();
75                 app.setName("Test");
76                 app.setImageUrl("test");
77                 app.setDescription("test");
78                 app.setNotes("test");
79                 app.setUrl("test");
80                 app.setId((long) 10);
81                 app.setAppRestEndpoint("test");
82                 app.setAlternateUrl("test");
83                 app.setName("test");
84                 app.setMlAppName("test");
85                 app.setMlAppAdminId("test");
86                 app.setUsername("test");
87                 app.setAppPassword("test");
88                 app.setOpen(false);
89                 app.setEnabled(false);
90                 app.setUebKey("test");
91                 app.setUebSecret("test");
92                 app.setUebTopicName("test");
93                 app.setAppType(1);
94                 return app;
95         }
96
97         @Test
98         public void getAppRolesIfAppIsPortalTest() throws Exception {
99                 List<EPRole> applicationRoles = new ArrayList<>();
100                 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
101                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 1);
102                 assertEquals(expectedApplicationRoles, applicationRoles);
103         }
104
105         @Test
106         public void getAppRolesTest() throws Exception {
107                 List<EPRole> applicationRoles = new ArrayList<>();
108                 Mockito.when(dataAccessService1.getList(EPRole.class, "test", null, null)).thenReturn(applicationRoles);
109                 List<EPRole> expectedApplicationRoles = externalAccessRolesServiceImpl.getAppRoles((long) 10);
110                 assertEquals(expectedApplicationRoles, applicationRoles);
111         }
112
113         // @SuppressWarnings("null")
114         // @Test(expected = java.lang.Exception.class)
115         // public void getAppRolesExceptionTest() throws Exception{
116         // List<EPRole> applicationRoles = new ArrayList<>();
117         // DataAccessService dataAccessService = null ;
118         // Mockito.when(dataAccessService.getList(EPRole.class, "where app_id = 10",
119         // null, null)).thenThrow(nullPointerException);
120         // List<EPRole> expectedApplicationRoles =
121         // externalAccessRolesServiceImpl.getAppRoles((long) 10);
122         // assertEquals(expectedApplicationRoles,applicationRoles);
123         // }
124
125         @Test(expected = java.lang.Exception.class)
126         public void getAppExceptionTest() throws Exception {
127                 List<EPApp> app = new ArrayList<>();
128                 String uebKey = "test-ueb-key";
129                 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
130                                 .thenReturn(app);
131                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
132                 assertEquals(app, expectedapp);
133         }
134
135         @Test(expected = java.lang.Exception.class)
136         public void getAppTest() throws Exception {
137                 List<EPApp> appList = new ArrayList<>();
138                 EPApp app = mockApp();
139                 appList.add(app);
140                 String uebKey = "test-ueb-key";
141                 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
142                                 .thenReturn(appList);
143                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
144         }
145
146         @Test
147         public void getAppErrorTest() throws Exception {
148                 List<EPApp> appList = new ArrayList<>();
149                 EPApp app = mockApp();
150                 app.setId((long) 1);
151                 appList.add(app);
152                 String uebKey = "test-ueb-key";
153                 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
154                                 .thenReturn(appList);
155                 List<EPApp> expectedapp = externalAccessRolesServiceImpl.getApp(uebKey);
156                 assertEquals(appList, expectedapp);
157         }
158
159         @Test
160         public void addRoleTest() throws Exception {
161                 HttpHeaders headers = new HttpHeaders();
162                 PowerMockito.mockStatic(EcompPortalUtils.class);
163                 PowerMockito.mockStatic(SystemProperties.class);
164                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
165                 String uebKey = "test-ueb-key";
166                 Role role = new Role();
167                 role.setId((long) 25);
168                 EPApp app = mockApp();
169                 app.setEnabled(true);
170                 app.setId((long) 10);
171                 app.setNameSpace("test_namesapce");
172                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
173                 List<EPApp> appList = new ArrayList<>();
174                 appList.add(app);
175                 List<EPRole> roleList = new ArrayList<>();
176                 EPRole ePRole = new EPRole();
177                 role.setName("Test Role");
178                 roleList.add(ePRole);
179                 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
180                                 .thenReturn(appList);
181                 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
182                                 .thenReturn(roleList);
183                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
184                                 .thenReturn("Testurl");
185                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.CREATED);
186                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
187                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
188
189                 assertTrue(externalAccessRolesServiceImpl.addRole(role, uebKey));
190         }
191
192         @Test
193         public void addRoleMethodNotAllowedTest() throws Exception {
194                 HttpHeaders headers = new HttpHeaders();
195                 PowerMockito.mockStatic(EcompPortalUtils.class);
196                 PowerMockito.mockStatic(SystemProperties.class);
197                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
198                 String uebKey = "test-ueb-key";
199                 Role role = new Role();
200                 role.setId((long) 25);
201                 EPApp app = mockApp();
202                 app.setEnabled(true);
203                 app.setId((long) 10);
204                 app.setNameSpace("test_namesapce");
205                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
206                 List<EPApp> appList = new ArrayList<>();
207                 appList.add(app);
208                 List<EPRole> roleList = new ArrayList<>();
209                 EPRole ePRole = new EPRole();
210                 role.setName("Test Role");
211                 roleList.add(ePRole);
212                 Mockito.when(dataAccessService1.getList(EPApp.class, " where ueb_key = '" + uebKey + "'", null, null))
213                                 .thenReturn(appList);
214                 Mockito.when(dataAccessService1.getList(EPRole.class, " where app_role_id = " + role.getId(), null, null))
215                                 .thenReturn(roleList);
216                 Mockito.when(SystemProperties.getProperty(EPCommonSystemProperties.EXTERNAL_CENTRAL_ACCESS_URL))
217                                 .thenReturn("Testurl");
218                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED);
219                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.POST),
220                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
221
222                 assertFalse(externalAccessRolesServiceImpl.addRole(role, uebKey));
223         }
224
225         public EPApp getApp() {
226                 EPApp app = new EPApp();
227                 app.setName("Test");
228                 app.setImageUrl("test");
229                 app.setDescription("test");
230                 app.setNotes("test");
231                 app.setUrl("test");
232                 app.setId((long) 1);
233                 app.setAppRestEndpoint("test");
234                 app.setAlternateUrl("test");
235                 app.setName("test");
236                 app.setMlAppName("test");
237                 app.setMlAppAdminId("test");
238                 app.setUsername("test");
239                 app.setAppPassword("test");
240                 app.setOpen(true);
241                 app.setEnabled(false);
242                 app.setUebKey("test");
243                 app.setUebSecret("test");
244                 app.setUebTopicName("test");
245                 app.setAppType(1);
246                 return app;
247         }
248
249         @Test
250         public void deleteCentralRoleFunctionTest() throws Exception {
251                 final Map<String, String> params = new HashMap<>();
252                 EPApp app = mockApp();
253                 params.put("functionCd", "menu_fun_code");
254                 params.put("appId", String.valueOf(10));
255                 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
256                 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
257                 domainCentralRoleFunction.setCode("menu_fun_code");
258                 centralRoleFunctionList.add(domainCentralRoleFunction);
259                 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
260                                 .thenReturn(centralRoleFunctionList);
261                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
262                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
263                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
264                 HttpHeaders headers = new HttpHeaders();
265                 PowerMockito.mockStatic(EcompPortalUtils.class);
266                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
267
268                 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
269                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
270
271                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
272         }
273
274         @Test
275         public void deleteCentralRoleFunctionFailTest() throws Exception {
276                 final Map<String, String> params = new HashMap<>();
277                 EPApp app = mockApp();
278                 params.put("functionCd", "menu_fun_code");
279                 params.put("appId", String.valueOf(10));
280                 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
281                 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
282                 domainCentralRoleFunction.setCode("menu_fun_code");
283                 centralRoleFunctionList.add(domainCentralRoleFunction);
284                 HttpClientErrorException httpClientErrorException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
285                 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
286                                 .thenReturn(centralRoleFunctionList);
287                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.NOT_FOUND);
288                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.DELETE),
289                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenThrow(httpClientErrorException);
290                 HttpHeaders headers = new HttpHeaders();
291                 PowerMockito.mockStatic(EcompPortalUtils.class);
292                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
293
294                 Mockito.doNothing().when(dataAccessService1).deleteDomainObjects(EPAppRoleFunction.class,
295                                 "app_id = " + app.getId() + " and function_cd = '" + "menu_fun_code" + "'", null);
296
297                 assertTrue(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
298         }
299
300         @Test
301         public void deleteCentralRoleFunctionExceptionTest() {
302                 final Map<String, String> params = new HashMap<>();
303                 EPApp app = mockApp();
304                 params.put("functionCd", "menu_fun_code");
305                 params.put("appId", String.valueOf(10));
306                 List<CentralRoleFunction> centralRoleFunctionList = new ArrayList<>();
307                 CentralRoleFunction domainCentralRoleFunction = new CentralRoleFunction();
308                 domainCentralRoleFunction.setCode("menu_fun_code");
309                 centralRoleFunctionList.add(domainCentralRoleFunction);
310                 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params, null))
311                                 .thenThrow(nullPointerException);
312                 assertFalse(externalAccessRolesServiceImpl.deleteCentralRoleFunction("menu_fun_code", app));
313         }
314
315         @Test
316         public void getUserTest() {
317                 List<EPUser> userList = new ArrayList<>();
318                 EPUser user = mockUser.mockEPUser();
319                 userList.add(user);
320                 Mockito.when(dataAccessService1.getList(EPUser.class, " where org_user_id = '" + "guestT" + "'", null, null))
321                                 .thenReturn(userList);
322                 List<EPUser> expectedUserList = externalAccessRolesServiceImpl.getUser("guestT");
323                 assertEquals(expectedUserList, userList);
324         }
325
326         public void saveCentralRoleFunctionTest() throws Exception {
327                 CentralRoleFunction centralRoleFunction = new CentralRoleFunction();
328                 centralRoleFunction.setCode("menu_test");
329                 EPApp app = mockApp();
330                 app.setId((long) 1);
331                 final Map<String, String> params = new HashMap<>();
332                 params.put("functionCd", "menu_test");
333                 params.put("appId", String.valueOf(1));
334                 ExternalAccessPerms extPerms = new ExternalAccessPerms();
335                 PowerMockito.mockStatic(EcompPortalUtils.class);
336                 HttpHeaders headers = new HttpHeaders();
337                 Mockito.when(EcompPortalUtils.base64encodeKeyForAAFBasicAuth()).thenReturn(headers);
338                 List<CentralRoleFunction> appRoleFunc  = new ArrayList<>();
339                 appRoleFunc.add(centralRoleFunction);
340                 Mockito.when(dataAccessService1.executeNamedQuery("getAppFunctionDetails", params,
341                                 null)).thenReturn(appRoleFunc);
342                 ResponseEntity<String> addResponse = new ResponseEntity<>(HttpStatus.OK);
343                 Mockito.when(template.exchange(Matchers.anyString(), Matchers.eq(HttpMethod.GET),
344                                 Matchers.<HttpEntity<String>>any(), Matchers.eq(String.class))).thenReturn(addResponse);
345         }
346 }