@SuppressWarnings("unchecked") @Test public void testAppendCloudServiceGuidConstraint() throws Exception { CustomerDAO dao = new CustomerDAO(); // list List<String> list = new LinkedList<String>(); list.add("val1"); list.add("val2"); SearchConstraint sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_IN_LIST, list); String got = dao.appendCloudServiceGuidConstraint(sc); String expected = CustomerDAO.SQL_CUSTOMER_QUERY_CLOUD_SERVICE_CONSTRAINT + "in (E'val1',E'val2'))"; assertEquals("Wrong constraint added.", expected, got); // equal sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "val3"); got = dao.appendCloudServiceGuidConstraint(sc); expected = CustomerDAO.SQL_CUSTOMER_QUERY_CLOUD_SERVICE_CONSTRAINT + "= E'val3' and g_inner.disabled = false)"; assertEquals("Wrong constraint added.", expected, got); }
private static Set<Integer> purgeDeletedSites(Connection c) throws SQLException { Statement stmt = null; ResultSet rs = null; try { // find server objects that need to be purged List<Integer> purgeSiteIds = new ArrayList<Integer>(); Set<Integer> custIds = new HashSet<Integer>(); String query = "select site.site_id, src.customer_id from dat_dirsync_sources src, dat_customer_sites site where site.is_deleted = true and current_timestamp > site.purge_time and src.source_id = site.source_id"; stmt = c.createStatement(); s_logger.debug(query); rs = stmt.executeQuery(query); while (rs.next()) { purgeSiteIds.add(rs.getInt(1)); custIds.add(rs.getInt(2)); } if (CollectionsUtils.isNullOrEmpty(purgeSiteIds)) { return null; } int totalDeletes = 0; for (List<Integer> ids : ChunkedListIterator.iterable(purgeSiteIds, BATCH_SIZE)) { logPoliciesUsingRemovedTopologyObjs( "Purging ", "site", ids, IUserManager.PROP_ROUTING_GROUP_ID, c); // purge servers String idList = QueryUtils.literal(ids); query = "delete from dat_customer_sites where site_id in " + idList; s_logger.debug(query); totalDeletes += stmt.executeUpdate(query); // don't purge constraints because the scope of the constraints will expand. Worst case, // when all constraints are deleted, a saved user set will have global scope and this // is definitely not what the customer wants. } s_logger.info("Purged " + totalDeletes + " sites for " + custIds.size() + " customers"); return custIds; } finally { if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } }
@Test public void testGetCustomerWithConnection() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("findCustomers", List.class, Connection.class) .createStrictMock(); Customer customer = EasyMock.createStrictMock(Customer.class); Connection connection = EasyMock.createStrictMock(Connection.class); int custId = 322; List<ICustomer> customerList = new LinkedList<ICustomer>(); customerList.add(customer); Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection))) .andReturn(customerList); EasyMock.replay(dao, customer, connection); assertEquals("Wrong customer returned.", customer, dao.getCustomer(custId, connection)); EasyMock.verify(dao, customer, connection); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); SearchConstraint constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CUSTOMER_ID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", custId, constraint.getValue()); // no customers customerList.clear(); EasyMock.reset(dao, customer, connection); capture = new Capture<List<SearchConstraint>>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture), EasyMock.eq(connection))) .andReturn(customerList); EasyMock.replay(dao, customer, connection); assertEquals( "Should not have found a customer returned.", null, dao.getCustomer(custId, connection)); EasyMock.verify(dao, customer, connection); }
@SuppressWarnings("unchecked") @Test public void testAppendDomainConstraint() throws Exception { CustomerDAO dao = new CustomerDAO(); // list List<String> list = new LinkedList<String>(); list.add("domain0"); list.add("domain1"); SearchConstraint sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_IN_LIST, list); String got = dao.appendDomainConstraint(sc); String expected = CustomerDAO.SQL_CUSTOMER_QUERY_DOMAIN_CONSTRAINT + "in (E'domain0',E'domain1'))"; assertEquals("Wrong constraint added.", expected, got); // equal sc = new SearchConstraint("TEST_PROP", SearchConstraintOperator.CONSTRAINT_EQUALS, "domain3"); got = dao.appendDomainConstraint(sc); expected = CustomerDAO.SQL_CUSTOMER_QUERY_DOMAIN_CONSTRAINT + "= E'domain3')"; assertEquals("Wrong constraint added.", expected, got); }
@SuppressWarnings("unchecked") @Test public void testAppendConstraints() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("appendDomainConstraint") .addMockedMethod("appendCloudServiceGuidConstraint") .addMockedMethod("appendCustomerPrefixConstraint") .addMockedMethod("appendUserStateConstraint") .createStrictMock(); List<SearchConstraint> constraints = new LinkedList<SearchConstraint>(); // domain constraint SearchConstraint sc = new SearchConstraint( ICustomerService.PROP_DOMAINS, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendDomainConstraint(EasyMock.eq(sc))).andReturn("a"); // cloud service guid sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendCloudServiceGuidConstraint(EasyMock.eq(sc))).andReturn("b"); sc = new SearchConstraint( ICustomerService.PROP_CLOUD_SERVICE_ANY_GUID, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendCloudServiceGuidConstraint(EasyMock.eq(sc))).andReturn("c"); // customer prefix sc = new SearchConstraint( ICustomerService.PROP_CUST_CAPABILITY_PREFIX, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); dao.appendCustomerPrefixConstraint(EasyMock.eq(sc), EasyMock.isA(StringBuffer.class)); EasyMock.expectLastCall(); // users in state sc = new SearchConstraint( ICustomerService.PROP_USERS_IN_STATE, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendUserStateConstraint(EasyMock.eq(sc))).andReturn("d"); // users NOT in state sc = new SearchConstraint( ICustomerService.PROP_NO_USERS_IN_STATE, SearchConstraintOperator.CONSTRAINT_EQUALS, "blah"); constraints.add(sc); EasyMock.expect(dao.appendUserStateConstraint(EasyMock.eq(sc))).andReturn("e"); StringBuilder whereClause = new StringBuilder(); EasyMock.replay(dao); dao.appendConstraints(constraints, whereClause); EasyMock.verify(dao); String expected = " and (a) and (b) and (c) and (not )"; assertEquals("Wrong where clause returned.", expected, whereClause.toString()); }
@Test public void testFindCustomerByGuid() throws Exception { CustomerDAO dao = EasyMock.createMockBuilder(CustomerDAO.class) .addMockedMethod("findCustomers", List.class) .createStrictMock(); // null guid EasyMock.replay(dao); try { dao.findCustomerByGuid(null, true); fail("Should have thrown an exception when null guid passed in."); } catch (IllegalArgumentException ex) { // expected } EasyMock.verify(dao); // no customer found EasyMock.reset(dao); boolean allowDeleted = true; String guid = "myGuid"; Capture<List<SearchConstraint>> capture = new Capture<List<SearchConstraint>>(); List<ICustomer> customerList = new LinkedList<ICustomer>(); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList); EasyMock.replay(dao); assertNull("Should not have found a customer.", dao.findCustomerByGuid(guid, allowDeleted)); EasyMock.verify(dao); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); SearchConstraint constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CLOUD_SERVICE_ANY_GUID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", guid, constraint.getValue()); // customer found and don't allow deleted EasyMock.reset(dao); allowDeleted = false; capture = new Capture<List<SearchConstraint>>(); Customer customer = EasyMock.createStrictMock(Customer.class); customerList.add(customer); EasyMock.expect(dao.findCustomers(EasyMock.capture(capture))).andReturn(customerList); EasyMock.replay(dao); assertEquals( "Should have found customer.", customer, dao.findCustomerByGuid(guid, allowDeleted)); EasyMock.verify(dao); assertEquals("Wrong # of constraints.", 1, capture.getValue().size()); constraint = capture.getValue().get(0); assertEquals( "Wrong property in constraint.", ICustomerService.PROP_CLOUD_SERVICE_GUID, constraint.getProperty()); assertEquals( "Wrong operator in constraint.", SearchConstraintOperator.CONSTRAINT_EQUALS, constraint.getOperator()); assertEquals("Wrong value in constraint.", guid, constraint.getValue()); }
private static void deleteTopologyData( Connection c, int sourceID, Collection groupIDs, Collection serverIDs, Collection storeIDs) throws SQLException { PreparedStatement ps = null; int purgeInterval = ManagementContainer.getInstance() .getConfiguration() .getIntProperty(PURGE_DAY_INTERVAL_PROP, DEFAULT_PURGE_DAY_INTERVAL); List<Integer> deletedStores = null; List<Integer> deletedServers = null; List<Integer> deletedSites = null; try { // First determine what stores to delete String strQuery = "select store_id from dat_customer_stores where exists ( " + " select * from dat_customer_servers svr " + " where svr.server_id = dat_customer_stores.server_id and " + " exists ( " + " select * from dat_customer_sites s " + " where s.site_id = svr.admin_group_id and source_id = ? " + " ) " + " ) and " + " store_id not in " + QueryUtils.literal(storeIDs) + " and is_deleted = false"; ps = c.prepareStatement(strQuery); ps.setInt(1, sourceID); ResultSet rs = ps.executeQuery(); // Convert the result set to a list of store id's to be deleted while (rs.next()) { if (deletedStores == null) deletedStores = new ArrayList<Integer>(); deletedStores.add(rs.getInt(1)); } ps.close(); if (deletedStores != null) // Check to see if we have anything to delete { strQuery = "update dat_customer_stores set is_deleted = true, purge_time = current_timestamp + '" + purgeInterval + " days'::interval" + " where store_id in " + QueryUtils.literal(deletedStores); ps = c.prepareStatement(strQuery); ps.executeUpdate(); ps.close(); // Log what we marked for deletion logPoliciesUsingRemovedTopologyObjs( "Marking as deleted", "store", deletedStores, IUserManager.PROP_STORE_ID, c); } ps = null; // delete the servers // First determine what servers to delete strQuery = "select server_id from dat_customer_servers " + "where " + " exists ( " + " select * from dat_customer_sites s " + " where s.site_id = dat_customer_servers.admin_group_id and source_id = ? " + " ) and " + " server_id not in " + QueryUtils.literal(serverIDs) + " and is_deleted = false"; ps = c.prepareStatement(strQuery); ps.setInt(1, sourceID); rs = ps.executeQuery(); // Convert the result set to a list of server id's to be deleted while (rs.next()) { if (deletedServers == null) deletedServers = new ArrayList<Integer>(); deletedServers.add(rs.getInt(1)); } ps.close(); if (deletedServers != null) // Check to see if we have anything to delete { strQuery = "update dat_customer_servers set is_deleted = true, purge_time = current_timestamp + '" + purgeInterval + " days'::interval" + " where server_id in " + QueryUtils.literal(deletedServers); ps = c.prepareStatement(strQuery); ps.executeUpdate(); ps.close(); // Log what we marked for deletion logPoliciesUsingRemovedTopologyObjs( "Marking as deleted", "server", deletedServers, IUserManager.PROP_SERVER_ID, c); } ps = null; // delete the sites // First determine what sites to delete strQuery = "select site_id from dat_customer_sites " + "where " + " source_id = ? and is_deleted = false and " + " site_id not in " + QueryUtils.literal(groupIDs); ps = c.prepareStatement(strQuery); ps.setInt(1, sourceID); rs = ps.executeQuery(); // Convert the result set to a list of site id's to be deleted while (rs.next()) { if (deletedSites == null) deletedSites = new ArrayList<Integer>(); deletedSites.add(rs.getInt(1)); } ps.close(); if (deletedSites != null) // Check to see if we have anything to delete { strQuery = "update dat_customer_sites set is_deleted = true, purge_time = current_timestamp + '" + purgeInterval + " days'::interval" + " where site_id in " + QueryUtils.literal(deletedSites); ps = c.prepareStatement(strQuery); ps.executeUpdate(); ps.close(); // Log what we marked for deletion logPoliciesUsingRemovedTopologyObjs( "Marking as deleted", "site", deletedSites, IUserManager.PROP_ROUTING_GROUP_ID, c); } ps = null; } finally { if (ps != null) { ps.close(); } } }