package gcx;

import com.mongodb.client.MongoCollection;

import org.bson.Document;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;

import java.util.Date;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;

/**
 * Created by greg on 01/01/16.
 */
public class JodaTimeTest extends MongoTest {

  @Test
  public void testDateTime_Serial() {
    MongoCollection<Foo> foos = getMongoCollection("serial", Foo.class);
    foos.drop();

    Foo foo = new Foo();
    foos.insertOne(foo);

    Foo anotherFoo = foos.find().first();
    assertEquals(foo, anotherFoo);
  }

  @Test
  public void testDateTime_isMongoDateTime() {
    MongoCollection<Foo> foos = getMongoCollection("isDateTime", Foo.class);
    foos.drop();

    Foo foo = new Foo();
    foos.insertOne(foo);

    MongoCollection<Document> raws = testDatabase.getCollection("isDateTime");
    Document firstFoo = raws.find().first();
    assertTrue(firstFoo.containsKey("ts"));
    Date date = firstFoo.getDate("ts");

    assertEquals(foo.ts.getMillis(), date.getTime());
  }

  private static class Foo {
    public DateTime ts = new DateTime(DateTimeZone.UTC);

    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      Foo foo = (Foo) o;

      return ts != null ? ts.equals(foo.ts) : foo.ts == null;

    }

    @Override
    public int hashCode() {
      return ts != null ? ts.hashCode() : 0;
    }
  }

}

@@ -7,16 +7,124 @@
 e gcx;%0A%0A
+import com.fasterxml.jackson.annotation.JsonSubTypes;%0Aimport com.fasterxml.jackson.annotation.JsonTypeInfo;%0A
 import c
@@ -299,16 +299,37 @@
 .Date;%0A%0A
+import lombok.Data;%0A%0A
 import s
@@ -562,35 +562,36 @@
 MongoCollection%3C
-Foo
+Base
 %3E foos = getMong
@@ -612,19 +612,20 @@
 erial%22, 
-Foo
+Base
 .class);
@@ -657,32 +657,62 @@
 o foo = new Foo(
+new DateTime(DateTimeZone.UTC)
 );%0A    foos.inse
@@ -728,19 +728,20 @@
 );%0A%0A    
-Foo
+Base
  another
@@ -979,16 +979,46 @@
 new Foo(
+new DateTime(DateTimeZone.UTC)
 );%0A    f
@@ -1310,428 +1310,498 @@
  %7D%0A%0A
+%0A
   
-private static class Foo %7B%0A    public DateTime ts = new DateTime(DateTimeZone.UTC);%0A%0A    @Override%0A    public boolean equals(Object o) %7B%0A      if (this == o) return true;%0A      if (o == null %7C%7C getClass() != o.getClass()) return fal
+@JsonTypeInfo(%0A          use = JsonTypeInfo.Id.NAME,%0A          include = JsonTypeInfo.As.PROPERTY,%0A          property = %22type%22)%0A  @JsonSubTypes(%7B%0A          @JsonSubTypes.Type(value = Foo.class, name = %22foo%22)%0A  %7D)%0A  @Data%0A  public static abstract class Base %7B%0A    public String name = %22ba
 se
+%22
 ;%0A
-%0A
   
-    Foo foo = (Foo) o;%0A%0A      return
+%7D%0A%0A  @Data%0A  public static class Foo extends Base %7B%0A    public DateTime
  ts 
-!
 = null
- ? ts.equals(foo.ts) : foo.ts == null;%0A%0A    %7D%0A%0A    @Override%0A    public int hashCode() %7B%0A      return ts != null ? ts.hashCode() : 0
+;%0A    public Foo()%7B%0A      this.name = %22foo%22;%0A    %7D%0A    public Foo(DateTime ts)%7B%0A      this();%0A      this.ts = ts
 ;%0A  

add jodatime+polymorphism test