package devopsdistilled.operp.server.data.repo;

import java.util.List;

import javax.inject.Inject;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import devopsdistilled.operp.server.context.AppTestContext;
import devopsdistilled.operp.server.data.entity.items.Product;
import devopsdistilled.operp.server.data.repo.items.ProductRepository;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppTestContext.class)
@Transactional
public class ProductRepositoryTest {
	/*
	 * @PersistenceContext private EntityManager entityManager;
	 */

	@Inject
	ProductRepository productRepository;

	Product testProduct;

	@Before
	public void setUp() throws Exception {
		/*
		 * JpaRepositoryFactory jpaRepositoryFactory = new JpaRepositoryFactory(
		 * entityManager); productRepository = jpaRepositoryFactory
		 * .getRepository(ProductRepository.class);
		 */
		testProduct = new Product();
		testProduct.setProductName("Test Product");

		assertNotNull(productRepository);
	}

	@Test
	public void testSave() {
		testProduct = productRepository.save(testProduct);
		Assert.assertEquals(testProduct,
				productRepository.findOne(testProduct.getProductId()));
	}

	@Test
	public void testFindByProductName() {
		Product savedProduct = productRepository.save(testProduct);
		List<Product> returnedProducts = productRepository
				.findByProductName("Test Product");
		assertNotNull(returnedProducts);
		System.out.println(returnedProducts.size());
		Assert.assertEquals(1, returnedProducts.size());
		Assert.assertThat(
				"Must have saved productName",
				savedProduct.getProductName(),
				CoreMatchers.is(returnedProducts.get(
						returnedProducts.size() - 1).getProductName()));
	}

	@Test
	public void testSaveCustom() {
		Product fromSave = productRepository.save(testProduct);
		Product fromSaveCustom = productRepository.saveCustom(testProduct);
		Assert.assertThat("save and saveCustom must return same object",
				fromSave, CoreMatchers.is(fromSaveCustom));
	}
}

@@ -42,16 +42,63 @@
 .repo;%0A%0A
+import static org.junit.Assert.assertNotNull;%0A%0A
 import j
@@ -679,54 +679,8 @@
 ory;
-%0Aimport static org.junit.Assert.assertNotNull;
 %0A%0A@R
@@ -2074,24 +2074,32 @@
 ustom() %7B%0A%09%09
+/*%0A%09%09 * 
 Product from
@@ -2141,26 +2141,29 @@
 roduct);
-%0A%09%09
+ 
 Product
+%0A%09%09 *
  fromSav
@@ -2209,32 +2209,35 @@
 testProduct);%0A%09%09
+ * 
 Assert.assertTha
@@ -2287,18 +2287,19 @@
 ect%22,%0A%09%09
-%09%09
+ * 
 fromSave
@@ -2334,13 +2334,19 @@
 stom));%0A
+%09%09 */%0A
 %09%7D%0A%7D%0A

Comment obsolete test definitions