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============================================
36 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
38 package org.onap.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.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;
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() {
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.decryptPKC(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 = 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);
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);