Sync Integ to Master
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / UserAdminServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
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;
43
44 import javax.servlet.ServletContext;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpSession;
47 import javax.ws.rs.core.Application;
48
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;
54
55 public class UserAdminServletTest extends JerseyTest {
56
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);
66
67     final static String ADMIN_ATT_UID = "jh0003";
68     Gson gson = new Gson();
69
70     @BeforeClass
71     public static void setup() {
72         ExternalConfiguration.setAppName("catalog-be");
73
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);
77
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());
83
84     }
85
86     @Before
87     public void beforeTest() {
88         reset(userAdminManager);
89         doReturn(buildEitherUser(ADMIN_ATT_UID, true)).when(userAdminManager).getUser(ADMIN_ATT_UID, false);
90
91         reset(request);
92         when(request.getSession()).thenReturn(session);
93         when(request.getHeader("USER_ID")).thenReturn(ADMIN_ATT_UID);
94     }
95
96     /*
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);
99      *
100      * Either<User, ActionStatus> eitherInactiveUser = buildEitherUser(userToDeleteUserId, false); doReturn(eitherInactiveUser).when(userAdminManager).deActivateUser( adminUser, userToDelete.getUserId());
101      *
102      *
103      * Response response = target().path("/v1/user/"+userToDeleteUserId).request().delete(); assertTrue(response.getStatus() == HttpStatus.OK.value()); verify(userAdminManager, times(1)).deActivateUser(adminUser, userToDelete.getUserId()); }
104      *
105      *
106      * @Test public void forceDeleteUserSuccessfullyTest(){ String userToDeleteUserId = "admin1"; when(request.getHeader(User.FORCE_DELETE_HEADER_FLAG)).thenReturn(User. FORCE_DELETE_HEADER_FLAG);
107      *
108      * User adminUser = new User(); adminUser.setUserId(ADMIN_ATT_UID);
109      *
110      * Either<User, ActionStatus> eitherActiveUser = buildEitherUser(userToDeleteUserId, true); User userToDelete = eitherActiveUser.left().value(); doReturn(eitherActiveUser).when(userAdminManager).getUser( userToDeleteUserId);
111      *
112      * Either<User, ActionStatus> eitherUser = buildEitherUser(userToDeleteUserId, true); doReturn(eitherUser).when(userAdminManager).deleteUser(userToDelete. getUserId());
113      *
114      *
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()); }
117      */
118
119     @Override
120     protected Application configure() {
121
122         ResourceConfig resourceConfig = new ResourceConfig(UserAdminServlet.class);
123
124         resourceConfig.register(new AbstractBinder() {
125
126             @Override
127             protected void configure() {
128                 bind(request).to(HttpServletRequest.class);
129             }
130         });
131
132         return resourceConfig;
133     }
134
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());
139         if (!isActive) {
140             user.setStatus(UserStatusEnum.INACTIVE);
141         }
142         return Either.left(user);
143     }
144
145 }