private int createMailRoutingPolicy(int custId, String name, boolean isDefault) throws Exception { PreparedStatement statement = null; ResultSet rs = null; Connection conn = getDBConnection(); try { String sql = "insert into mail_routing_policies (customer_id, routing_policy_name, is_default) values (?,?,?) returning mail_routing_policy_id"; statement = conn.prepareStatement(sql); statement.setInt(1, custId); statement.setString(2, name); statement.setBoolean(3, isDefault); rs = statement.executeQuery(); assertTrue("Did not insert mail routing policy.", rs.next()); return rs.getInt(1); } finally { if (rs != null) { rs.close(); } if (statement != null) { statement.close(); } if (conn != null) { conn.close(); } } }
@SuppressWarnings("unchecked") @Test public void testFindCustomersWithConnection() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("readNextCustomer") .addMockedMethod("getCustomerQuery") .createStrictMock(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); Statement statement = EasyMock.createStrictMock(Statement.class); List<SearchConstraint> constraints = new LinkedList<SearchConstraint>(); EasyMock.expect(dao.getCustomerQuery(constraints)).andReturn("aQuery"); EasyMock.expect(connection.createStatement()).andReturn(statement); EasyMock.expect(statement.executeQuery("aQuery")).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class))) .andReturn(true); EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class))) .andReturn(true); EasyMock.expect(dao.readNextCustomer(EasyMock.eq(resultSet), EasyMock.isA(List.class))) .andReturn(false); resultSet.close(); EasyMock.expectLastCall(); statement.close(); }
@Test public void testReadWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); int id = 34; String name = "myName"; SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(dao.resultFromResultSet(resultSet)).andReturn(search); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.read(connection, id, name); assertEquals("Wrong search returned", search, result); EasyMock.verify(preparedStatement, resultSet, connection, dao); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.READ_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, id); EasyMock.expectLastCall(); preparedStatement.setString(2, name); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); try { dao.read(connection, id, name); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }
@Test public void testIsUpdate() throws Exception { SavedReviewerSearch search = new SavedReviewerSearch(); search.setSearchUserId(34); search.setSearchName("hello"); SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); Connection connection = EasyMock.createStrictMock(Connection.class); PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertTrue("Should be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); // now not an update EasyMock.reset(connection, preparedStatement, resultSet); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CHECK_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, search.getSearchUserId()); EasyMock.expectLastCall(); preparedStatement.setString(2, search.getSearchName()); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.expectLastCall(); EasyMock.replay(connection, preparedStatement, resultSet); assertFalse("Should not be an update.", dao.isUpdate(connection, search)); EasyMock.verify(connection, preparedStatement, resultSet); }
@Test public void testCreateWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); SavedReviewerSearch search = new SavedReviewerSearch(); Date date = new Date(); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("updatePreparedStatementForCreate") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getTimestamp(1)).andReturn(new Timestamp(date.getTime())); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); SavedReviewerSearch result = dao.create(connection, search); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong created date.", date, result.getCreatedDate()); // failure case EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.CREATE_SQL)) .andReturn(preparedStatement); dao.updatePreparedStatementForCreate(preparedStatement, search); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); try { dao.create(connection, search); fail("Should have thrown a sql exception."); } catch (SQLException ex) { // expected } EasyMock.verify(preparedStatement, resultSet, connection, dao); }
@SuppressWarnings("unchecked") @Test public void testReadNextCustomer() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("readDomainsAndGuids") .createStrictMock(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); List<ICustomer> customerList = new LinkedList<ICustomer>(); int custID = 34; int state = 3; String name = "myName"; String fromAddress = "myFromAddr"; String clientID = "myClientId"; String clientKey = "myClientKey"; String backendId = "myBackendId"; String templateId = "myTemplateId"; String externalID = "myExternalId"; int estMailboxCount = 34453; int estCloudMailboxCount = 345234; int estOnpremisesJournalMailboxCount = 3523; int estWelcomedCount = 938423; int estNotificationCount = 3423; int estAdminCount = 45345; Date countsUpdatedTime = new Date(); int estTotalContacts = 345234; int estTotalUsersWithContacts = 87432; int estTotalCalendarEntries = 343460; int estTotalUsersWithCalendar = 9892; int estTotalDLists = 8932; int estTotalDListMembers = 34982; int estTotalPDLs = 12342142; int estTotalPDLMembers = 8732324; int estNoWelcomeResponseCount = 734232; int estNoLoginCount = 90832; int estNoActiveLoginCount = 89923; int estIgnoredCount = 349823; String channel = "myChannel"; EasyMock.expect(resultSet.getInt(1)).andReturn(custID); EasyMock.expect(resultSet.getInt(2)).andReturn(state); EasyMock.expect(resultSet.getString(3)).andReturn(name); EasyMock.expect(resultSet.getString(4)).andReturn(fromAddress); EasyMock.expect(resultSet.getString(5)).andReturn(clientID); EasyMock.expect(resultSet.getString(6)).andReturn(clientKey); EasyMock.expect(resultSet.getString(7)).andReturn(backendId); EasyMock.expect(resultSet.getString(8)).andReturn(templateId); EasyMock.expect(resultSet.getString(9)).andReturn(externalID); EasyMock.expect(resultSet.getInt(14)).andReturn(estMailboxCount); EasyMock.expect(resultSet.getInt(15)).andReturn(estCloudMailboxCount); EasyMock.expect(resultSet.getInt(16)).andReturn(estOnpremisesJournalMailboxCount); EasyMock.expect(resultSet.getInt(17)).andReturn(estWelcomedCount); EasyMock.expect(resultSet.getInt(18)).andReturn(estNotificationCount); EasyMock.expect(resultSet.getInt(19)).andReturn(estAdminCount); EasyMock.expect(resultSet.getTimestamp(20)) .andReturn(new Timestamp(countsUpdatedTime.getTime())); EasyMock.expect(resultSet.getInt(21)).andReturn(estTotalContacts); EasyMock.expect(resultSet.getInt(22)).andReturn(estTotalUsersWithContacts); EasyMock.expect(resultSet.getInt(23)).andReturn(estTotalCalendarEntries); EasyMock.expect(resultSet.getInt(24)).andReturn(estTotalUsersWithCalendar); EasyMock.expect(resultSet.getInt(25)).andReturn(estTotalDLists); EasyMock.expect(resultSet.getInt(26)).andReturn(estTotalDListMembers); EasyMock.expect(resultSet.getInt(27)).andReturn(estTotalPDLs); EasyMock.expect(resultSet.getInt(28)).andReturn(estTotalPDLMembers); EasyMock.expect(resultSet.getInt(29)).andReturn(estNoWelcomeResponseCount); EasyMock.expect(resultSet.getInt(30)).andReturn(estNoLoginCount); EasyMock.expect(resultSet.getInt(31)).andReturn(estNoActiveLoginCount); EasyMock.expect(resultSet.getInt(32)).andReturn(estIgnoredCount); EasyMock.expect(resultSet.getString(33)).andReturn(channel); EasyMock.expect( dao.readDomainsAndGuids( EasyMock.eq(resultSet), EasyMock.eq(custID), EasyMock.isA(Set.class), EasyMock.isA(Set.class))) .andReturn(true); EasyMock.replay(dao, resultSet); assertTrue( "Should have found another customer.", dao.readNextCustomer(resultSet, customerList)); EasyMock.verify(dao, resultSet); assertEquals("Should have customer in list.", 1, customerList.size()); Customer cust = (Customer) customerList.get(0); assertEquals("Wrong customer id.", custID, cust.getCustID()); assertEquals("Wrong customer state.", state, cust.getState().toInt()); assertEquals("Wrong customer name.", name, cust.getName()); assertEquals("Wrong from address.", fromAddress, cust.getFromAddress()); assertEquals("Wrong backend id.", backendId, cust.getBackendHostname()); assertEquals("Wrong template id.", templateId, cust.getTemplateId()); assertEquals("Wrong channel.", channel, cust.getChannel()); assertEquals("Wrong client id.", clientID, cust.getClientID()); assertEquals("Wrong client key.", clientKey, cust.getClientKey()); assertEquals("Wrong channel.", channel, cust.getChannel()); assertEquals("Wrong est mailbox count.", estMailboxCount, cust.getEstMailboxCount()); assertEquals( "Wrong est cloud mailbox count.", estCloudMailboxCount, cust.getEstCloudMailboxCount()); assertEquals( "Wrong est on premises journal mailbox count.", estOnpremisesJournalMailboxCount, cust.getEstOnpremisesJournalMailboxCount()); assertEquals("Wrong est not welcomed count.", estWelcomedCount, cust.getEstNotWelcomedCount()); assertEquals( "Wrong est notification not set count.", estNotificationCount, cust.getEstNotificationNotSetCount()); assertEquals("Wrong est admin count.", estAdminCount, cust.getEstAdminCount()); assertEquals("Wrong counts updated time.", countsUpdatedTime, cust.getCountsUpdatedTime()); assertEquals("Wrong total contacts.", estTotalContacts, cust.getTotalContacts()); assertEquals( "Wrong total users with contacts.", estTotalUsersWithContacts, cust.getTotalUsersWithContacts()); assertEquals( "Wrong total calendar entries.", estTotalCalendarEntries, cust.getTotalCalendarEntries()); assertEquals( "Wrong total users with calendar entries.", estTotalUsersWithCalendar, cust.getTotalUsersWithCalendarEntries()); assertEquals("Wrong total dlists.", estTotalDLists, cust.getTotalDlists()); assertEquals("Wrong total dlist members.", estTotalDListMembers, cust.getTotalDlistMembers()); assertEquals("Wrong total pdls.", estTotalPDLs, cust.getTotalPDLs()); assertEquals("Wrong total pdl members.", estTotalPDLMembers, cust.getTotalPDLMembers()); assertEquals( "Wrong est no welcome reponse count.", estNoWelcomeResponseCount, cust.getEstNoWelcomeResponseCount()); assertEquals("Wrong est no login count.", estNoLoginCount, cust.getEstNoLoginCount()); assertEquals( "Wrong est no active login count.", estNoActiveLoginCount, cust.getEstNoActiveLoginCount()); assertEquals("Wrong est ignored count.", estIgnoredCount, cust.getEstIgnoredCount()); assertEquals("Wrong external id.", externalID, cust.getExternalID()); }
@Test public void testReadDomainsAndGuids() throws Exception { CustomerDAO dao = new CustomerDAO(); IConfiguration configuration = EasyMock.createStrictMock(IConfiguration.class); dao.setConfiguration(configuration); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Set<String> domains = new HashSet<String>(); Set<GlobalIdentifier> guids = new HashSet<GlobalIdentifier>(); int custId = 34; int cloudService = 2; int replicationZone = 453; int cloudService2 = 1; int replicationZone2 = 13; int cloudService3 = 3; // first exec of loop EasyMock.expect(resultSet.getString(10)).andReturn("domain123"); EasyMock.expect(resultSet.getString(11)).andReturn("guid123"); EasyMock.expect(resultSet.getInt(12)).andReturn(cloudService); EasyMock.expect(resultSet.getInt(13)).andReturn(replicationZone); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getInt(1)).andReturn(custId); // second exec of loop EasyMock.expect(resultSet.getString(10)).andReturn("domain456"); EasyMock.expect(resultSet.getString(11)).andReturn("guid456"); EasyMock.expect(resultSet.getInt(12)).andReturn(cloudService2); EasyMock.expect(resultSet.getInt(13)).andReturn(replicationZone2); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getInt(1)).andReturn(custId); // third exec of loop (guid not valid with no cloud service) EasyMock.expect(resultSet.getString(10)).andReturn("domain456"); EasyMock.expect(resultSet.getString(11)).andReturn("guid789"); EasyMock.expect(resultSet.getInt(12)).andReturn(cloudService3); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getInt(1)).andReturn(custId + 1); // ends loop with mismatched custid EasyMock.replay(resultSet); assertTrue( "Should have another item even.", dao.readDomainsAndGuids(resultSet, custId, domains, guids)); EasyMock.verify(resultSet); assertEquals("Should have 2 domains.", 2, domains.size()); assertTrue("Domain123 not found.", domains.contains("domain123")); assertTrue("Domain456 not found.", domains.contains("domain456")); assertEquals("Should have 2 guids.", 2, guids.size()); for (GlobalIdentifier guid : guids) { if (guid.getGuid().equals("guid123")) { assertEquals("Wrong cloud service in guid123", CloudService.GOOGLE, guid.getService()); assertEquals("Wrong replication zone.", replicationZone, guid.getReplicationZone()); } else { assertEquals("Wrong cloud service in guid456", CloudService.OFFICE365, guid.getService()); assertEquals("Wrong replication zone.", replicationZone2, guid.getReplicationZone()); } } }
@Test public void testFinddWithConnection() throws Exception { PreparedStatement preparedStatement = EasyMock.createStrictMock(PreparedStatement.class); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Connection connection = EasyMock.createStrictMock(Connection.class); String search1 = "search1"; String search2 = "search2"; int userId = 34; int messageTypeId = 1423; String queryType = "myQueryType"; SavedReviewerSearchQuery constraints = new SavedReviewerSearchQuery(); constraints.setQueryType(queryType); constraints.setMessageTypeId(messageTypeId); constraints.setSearchUserId(userId); SavedReviewerSearchDAO dao = EasyMock.createMockBuilder(SavedReviewerSearchDAO.class) .withConstructor() .addMockedMethod("resultFromResultSet") .createStrictMock(); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search1); EasyMock.expect(resultSet.next()).andReturn(true); EasyMock.expect(resultSet.getString(1)).andReturn(search2); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); List<String> results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 2, results.size()); assertTrue("Did not find result 1 in list.", results.contains(search1)); assertTrue("Did not find result 2 in list.", results.contains(search2)); // no items EasyMock.reset(preparedStatement, resultSet, connection, dao); EasyMock.expect(connection.prepareStatement(SavedReviewerSearchDAO.FIND_SQL)) .andReturn(preparedStatement); preparedStatement.setInt(1, userId); EasyMock.expectLastCall(); preparedStatement.setInt(2, messageTypeId); EasyMock.expectLastCall(); preparedStatement.setString(3, queryType); EasyMock.expectLastCall(); EasyMock.expect(preparedStatement.executeQuery()).andReturn(resultSet); EasyMock.expect(resultSet.next()).andReturn(false); resultSet.close(); EasyMock.expectLastCall(); preparedStatement.close(); EasyMock.replay(preparedStatement, resultSet, connection, dao); results = dao.find(connection, constraints); EasyMock.verify(preparedStatement, resultSet, connection, dao); assertEquals("Wrong # of items in list.", 0, results.size()); }
@Test public void testResultFromResultSet() throws Exception { SavedReviewerSearchDAO dao = new SavedReviewerSearchDAO(); ResultSet resultSet = EasyMock.createStrictMock(ResultSet.class); Date createdDate = new Date(); int customerId = 56; int userId = 32422; int msgTypeId = 2342; String name = "myName"; String queryType = "myQueryType"; String jsonData = "this is my json data but I don't check that it is json."; EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(new Timestamp(createdDate.getTime())); EasyMock.replay(resultSet); SavedReviewerSearch search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertEquals("Wrong created date.", createdDate, search.getCreatedDate()); assertEquals("Wrong customer id.", customerId, search.getCustomerId()); assertEquals("Wrong user id.", userId, search.getSearchUserId()); assertEquals("Wrong msg type id.", msgTypeId, search.getMessageTypeId()); assertEquals("Wrong name.", name, search.getSearchName()); assertEquals("Wrong query type.", queryType, search.getQueryType()); assertEquals("Wrong json data.", jsonData, search.getJsonData()); // now with no date EasyMock.reset(resultSet); EasyMock.expect(resultSet.getString(1)).andReturn(name); EasyMock.expect(resultSet.getInt(2)).andReturn(customerId); EasyMock.expect(resultSet.getInt(3)).andReturn(userId); EasyMock.expect(resultSet.getInt(4)).andReturn(msgTypeId); EasyMock.expect(resultSet.getString(5)).andReturn(queryType); EasyMock.expect(resultSet.getString(6)).andReturn(jsonData); EasyMock.expect(resultSet.getTimestamp(7)).andReturn(null); EasyMock.replay(resultSet); search = dao.resultFromResultSet(resultSet); EasyMock.verify(resultSet); assertNull("Created date should not be set.", search.getCreatedDate()); }