package com.github.kohanyirobert.ebson;

import com.google.common.collect.Maps;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import java.util.Map;

@SuppressWarnings("static-method")
public final class BsonDocumentsTest extends AbstractBsonTest {

  private static final String KEY1 = "key1";
  private static final String KEY2 = "key2";
  private static final String KEY3 = "key3";

  public BsonDocumentsTest() {}

  @Test
  public void of_nothing_shouldBeEmpty() {
    BsonDocument empty = BsonDocuments.of();
    assertTrue(empty.keySet().isEmpty());
    assertTrue(empty.values().isEmpty());
  }

  @Test
  public void of_nothing_shouldEqualOtherEmptyDocuments() {
    BsonDocument empty = BsonDocuments.of();
    assertEquals(empty, BsonDocuments.of());
    assertEquals(empty, BsonDocuments.copyOf(empty));
    assertEquals(empty, BsonDocuments.copyOf(Maps.<String, Object>newLinkedHashMap()));
  }

  @Test(expected = NullPointerException.class)
  public void copyOf_document_withNullDocument() {
    BsonDocuments.copyOf((BsonDocument) null);
  }

  @Test
  public void copyOf_map_withLegalKeys() {
    Map<String, Object> map = BsonDocuments.builder()
        .put(KEY1, null)
        .put(KEY2, null)
        .put(KEY3, null)
        .build();
    BsonDocument document = BsonDocuments.copyOf(map);
    assertArrayEquals(map.keySet().toArray(), document.keySet().toArray());
    assertArrayEquals(map.values().toArray(), document.values().toArray());
  }

  @Test(expected = NullPointerException.class)
  public void copyOf_map_withNullMap() {
    BsonDocuments.copyOf((Map<String, Object>) null);
  }

  @Test(expected = NullPointerException.class)
  public void copyOf_map_withNullKey() {
    BsonDocuments.copyOf(BsonDocuments.builder()
        .put(KEY1, null)
        .put(KEY2, null)
        .put(null, null)
        .build());
  }

  @Test
  public void of_singleKeyValuePair_withLegalKey() {
    BsonDocument document = BsonDocuments.of(KEY1, null);
    assertTrue(document.keySet().contains(KEY1));
  }

  @Test(expected = NullPointerException.class)
  public void of_singleKeyValuePair_withNullKey() {
    BsonDocuments.of(null, null);
  }

  @Test
  public void of_multipleKeyValuePairs_withLegalKeys() {
    BsonDocument document = BsonDocuments.of(KEY1, null, KEY2, null, KEY3, null);
    assertArrayEquals(document.keySet().toArray(), new Object[] {KEY1, KEY2, KEY3});
  }

  @Test(expected = NullPointerException.class)
  public void of_multipleKeyValuePairs_withNullKey() {
    BsonDocuments.of(KEY1, null, null, null, KEY3, null);
  }

  @Test(expected = IllegalArgumentException.class)
  public void of_multipleKeyValuePairs_withDuplicateKeys() {
    BsonDocuments.of(KEY1, null, KEY1, null, KEY3, null);
  }
}

@@ -237,16 +237,44 @@
 .Test;%0A%0A
+import java.nio.ByteBuffer;%0A
 import j
@@ -2877,11 +2877,649 @@
 ll);%0A  %7D
+%0A%0A  @Test%0A  public void readFrom_singleByteBuffer_withMultipleDocuments() %7B%0A    BsonDocument document1 = BsonDocuments.of(KEY1, null);%0A    BsonDocument document2 = BsonDocuments.of(KEY2, null);%0A    BsonDocument document3 = BsonDocuments.of(KEY3, null);%0A%0A    ByteBuffer buffer = BUFFER.get();%0A%0A    BsonDocuments.writeTo(buffer, document1);%0A    BsonDocuments.writeTo(buffer, document3);%0A    BsonDocuments.writeTo(buffer, document2);%0A%0A    buffer.flip();%0A%0A    assertEquals(BsonDocuments.readFrom(buffer), document1);%0A    assertEquals(BsonDocuments.readFrom(buffer), document3);%0A    assertEquals(BsonDocuments.readFrom(buffer), document2);%0A  %7D
 %0A%7D%0A

Update BsonDocumentsTest