<commit_before>package com.telekom.m2m.cot.restsdk.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.telekom.m2m.cot.restsdk.CloudOfThingsRestClient;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.annotation.Nonnull;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class IterableObjectPaginationTest {

    private static final int PAGE_SIZE_IN_TESTS = 3;

    /**
     * System under test.
     */
    private IterableObjectPagination<ExtensibleObject> pagination;

    /**
     * A mocked REST client.
     */
    private CloudOfThingsRestClient cloudOfThingsRestClient;

    @BeforeTest
    public void setup() {
        cloudOfThingsRestClient = createRestClient();
        pagination = new IterableObjectPagination<ExtensibleObject>(
            cloudOfThingsRestClient,
            "test/url",
            GsonUtils.createGson(),
            "application/json",
            "operations",
            null,
            PAGE_SIZE_IN_TESTS
        ) {
            @Nonnull
            @Override
            protected ExtensibleObject convertJsonToObject(@Nonnull final JsonElement element) {
                return gson.fromJson(element, ExtensibleObject.class);
            }
        };
    }

    @Test
    public void streamReadsObjectsUntilLastPageIsReached() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 10);
    }

    @Test
    public void streamReadsObjectsIfThereIsOnlyOnePage() {
        simulateNumberOfObjectsOnPages(2);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 2);
    }

    @Test
    public void loadsOnlyNecessaryPagesIfNotAllObjectsInStreamAreConsumed() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().limit(5).collect(Collectors.toList());

        assertEquals(objects.size(), 5);
        assertPageNotRequested(3);
        assertPageNotRequested(4);
    }

    @Test
    public void doesNotLoadAnyPageIfStreamDoesNotReadAnyObject() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().limit(0).collect(Collectors.toList());

        assertEquals(objects.size(), 0);
        assertPageNotRequested(1);
        assertPageNotRequested(2);
        assertPageNotRequested(3);
        assertPageNotRequested(4);
    }

    @Test
    public void returnsNoObjectsIfPageIsEmpty() {
        simulateNumberOfObjectsOnPages(0);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 0);
    }

    private void simulateNumberOfObjectsOnPages(final int numberOfObjects) {
        if (numberOfObjects == 0) {
            simulatePageResponse(1, readTemplate("pages/no-filter/empty-page.json"));
            return;
        }
        final String pageTemplate = readTemplate("pages/no-filter/page.json.template");
        final String itemTemplate = readTemplate("pages/no-filter/item.json.template");
        final int totalPages = ceilDiv(numberOfObjects, PAGE_SIZE_IN_TESTS);
        for (int page = 1; page <= totalPages; page++) {
            final int itemsOnPage = (page == totalPages) ? numberOfObjects % PAGE_SIZE_IN_TESTS : PAGE_SIZE_IN_TESTS;
            final String itemsJson = IntStream.range(1, itemsOnPage)
                .mapToObj(id -> itemTemplate.replace("%%id%%", String.valueOf(id)))
                .collect(Collectors.joining(","));<commit_msg>fixed test setup: last value was excluded from range, which led to missing (simulated) objects<commit_after>package com.telekom.m2m.cot.restsdk.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.telekom.m2m.cot.restsdk.CloudOfThingsRestClient;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import javax.annotation.Nonnull;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class IterableObjectPaginationTest {

    private static final int PAGE_SIZE_IN_TESTS = 3;

    /**
     * System under test.
     */
    private IterableObjectPagination<ExtensibleObject> pagination;

    /**
     * A mocked REST client.
     */
    private CloudOfThingsRestClient cloudOfThingsRestClient;

    @BeforeTest
    public void setup() {
        cloudOfThingsRestClient = createRestClient();
        pagination = new IterableObjectPagination<ExtensibleObject>(
            cloudOfThingsRestClient,
            "test/url",
            GsonUtils.createGson(),
            "application/json",
            "operations",
            null,
            PAGE_SIZE_IN_TESTS
        ) {
            @Nonnull
            @Override
            protected ExtensibleObject convertJsonToObject(@Nonnull final JsonElement element) {
                return gson.fromJson(element, ExtensibleObject.class);
            }
        };
    }

    @Test
    public void streamReadsObjectsUntilLastPageIsReached() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 10);
    }

    @Test
    public void streamReadsObjectsIfThereIsOnlyOnePage() {
        simulateNumberOfObjectsOnPages(2);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 2);
    }

    @Test
    public void loadsOnlyNecessaryPagesIfNotAllObjectsInStreamAreConsumed() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().limit(5).collect(Collectors.toList());

        assertEquals(objects.size(), 5);
        assertPageNotRequested(3);
        assertPageNotRequested(4);
    }

    @Test
    public void doesNotLoadAnyPageIfStreamDoesNotReadAnyObject() {
        simulateNumberOfObjectsOnPages(10);

        final List<ExtensibleObject> objects = pagination.stream().limit(0).collect(Collectors.toList());

        assertEquals(objects.size(), 0);
        assertPageNotRequested(1);
        assertPageNotRequested(2);
        assertPageNotRequested(3);
        assertPageNotRequested(4);
    }

    @Test
    public void returnsNoObjectsIfPageIsEmpty() {
        simulateNumberOfObjectsOnPages(0);

        final List<ExtensibleObject> objects = pagination.stream().collect(Collectors.toList());

        assertEquals(objects.size(), 0);
    }

    private void simulateNumberOfObjectsOnPages(final int numberOfObjects) {
        if (numberOfObjects == 0) {
            simulatePageResponse(1, readTemplate("pages/no-filter/empty-page.json"));
            return;
        }
        final String pageTemplate = readTemplate("pages/no-filter/page.json.template");
        final String itemTemplate = readTemplate("pages/no-filter/item.json.template");
        final int totalPages = ceilDiv(numberOfObjects, PAGE_SIZE_IN_TESTS);
        for (int page = 1; page <= totalPages; page++) {
            final int itemsOnPage = (page == totalPages) ? numberOfObjects % PAGE_SIZE_IN_TESTS : PAGE_SIZE_IN_TESTS;
            final String itemsJson = IntStream.range(1, itemsOnPage + 1)
                .mapToObj(id -> itemTemplate.replace("%%id%%", String.valueOf(id)))
                .collect(Collectors.joining(","));
{'fraction_non_alphanumeric': 0.10057860396405269, 'fraction_numerical': 0.007263326357257171, 'mean_word_length': 3.1640184520758585, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1796570', 'n_tokens_mistral': 2337, 'n_tokens_neox': 2233, 'n_words': 406}