package com.petterfactory.couchbaseliteorm;

import com.couchbase.lite.Document;
import com.samples.Person;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.HashMap;

import static com.google.common.truth.Truth.ASSERT;
import static org.mockito.Mockito.when;

/**
 * Created by brais on 9/1/15.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest(Document.class)
public class CouchbaseLiteOrmInternalBaseTest {

  @Test
  public void checkGet_correct() {
    Document document = PowerMockito.mock(Document.class); // FIXME remove PowerMockito
    final HashMap<String, Object> properties = new HashMap<>();
    properties.put("type", "person");
    properties.put("name", "Pepe");
    properties.put("age", 23);
    when(document.getProperties()).thenReturn(properties);

    CouchbaseLiteOrmInternalBase orm = new CouchbaseLiteOrmInternal();

    Person person = orm.get(document);

    ASSERT.that(person.getName()).isEqualTo("Pepe");
    ASSERT.that(person.getAge()).isEqualTo(23);
  }

  @Test
  public void checkGet_noType() {
    Document document = PowerMockito.mock(Document.class); // FIXME remove PowerMockito
    final HashMap<String, Object> properties = new HashMap<>();
    when(document.getProperties()).thenReturn(properties);
    when(document.getId()).thenReturn("foo");

    CouchbaseLiteOrmInternalBase orm = new CouchbaseLiteOrmInternal();

    try {
      orm.get(document);
      ASSERT.fail();
    } catch (IllegalArgumentException e) {
      ASSERT.that(e.getMessage()).isEqualTo("The document foo doesn't have set the \"type\" property.");
    }
  }

  @Test
  public void checkGet_unknownType() {
    Document document = PowerMockito.mock(Document.class); // FIXME remove PowerMockito
    final HashMap<String, Object> properties = new HashMap<>();
    properties.put("type", "bar");
    when(document.getProperties()).thenReturn(properties);
    when(document.getId()).thenReturn("foo");

    CouchbaseLiteOrmInternalBase orm = new CouchbaseLiteOrmInternal();

    try {
      orm.get(document);
      ASSERT.fail();
    } catch (IllegalArgumentException e) {
      ASSERT.that(e.getMessage()).isEqualTo("Unknown type bar at document foo.");
    }
  }
}

@@ -119,20 +119,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0Aimport
@@ -662,16 +662,87 @@
 Test %7B%0A%0A
+  @Rule%0A  public ExpectedException thrown = ExpectedException.none();%0A%0A
   @Test%0A
@@ -1692,145 +1692,78 @@
    t
-ry %7B%0A      orm.get(document);%0A      ASSERT.fail();%0A    %7D catch (IllegalArgumentException e) %7B%0A      ASSERT.that(e.getMessage()).isEqualTo
+hrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage
 (%22Th
@@ -1820,25 +1820,42 @@
 rty.%22);%0A    
-%7D
+orm.get(document);
 %0A  %7D%0A%0A  @Tes
@@ -2269,145 +2269,78 @@
    t
-ry %7B%0A      orm.get(document);%0A      ASSERT.fail();%0A    %7D catch (IllegalArgumentException e) %7B%0A      ASSERT.that(e.getMessage()).isEqualTo
+hrown.expect(IllegalArgumentException.class);%0A    thrown.expectMessage
 (%22Un
@@ -2378,16 +2378,33 @@
 %22);%0A    
-%7D
+orm.get(document);
 %0A  %7D%0A%7D%0A

Use ExpectedException