// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/CollectionMatchers.java
// public static <T> Matcher<T> isInCollection(Collection<T> collection) {
// 	return new TypeSafeMatcher<T>() {
// 
// 		@Override
// 		public void describeTo(Description description) {
// 			description.appendText("in collection: ")
// 					.appendValueList("[", ",", "]", collection);
// 		}
// 
// 		@Override
// 		protected boolean matchesSafely(T item) {
// 			return collection.contains(item);
// 		}
// 			
// 	};
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/SampleEntityMatchers.java
// public static Matcher<SampleEntity> hasAmountLowerThan(double threshold) {
// 	return new TypeSafeMatcher<SampleEntity>() {
// 		@Override
// 		public void describeTo(Description description) {
// 			description.appendText("having amount lower than")
// 					.appendValue(threshold);
// 		}
// 			
// 		@Override
// 		protected boolean matchesSafely(SampleEntity item) {
// 			return item.getAmount() < threshold;
// 		}
// 	};
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/conf/MyGuiceModule.java
// public class MyGuiceModule extends AbstractModule {
// 
// 	@Override
// 	protected void configure() {
// 		install(new JpaPersistModule("my-persistence-unit"));
// 		
// 		bind(MyJpaInitializer.class).asEagerSingleton();
// 	}
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/conf/MyJpaInitializer.java
// public class MyJpaInitializer {
// 	private PersistService persistService;
// 	
// 	@Inject
// 	MyJpaInitializer(PersistService service) {
// 		persistService = service;
// 		persistService.start();
// 	}
// 	
// 	public void stopService() {
// 		persistService.stop();
// 	}
// }


import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static pl.ciruk.blog.jpa_unit_tests.CollectionMatchers.isInCollection;
import static pl.ciruk.blog.jpa_unit_tests.SampleEntityMatchers.hasAmountLowerThan;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import pl.ciruk.blog.jpa_unit_tests.conf.MyGuiceModule;
import pl.ciruk.blog.jpa_unit_tests.conf.MyJpaInitializer;
import com.google.inject.Guice;

	public void setUp() {
		Guice.createInjector(new MyGuiceModule()).injectMembers(this);
		entityManager.getTransaction().begin();
	}
	
	@After
	public void cleanUp() {
		entityManager.getTransaction().rollback();
		jpaInitializer.stopService();
	}
	
	@Test
	public void shouldRetrieveEntitiesWithAmountBelowThreshold() {
		int numberOfMockEntities = 100;
		List<SampleEntity> mockEntities = Entities.mockListOf(SampleEntity.class, numberOfMockEntities);
		
		double amountThreshold = 0.75;
		long numberOfEntitesWithAmountBelowThreshold = mockEntities.stream()
			.filter(entity -> entity.getAmount() < amountThreshold)
			.count();
		
		mockEntities.stream()
			.forEach(entityManager::persist);
	
		@SuppressWarnings("unchecked")
		List<SampleEntity> actualEntities = entityManager
				.createNamedQuery(SampleEntity.Query.WITH_AMOUNT_BELOW)
				.setParameter("amountThreshold", amountThreshold)
				.getResultList();
		

// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/CollectionMatchers.java
// public static <T> Matcher<T> isInCollection(Collection<T> collection) {
// 	return new TypeSafeMatcher<T>() {
// 
// 		@Override
// 		public void describeTo(Description description) {
// 			description.appendText("in collection: ")
// 					.appendValueList("[", ",", "]", collection);
// 		}
// 
// 		@Override
// 		protected boolean matchesSafely(T item) {
// 			return collection.contains(item);
// 		}
// 			
// 	};
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/SampleEntityMatchers.java
// public static Matcher<SampleEntity> hasAmountLowerThan(double threshold) {
// 	return new TypeSafeMatcher<SampleEntity>() {
// 		@Override
// 		public void describeTo(Description description) {
// 			description.appendText("having amount lower than")
// 					.appendValue(threshold);
// 		}
// 			
// 		@Override
// 		protected boolean matchesSafely(SampleEntity item) {
// 			return item.getAmount() < threshold;
// 		}
// 	};
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/conf/MyGuiceModule.java
// public class MyGuiceModule extends AbstractModule {
// 
// 	@Override
// 	protected void configure() {
// 		install(new JpaPersistModule("my-persistence-unit"));
// 		
// 		bind(MyJpaInitializer.class).asEagerSingleton();
// 	}
// }
// 
// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/conf/MyJpaInitializer.java
// public class MyJpaInitializer {
// 	private PersistService persistService;
// 	
// 	@Inject
// 	MyJpaInitializer(PersistService service) {
// 		persistService = service;
// 		persistService.start();
// 	}
// 	
// 	public void stopService() {
// 		persistService.stop();
// 	}
// }


// Path: blog-code/src/test/java/pl/ciruk/blog/jpa_unit_tests/SampleEntityTest.java
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static pl.ciruk.blog.jpa_unit_tests.CollectionMatchers.isInCollection;
import static pl.ciruk.blog.jpa_unit_tests.SampleEntityMatchers.hasAmountLowerThan;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import pl.ciruk.blog.jpa_unit_tests.conf.MyGuiceModule;
import pl.ciruk.blog.jpa_unit_tests.conf.MyJpaInitializer;
import com.google.inject.Guice;

	public void setUp() {
		Guice.createInjector(new MyGuiceModule()).injectMembers(this);
		entityManager.getTransaction().begin();
	}
	
	@After
	public void cleanUp() {
		entityManager.getTransaction().rollback();
		jpaInitializer.stopService();
	}
	
	@Test
	public void shouldRetrieveEntitiesWithAmountBelowThreshold() {
		int numberOfMockEntities = 100;
		List<SampleEntity> mockEntities = Entities.mockListOf(SampleEntity.class, numberOfMockEntities);
		
		double amountThreshold = 0.75;
		long numberOfEntitesWithAmountBelowThreshold = mockEntities.stream()
			.filter(entity -> entity.getAmount() < amountThreshold)
			.count();
		
		mockEntities.stream()
			.forEach(entityManager::persist);
	
		@SuppressWarnings("unchecked")
		List<SampleEntity> actualEntities = entityManager
				.createNamedQuery(SampleEntity.Query.WITH_AMOUNT_BELOW)
				.setParameter("amountThreshold", amountThreshold)
				.getResultList();
		

		assertThat(actualEntities.stream().count(), 
