package study.hard.spring.core.account.dao;

import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import study.hard.spring.core.account.entity.Account;
import study.hard.spring.core.account.fixture.AccountFixture;
import study.hard.spring.core.commons.db.BaseDAOTest;

public class AccountDAOImplTest extends BaseDAOTest<AccountDAOImpl, Account> {

	private Logger logger = LoggerFactory.getLogger(AccountDAOImplTest.class);

	@Before
	public void setUp() {
		Account account = AccountFixture.createEntity();
		Account insertedAccount = suite.insert(account);
		logger.debug("insertedAccount: {}", insertedAccount);
		validateEquals(account, insertedAccount);
	}

	@Test
	public void selectOne() {
		Account account = suite.selectOne(AccountFixture.getUserKey());
		logger.debug("account: {}", account);
		validateIsNotNull(account);
	}

	@Test
	public void update() {
		Account account = suite.selectOne(AccountFixture.getUserKey());
		account.setFirstName("SOMEONE");
		account.setLastName("ELSE");
		account.setRegistYmdt(DateUtils.addDays(account.getRegistYmdt(), -3));
		Integer updatedCount = suite.update(account);
		assertTrue(1 == updatedCount);
		logger.debug("updatedCount: {}", updatedCount);
	}

	@Test
	public void delete() {
		Account account = suite.selectOne(AccountFixture.getUserKey());
		Integer deletedCount = suite.delete(account.getUserKey());
		assertTrue(1 == deletedCount);
	}

	@Override
	protected Class<AccountDAOImpl> getRepsitoryClass() {
		return AccountDAOImpl.class;
	}

	@Override
	protected void validateIsNotNull(Account target) {
		assertNotNull(target);
		assertNotNull(target.getUserKey());
		assertNotNull(target.getEmail());
	}

	@Override
	protected void validateEquals(Account target1, Account target2) {
		validateIsNotNull(target1);
		validateIsNotNull(target2);
		assertEquals(target1.getUserKey(), target2.getUserKey());
		assertEquals(target1.getEmail(), target2.getEmail());
	}

}

@@ -189,16 +189,70 @@
 Factory;
+%0Aimport org.springframework.dao.DuplicateKeyException;
 %0A%0Aimport
@@ -615,23 +615,22 @@
 Account 
-account
+entity
  = Accou
@@ -673,23 +673,22 @@
 inserted
-Account
+Entity
  = suite
@@ -695,23 +695,22 @@
 .insert(
-account
+entity
 );%0A%09%09log
@@ -728,23 +728,22 @@
 inserted
-Account
+Entity
 : %7B%7D%22, i
@@ -745,31 +745,30 @@
 %7D%22, inserted
-Account
+Entity
 );%0A%09%09validat
@@ -775,23 +775,22 @@
 eEquals(
-account
+entity
 , insert
@@ -791,23 +791,187 @@
 inserted
-Account
+Entity);%0A%09%7D%0A%0A%09@Test(expected = DuplicateKeyException.class)%0A%09public void insertWithDuplicateKey() %7B%0A%09%09Account entity = AccountFixture.createEntity();%0A%09%09suite.insert(entity
 );%0A%09%7D%0A%0A%09

Add test case to test to insert with duplicate key.