/*
 * 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.user;

import integration.anyframe.services.AbstractTest;
import anyframe.core.query.IQueryService;

import com.sds.emp.common.EmpException;
import com.sds.emp.user.services.UserService;
import com.sds.emp.user.services.UserVO;

public class UserServiceWithXMLTest extends AbstractTest {
	/**
	 * 테스트 수행을 위한 main
	 */
	public static void main(String[] args) throws Exception {
		UserServiceWithXMLTest userTest = new UserServiceWithXMLTest();

		// 1. initialize context
		userTest.setup();
		// 2. test
		userTest.testAddUser();
		userTest.testUpdateUserWithNotExistUser();
		// 3. close context
		userTest.teardown();

		System.out.println("Successful!!!!!");
	}

	protected void setup() {
		super.setup();
		try {
			testInit();
		} catch (Exception e) {
			// ignore this exception
		}
	}

	public void testInit() throws Exception {
		IQueryService m_service = (IQueryService) context
				.getBean("oracle_queryservice");

		// Try to drop the table. It may not exist and throw an exception.
		System.out.println("Attempting to drop old table");
		m_service.updateBySQL("DROP TABLE USERS", new String[] {},
				new Object[] {});

		// Create the table that we will use in this test.
		// Different depending on the db. Please add new statements as new
		// databases are tested.
		System.out.println("Create new table");
		m_service.updateBySQL("CREATE TABLE USERS ("
				+ "USER_ID VARCHAR(20) PRIMARY KEY, "
				+ "USER_NAME VARCHAR(50) NOT NULL, "
				+ "PASSWORD VARCHAR(10) NOT NULL, " + "ROLE VARCHAR(5), "
				+ "SSN VARCHAR(13), " + "SL_YN CHAR(1), "
				+ "BIRTH_DAY VARCHAR(8), " + "AGE NUMERIC(3), "
				+ "CELL_PHONE VARCHAR(14), " + "ADDR VARCHAR(100), "
				+ "EMAIL VARCHAR(50), " + "EMAIL_YN CHAR(1), "
				+ "IMAGE_FILE VARCHAR(100), " + "REG_DATE DATE )",
				new String[] {}, new Object[] {});
	}

	/**
	 * This testcase is used to test the functionality of AddUser method
	 * 
	 * @throws EmpException
	 *             EmpException will raise, when testAddUser() failed
	 */

	public void testAddUser() throws EmpException {
		UserService userService = (UserService) context
				.getBean("UserServiceWithXML");
		String userID = "woos41";
		/**
		 * we will add this user.. and fetch the record again to see if the
		 * record was updated.
		 */
		{
			// 1. add user
			UserVO userVO1 = new UserVO();
			userVO1.setUserId(userID);
			userVO1.setUserName("gang");
			userVO1.setPassword("gang");
			userVO1.setRole("user");
			userVO1.setSsn("1234567890");

			userVO1.setSlYn("Y");
			userVO1.setBirthDay("19750319");

			userVO1.setAge(null);
			userVO1.setCellPhone("1234567890");
			userVO1.setAddr("kamala road");
			userVO1.setEmail("ga@samsung.com");
			userVO1.setEmailYn("y");
			userVO1.setImageFile("ga");
			userVO1.setRegDate(null);

			userService.addUser(userVO1);

			// 2. get user
			userVO1 = userService.getUser(userID);

			// 3. assert
			boolean checked = userVO1.getUserId().equals(userID);
			if (!checked)
				throw new EmpException("fail to add user.");
		}
	}

	/**
	 * This testcase is used to test the functionality of UpdateUserList method
	 * 
	 * @throws EmpException
	 *             EmpException will raise, when
	 *             testUpdateUserWithNotExistUser() failed
	 */
	public void testUpdateUserWithNotExistUser() throws EmpException {
		UserService userService = null;
		try {
			userService = (UserService) context.getBean("UserServiceWithXML");

			// 1. update user list
			UserVO newUser = new UserVO();
			newUser.setUserId("testuser");
			newUser.setUserName("gang");
			newUser.setPassword("gang");
			newUser.setRole("user");
			newUser.setSsn("1234567890");
			newUser.setSlYn("Y");
			newUser.setBirthDay("19750319");
			newUser.setAge(null);
			newUser.setCellPhone("1234567890");
			newUser.setAddr("kamala road");
			newUser.setEmail("ga@samsung.com");
			newUser.setEmailYn("y");
			newUser.setImageFile("ga");
			newUser.setRegDate(null);

			UserVO updateUser = userService.getUser("woos41");
			updateUser.setUserId("woos");
			String name = "woostest";
			updateUser.setUserName(name);

			userService.updateUserList(newUser, updateUser);
			throw new EmpException("fail to get user.");
		} catch (EmpException e) {
			try {
				userService.getUser("testuser");
			} catch (EmpException ee) {
				throw new EmpException("fail to manage transaction.");
			}
		}
	}

	protected String[] getConfigLocations() {
		// TODO Auto-generated method stub
		return new String[] { "classpath*:/common/applicationContext-*.xml",
				"classpath*:/applications/user/applicationContext-user-aop.xml",
				"classpath*:/services/query/applicationContext-query-oracle.xml",
				"classpath*:/services/query/applicationContext-query-sqlloader.xml",
				"classpath*:/services/datasource/applicationContext-datasource-oracle.xml",
				"classpath*:/services/properties/applicationContext-*.xml",
				"classpath*:/services/transaction/applicationContext-transaction-datasource-oracle.xml"};
	}
}

