package com.cosium.spring.data.jpa.entity.graph.repository;

import static org.assertj.core.api.Assertions.assertThat;

import javax.inject.Inject;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import java.util.List;

import com.cosium.spring.data.jpa.entity.graph.BaseTest;
import com.cosium.spring.data.jpa.entity.graph.domain.EntityGraphUtils;
import com.cosium.spring.data.jpa.entity.graph.repository.sample.EntityGraphSpecification;
import com.cosium.spring.data.jpa.entity.graph.repository.sample.Product;
import com.cosium.spring.data.jpa.entity.graph.repository.sample.ProductRepository;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.DatabaseTearDown;
import org.hibernate.Hibernate;
import org.junit.Test;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created on 22/11/16.
 *
 * @author Reda.Housni-Alaoui
 */
@DatabaseSetup(BaseTest.DATASET)
@DatabaseTearDown
public class JpaEntityGraphRepositoryTest extends BaseTest {

	@Inject
	private ProductRepository productRepository;

	@Transactional
	@Test
	public void given_null_eg_when_findone_then_then_brand_should_not_be_loaded(){
		Product product = productRepository.findOne(1L, null);
		assertThat(product).isNotNull();
		assertThat(Hibernate.isInitialized(product.getBrand())).isFalse();
	}

	@Transactional
	@Test
	public void given_empty_eg_when_findone_then_then_brand_should_not_be_loaded(){
		Product product = productRepository.findOne(1L, EntityGraphUtils.empty());
		assertThat(product).isNotNull();
		assertThat(Hibernate.isInitialized(product.getBrand())).isFalse();
	}

	@Transactional
	@Test
	public void given_brand_eg_when_findone_then_brand_should_be_loaded(){
		Product product = productRepository.findOne(1L, EntityGraphUtils.fromName(Product.PRODUCT_BRAND_EG));
		assertThat(product).isNotNull();
		assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();
	}

	@Transactional
	@Test
	public void given_brand_eg_when_findByName_then_brand_should_be_loaded(){
		List<Product> products = productRepository.findByName("Product 1", EntityGraphUtils.fromName(Product.PRODUCT_BRAND_EG));
		assertThat(products).hasSize(1);
		for(Product product: products){
			assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();
		}
	}

}
@@ -905,24 +905,121 @@
 junit.Test;%0A
+import org.springframework.data.domain.Page;%0Aimport org.springframework.data.domain.PageRequest;%0A
 import org.s
@@ -2594,9 +2594,1009 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09@Transactional%0A%09@Test%0A%09public void given_brand_eg_when_findAll_paginated_then_brand_should_be_loaded()%7B%0A%09%09Page%3CProduct%3E productPage = productRepository.findAll(new PageRequest(0, 10), EntityGraphUtils.fromName(Product.PRODUCT_BRAND_EG));%0A%09%09assertThat(productPage.getContent()).isNotEmpty();%0A%09%09for(Product product: productPage.getContent())%7B%0A%09%09%09assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();%0A%09%09%7D%0A%09%7D%0A%0A%09@Transactional%0A%09@Test%0A%09public void given_brand_eg_when_findAll_paginated_with_spec_then_brand_should_be_loaded()%7B%0A%09%09Page%3CProduct%3E productPage = productRepository.findAll(new EntityGraphSpecification%3CProduct%3E(Product.PRODUCT_BRAND_EG) %7B%0A%09%09%09@Override%0A%09%09%09public Predicate toPredicate(Root%3CProduct%3E root, CriteriaQuery%3C?%3E query, CriteriaBuilder cb) %7B%0A%09%09%09%09return null;%0A%09%09%09%7D%0A%09%09%7D, new PageRequest(0, 10));%0A%0A%09%09assertThat(productPage.getContent()).isNotEmpty();%0A%09%09for(Product product: productPage.getContent())%7B%0A%09%09%09assertThat(Hibernate.isInitialized(product.getBrand())).isTrue();%0A%09%09%7D%0A%09%7D%0A%0A
 %7D

Add test on pagination