Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-os / src / test / java / org / openecomp / portalapp / portal / authentication / SimpleLoginStrategyTest.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.authentication;
39
40 import static org.junit.Assert.assertFalse;
41 import static org.junit.Assert.assertTrue;
42
43 import javax.servlet.http.HttpServletRequest;
44 import javax.servlet.http.HttpServletResponse;
45
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.openecomp.portalapp.authentication.SimpleLoginStrategy;
54 import org.openecomp.portalapp.command.EPLoginBean;
55 import org.openecomp.portalapp.portal.domain.EPUser;
56 import org.openecomp.portalapp.portal.framework.MockitoTestSuite;
57 import org.openecomp.portalapp.portal.service.EPLoginService;
58 import org.openecomp.portalapp.portal.service.EPRoleFunctionService;
59 import org.openecomp.portalapp.portal.service.EPRoleService;
60 import org.openecomp.portalapp.util.EPUserUtils;
61 import org.openecomp.portalapp.util.SessionCookieUtil;
62 import org.openecomp.portalsdk.core.menu.MenuProperties;
63 import org.openecomp.portalsdk.core.onboarding.exception.PortalAPIException;
64 import org.openecomp.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;
69
70 @RunWith(PowerMockRunner.class)
71 @PrepareForTest({ StringUtils.class, EPUserUtils.class, SessionCookieUtil.class, SystemProperties.class,
72                 SessionCookieUtil.class, MenuProperties.class })
73 public class SimpleLoginStrategyTest {
74
75         @InjectMocks
76         SimpleLoginStrategy simpleLoginStrategy = new SimpleLoginStrategy();
77
78         @Mock
79         EPLoginService loginService;
80         @Mock
81         EPRoleService roleService;
82         @Mock
83     EPRoleFunctionService ePRoleFunctionService;
84
85         @Before
86         public void setup() {
87                 MockitoAnnotations.initMocks(this);
88         }
89
90         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
91
92         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
93         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
94
95         NullPointerException nullPointerException = new NullPointerException();
96
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));
112         }
113
114         @Test
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));
126         }
127
128         @Test
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));
137         }
138
139         @Test
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));
148         }
149
150         @Test
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));
160         }
161
162         @Test(expected = Exception.class)
163         public void doLoginTest() throws Exception {
164                 simpleLoginStrategy.doLogin(mockedRequest, mockedResponse);
165         }
166
167         @Test(expected = PortalAPIException.class)
168         public void getUserIdTest() throws Exception {
169                 simpleLoginStrategy.getUserId(mockedRequest);
170         }
171 }