package org.zheng.demo.repository;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.transaction.annotation.Transactional;
import org.zheng.demo.entity.Product;
import org.zheng.demo.entity.ProductProfile;

@DataJpaTest
@Transactional
class ProductRepoTest {

	@Autowired
	private ProductRepo prodRepo;

	@Autowired
	private ProductProfileRepo prodProfileRepo;

	@Test
	void save_product_and_profile() {
		Product prod = new Product();
		prod.setName("product");

		prodRepo.save(prod);

		ProductProfile prodProfile = new ProductProfile();
		prodProfile.setProduct(prod);
		prodProfile.setProductDetail("some details");

		prodProfileRepo.save(prodProfile);

		Optional<Product> foundProd = prodRepo.findById(prod.getId());
		Optional<ProductProfile> foundProdProfile = prodProfileRepo.findById(prodProfile.getId());

		assertTrue(foundProd.isPresent());
		assertTrue(foundProdProfile.isPresent());
		assertEquals(foundProd.get().getId(), foundProdProfile.get().getId());
	}

}
