CADI AAF Integration and merging the code
[portal.git] / ecomp-portal-BE-common / src / test / java / org / onap / portalapp / portal / controller / UserControllerTest.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
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoAnnotations;
52 import org.onap.portalapp.portal.controller.UserController;
53 import org.onap.portalapp.portal.core.MockEPUser;
54 import org.onap.portalapp.portal.domain.EPUser;
55 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
56 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
57 import org.onap.portalapp.portal.framework.MockitoTestSuite;
58 import org.onap.portalapp.portal.service.UserService;
59 import org.onap.portalapp.portal.service.UserServiceImpl;
60 import org.onap.portalapp.portal.transport.ProfileDetail;
61 import org.onap.portalapp.util.EPUserUtils;
62 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
63 import org.powermock.api.mockito.PowerMockito;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest(CipherUtil.class)
69 public class UserControllerTest extends MockitoTestSuite {
70
71         @InjectMocks
72         UserController userController = new UserController();
73
74         @Mock
75         UserService userService = new UserServiceImpl();
76
77         @Before
78         public void setup() {
79                 MockitoAnnotations.initMocks(this);
80         }
81
82         @Mock
83         EPUserUtils ePUserUtils = new EPUserUtils();
84
85         MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
86
87         HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
88         HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
89         NullPointerException nullPointerException = new NullPointerException();
90
91         MockEPUser mockUser = new MockEPUser();
92
93         @Test
94         public void getLoggedinUserExceptionTest() {
95                 EPUser epUser = null;
96                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
97                 PortalRestResponse<ProfileDetail> expectedResponse = new PortalRestResponse<ProfileDetail>();
98                 expectedResponse.setMessage(null);
99                 expectedResponse.setResponse(null);
100                 PortalRestStatusEnum enu = null;
101                 expectedResponse.setStatus(enu.ERROR);
102                 PortalRestResponse<ProfileDetail> response = userController.getLoggedinUser(mockedRequest);
103                 assertEquals(response, expectedResponse);
104         }
105         
106         @Test
107         public void getLoggedinUserTest() throws Exception {
108                 EPUser epUser = mockUser.mockEPUser();
109                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
110                 PortalRestResponse<ProfileDetail> expectedResponse = new PortalRestResponse<ProfileDetail>();
111                 expectedResponse.setMessage("success");
112                 ProfileDetail profileDetail = new ProfileDetail();
113                 expectedResponse.setResponse(profileDetail);
114                 PortalRestStatusEnum enu = null;
115                 expectedResponse.setStatus(enu.OK);
116                 PowerMockito.mockStatic(CipherUtil.class);
117                 Mockito.when(CipherUtil.decryptPKC(epUser.getLoginPwd())).thenReturn("Password");
118                 PortalRestResponse<ProfileDetail> response = userController.getLoggedinUser(mockedRequest);
119                 assertEquals(response.getMessage(), expectedResponse.getMessage());
120                 assertEquals(response.getStatus(), expectedResponse.getStatus());
121         }
122
123         @Test
124         public void modifyLoggedinUserIfProfileNullTest() {
125                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
126                 expectedResponse.setMessage("java.lang.NullPointerException");
127                 expectedResponse.setResponse(null);
128                 PortalRestStatusEnum enu = null;
129                 expectedResponse.setStatus(enu.ERROR);
130                 ProfileDetail profileDetail = null;
131                 PortalRestResponse<String> actualResponse = userController.modifyLoggedinUser(mockedRequest, profileDetail);
132                 assertEquals(actualResponse, expectedResponse);
133                 assertEquals(actualResponse.getStatus(), expectedResponse.getStatus());
134         }
135
136         @Test
137         public void modifyLoggedinUserExceptionTest() {
138                 EPUser epUser =null;
139                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
140                 expectedResponse.setMessage("java.lang.NullPointerException");
141                 expectedResponse.setResponse(null);
142                 PortalRestStatusEnum enu = null;
143                 expectedResponse.setStatus(enu.ERROR);
144                 ProfileDetail profileDetail = new ProfileDetail();
145                 profileDetail.setFirstName("Test_FirstName");
146                 profileDetail.setLastName("Test_LastName");
147                 profileDetail.setEmail("Test_Email");
148                 profileDetail.setLoginId("Test_LoginId");
149                 profileDetail.setLoginPassword("Test_LoginPassword");
150                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
151                 PortalRestResponse<String> actualResponse = userController.modifyLoggedinUser(mockedRequest, profileDetail);
152                 assertEquals(actualResponse, expectedResponse);
153
154         }
155
156         /*@Test
157         public void modifyLoggedinUserTest() throws Exception {
158                 EPUser epUser = mockUser.mockEPUser();
159                 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
160                 expectedResponse.setMessage("success");
161                 expectedResponse.setResponse(null);
162                 PortalRestStatusEnum enu = null;
163                 expectedResponse.setStatus(enu.OK);
164                 ProfileDetail profileDetail = new ProfileDetail();
165                 profileDetail.setFirstName("Test_FirstName");
166                 profileDetail.setLastName("Test_LastName");
167                 profileDetail.setEmail("Test_Email");
168                 profileDetail.setLoginId("Test_LoginId");
169                 profileDetail.setLoginPassword("Test_LoginPassword");
170                 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
171                 PowerMockito.mockStatic(CipherUtil.class);
172                 Mockito.when(CipherUtil.decrypt(epUser.getLoginPwd())).thenReturn("Password");
173                 PortalRestResponse<String> actualResponse = userController.modifyLoggedinUser(mockedRequest, profileDetail);
174                 System.out.println(actualResponse);
175         }*/
176 }