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.authentication;
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
46 import org.junit.Before;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.InjectMocks;
50 import org.mockito.Mock;
51 import org.mockito.Mockito;
52 import org.mockito.MockitoAnnotations;
53 import org.onap.portalapp.authentication.SimpleLoginStrategy;
54 import org.onap.portalapp.command.EPLoginBean;
55 import org.onap.portalapp.portal.domain.EPUser;
56 import org.onap.portalapp.portal.framework.MockitoTestSuite;
57 import org.onap.portalapp.portal.service.EPLoginService;
58 import org.onap.portalapp.portal.service.EPRoleFunctionService;
59 import org.onap.portalapp.portal.service.EPRoleService;
60 import org.onap.portalapp.util.EPUserUtils;
61 import org.onap.portalapp.util.SessionCookieUtil;
62 import org.onap.portalsdk.core.menu.MenuProperties;
63 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
64 import org.onap.portalsdk.core.util.SystemProperties;
65 import org.powermock.api.mockito.PowerMockito;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
68 import org.springframework.util.StringUtils;
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ StringUtils.class, EPUserUtils.class, SessionCookieUtil.class, SystemProperties.class,
72 SessionCookieUtil.class, MenuProperties.class })
73 public class SimpleLoginStrategyTest {
76 SimpleLoginStrategy simpleLoginStrategy = new SimpleLoginStrategy();
79 EPLoginService loginService;
81 EPRoleService roleService;
83 EPRoleFunctionService ePRoleFunctionService;
87 MockitoAnnotations.initMocks(this);
90 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
92 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
93 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
95 NullPointerException nullPointerException = new NullPointerException();
97 @Test(expected = Exception.class)
98 public void loginTest() throws Exception {
99 PowerMockito.mockStatic(MenuProperties.class);
100 PowerMockito.mockStatic(SessionCookieUtil.class);
101 PowerMockito.mockStatic(StringUtils.class);
102 Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT");
103 Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(false);
104 EPLoginBean commandBean = new EPLoginBean();
105 EPUser user = new EPUser();
106 commandBean.setUser(user);
107 commandBean.setOrgUserId("guestT");
108 Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY))
109 .thenReturn("menu_properties_filename");
110 Mockito.when(loginService.findUser(commandBean, "menu_properties_filename", null)).thenReturn(commandBean);
111 assertTrue(simpleLoginStrategy.login(mockedRequest, mockedResponse));
115 public void loginIfUserEmptyTest() throws Exception {
116 PowerMockito.mockStatic(MenuProperties.class);
117 PowerMockito.mockStatic(SessionCookieUtil.class);
118 PowerMockito.mockStatic(StringUtils.class);
119 Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT");
120 Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true);
121 EPLoginBean commandBean = new EPLoginBean();
122 EPUser user = new EPUser();
123 commandBean.setUser(user);
124 commandBean.setOrgUserId("guestT");
125 assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse));
129 public void loginIfAuthIsBothTest() throws Exception {
130 PowerMockito.mockStatic(SystemProperties.class);
131 PowerMockito.mockStatic(SessionCookieUtil.class);
132 PowerMockito.mockStatic(StringUtils.class);
133 Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT");
134 Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true);
135 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("BOTH");
136 assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse));
140 public void loginIfAuthIsNotNullTest() throws Exception {
141 PowerMockito.mockStatic(SystemProperties.class);
142 PowerMockito.mockStatic(SessionCookieUtil.class);
143 PowerMockito.mockStatic(StringUtils.class);
144 Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT");
145 Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true);
146 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test");
147 assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse));
151 public void loginExceptionTest() throws Exception {
152 PowerMockito.mockStatic(SystemProperties.class);
153 PowerMockito.mockStatic(SessionCookieUtil.class);
154 PowerMockito.mockStatic(StringUtils.class);
155 Mockito.when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("guestT");
156 Mockito.when(StringUtils.isEmpty("guestT")).thenReturn(true);
157 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM))
158 .thenThrow(nullPointerException);
159 assertFalse(simpleLoginStrategy.login(mockedRequest, mockedResponse));
162 @Test(expected = Exception.class)
163 public void doLoginTest() throws Exception {
164 simpleLoginStrategy.doLogin(mockedRequest, mockedResponse);
167 @Test(expected = PortalAPIException.class)
168 public void getUserIdTest() throws Exception {
169 simpleLoginStrategy.getUserId(mockedRequest);