Bulk upload changes and music health check apis
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / controller / LoginControllerTest.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  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotEquals;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44
45 import java.io.BufferedReader;
46 import java.io.ByteArrayInputStream;
47 import java.io.StringReader;
48 import java.nio.charset.StandardCharsets;
49 import java.util.HashSet;
50 import java.util.Set;
51
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import javax.servlet.http.HttpSession;
55
56 import org.json.JSONObject;
57 import org.junit.Before;
58 import org.junit.Test;
59 import org.junit.runner.RunWith;
60 import org.mockito.InjectMocks;
61 import org.mockito.Matchers;
62 import org.mockito.Mock;
63 import org.mockito.MockitoAnnotations;
64 import org.onap.portalapp.command.EPLoginBean;
65 import org.onap.portalapp.controller.LoginController;
66 import org.onap.portalapp.portal.core.MockEPUser;
67 import org.onap.portalapp.portal.domain.EPUser;
68 import org.onap.portalapp.portal.framework.MockitoTestSuite;
69 import org.onap.portalapp.portal.service.EPLoginService;
70 import org.onap.portalapp.portal.service.EPRoleFunctionService;
71 import org.onap.portalapp.portal.service.EPRoleService;
72 import org.onap.portalapp.portal.service.SharedContextService;
73 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
74 import org.onap.portalapp.service.EPProfileService;
75 import org.onap.portalsdk.core.domain.MenuData;
76 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
77 import org.onap.portalsdk.core.util.SystemProperties;
78 import org.onap.portalsdk.core.web.support.AppUtils;
79 import org.onap.portalsdk.core.web.support.UserUtils;
80 import org.powermock.api.mockito.PowerMockito;
81 import org.powermock.core.classloader.annotations.PrepareForTest;
82 import org.powermock.modules.junit4.PowerMockRunner;
83 import org.springframework.mock.web.DelegatingServletInputStream;
84 import org.springframework.web.servlet.ModelAndView;
85
86 @RunWith(PowerMockRunner.class)
87 @PrepareForTest({ SystemProperties.class, CipherUtil.class, AppUtils.class, UserUtils.class, EPCommonSystemProperties.class})
88 public class LoginControllerTest {
89
90         @Mock
91         EPProfileService service;
92         @Mock
93         EPLoginService loginService;
94         @Mock
95         SharedContextService sharedContextService;
96         @Mock
97         EPRoleService roleService;
98         @Mock
99         EPRoleFunctionService ePRoleFunctionService;
100
101         @InjectMocks
102         LoginController loginController = new LoginController();
103
104         @Before
105         public void setup() {
106                 MockitoAnnotations.initMocks(this);
107         }
108
109         MockEPUser mockUser = new MockEPUser();
110
111         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
112
113         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
114         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
115
116         NullPointerException nullPointerException = new NullPointerException();
117         private DelegatingServletInputStream dsi;
118
119         @Test
120         public void loginIfAuthNullTest() {
121                 PowerMockito.mockStatic(SystemProperties.class);
122                 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null);
123                 ModelAndView result = loginController.login(mockedRequest);
124                 assertEquals(result.getViewName(), "openIdLogin");
125         }
126
127         @Test
128         public void loginIfAuthOIDCTest() {
129                 PowerMockito.mockStatic(SystemProperties.class);
130                 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
131                 ModelAndView result = loginController.login(mockedRequest);
132                 assertEquals(result.getViewName(), "login");
133         }
134
135         @Test
136         public void loginTest() {
137                 PowerMockito.mockStatic(SystemProperties.class);
138                 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test");
139                 ModelAndView result = loginController.login(mockedRequest);
140                 assertEquals(result.getViewName(), "login");
141         }
142
143         @Test
144         public void loginValidateTest() throws Exception {
145                 PowerMockito.mockStatic(SystemProperties.class);
146                 PowerMockito.mockStatic(AppUtils.class);
147                 PowerMockito.mockStatic(UserUtils.class);
148                 PowerMockito.mockStatic(CipherUtil.class);
149                 PowerMockito.mockStatic(EPCommonSystemProperties.class);
150                 EPUser user = mockUser.mockEPUser();
151                 HttpServletRequest request = mock(HttpServletRequest.class);
152                 HttpServletResponse response = mock(HttpServletResponse.class);
153                 HttpSession session = mock(HttpSession.class);
154                 String json = "{\"loginId\":\"test\", \"password\":\"xyz\"}";
155                 dsi = new DelegatingServletInputStream(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
156                 when(request.getInputStream()).thenReturn(dsi);
157                 when(request.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
158                 when(request.getContentType()).thenReturn("application/json");
159                 when(request.getCharacterEncoding()).thenReturn("UTF-8");
160                 when(request.getAttribute("menu_properties_filename")).thenReturn("test");
161                 StringBuffer reqUrl = new StringBuffer("http://localhost.com");
162                 when(request.getRequestURL()).thenReturn(reqUrl);
163                 when(request.getQueryString()).thenReturn("demo?test");
164                 when(request.getSession(true)).thenReturn(session);
165                 when(request.getSession()).thenReturn(session);
166                 EPLoginBean commandBean = new EPLoginBean();
167                 commandBean.setLoginId("guestT");
168                 commandBean.setUser(user);
169                 commandBean.setOrgUserId("guestT");
170                 commandBean.setLoginPwd("xyz");
171                 Set<MenuData> menus = new HashSet<MenuData>();
172                 MenuData menuData = new MenuData();
173                 menuData.setFunctionCd("test");
174                 MenuData menuData2 = new MenuData();
175                 menuData2.setFunctionCd("test2");
176                 menus.add(menuData);
177                 menus.add(menuData2);
178                 commandBean.setMenu(menus);
179                 commandBean.setBusinessDirectMenu(menus);
180                 when(loginController.getLoginService().findUser(Matchers.any(EPLoginBean.class), Matchers.anyString(),
181                                 Matchers.any())).thenReturn(commandBean);
182                 when(AppUtils.getSession(request)).thenReturn(session);
183                 when(UserUtils.isAccessible(request, menuData.getFunctionCd())).thenReturn(true);
184                 when(UserUtils.isAccessible(request, menuData2.getFunctionCd())).thenReturn(true);
185                 when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.COOKIE_DOMAIN)).thenReturn("cookie_domain");
186                 when(CipherUtil.encryptPKC(Matchers.anyString(), Matchers.anyString())).thenReturn("guestT");
187                 String actual = loginController.loginValidate(request, response);
188                 JSONObject expected = new JSONObject("{success: success}");
189                 assertNotEquals(actual, expected);
190         }
191
192 }