package com.cin.test;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import com.cin.dto.*;
import com.cin.ejb.transactionejb.TransactionRemote;
import com.cin.ejb.transactionejb.UserFactory;
import com.cin.entity.Occupation;
import com.cin.jndi.lookup.EJBLookup;
import com.cin.test.util.*;
import static org.junit.Assert.*;
import org.junit.*;
public class TransactionEjbTest {
static Database db;
static TransactionRemote aRemote;
static UserFactory userFactory;
@BeforeClass
public static void initClass() throws Exception{
db = new Database();
db.resetAllData();
aRemote = EJBLookup.getInstance().getTransactionEJB();
userFactory = EJBLookup.getInstance().getUserFactory();
}
@Test
public final void testPersistOccupation() throws SQLException{
try{
Occupation occupation = new Occupation("my_occupation",123,60 );
aRemote.persist(occupation);
}finally{
}
}
@Test
public final void testAddInvitation() throws SQLException {
InvitationTable table = db.getTable(InvitationTable.class);
try{
// prepare
int id = 0;
UserDTO customer = userFactory.findUserBySsn(10);
UserDTO agent = null;
// execute
InvitationDTO invitation = new InvitationDTO(id, agent, customer, new Date());
InvitationDTO invitationSent = aRemote.addInvitation(invitation);
// check if added
InvitationDTO dbInvitation = table.getNewest();
assertNotNull(invitationSent);
assertNotNull(dbInvitation);
assertEquals(10, dbInvitation.getCustomer().getSsn());
assertEquals(invitationSent.getId(), dbInvitation.getId());
}finally{
table.clear();
}
}
@Test
public final void testFindInvitationByUser() throws SQLException {
InvitationTable table = db.getTable(InvitationTable.class);
try{
InvitationDTO invitation = new InvitationDTO();
invitation.setCustomer(new UserDTO(10));
invitation.setDateCreated(new Date());
aRemote.addInvitation(invitation);
// add it again - 2 invites to same customer
aRemote.addInvitation(invitation);
List<InvitationDTO> invites = aRemote.getInvitationsByCustomer(10);
assertEquals(invites.size(), 2);
assertEquals(invites.get(0).getCustomer().getSsn(), 10);
assertEquals(invites.get(1).getCustomer().getSsn(), 10);
}finally{
table.clear();
}
}
@Test
public void testFindZipForState() throws Exception {
List<ZipDTO> zips = aRemote.findZipForState("Illinois");
assertTrue( zips.size() > 0 );
}
@Test(expected=Exception.class)
public void testFindZipForStateInvalidState() throws Exception {
aRemote.findZipForState("Nowhere");
}
@Test
public final void testAddPayroll() throws SQLException {
PayrollTable table = db.getTable(PayrollTable.class);
try{
// prepare
UserDTO agent = new UserDTO();
agent.setSsn(10);
// execute
aRemote.addPayroll(agent, 123);
// check
List<Integer> payments = table.getByUser(10);
assertEquals(payments.size(), 1);
assertEquals((int)payments.get(0), 123);
}finally{
table.clear();
}
}
@Test
public void testGetPrincipleStates() throws Exception {
List<String> ret;
// check if correct number of states is return
ret = aRemote.getPrincipleStates(3, null, null);
assertEquals(2, ret.size());
// check if the retSize argument works
ret = aRemote.getPrincipleStates(1, null, null);
assertEquals(1, ret.size());
// try all combinations
ret = aRemote.getPrincipleStates(3, 9, null);
assertEquals(2, ret.size());
ret = aRemote.getPrincipleStates(3, null, 1);
assertEquals(2, ret.size());
ret = aRemote.getPrincipleStates(3, 9, 1);
assertEquals(2, ret.size());
// what happens if there is state - output of size 0
ret = aRemote.getPrincipleStates(3, 8, 2);
assertEquals(0, ret.size());
}
@Test
public void testGetLargestOccupations() throws Exception {
List<OccupationDTO> ret;
// typical case
ret = aRemote.getLargestOccupations(3, "IL", null);
assertEquals(2, ret.size());
assertEquals(1, ret.get(0).getOccupationCode());
assertEquals(2, ret.get(1).getOccupationCode());
ret = aRemote.getLargestOccupations(3, "ny", null);
assertEquals(1, ret.size());
// all combinations
ret = aRemote.getLargestOccupations(3, "Nowhere", null);
assertEquals(0, ret.size());
ret = aRemote.getLargestOccupations(3, null, "12303");
assertEquals(1, ret.size());
ret = aRemote.getLargestOccupations(3, "NY", "12303");
assertEquals(1, ret.size());
ret = aRemote.getLargestOccupations(3, null, null);
assertEquals(2, ret.size());
// check if retSize works
ret = aRemote.getLargestOccupations(0, null, null);
assertEquals(0, ret.size());
}
@Test
public void testGetLargestIndustries() throws Exception {
List<IndustryDTO> ret;
ret = aRemote.getLargestIndustries(3, "IL", null);
assertEquals(2, ret.size());
assertEquals(9, ret.get(0).getIndustryCode());
assertEquals(8, ret.get(1).getIndustryCode());
// typical case
ret = aRemote.getLargestIndustries(3, "NY", null);
assertEquals(1, ret.size());
// unknown state
ret = aRemote.getLargestIndustries(3, "Nowhere", null);
assertEquals(0, ret.size());
// all combinations
ret = aRemote.getLargestIndustries(3, null, "12303");
assertEquals(1, ret.size());
ret = aRemote.getLargestIndustries(3, "NY", "12303");
assertEquals(1, ret.size());
ret = aRemote.getLargestIndustries(3, null, null);
assertEquals(2, ret.size());
// check if retSize works
ret = aRemote.getLargestIndustries(0, null, null);
assertEquals(0, ret.size());
}
@Test
public void testZipCodeExists(){
assertTrue( aRemote.zipCodeExists("12301") );
assertFalse( aRemote.zipCodeExists("01010") );
}
}