<commit_before>package com.epam.rft.atsy.web.filter;

import com.epam.rft.atsy.service.domain.UserDTO;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;

/**
 * Created by tothd on 2015. 11. 02..
 */
public class SecurityFilterTest {

    @InjectMocks
    SecurityFilter securityFilter;

    @Mock
    HttpServletRequest servletRequest;

    @Mock
    HttpServletResponse servletResponse;

    @Mock
    HttpSession session;

    @Mock
    UserDTO userDTO;

    @Mock
    FilterChain filterChain;



    @BeforeMethod
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void redirectTest() throws IOException, ServletException {

        given(userDTO.getName()).willReturn("test");
        given(userDTO.getPassword()).willReturn("<PASSWORD>");
        given(servletRequest.getSession()).willReturn(session);
        given(servletRequest.getQueryString()).willReturn("");
        given(servletRequest.getContextPath()).willReturn("/atsy");
        given(servletRequest.getRequestURL()).willReturn(new StringBuffer("/atsy/secure/settings"));
        given(session.getAttribute("user")).willReturn(null);

        //when
        securityFilter.doFilter(servletRequest, servletResponse, filterChain);

        verify(servletResponse).sendRedirect(anyString());

    }

    @Test
    public void filterChainTest() throws IOException, ServletException {

        given(userDTO.getName()).willReturn("test");
        given(userDTO.getPassword()).willReturn("<PASSWORD>");
        given(servletRequest.getSession()).willReturn(session);
        given(session.getAttribute("user")).willReturn(userDTO);


        //when
        securityFilter.doFilter(servletRequest, servletResponse, filterChain);

        verify(filterChain).doFilter(servletRequest, servletResponse);

    }

    @Test
    public void buildRedirectParamStringUtilIsNotBlank() throws IOException, ServletException{
        given(servletRequest.getQueryString()).willReturn("anything");
        given(servletRequest.getRequestURL()).willReturn(new StringBuffer("anything"));
        given(servletRequest.getContextPath()).willReturn("/atsy");
        given(servletRequest.getSession()).willReturn(session);
        given(session.getAttribute("user")).willReturn(null);
        //given(servletRequest.getRequestURL().length()).willReturn(8);
        //when
        securityFilter.doFilter(servletRequest,servletResponse,filterChain);

        verify(servletResponse).sendRedirect(anyString());
    }
}<commit_msg>Additional test method for SecurityFilter<commit_after>package com.epam.rft.atsy.web.filter;

import com.epam.rft.atsy.service.domain.UserDTO;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.verify;

/**
 * Created by tothd on 2015. 11. 02..
 */
public class SecurityFilterTest {

    @InjectMocks
    SecurityFilter securityFilter;

    @Mock
    HttpServletRequest servletRequest;

    @Mock
    HttpServletResponse servletResponse;

    @Mock
    HttpSession session;

    @Mock
    UserDTO userDTO;

    @Mock
    FilterChain filterChain;



    @BeforeMethod
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }


    @Test
    public void redirectTest() throws IOException, ServletException {

        given(userDTO.getName()).willReturn("test");
        given(userDTO.getPassword()).willReturn("<PASSWORD>");
        given(servletRequest.getSession()).willReturn(session);
        given(servletRequest.getQueryString()).willReturn("");
        given(servletRequest.getContextPath()).willReturn("/atsy");
        given(servletRequest.getRequestURL()).willReturn(new StringBuffer("/atsy/secure/settings"));
        given(session.getAttribute("user")).willReturn(null);

        //when
        securityFilter.doFilter(servletRequest, servletResponse, filterChain);

        verify(servletResponse).sendRedirect(anyString());

    }

    @Test
    public void filterChainTest() throws IOException, ServletException {

        given(userDTO.getName()).willReturn("test");
        given(userDTO.getPassword()).willReturn("<PASSWORD>");
        given(servletRequest.getSession()).willReturn(session);
        given(session.getAttribute("user")).willReturn(userDTO);


        //when
        securityFilter.doFilter(servletRequest, servletResponse, filterChain);

        verify(filterChain).doFilter(servletRequest, servletResponse);

    }

    @Test
    public void buildRedirectParamStringUtilIsNotBlank() throws IOException, ServletException{
        given(servletRequest.getQueryString()).willReturn("anything");
        given(servletRequest.getRequestURL()).willReturn(new StringBuffer("anything"));
        given(servletRequest.getContextPath()).willReturn("/atsy");
        given(servletRequest.getSession()).willReturn(session);
        given(session.getAttribute("user")).willReturn(null);
        //given(servletRequest.getRequestURL().length()).willReturn(8);
        //when
        securityFilter.doFilter(servletRequest,servletResponse,filterChain);

        verify(servletResponse).sendRedirect(anyString());
    }

    @Test
    public void buildRedirectParamRequestURLIsEmpty() throws IOException, ServletException{
        given(servletRequest.getQueryString()).willReturn("anything");
        given(servletRequest.getRequestURL()).willReturn(new StringBuffer(""));
        given(servletRequest.getContextPath()).willReturn("/atsy");
        given(servletRequest.getSession()).willReturn(session);
        given(session.getAttribute("user")).willReturn(null);
        //given(servletRequest.getRequestURL().length()).willReturn(8);
        //when
        securityFilter.doFilter(servletRequest,servletResponse,filterChain);

        verify(servletResponse).sendRedirect(anyString());
    }
}
{'fraction_non_alphanumeric': 0.12091503267973856, 'fraction_numerical': 0.002822341057635175, 'mean_word_length': 4.207269914926528, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1794157', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1787, 'n_words': 265}