2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.openecomp.portalapp.portal.controller;
40 import static org.junit.Assert.assertEquals;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
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.openecomp.portalapp.portal.framework.MockitoTestSuite;
53 import org.openecomp.portalapp.portal.controller.UserController;
54 import org.openecomp.portalapp.portal.core.MockEPUser;
55 import org.openecomp.portalapp.portal.domain.EPUser;
56 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
57 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
58 import org.openecomp.portalapp.portal.service.UserService;
59 import org.openecomp.portalapp.portal.service.UserServiceImpl;
60 import org.openecomp.portalapp.portal.transport.ProfileDetail;
61 import org.openecomp.portalapp.util.EPUserUtils;
62 import org.openecomp.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;
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest(CipherUtil.class)
69 public class UserControllerTest extends MockitoTestSuite {
72 UserController userController = new UserController();
75 UserService userService = new UserServiceImpl();
79 MockitoAnnotations.initMocks(this);
83 EPUserUtils ePUserUtils = new EPUserUtils();
85 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
87 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
88 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
89 NullPointerException nullPointerException = new NullPointerException();
91 MockEPUser mockUser = new MockEPUser();
94 public void getLoggedinUserExceptionTest() {
95 EPUser epUser = mockUser.mockEPUser();
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);
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.decrypt(epUser.getLoginPwd())).thenReturn("Password");
118 PortalRestResponse<ProfileDetail> response = userController.getLoggedinUser(mockedRequest);
119 assertEquals(response.getMessage(), expectedResponse.getMessage());
120 assertEquals(response.getStatus(), expectedResponse.getStatus());
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());
137 public void modifyLoggedinUserExceptionTest() {
138 EPUser epUser = mockUser.mockEPUser();
140 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
141 expectedResponse.setMessage(
142 "java.lang.ClassCastException: com.sun.crypto.provider.AESCipher$General cannot be cast to javax.crypto.CipherSpi");
143 expectedResponse.setResponse(null);
144 PortalRestStatusEnum enu = null;
145 expectedResponse.setStatus(enu.ERROR);
146 ProfileDetail profileDetail = new ProfileDetail();
147 profileDetail.setFirstName("Test_FirstName");
148 profileDetail.setLastName("Test_LastName");
149 profileDetail.setEmail("Test_Email");
150 profileDetail.setLoginId("Test_LoginId");
151 profileDetail.setLoginPassword("Test_LoginPassword");
152 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
153 PortalRestResponse<String> actualResponse = userController.modifyLoggedinUser(mockedRequest, profileDetail);
154 assertEquals(actualResponse, expectedResponse);
159 public void modifyLoggedinUserTest() throws Exception {
160 EPUser epUser = mockUser.mockEPUser();
161 PortalRestResponse<String> expectedResponse = new PortalRestResponse<String>();
162 expectedResponse.setMessage("success");
163 expectedResponse.setResponse(null);
164 PortalRestStatusEnum enu = null;
165 expectedResponse.setStatus(enu.OK);
166 ProfileDetail profileDetail = new ProfileDetail();
167 profileDetail.setFirstName("Test_FirstName");
168 profileDetail.setLastName("Test_LastName");
169 profileDetail.setEmail("Test_Email");
170 profileDetail.setLoginId("Test_LoginId");
171 profileDetail.setLoginPassword("Test_LoginPassword");
172 Mockito.when(EPUserUtils.getUserSession(mockedRequest)).thenReturn(epUser);
173 PowerMockito.mockStatic(CipherUtil.class);
174 Mockito.when(CipherUtil.decrypt(epUser.getLoginPwd())).thenReturn("Password");
175 PortalRestResponse<String> actualResponse = userController.modifyLoggedinUser(mockedRequest, profileDetail);
176 System.out.println(actualResponse);