2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import com.google.gson.Gson;
24 import fj.data.Either;
25 import org.glassfish.hk2.utilities.binding.AbstractBinder;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
33 import org.openecomp.sdc.be.impl.ComponentsUtils;
34 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.user.UserBusinessLogic;
37 import org.openecomp.sdc.common.api.Constants;
38 import org.openecomp.sdc.common.api.UserRoleEnum;
39 import org.openecomp.sdc.common.impl.ExternalConfiguration;
40 import org.openecomp.sdc.exception.ResponseFormat;
41 import org.springframework.http.HttpStatus;
42 import org.springframework.web.context.WebApplicationContext;
44 import javax.servlet.ServletContext;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpSession;
47 import javax.ws.rs.core.Application;
49 import static org.mockito.Mockito.doReturn;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.reset;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.when;
55 public class UserAdminServletTest extends JerseyTest {
57 final static HttpServletRequest request = mock(HttpServletRequest.class);
58 final static HttpSession session = mock(HttpSession.class);
59 final static ServletContext servletContext = mock(ServletContext.class);
60 final static WebAppContextWrapper webAppContextWrapper = mock(WebAppContextWrapper.class);
61 final static WebApplicationContext webApplicationContext = mock(WebApplicationContext.class);
62 final static UserBusinessLogic userAdminManager = spy(UserBusinessLogic.class);
63 final static AuditingManager auditingManager = mock(AuditingManager.class);
64 final static ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
65 final static ResponseFormat okResponseFormat = mock(ResponseFormat.class);
67 final static String ADMIN_ATT_UID = "jh0003";
68 Gson gson = new Gson();
71 public static void setup() {
72 ExternalConfiguration.setAppName("catalog-be");
74 when(session.getServletContext()).thenReturn(servletContext);
75 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
76 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
78 when(webApplicationContext.getBean(UserBusinessLogic.class)).thenReturn(userAdminManager);
79 when(webApplicationContext.getBean(ComponentsUtils.class)).thenReturn(componentsUtils);
80 when(componentsUtils.getAuditingManager()).thenReturn(auditingManager);
81 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(okResponseFormat);
82 when(okResponseFormat.getStatus()).thenReturn(HttpStatus.OK.value());
87 public void beforeTest() {
88 reset(userAdminManager);
89 doReturn(buildEitherUser(ADMIN_ATT_UID, true)).when(userAdminManager).getUser(ADMIN_ATT_UID, false);
92 when(request.getSession()).thenReturn(session);
93 when(request.getHeader("USER_ID")).thenReturn(ADMIN_ATT_UID);
97 * @Test public void deactivateUserSuccessfullyTest(){ String userToDeleteUserId = "admin1"; User adminUser = new User(); adminUser.setUserId(ADMIN_ATT_UID); Either<User, ActionStatus> eitherActiveUser = buildEitherUser(userToDeleteUserId, true);
98 * User userToDelete = eitherActiveUser.left().value(); doReturn(eitherActiveUser).when(userAdminManager).getUser( userToDeleteUserId);
100 * Either<User, ActionStatus> eitherInactiveUser = buildEitherUser(userToDeleteUserId, false); doReturn(eitherInactiveUser).when(userAdminManager).deActivateUser( adminUser, userToDelete.getUserId());
103 * Response response = target().path("/v1/user/"+userToDeleteUserId).request().delete(); assertTrue(response.getStatus() == HttpStatus.OK.value()); verify(userAdminManager, times(1)).deActivateUser(adminUser, userToDelete.getUserId()); }
106 * @Test public void forceDeleteUserSuccessfullyTest(){ String userToDeleteUserId = "admin1"; when(request.getHeader(User.FORCE_DELETE_HEADER_FLAG)).thenReturn(User. FORCE_DELETE_HEADER_FLAG);
108 * User adminUser = new User(); adminUser.setUserId(ADMIN_ATT_UID);
110 * Either<User, ActionStatus> eitherActiveUser = buildEitherUser(userToDeleteUserId, true); User userToDelete = eitherActiveUser.left().value(); doReturn(eitherActiveUser).when(userAdminManager).getUser( userToDeleteUserId);
112 * Either<User, ActionStatus> eitherUser = buildEitherUser(userToDeleteUserId, true); doReturn(eitherUser).when(userAdminManager).deleteUser(userToDelete. getUserId());
115 * Response response = target().path("/v1/user/"+userToDeleteUserId).request().delete(); assertTrue(response.getStatus() == HttpStatus.OK.value()); verify(userAdminManager, times(0)).deActivateUser(adminUser, userToDelete.getUserId());
116 * verify(userAdminManager, times(1)).deleteUser(userToDelete.getUserId()); }
120 protected Application configure() {
122 ResourceConfig resourceConfig = new ResourceConfig(UserAdminServlet.class);
124 resourceConfig.register(new AbstractBinder() {
127 protected void configure() {
128 bind(request).to(HttpServletRequest.class);
132 return resourceConfig;
135 private static Either<User, ActionStatus> buildEitherUser(String userId, boolean isActive) {
136 User user = new User();
137 user.setUserId(userId);
138 user.setRole(UserRoleEnum.ADMIN.getName());
140 user.setStatus(UserStatusEnum.INACTIVE);
142 return Either.left(user);