2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
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;
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;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.servlet.http.HttpServletResponse;
54 import javax.servlet.http.HttpSession;
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;
86 @RunWith(PowerMockRunner.class)
87 @PrepareForTest({ SystemProperties.class, CipherUtil.class, AppUtils.class, UserUtils.class, EPCommonSystemProperties.class})
88 public class LoginControllerTest {
91 EPProfileService service;
93 EPLoginService loginService;
95 SharedContextService sharedContextService;
97 EPRoleService roleService;
99 EPRoleFunctionService ePRoleFunctionService;
102 LoginController loginController = new LoginController();
105 public void setup() {
106 MockitoAnnotations.initMocks(this);
109 MockEPUser mockUser = new MockEPUser();
111 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
113 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
114 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
116 NullPointerException nullPointerException = new NullPointerException();
119 public void loginIfAuthNullTest() {
120 PowerMockito.mockStatic(SystemProperties.class);
121 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null);
122 ModelAndView result = loginController.login(mockedRequest);
123 assertEquals(result.getViewName(), "openIdLogin");
127 public void loginIfAuthOIDCTest() {
128 PowerMockito.mockStatic(SystemProperties.class);
129 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
130 ModelAndView result = loginController.login(mockedRequest);
131 assertEquals(result.getViewName(), "login");
135 public void loginTest() {
136 PowerMockito.mockStatic(SystemProperties.class);
137 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test");
138 ModelAndView result = loginController.login(mockedRequest);
139 assertEquals(result.getViewName(), "login");
143 public void loginValidateTest() throws Exception {
144 PowerMockito.mockStatic(SystemProperties.class);
145 PowerMockito.mockStatic(AppUtils.class);
146 PowerMockito.mockStatic(UserUtils.class);
147 PowerMockito.mockStatic(CipherUtil.class);
148 PowerMockito.mockStatic(EPCommonSystemProperties.class);
149 EPUser user = mockUser.mockEPUser();
150 HttpServletRequest request = mock(HttpServletRequest.class);
151 HttpServletResponse response = mock(HttpServletResponse.class);
152 HttpSession session = mock(HttpSession.class);
153 String json = "{\"loginId\":\"test\", \"password\":\"xyz\"}";
154 when(request.getInputStream()).thenReturn(
155 new DelegatingServletInputStream(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8))));
156 when(request.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
157 when(request.getContentType()).thenReturn("application/json");
158 when(request.getCharacterEncoding()).thenReturn("UTF-8");
159 when(request.getAttribute("menu_properties_filename")).thenReturn("test");
160 StringBuffer reqUrl = new StringBuffer("http://localhost.com");
161 when(request.getRequestURL()).thenReturn(reqUrl);
162 when(request.getQueryString()).thenReturn("demo?test");
163 when(request.getSession(true)).thenReturn(session);
164 when(request.getSession()).thenReturn(session);
165 EPLoginBean commandBean = new EPLoginBean();
166 commandBean.setLoginId("guestT");
167 commandBean.setUser(user);
168 commandBean.setOrgUserId("guestT");
169 commandBean.setLoginPwd("xyz");
170 Set<MenuData> menus = new HashSet<MenuData>();
171 MenuData menuData = new MenuData();
172 menuData.setFunctionCd("test");
173 MenuData menuData2 = new MenuData();
174 menuData2.setFunctionCd("test2");
176 menus.add(menuData2);
177 commandBean.setMenu(menus);
178 commandBean.setBusinessDirectMenu(menus);
179 when(loginController.getLoginService().findUser(Matchers.any(EPLoginBean.class), Matchers.anyString(),
180 Matchers.any())).thenReturn(commandBean);
181 when(AppUtils.getSession(request)).thenReturn(session);
182 when(UserUtils.isAccessible(request, menuData.getFunctionCd())).thenReturn(true);
183 when(UserUtils.isAccessible(request, menuData2.getFunctionCd())).thenReturn(true);
184 when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.COOKIE_DOMAIN)).thenReturn("cookie_domain");
185 when(CipherUtil.encryptPKC(Matchers.anyString(), Matchers.anyString())).thenReturn("guestT");
186 String actual = loginController.loginValidate(request, response);
187 JSONObject expected = new JSONObject("{success: success}");
188 assertNotEquals(actual, expected);