package de.bwaldvogel.mongo.backend;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import de.bwaldvogel.mongo.bson.Document;

class InMemoryCursorTest {

    @Test
    void testDocumentsCount() {
        InMemoryCursor cursor = new InMemoryCursor(1L, Collections.singleton(new Document()));
        assertThat(cursor.documentsCount()).isEqualTo(1);
    }

    @Test
    void testIsEmpty() {
        InMemoryCursor cursor = new InMemoryCursor(1L, Collections.singleton(new Document()));
        assertThat(cursor.isEmpty()).isFalse();
    }

    @Test
    void testGetCursorId() {
        Cursor cursor = new InMemoryCursor(1L, Collections.singleton(new Document()));
        assertThat(cursor.getCursorId()).isEqualTo(1);
    }

    @Test
    void testTakeDocuments() {
        List<Document> documents = Arrays.asList(new Document("name", "Joe"), new Document("name", "Mary"));

        InMemoryCursor cursor1 = new InMemoryCursor(1L, documents);
        assertThat(cursor1.takeDocuments(10)).containsExactlyElementsOf(documents);

        InMemoryCursor cursor2 = new InMemoryCursor(1L, documents);
        assertThat(cursor2.takeDocuments(1)).containsExactly(documents.get(0));
        assertThat(cursor2.takeDocuments(1)).containsExactly(documents.get(1));
        assertThat(cursor2.takeDocuments(1)).isEmpty();

        assertThatExceptionOfType(IllegalArgumentException.class)
            .isThrownBy(() -> cursor2.takeDocuments(0))
            .withMessage("Illegal number to return: 0");
    }

    @Test
    void testToString() throws Exception {
        assertThat(new InMemoryCursor(123L, Collections.singleton(new Document())))
            .hasToString("InMemoryCursor(id: 123)");
    }

}

@@ -167,33 +167,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -215,16 +215,87 @@
 il.List;
+%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.IntStream;
 %0A%0Aimport
@@ -1050,78 +1050,132 @@
 s = 
-Arrays.asList(new Document(%22name%22, %22Joe%22), new Document(%22name%22, %22Mary%22
+IntStream.range(0, 22)%0A            .mapToObj(idx -%3E new Document(%22number%22, idx + 1))%0A            .collect(Collectors.toList(
 ));%0A
@@ -1204,17 +1204,16 @@
 r cursor
-1
  = new I
@@ -1267,17 +1267,16 @@
 t(cursor
-1
 .takeDoc
@@ -1283,17 +1283,16 @@
 uments(1
-0
 )).conta
@@ -1305,19 +1305,106 @@
 ctly
-ElementsOf(
+(documents.get(0));%0A        assertThat(cursor.takeDocuments(2)).containsExactly(documents.get(1), 
 docu
@@ -1408,28 +1408,34 @@
 ocuments
+.get(2)
 );%0A
-%0A
         
 InMemory
@@ -1430,65 +1430,173 @@
     
-InMemoryCursor cursor2 = new InMemoryCursor(1L, documents
+assertThat(cursor.takeDocuments(1)).containsExactly(documents.get(3));%0A        assertThat(cursor.takeDocuments(6)).containsExactlyElementsOf(documents.subList(4, 10)
 );%0A 
@@ -1611,33 +1611,32 @@
 ssertThat(cursor
-2
 .takeDocuments(1
@@ -1627,32 +1627,33 @@
 .takeDocuments(1
+0
 )).containsExact
@@ -1646,32 +1646,42 @@
 .containsExactly
+ElementsOf
 (documents.get(0
@@ -1667,36 +1667,45 @@
 ntsOf(documents.
-get(
+subList(10, 2
 0));%0A        ass
@@ -1710,33 +1710,32 @@
 ssertThat(cursor
-2
 .takeDocuments(1
@@ -1726,32 +1726,33 @@
 .takeDocuments(1
+0
 )).containsExact
@@ -1768,16 +1768,36 @@
 nts.get(
+20), documents.get(2
 1));%0A   
@@ -1814,25 +1814,24 @@
 tThat(cursor
-2
 .takeDocumen
@@ -1834,16 +1834,17 @@
 uments(1
+0
 )).isEmp
@@ -1952,17 +1952,16 @@
 %3E cursor
-2
 .takeDoc

Extend InMemoryCursorTest