// Copyright 2011 Google Inc. All Rights Reserved.

package com.google.appengine.tck.datastore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;

/**
 * datastore key data type test.
 *
 * @author hchen@google.com (Hannah Chen)
 */
@RunWith(Arquillian.class)
public class EntityTest extends DatastoreTestBase {
    private String kindName = "EntityData";

    @Test
    public void testKeyName() throws InterruptedException {
        clearData(kindName);
        Entity newRec = new Entity(kindName, "test4116132", null);
        newRec.setProperty("stamp", new Date());
        Key key = service.put(newRec);
        assertEquals("test4116132", key.getName());
        assertEquals(null, key.getParent());

        newRec = new Entity(kindName, "test4116132-child", key);
        newRec.setProperty("stamp", new Date());
        Key ckey = service.put(newRec);
        assertEquals("test4116132-child", ckey.getName());
        assertEquals(key, ckey.getParent());
    }

    @Test
    public void testKeyId() throws InterruptedException {
        clearData(kindName);
        Entity newRec = new Entity(kindName, 4116132, null);
        newRec.setProperty("stamp", new Date());
        Key key = service.put(newRec);
        assertEquals(4116132, key.getId());
        assertEquals(null, key.getParent());

        newRec = new Entity(kindName, 41161320, key);
        newRec.setProperty("stamp", new Date());
        Key ckey = service.put(newRec);
        assertEquals(41161320, ckey.getId());
        assertEquals(key, ckey.getParent());
    }

    @Test
    public void testKey() throws InterruptedException {
        clearData(kindName);
        Entity newRec = new Entity(kindName);
        newRec.setProperty("stamp", new Date());
        Key key = service.put(newRec);

        Entity newRec2 = new Entity(key);
        Date newDate = new Date();
        newRec2.setProperty("stamp", newDate);
        service.put(newRec2);
        assertEquals(newRec, newRec2);
        assertEquals(newDate, newRec2.getProperty("stamp"));
    }

    @Override
    public void clearData(String kind) throws InterruptedException {
        List<Key> eList = new ArrayList<Key>();
        Query query = new Query(kind);
        for (Entity readRec : service.prepare(query).asIterable()) {
            eList.add(readRec.getKey());
        }
        if (eList.size() > 0) {
            service.delete(eList);
            sync(waitTime);
        }
    }
}

@@ -114,24 +114,54 @@
 .ArrayList;%0A
+import java.util.Collections;%0A
 import java.
@@ -572,16 +572,59 @@
 h Chen)%0A
+ * @author mluksa@redhat.com (Marko Luksa)%0A
  */%0A@Run
@@ -2424,24 +2424,351 @@
 %22));%0A    %7D%0A%0A
+    @Test%0A    public void testEntityGainsNoAdditionalPropertiesWhenStored() throws Exception %7B%0A      clearData(kindName);%0A      Entity entity = new Entity(kindName);%0A      Key key = service.put(entity);%0A      entity = service.get(key);%0A      assertEquals(Collections.%3CString, Object%3EemptyMap(), entity.getProperties());%0A    %7D%0A%0A
     @Overrid

Add test to check if Entity gains any properties when stored