2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.filters;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
31 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
32 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
33 import org.openecomp.sdc.be.config.ConfigurationManager;
34 import org.openecomp.sdc.be.servlets.exception.ComponentExceptionMapper;
35 import org.openecomp.sdc.common.api.ConfigurationSource;
36 import org.openecomp.sdc.common.api.UserRoleEnum;
37 import org.openecomp.sdc.common.datastructure.UserContext;
38 import org.openecomp.sdc.common.impl.ExternalConfiguration;
39 import org.openecomp.sdc.common.impl.FSConfigurationSource;
40 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
42 import javax.servlet.FilterChain;
43 import javax.servlet.ServletException;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.servlet.http.HttpServletResponse;
46 import java.io.IOException;
47 import java.util.Collections;
48 import java.util.HashSet;
50 import static org.mockito.Mockito.any;
51 import static org.mockito.Mockito.doThrow;
52 import static org.mockito.Mockito.eq;
53 import static org.mockito.Mockito.times;
54 import static org.mockito.Mockito.verify;
56 @RunWith(MockitoJUnitRunner.class)
57 public class ReqValidationFilterTest {
60 private ReqValidationFilter reqValidationFilter;
62 private HttpServletRequest request;
64 private FilterChain filterChain;
66 private HttpServletResponse response;
68 private ComponentExceptionMapper componentExceptionMapper;
70 static ResponseFormatManager responseFormatManager = new ResponseFormatManager();
71 private static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
72 static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
75 public void initMocks(){
76 MockitoAnnotations.initMocks(this);
81 reqValidationFilter = new ReqValidationFilter();
85 public void testValidRequestWithNoUser() throws IOException, ServletException {
86 ThreadLocalsHolder.setUserContext(null);
87 reqValidationFilter.doFilter(request, response, filterChain);
88 verify(filterChain, times(1)).doFilter(request, response);
92 public void testValidRequestWithDesignerRole() throws IOException, ServletException {
93 UserContext userContext = new UserContext("designer", new HashSet<>(Collections.singletonList(UserRoleEnum.DESIGNER.getName())),"f", "l");
94 ThreadLocalsHolder.setUserContext(userContext);
95 reqValidationFilter.doFilter(request, response, filterChain);
96 verify(filterChain, times(1)).doFilter(request, response);
100 public void testValidRequestWithNoRoles() throws IOException, ServletException {
101 UserContext userContext = new UserContext("designer", null,"f", "l");
102 ThreadLocalsHolder.setUserContext(userContext);
103 reqValidationFilter.doFilter(request, response, filterChain);
104 verify(filterChain, times(1)).doFilter(request, response);
108 public void testValidRequestWithEmptyRoles() throws IOException, ServletException {
109 UserContext userContext = new UserContext("designer", new HashSet<>(),"f", "l");
110 ThreadLocalsHolder.setUserContext(userContext);
111 reqValidationFilter.doFilter(request, response, filterChain);
112 verify(filterChain, times(1)).doFilter(request, response);
116 public void testValidRequestWithAdminRole() throws IOException, ServletException {
117 UserContext userContext = new UserContext("admin", new HashSet<>(Collections.singletonList(UserRoleEnum.ADMIN.getName())),"f", "l");
118 ThreadLocalsHolder.setUserContext(userContext);
119 reqValidationFilter.doFilter(request, response, filterChain);
120 verify(filterChain, times(1)).doFilter(request, response);
123 @Test(expected = ComponentException .class)
124 public void testValidRequestWithTesterRole() throws IOException, ServletException {
125 UserContext userContext = new UserContext("tester", new HashSet<>(Collections.singletonList(UserRoleEnum.TESTER.getName())),"f", "l");
126 ThreadLocalsHolder.setUserContext(userContext);
127 doThrow(ByActionStatusComponentException.class).when(componentExceptionMapper).writeToResponse(any(ComponentException.class),eq(response));
128 reqValidationFilter.doFilter(request, response, filterChain);