package com.codingbrothers.futurimages.service.impl;

import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;
import static org.junit.Assert.assertEquals;

import java.util.Date;

import javax.inject.Inject;

import org.jukito.UseModules;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.codingbrothers.appengine.testing.GAERunner;
import com.codingbrothers.appengine.testing.GAETest;
import com.codingbrothers.futurimages.config.FuturimagesCommonModule;
import com.codingbrothers.futurimages.config.FuturimagesTestModule;
import com.codingbrothers.futurimages.domain.Image;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.SortDirection;

@RunWith(GAERunner.class)
@UseModules({ FuturimagesTestModule.class, FuturimagesCommonModule.class })
public class FuturimagesImplTest {

	@Inject
	private FuturimagesImpl futurimages;

	@Test
	@GAETest
	public void test() {
		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

		Entity image = new Entity(Image.class.getName());
		image.setProperty("public", true);
		image.setProperty("userId", "dusan.kaloc@gmail.com");
		image.setProperty("createdAt", new Date());

		ds.put(image);

		Query q = new Query(Image.class.getName());
		q.setFilter(CompositeFilterOperator.and(FilterOperator.EQUAL.of("public", true),
				FilterOperator.EQUAL.of("userId", "dusan.kaloc@gmail.com"),
				FilterOperator.LESS_THAN_OR_EQUAL.of("createdAt", new Date())));
		q.addSort("createdAt", SortDirection.DESCENDING);

		assertEquals(1, ds.prepare(q).countEntities(withLimit(10)));
	}
}

@@ -53,164 +53,8 @@
 %0D%0A%0D%0A
-import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit;%0D%0Aimport static org.junit.Assert.assertEquals;%0D%0A%0D%0Aimport java.util.Date;%0D%0A%0D%0A
 impo
@@ -111,16 +111,42 @@
 dules;%0D%0A
+import org.junit.Before;%0D%0A
 import o
@@ -450,494 +450,8 @@
 le;%0D
-%0Aimport com.codingbrothers.futurimages.domain.Image;%0D%0Aimport com.google.appengine.api.datastore.DatastoreService;%0D%0Aimport com.google.appengine.api.datastore.DatastoreServiceFactory;%0D%0Aimport com.google.appengine.api.datastore.Entity;%0D%0Aimport com.google.appengine.api.datastore.Query;%0D%0Aimport com.google.appengine.api.datastore.Query.CompositeFilterOperator;%0D%0Aimport com.google.appengine.api.datastore.Query.FilterOperator;%0D%0Aimport com.google.appengine.api.datastore.Query.SortDirection;%0D
 %0A%0D%0A@
@@ -489,17 +489,16 @@
 odules(%7B
- 
 Futurima
@@ -547,17 +547,16 @@
 le.class
- 
 %7D)%0D%0Apubl
@@ -642,16 +642,53 @@
 s;%0D%0A%0D%0A%09@
+Before%0D%0A%09public void setUp() %7B%7D%0D%0A%0D%0A%09@
 Test%0D%0A%09@
@@ -721,687 +721,8 @@
 () %7B
-%0D%0A%09%09DatastoreService ds = DatastoreServiceFactory.getDatastoreService();%0D%0A%0D%0A%09%09Entity image = new Entity(Image.class.getName());%0D%0A%09%09image.setProperty(%22public%22, true);%0D%0A%09%09image.setProperty(%22userId%22, %22dusan.kaloc@gmail.com%22);%0D%0A%09%09image.setProperty(%22createdAt%22, new Date());%0D%0A%0D%0A%09%09ds.put(image);%0D%0A%0D%0A%09%09Query q = new Query(Image.class.getName());%0D%0A%09%09q.setFilter(CompositeFilterOperator.and(FilterOperator.EQUAL.of(%22public%22, true),%0D%0A%09%09%09%09FilterOperator.EQUAL.of(%22userId%22, %22dusan.kaloc@gmail.com%22),%0D%0A%09%09%09%09FilterOperator.LESS_THAN_OR_EQUAL.of(%22createdAt%22, new Date())));%0D%0A%09%09q.addSort(%22createdAt%22, SortDirection.DESCENDING);%0D%0A%0D%0A%09%09assertEquals(1, ds.prepare(q).countEntities(withLimit(10)));%0D%0A%09
 %7D%0D%0A%7D

Clean test