Replace ecomp references
[portal.git] / ecomp-portal-BE-os / src / test / java / org / onap / portalapp / portal / authentication / OpenIdConnectLoginStrategyTest.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.authentication;
39
40 import static org.junit.Assert.*;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.onap.portalapp.authentication.OpenIdConnectLoginStrategy;
46 import org.onap.portalapp.portal.framework.MockitoTestSuite;
47 import org.onap.portalapp.portal.utils.EPSystemProperties;
48 import org.onap.portalapp.util.EPUserUtils;
49 import org.onap.portalapp.util.SessionCookieUtil;
50 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
51 import org.onap.portalsdk.core.util.SystemProperties;
52
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.mitre.openid.connect.model.DefaultUserInfo;
57 import org.mitre.openid.connect.model.UserInfo;
58 import org.mockito.InjectMocks;
59 import org.mockito.Mockito;
60 import org.mockito.MockitoAnnotations;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
64 import org.springframework.util.StringUtils;
65
66 @RunWith(PowerMockRunner.class)
67 @PrepareForTest({StringUtils.class, EPUserUtils.class , SessionCookieUtil.class,SystemProperties.class})
68 public class OpenIdConnectLoginStrategyTest {
69         
70         @InjectMocks
71         OpenIdConnectLoginStrategy OpenIdConnectLoginStrategy = new OpenIdConnectLoginStrategy();
72
73         @Before
74         public void setup() {
75                 MockitoAnnotations.initMocks(this);
76         }
77
78         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
79
80         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
81         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
82
83         NullPointerException nullPointerException = new NullPointerException();
84                 
85         @Test
86         public void loginTest() throws Exception
87         {
88                 PowerMockito.mockStatic(StringUtils.class);
89                 PowerMockito.mockStatic(EPUserUtils.class);
90                 PowerMockito.mockStatic(SessionCookieUtil.class);
91                 
92                 UserInfo  userInfo = new DefaultUserInfo();
93                 userInfo.setPreferredUsername("Test");
94                 userInfo.setEmail("test@gmail.com");
95                 userInfo.setName("first_name");
96                 userInfo.setFamilyName("last_name");
97                 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
98                 assertTrue(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
99         }
100         
101         @Test
102         public void loginIfUserNullTest() throws Exception
103         {               
104                 PowerMockito.mockStatic(SystemProperties.class);
105                 UserInfo  userInfo = null;
106                 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
107 //              Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("auth");
108                 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
109         }
110         
111         @Test
112         public void loginIfUserIfAuthIsOIDCTest() throws Exception
113         {               
114                 PowerMockito.mockStatic(SystemProperties.class);
115                 UserInfo  userInfo = null;
116                 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
117                 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("OIDC");
118                 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
119                 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
120         }
121         
122         @Test
123         public void loginIfUserIfAuthNotNullTest() throws Exception
124         {               
125                 PowerMockito.mockStatic(SystemProperties.class);
126                 UserInfo  userInfo = null;
127                 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
128                 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenReturn("test");
129                 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
130                 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
131         }
132         
133         @Test
134         public void loginIfUserExceptionest() throws Exception
135         {               
136                 PowerMockito.mockStatic(SystemProperties.class);
137                 UserInfo  userInfo = null;
138                 Mockito.when(mockedRequest.getAttribute("userInfo")).thenReturn(userInfo);
139                 Mockito.when(SystemProperties.getProperty("authentication_mechanism")).thenThrow(nullPointerException);
140                 Mockito.when(SystemProperties.getProperty(EPSystemProperties.LOGIN_URL_NO_RET_VAL)).thenReturn("login_url");
141                 assertFalse(OpenIdConnectLoginStrategy.login(mockedRequest, mockedResponse));
142         }
143         @Test(expected =  Exception.class)
144         public void doLoginTest() throws Exception
145         {
146                 OpenIdConnectLoginStrategy.doLogin(mockedRequest, mockedResponse);
147         }
148         
149         @Test(expected =  PortalAPIException.class)
150         public void getUserIdTest() throws Exception
151         {
152                 OpenIdConnectLoginStrategy.getUserId(mockedRequest);
153         }
154         
155 }