<commit_before>package org.zalando.logbook;

import org.junit.Test;

import java.util.List;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.zalando.logbook.HeaderFilters.defaultValue;
import static org.zalando.logbook.HeaderFilters.eachHeader;

public final class HeaderFiltersTest {

    @Test
    public void shouldFilterHeaders() {
        final Map<String, List<String>> actual = eachHeader((k, v) -> v.toUpperCase())
                .filter(MockHeaders.of("hello", "world", "chao", "bambina"));

        assertEquals(2, actual.size());
        assertTrue(actual.containsKey("hello"));
        assertTrue(actual.containsKey("chao"));
        assertEquals(singletonList("WORLD"), actual.get("hello"));
        assertEquals(singletonList("BAMBINA"), actual.get("chao"));
    }

    @Test
    public void shouldOnlyFilterHeaderIfBothNameAndValueApply() {
        final HeaderFilter unit = HeaderFilters.replaceHeaders((name, value) ->
                "name".equals(name) && "Alice".equals(value), "<secret>");

        assertThat(unit.filter(MockHeaders.of("name", "Alice")), hasEntry("name", singletonList("<secret>")));
        assertThat(unit.filter(MockHeaders.of("name", "Bob")), hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void authorizationShouldFilterAuthorizationByDefault() {
        final HeaderFilter unit = defaultValue();

        assertThat(unit.filter(MockHeaders.of("Authorization", "Bearer c61a8f84-6834-11e5-a607-10ddb1ee7671")),
                hasEntry("Authorization", singletonList("XXX")));
    }

    @Test
    public void authorizationShouldNotFilterNonAuthorizationByDefault() {
        final HeaderFilter unit = defaultValue();

        assertThat(unit.filter(MockHeaders.of("Accept", "text/plain")), hasEntry("Accept", singletonList("text/plain")));
    }

    @Test
    public void shouldRemoveHeaderByNameAndValue() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) ->
                "name".equals(name) && "Alice".equals(value));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void shouldRemoveHeaderByName() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) -> "name".equals(name));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasKey("name")));
    }

    @Test
    public void shouldRemoveHeaderByNamePredicate() {
        final HeaderFilter unit = HeaderFilters.removeHeaders("name"::equals);

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob", "age", "18"));

        assertThat(filtered, not(hasKey("name")));
        assertThat(filtered, hasEntry("age", singletonList("18")));
    }

    @Test
    public void shouldRemoveHeaderByValue() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) -> "Alice".equals(value));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void shouldRemoveAndChangeHeader() {
        final HeaderFilter unit = HeaderFilters.eachHeader((name, value) -> {
                    if ("name".equals(name)) {
                        if ("Bob".equals(value)) {
                            return null;
                        } else if ("Alice".equals(value)) {
                            return "Carol";
                        } else {
                            return null;
                        }
                    }
                    return value;
                });

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, not(hasEntry("name", singletonList("Bob"))));
        assertThat(filtered, hasEntry("name", singletonList("Carol")));
    }

}<commit_msg>GH-151 Refactors test<commit_after>package org.zalando.logbook;

import org.junit.Test;

import java.util.List;
import java.util.Map;

import static java.util.Collections.singletonList;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.zalando.logbook.HeaderFilters.defaultValue;
import static org.zalando.logbook.HeaderFilters.eachHeader;

public final class HeaderFiltersTest {

    @Test
    public void shouldFilterHeaders() {
        final Map<String, List<String>> actual = eachHeader((k, v) -> v.toUpperCase())
                .filter(MockHeaders.of("hello", "world", "chao", "bambina"));

        assertEquals(2, actual.size());
        assertTrue(actual.containsKey("hello"));
        assertTrue(actual.containsKey("chao"));
        assertEquals(singletonList("WORLD"), actual.get("hello"));
        assertEquals(singletonList("BAMBINA"), actual.get("chao"));
    }

    @Test
    public void shouldOnlyFilterHeaderIfBothNameAndValueApply() {
        final HeaderFilter unit = HeaderFilters.replaceHeaders((name, value) ->
                "name".equals(name) && "Alice".equals(value), "<secret>");

        assertThat(unit.filter(MockHeaders.of("name", "Alice")), hasEntry("name", singletonList("<secret>")));
        assertThat(unit.filter(MockHeaders.of("name", "Bob")), hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void authorizationShouldFilterAuthorizationByDefault() {
        final HeaderFilter unit = defaultValue();

        assertThat(unit.filter(MockHeaders.of("Authorization", "Bearer c61a8f84-6834-11e5-a607-10ddb1ee7671")),
                hasEntry("Authorization", singletonList("XXX")));
    }

    @Test
    public void authorizationShouldNotFilterNonAuthorizationByDefault() {
        final HeaderFilter unit = defaultValue();

        assertThat(unit.filter(MockHeaders.of("Accept", "text/plain")), hasEntry("Accept", singletonList("text/plain")));
    }

    @Test
    public void shouldRemoveHeaderByNameAndValue() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) ->
                "name".equals(name) && "Alice".equals(value));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void shouldRemoveHeaderByName() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) -> "name".equals(name));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasKey("name")));
    }

    @Test
    public void shouldRemoveHeaderByNamePredicate() {
        final HeaderFilter unit = HeaderFilters.removeHeaders("name"::equals);

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob", "age", "18"));

        assertThat(filtered, not(hasKey("name")));
        assertThat(filtered, hasEntry("age", singletonList("18")));
    }

    @Test
    public void shouldRemoveHeaderByValue() {
        final HeaderFilter unit = HeaderFilters.removeHeaders((name, value) -> "Alice".equals(value));

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, hasEntry("name", singletonList("Bob")));
    }

    @Test
    public void shouldRemoveAndChangeHeader() {
        final HeaderFilter unit = HeaderFilters.eachHeader((name, value) -> {
            if ("name".equals(name) && "Bob".equals(value)) {
                return null;
            } else if ("name".equals(name) && "Alice".equals(value)) {
                return "Carol";
            } else {
                return value;
            }
        });

        final Map<String, List<String>> filtered = unit.filter(MockHeaders.of("name", "Alice", "name", "Bob"));

        assertThat(filtered, not(hasEntry("name", singletonList("Alice"))));
        assertThat(filtered, not(hasEntry("name", singletonList("Bob"))));
        assertThat(filtered, hasEntry("name", singletonList("Carol")));
    }

}
{'fraction_non_alphanumeric': 0.15059978189749182, 'fraction_numerical': 0.0062159214830970555, 'mean_word_length': 3.462773722627737, 'pattern_counts': {'":': 2, '<': 31, '<?xml version=': 0, '>': 43, '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': '5218333', 'n_tokens_mistral': 2629, 'n_tokens_neox': 2538, 'n_words': 482}