/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/
package anyframe.core.hibernate.fetch;

import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import anyframe.core.hibernate.AbstractConfigurationalTest;
import anyframe.core.hibernate.SetUpInitData;
import anyframe.sample.model.bidirection.Category;

/**
 * TestCase Name : HibernateFetchWithDefaultLazyLoadingTest<br>
 * <br>
 * [Description] : Hibernate Lazy Loading으로 발생할 수 있는 N+1 SELECT 문제에 대해 살펴본다.
 * 수행되는 쿼리문의 개수와 쿼리문을 직접 확인해 볼 수 있다.<br>
 * [Main Flow]
 * <ul>
 * <li>#-1 Positive Case : CATEGORY 테이블을 대상으로 HQL을 이용한 조회 작업을 수행한다. 특정
 * Category에 속한 Movie Set 조회시 Movie 정보 조회를 위한 SELECT문이 수행된다.</li>
 * </ul>
 * 
 * @author SoYon Lim
 */
public class HibernateFetchWithDefaultLazyLoadingTest extends
		AbstractConfigurationalTest {
	protected String getHibernateConfigLocation() {
		return "anyframe/core/hibernate/fetch/hibernate.cfg.xml";
	}

	/**
	 * [Flow #-1] Positive Case : CATEGORY 테이블을 대상으로 HQL을 이용한 조회 작업을 수행한다. 특정
	 * Category에 속한 Movie Set 조회시 Movie 정보 조회를 위한 SELECT문이 수행된다.
	 * 
	 * @throws Exception
	 *             throws exception which is from hibernate
	 */
	public void testCategoryList() throws Exception {
		// 1. insert init data
		session = initialSessionFactory.openSession();
		session.beginTransaction();
		SetUpInitData.initializeData(session);
		session.getTransaction().commit();
		session.close();

		// 2. execute hql with another session
		session = initialSessionFactory.openSession();
		StringBuffer hqlBuf = new StringBuffer();
		hqlBuf.append("FROM Category category ");
		hqlBuf.append("ORDER BY category.categoryName ASC");
		Query query = session.createQuery(hqlBuf.toString());
		List categoryList = query.list();

		// 3. assert result - category
		assertEquals("fail to match the size of category list.", 4,
				categoryList.size());

		for (int i = 0; i < categoryList.size(); i++) {
			Category category = (Category) categoryList.get(i);

			if (i == 0) {
				assertEquals("fail to match a category name.", "Comedy",
						category.getCategoryName());

				Set movies = category.getMovies();
				assertEquals("fail to match the size of movie list.", 2, movies
						.size());
			} else if (i == 1) {
				assertEquals("fail to match a category name.", "Horror",
						category.getCategoryName());

				Set movies = category.getMovies();
				assertEquals("fail to match the size of movie list.", 1, movies
						.size());
			} else if (i == 2) {
				assertEquals("fail to match a category name.", "Romantic",
						category.getCategoryName());

				Set movies = category.getMovies();
				assertEquals("fail to match the size of movie list.", 2, movies
						.size());
			} else if (i == 3) {
				assertEquals("fail to match a category name.", "SF", category
						.getCategoryName());

				Set movies = category.getMovies();
				assertEquals("fail to match the size of movie list.", 0, movies
						.size());

			}
		}
		session.close();
	}
}

