package com.github.aureliano.edocs.service.bean;

import static org.junit.Assert.assertEquals;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

import com.github.aureliano.edocs.common.persistence.PersistenceService;
import com.github.aureliano.edocs.domain.dao.UserDao;
import com.github.aureliano.edocs.domain.entity.User;
import com.github.aureliano.edocs.domain.helper.PersistenceHelper;

public class UserServiceBeanTest {

	public UserServiceBeanTest() {
		PersistenceHelper.instance().prepareDatabase();
	}

	@Before
	public void beforeTest() throws SQLException {
		PersistenceHelper.instance().deleteAllRecords();;
	}
	
	@Test
	public void testDeleteUser() throws SQLException {
		String name = "delete-by-entity";
		User u = new User()
			.withName(name)
			.withPassword("test123");
		new UserDao().save(u);
		
		Connection conn = PersistenceService.instance().getPersistenceManager().getConnection();
		conn.setAutoCommit(true);
		
		ResultSet rs = conn.prepareStatement("select count(id) from users where name = '" + name + "'").executeQuery();
		rs.next();
		assertEquals(1, rs.getInt(1));
		rs.close();
		
		UserServiceBean bean = new UserServiceBean();
		bean.deleteUser(u);
		
		rs = conn.prepareStatement("select count(id) from users where name = '" + name + "'").executeQuery();
		rs.next();
		assertEquals(0, rs.getInt(1));
		rs.close();
	}
}
@@ -471,16 +471,90 @@
 eHelper;
+%0Aimport com.github.aureliano.edocs.service.EdocsServicePersistenceManager;
 %0A%0Apublic
@@ -665,16 +665,306 @@
 base();%0A
+%09%09PersistenceService ps = PersistenceService.instance();%0A%09%09%0A%09%09if (ps.getPersistenceManager() == null) %7B%0A%09%09%09EdocsServicePersistenceManager pm = new EdocsServicePersistenceManager();%0A%09%09%09pm.setConnection(PersistenceHelper.instance().getConnection());%0A%09%09%09ps.registerPersistenceManager(pm);%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Be
@@ -1240,16 +1240,28 @@
 23%22);%0A%09%09
+User user = 
 new User
@@ -1625,16 +1625,16 @@
 Bean();%0A
-
 %09%09bean.d
@@ -1644,16 +1644,19 @@
 teUser(u
+ser
 );%0A%09%09%0A%09%09

Test delete user service method.