<commit_before>package eu.hinsch.spring.boot.actuator.useragent;

import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.actuate.metrics.CounterService;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by lukas.hinsch on 02.06.2015.
 */
public class UserAgentMetricFilterTest {

    private static final String CHROME = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)" +
            "Chrome/41.0.2228.0 Safari/537.36";

    @Mock
    private BeanFactory beanFactory;

    @Mock
    private CounterService counterService;

    @Mock
    private UserAgentMetricFilterConfiguration configuration;

    @Mock
    private HttpServletRequest request;

    @Mock
    private HttpServletResponse response;

    @Mock
    private FilterChain filterChain;

    @InjectMocks
    private UserAgentMetricFilter filter;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldLogUserAgentWithDefaultConfig() throws Exception {
        // given
        mockUserAgentHeader(CHROME);

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.chrome.41");
    }

    @Test
    public void shouldLogUserAgentWithConfiguredKeys() throws Exception {
        // given
        mockUserAgentHeader(CHROME);
        mockKeyConfiguration("#this.name", "#this.operatingSystem.name");

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.chrome");
        verify(counterService).increment("user-agent.windows-7");
    }

    @Test
    public void shouldLogRequestData() throws Exception {
        // given
        mockUserAgentHeader(CHROME);
        mockKeyConfiguration("@currentRequest.getHeader('MyHeader')");
        when(request.getHeader("MyHeader")).thenReturn("MyHeaderValue");

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.myheadervalue");
    }

    private void mockKeyConfiguration(String... keys) {
        when(configuration.getKeys()).thenReturn(asList(keys));
    }

    private void mockUserAgentHeader(String userAgent) {
        when(request.getHeader("User-Agent")).thenReturn(userAgent);
    }
}<commit_msg>Yet another test case<commit_after>package eu.hinsch.spring.boot.actuator.useragent;

import org.apache.coyote.Request;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.actuate.metrics.CounterService;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import static java.util.Arrays.asList;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by lukas.hinsch on 02.06.2015.
 */
public class UserAgentMetricFilterTest {

    private static final String CHROME = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)" +
            "Chrome/41.0.2228.0 Safari/537.36";

    @Mock
    private BeanFactory beanFactory;

    @Mock
    private CounterService counterService;

    @Mock
    private UserAgentMetricFilterConfiguration configuration;

    @Mock
    private HttpServletRequest request;

    @Mock
    private HttpServletResponse response;

    @Mock
    private FilterChain filterChain;

    @InjectMocks
    private UserAgentMetricFilter filter;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void shouldLogUserAgentWithDefaultConfig() throws Exception {
        // given
        mockUserAgentHeader(CHROME);

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.chrome.41");
    }

    @Test
    public void shouldLogUserAgentWithConfiguredKeys() throws Exception {
        // given
        mockUserAgentHeader(CHROME);
        mockKeyConfiguration("#this.name", "#this.operatingSystem.name");

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.chrome");
        verify(counterService).increment("user-agent.windows-7");
    }

    @Test
    public void shouldLogRequestData() throws Exception {
        // given
        mockUserAgentHeader(CHROME);
        mockKeyConfiguration("@currentRequest.getHeader('MyHeader')");
        when(request.getHeader("MyHeader")).thenReturn("MyHeaderValue");

        // when
        filter.doFilter(request, response, filterChain);

        // then
        verify(counterService).increment("user-agent.myheadervalue");
    }

    @Test
    public void shouldNotLogAnythingIfRequestIsNotHttpServletRequest() throws Exception {
        // when
        filter.doFilter(Mockito.mock(ServletRequest.class), response, filterChain);

        // then
        verify(counterService, never()).increment(anyString());
    }

    private void mockKeyConfiguration(String... keys) {
        when(configuration.getKeys()).thenReturn(asList(keys));
    }

    private void mockUserAgentHeader(String userAgent) {
        when(request.getHeader("User-Agent")).thenReturn(userAgent);
    }
}
{'fraction_non_alphanumeric': 0.1042904290429043, 'fraction_numerical': 0.01089108910891089, 'mean_word_length': 3.357296908698778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3144879', 'n_tokens_mistral': 1752, 'n_tokens_neox': 1677, 'n_words': 320}