/*
 * 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 integration.anyframe.services.query;

import integration.anyframe.services.AbstractTest;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import anyframe.core.query.IQueryService;

public class QueryServiceTestEmbeddedSQL extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		QueryServiceTestEmbeddedSQL queryTest = new QueryServiceTestEmbeddedSQL();
		// 1. initialize context
		queryTest.setup();
		// 2. testInsertQuery();
		queryTest.testInsertQuery();
		// 3. testSelectQuery();
		queryTest.testSelectQuery();
		// 4. testUpdateQuery();
		queryTest.testUpdateQuery();
		// 5. testDeleteQuery();
		queryTest.testDeleteQuery();
		// 6. close context
		queryTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 테스트 코드를 샐행하기 위한 data table 생성
	 */
	public void testInit() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		queryService.updateBySQL("DROP TABLE TBL_CUSTOMER IF EXISTS",
				new String[] {}, new Object[] {});
		queryService.updateBySQL("CREATE TABLE TBL_CUSTOMER ( "
				+ "SSNO varchar(13) NOT NULL, " + "NAME varchar(20), "
				+ "ADDRESS varchar(20), " + "PRIMARY KEY (SSNO))",
				new String[] {}, new Object[] {});
	}

	/**
	 * Query 서비스를 통해 DB에 신규 데이터를 입력하는 테스트 코드
	 */	
	public void testInsertQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		//createBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 INSERT를 실행한다.
		int result = queryService.createBySQL(
				"insert into TBL_CUSTOMER values (?, ?, ?)", new String[] {
						"VARCHAR", "VARCHAR", "VARCHAR" }, new Object[] {
						"1234567890123", "GilDongHong", "Seoul" });
		if (result == -1) {
			throw new Exception("Insert Query failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 조회하는 테스트 코드
	 */	
	public void testSelectQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		// findBySQL() :  매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 SELECT를 실행한다.
		Collection result = queryService.findBySQL(
				"select NAME, ADDRESS from TBL_CUSTOMER where SSNO like ?",
				new String[] { "VARCHAR" }, new Object[] { "%4567890123" });
		Iterator resultItr = result.iterator();
		while (resultItr.hasNext()) {
			Map resultMap = (Map) resultItr.next();
			resultMap.get("name");
		}
		if (result.size() != 1) {
			throw new Exception("Select Query failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 수정하는 테스트 코드
	 */	
	public void testUpdateQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		// updateBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 UPDATE를 실행한다.
		int result = queryService.updateBySQL(
				"update TBL_CUSTOMER set NAME=? where SSNO=?", new String[] {
						"VARCHAR", "VARCHAR" }, new Object[] { "Anonymous",
						"1234567890123" });
		if (result == -1) {
			throw new Exception("Update Query failed");
		}
	}

	/**
	 * Query 서비스를 통해 DB에 입력된 데이터를 삭제하는 테스트 코드
	 */	
	public void testDeleteQuery() throws Exception {
		IQueryService queryService = (IQueryService) context
				.getBean("queryService");
		// removeBySQL() : 매핑 XML 파일에 쿼리문이 정의되어 있지 않을 때 DELETE를 실행한다.
		int result = queryService.removeBySQL(
				"delete from TBL_CUSTOMER where SSNO=?",
				new String[] { "VARCHAR" }, new Object[] { "1234567890123" });
		if (result == -1) {
			throw new Exception("Delete Query failed");
		}
	}

	protected String[] getConfigLocations() {
		return new String[] {
				"classpath*:/common/applicationContext-*.xml",
				"classpath*:/services/datasource/applicationContext-datasource-common.xml",
				"classpath*:/services/query/applicationContext-query-common.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader.xml" };
	}
}

