<commit_before>package de.otto.rx.composer.providers;

import com.google.common.collect.ImmutableList;
import de.otto.rx.composer.content.*;
import org.junit.Test;
import rx.Observable;

import java.time.LocalDateTime;
import java.util.Iterator;

import static de.otto.rx.composer.content.AbcPosition.X;
import static de.otto.rx.composer.content.Headers.emptyHeaders;
import static de.otto.rx.composer.content.Parameters.emptyParameters;
import static de.otto.rx.composer.providers.ContentProviders.fetchFirst;
import static java.time.LocalDateTime.now;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static rx.Observable.just;

public class SelectingContentProviderTest {

    @Test
    public void shouldFetchFirstWithContent() {
        // given
        final ContentProvider contentProvider = fetchFirst(ImmutableList.of(
                (position, parameters) -> just(new TestContent(X, "Foo")),
                (position, parameters) -> just(new TestContent(X, "Bar"))
        ));
        // when
        final Observable<Content> result =  contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.getBody(), is("Foo"));
    }

    @Test
    public void shouldSelectFirstNotEmpty() {
        // given
        final ContentProvider contentProvider = fetchFirst(ImmutableList.of(
                (position, parameters) -> just(new TestContent(X, "")),
                (position, parameters) -> just(new TestContent(X, "Hello World"))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Iterator<Content> content = result.toBlocking().toIterable().iterator();
        assertThat(content.next().getBody(), is("Hello World"));
        assertThat(content.hasNext(), is(false));
    }

    @Test
    public void shouldHandleEmptyContents() {
        // given
        final ContentProvider contentProvider = fetchFirst(ImmutableList.of(
                (position, parameters) -> just(new TestContent(X, "")),
                (position, parameters) -> just(new TestContent(X, ""))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Iterator<Content> content = result.toBlocking().getIterator();
        assertThat(content.hasNext(), is(false));
    }

    @Test
    public void shouldHandleExceptions() {
        // given
        final ContentProvider contentProvider = fetchFirst(ImmutableList.of(
                (position, parameters) -> {
                    throw new IllegalStateException("Bumm!!!");
                },
                (position, parameters) -> just(new TestContent(X, "Yeah!"))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.getBody(), is("Yeah!"));
    }

    @Test
    public void shouldExecuteStepMultipleTimes() {
        // given
        final ContentProvider nestedProvider = mock(ContentProvider.class);
        when(nestedProvider.getContent(X, emptyParameters())).thenReturn(just(new TestContent(X, "Foo")));
        // and
        final ContentProvider fetchFirstProvider = fetchFirst(ImmutableList.of(
                nestedProvider,
                (position, parameters) -> just(new TestContent(X, "Bar"))<commit_msg>Added missing tests for CompositeContent<commit_after>package de.otto.rx.composer.providers;

import com.google.common.collect.ImmutableList;
import de.otto.rx.composer.content.*;
import org.junit.Test;
import rx.Observable;

import java.time.LocalDateTime;
import java.util.Iterator;

import static com.google.common.collect.ImmutableList.*;
import static de.otto.rx.composer.content.AbcPosition.X;
import static de.otto.rx.composer.content.Headers.emptyHeaders;
import static de.otto.rx.composer.content.Parameters.emptyParameters;
import static de.otto.rx.composer.providers.ContentProviders.fetchAll;
import static de.otto.rx.composer.providers.ContentProviders.fetchFirst;
import static de.otto.rx.composer.providers.ContentProviders.fetchFirstMatching;
import static java.time.LocalDateTime.now;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static rx.Observable.just;

public class SelectingContentProviderTest {

    @Test
    public void shouldFetchFirstWithContent() {
        // given
        final ContentProvider contentProvider = fetchFirst(of(
                (position, parameters) -> just(new TestContent(X, "Foo")),
                (position, parameters) -> just(new TestContent(X, "Bar"))
        ));
        // when
        final Observable<Content> result =  contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.getBody(), is("Foo"));
    }

    @Test
    public void shouldFetchAllWithContent() {
        // given
        final ContentProvider contentProvider = fetchAll(of(
                (position, parameters) -> just(new TestContent(X, "Foo")),
                (position, parameters) -> just(new TestContent(X, "Bar"))
        ));
        // when
        final Observable<Content> result =  contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.isComposite(), is(true));
        assertThat(content.getBody(), is("Foo\nBar"));
    }

    @Test
    public void shouldFetchFirstMatchingPredicate() {
        // given
        final ContentProvider contentProvider = fetchFirstMatching(
                (content) -> content.getBody().contains("a"),
                of(
                        (position, parameters) -> just(new TestContent(X, "Foo")),
                        (position, parameters) -> just(new TestContent(X, "Bar"))
                )
        );
        // when
        final Observable<Content> result =  contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.getBody(), is("Bar"));
    }

    @Test
    public void shouldSelectFirstNotEmpty() {
        // given
        final ContentProvider contentProvider = fetchFirst(of(
                (position, parameters) -> just(new TestContent(X, "")),
                (position, parameters) -> just(new TestContent(X, "Hello World"))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Iterator<Content> content = result.toBlocking().toIterable().iterator();
        assertThat(content.next().getBody(), is("Hello World"));
        assertThat(content.hasNext(), is(false));
    }

    @Test
    public void shouldHandleEmptyContents() {
        // given
        final ContentProvider contentProvider = fetchFirst(of(
                (position, parameters) -> just(new TestContent(X, "")),
                (position, parameters) -> just(new TestContent(X, ""))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Iterator<Content> content = result.toBlocking().getIterator();
        assertThat(content.hasNext(), is(false));
    }

    @Test
    public void shouldHandleExceptions() {
        // given
        final ContentProvider contentProvider = fetchFirst(of(
                (position, parameters) -> {
                    throw new IllegalStateException("Bumm!!!");
                },
                (position, parameters) -> just(new TestContent(X, "Yeah!"))
        ));
        // when
        final Observable<Content> result = contentProvider.getContent(X, emptyParameters());
        // then
        final Content content = result.toBlocking().single();
        assertThat(content.getBody(), is("Yeah!"));
    }

    @Test
    public void shouldExecuteStepMultipleTimes() {
        // given
        final ContentProvider nestedProvider = mock(ContentProvider.class);
        when(nestedProvider.getContent(X, emptyParameters())).thenReturn(just(new TestContent(X, "Foo")));
        // and
        final ContentProvider fetchFirstProvider = fetchFirst(of(
                nestedProvider,
                (position, parameters) -> just(new TestContent(X, "Bar"))
{'fraction_non_alphanumeric': 0.12118834080717489, 'fraction_numerical': 0.0, 'mean_word_length': 2.786502546689304, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 40, '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': '3753744', 'n_tokens_mistral': 2327, 'n_tokens_neox': 2289, 'n_words': 493}