/*
 * 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.paging;

import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;

import anyframe.core.hibernate.AbstractConfigurationalTransactionalTest;
import anyframe.core.hibernate.SetUpInitData;
import anyframe.sample.model.bidirection.Movie;

/**
 * TestCase Name : HibernateCriteriaPagingTest<br>
 * <br>
 * [Description] : Criteria를 객체 조회시 페이징 처리된 조회 결과를 얻기 위한 방법에 대해 알아본다.<br>
 * [Main Flow]
 * <ul>
 * <li>#-1 Positive Case : MOVIE 테이블을 대상으로 HQL을 이용한 조회 작업을 수행한다. 이때, 조회를 시작해야
 * 하는 Row의 Number(FirstResult)와 조회 목록의 개수(MaxResult)를 정의함으로써, 페이징 처리가 가능해진다.</li>
 * </ul>
 * 
 * @author SoYon Lim
 */
public class HibernateCriteriaPagingTest extends
		AbstractConfigurationalTransactionalTest {
	protected String getHibernateConfigLocation() {
		return "hibernateconfig/hibernate.cfg.xml";
	}

	/**
	 * [Flow #-1] Positive Case : MOVIE 테이블을 대상으로 HQL을 이용한 조회 작업을 수행한다. 이때, 조회를
	 * 시작해야 하는 Row의 Number(FirstResult)와 조회 목록의 개수(MaxResult)를 정의함으로써, 페이징 처리가
	 * 가능해진다.
	 * 
	 * @throws Exception
	 *             throws exception which is from hibernate
	 */
	public void testMovieListWithCriteria() throws Exception {
		// 1. insert init data
		SetUpInitData.initializeData(session);

		// 2. execute criteria
		Criteria criteria = session.createCriteria(Movie.class);
		criteria.setFirstResult(1);
		criteria.setMaxResults(2);
		List movieList = criteria.list();

		// 3. assert result - movie
		assertEquals("fail to match the size of movie list.", 2, movieList
				.size());

		for (int i = 0; i < movieList.size(); i++) {
			Movie movie = (Movie) movieList.get(i);

			if (i == 0) {
				assertEquals("fail to match a movie title.", "My Little Bride",
						movie.getTitle());
				assertEquals("fail to match a movie director.", "Hojun Kim",
						movie.getDirector());

				Set categories = movie.getCategories();
				assertEquals("fail to match the size of category list.", 2,
						categories.size());
			} else if (i == 1) {
				assertEquals("fail to match a movie title.", "Ring 2", movie
						.getTitle());
				assertEquals("fail to match a movie director.", "Hideo Nakata",
						movie.getDirector());

				Set categories = movie.getCategories();
				assertEquals("fail to match the size of category list.", 1,
						categories.size());
			}
		}
	}
}

