package jp.agentec.sinaburocast.helper;

import jp.agentec.sinaburocast.common.exception.ExpectedException;
import jp.agentec.sinaburocast.vo.EmailKey;
import jp.agentec.sinaburocast.vo.EnqueteKey;
import junit.framework.TestSuite;

import org.apache.commons.lang.RandomStringUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.seasar.dao.unit.S2DaoTestCase;

public class KeyHelperTest extends S2DaoTestCase {

	public KeyHelper keyHelper;

	public KeyHelperTest(String string) {
		super(string);
	}

	public static TestSuite suite() {
		TestSuite suite = new TestSuite("KeyHelperTest");
		suite.addTest(new KeyHelperTest("encryptAndDecrypt"));
		suite.addTest(new KeyHelperTest("encryptAndDecryptEmail"));
		return suite;
	}


	/**
	 * @throws java.lang.Exception
	 */
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
        include("app.dicon");
	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
	}
	
	public void encryptAndDecrypt() throws Exception {
		String key = keyHelper.encrypt(1234, 5678, 1, 0, 0);
		System.out.println(key);
//		key = keyHelper.encrypt(1, 5, 1, 0, 0);
//		System.out.println(key);
		EnqueteKey org = keyHelper.decrypt(key);
		System.out.println(org);
		assertEquals(1234, (int)org.memberId);
		assertEquals(5678, (int)org.enqueteId);

		try {
			keyHelper.decrypt("yfA2MnN9B2cocr8-4Is2uEdUxj8oGebrO2Hx6dv71RM");
			assertFalse(true);
		} catch (ExpectedException e) {
			System.out.println(e.toString());
		}
	}

	public void encryptAndDecryptEmail() throws Exception {
		String key;
		while (true) {
			key = keyHelper.encryptEmail("test@mx1.abookcloud.jp" + RandomStringUtils.randomAlphanumeric(10), 1, 0, 0);
			if (key.contains("!")) {
				System.out.println(key);
				break;
			}
			
		}
		System.out.println(key);
//		key = keyHelper.encrypt(1, 5, 1, 0, 0);
//		System.out.println(key);
		EmailKey org = keyHelper.decryptEmail(key);
//		EmailKey org = keyHelper.decryptEmail("5GfMXbmWhDTImcjwFeC4Ws8fwbc5OHU6IkOP4dCC37vaiVZBdeEqTrM_f2R6bqrP");
		System.out.println(org);
		assertEquals("test@mx1.abookcloud.jp", org.email);

//		try {
//			keyHelper.decrypt("yfA2MnN9B2cocr8-4Is2uEdUxj8oGebrO2Hx6dv71RM");
//			assertFalse(true);
//		} catch (ExpectedException e) {
//			System.out.println(e.toString());
//		}
	}

}