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.Product;

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()));
	}
}

@@ -1958,11 +1958,297 @@
 ()));%0A%09%7D
+%0A%0A%09@Test%0A%09public void testSaveCustom() %7B%0A%09%09Product fromSave = productRepository.save(testProduct);%0A%09%09Product fromSaveCustom = productRepository.saveCustom(testProduct);%0A%09%09Assert.assertThat(%22save and saveCustom must return same object%22,%0A%09%09%09%09fromSave, CoreMatchers.is(fromSaveCustom));%0A%09%7D
 %0A%7D%0A

Update Test for ProductRepository