Catalog-be dead code removal
[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     @Override
97     protected Application configure() {
98
99         ResourceConfig resourceConfig = new ResourceConfig(UserAdminServlet.class);
100
101         resourceConfig.register(new AbstractBinder() {
102
103             @Override
104             protected void configure() {
105                 bind(request).to(HttpServletRequest.class);
106             }
107         });
108
109         return resourceConfig;
110     }
111
112     private static Either<User, ActionStatus> buildEitherUser(String userId, boolean isActive) {
113         User user = new User();
114         user.setUserId(userId);
115         user.setRole(UserRoleEnum.ADMIN.getName());
116         if (!isActive) {
117             user.setStatus(UserStatusEnum.INACTIVE);
118         }
119         return Either.left(user);
120     }
121
122 }