From 535172274b71cdc8a264355b79a3d8615eddfa88 Mon Sep 17 00:00:00 2001 From: Srinivas Veldurthi Date: Thu, 28 May 2015 18:48:35 -0700 Subject: [PATCH] Clean up and added additional test case. Code review: https://codereview.appspot.com/242760043/ --- .../adaptor/documentum/DocumentumAcls.java | 167 +++++++++--------- .../documentum/DocumentumAdaptorTest.java | 69 ++++---- 2 files changed, 118 insertions(+), 118 deletions(-) diff --git a/src/com/google/enterprise/adaptor/documentum/DocumentumAcls.java b/src/com/google/enterprise/adaptor/documentum/DocumentumAcls.java index 8b0b074..fc61b2a 100644 --- a/src/com/google/enterprise/adaptor/documentum/DocumentumAcls.java +++ b/src/com/google/enterprise/adaptor/documentum/DocumentumAcls.java @@ -16,6 +16,7 @@ import static java.util.Collections.singletonList; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.enterprise.adaptor.Acl; @@ -55,15 +56,16 @@ class DocumentumAcls { private static Logger logger = Logger.getLogger(DocumentumAcls.class.getName()); + @VisibleForTesting static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private static String aclModifiedDate; + private static String aclModifyId; + private final IDfClientX dmClientX; private final IDfSession dmSession; private final Principals principals; - private static String aclModifiedDate; - private static String aclModifyId; - DocumentumAcls(IDfClientX dmClientX, IDfSession dmSession, Principals principals) { Preconditions.checkNotNull(dmClientX, "dmClientX may not be null"); @@ -81,6 +83,39 @@ private IDfQuery makeAclQuery() { return query; } + private IDfQuery makeUpdateAclQuery() { + StringBuilder queryStr = new StringBuilder( + "select r_object_id, chronicle_id, audited_obj_id, event_name, " + + "time_stamp_utc, " + + "DATETOSTRING(time_stamp_utc, 'yyyy-mm-dd hh:mi:ss') " + + "as time_stamp_utc_str " + + "from dm_audittrail_acl " + + "where (event_name='dm_save' or event_name='dm_saveasnew' " + + "or event_name='dm_destroy')"); + + String whereBoundedClause = " and ((time_stamp_utc = " + + "date(''{0}'',''yyyy-mm-dd hh:mi:ss'') and (r_object_id > ''{1}'')) " + + "OR (time_stamp_utc > date(''{0}'',''yyyy-mm-dd hh:mi:ss'')))"; + + if (Strings.isNullOrEmpty(aclModifiedDate)) { + aclModifiedDate = new SimpleDateFormat(DATE_FORMAT).format(new Date()); + logger.log(Level.FINE, "Setting ModifiedDate to now: {0}", + aclModifiedDate); + aclModifyId = "0"; + } + + // TODO (Srinivas): Adjust modified date to server TZ offset + Object[] arguments = {aclModifiedDate, aclModifyId}; + queryStr.append(MessageFormat.format(whereBoundedClause, arguments)); + queryStr.append(" order by time_stamp_utc, r_object_id, event_name"); + logger.log(Level.FINE, "Modify date: {0} ; Modify ID: {1}", arguments); + logger.log(Level.FINER, "Update ACL query: {0}", queryStr); + + IDfQuery query = dmClientX.getQuery(); + query.setDQL(queryStr.toString()); + return query; + } + /** * Returns all Documentum ACLs in map with doc id and Acl. * @@ -113,6 +148,49 @@ public Map getAcls() throws DfException { } } + /** + * Returns all updated Documentum ACLs in map with doc id and Acl. + * + * In Documentum, ACLs are high level objects separate from content objects. + * An ACL can be applied to one or many content objects. Or, each object + * can have it's own individual ACL applied to it. So this method needs to + * send all the ACLs in Documentum to GSA. + * + * @return Documentum ACLs in map + * @throws DfException if error in getting ACL information. + */ + public Map getUpdateAcls() throws DfException { + HashSet aclModifiedIds = new HashSet(); + IDfQuery query = makeUpdateAclQuery(); + IDfCollection dmAclCollection = + query.execute(dmSession, IDfQuery.DF_EXECREAD_QUERY); + try { + Map aclMap = new HashMap(); + while (dmAclCollection.next()) { + aclModifiedDate = dmAclCollection.getString("time_stamp_utc_str"); + aclModifyId = dmAclCollection.getString("r_object_id"); + String chronicleId = dmAclCollection.getString("chronicle_id"); + String modifyObjectId = dmAclCollection.getString("audited_obj_id"); + + if (aclModifiedIds.contains(chronicleId)) { + logger.log(Level.FINE, + "Skipping redundant modify of: {0}", chronicleId); + continue; + } + IDfACL dmAcl = (IDfACL) dmSession.getObject(new DfId(modifyObjectId)); + addAclChainToMap(dmAcl, modifyObjectId, aclMap); + aclModifiedIds.add(chronicleId); + } + return aclMap; + } finally { + try { + dmAclCollection.close(); + } catch (DfException e) { + logger.log(Level.WARNING, "Error closing collection", e); + } + } + } + /** * Adds all of the Adaptor Acls for the Documentum ACL to the map. * @@ -247,87 +325,4 @@ private Acl getBasicAcl(String objectId, String parentAclId, } return builder.build(); } - - /** - * Returns all updated Documentum ACLs in map with doc id and Acl. - * - * In Documentum, ACLs are high level objects separate from content objects. - * An ACL can be applied to one or many content objects. Or, each object - * can have it's own individual ACL applied to it. So this method needs to - * send all the ACLs in Documentum to GSA. - * - * @return Documentum ACLs in map - * @throws DfException if error in getting ACL information. - */ - public Map getUpdateAcls() throws DfException { - HashSet aclModifiedIds = new HashSet(); - IDfQuery query = makeUpdateAclQuery(); - IDfCollection dmAclCollection = - query.execute(dmSession, IDfQuery.DF_EXECREAD_QUERY); - try { - Map aclMap = new HashMap(); - while (dmAclCollection.next()) { - aclModifyId = dmAclCollection.getString("r_object_id"); - aclModifiedDate = dmAclCollection.getString("time_stamp_utc_str"); - String chronicleId = dmAclCollection.getString("chronicle_id"); - String modifyObjectId = dmAclCollection.getString("audited_obj_id"); - - if (aclModifiedIds.contains(chronicleId)) { - logger.log(Level.FINE, - "Skipping redundant modify of: {0}", chronicleId); - continue; - } - IDfACL dmAcl = (IDfACL) dmSession.getObject(new DfId(modifyObjectId)); - addAclChainToMap(dmAcl, modifyObjectId, aclMap); - aclModifiedIds.add(chronicleId); - } - return aclMap; - } finally { - try { - dmAclCollection.close(); - } catch (DfException e) { - logger.log(Level.WARNING, "Error closing collection", e); - } - } - } - - private IDfQuery makeUpdateAclQuery() { - StringBuilder queryStr = new StringBuilder( - "select r_object_id, chronicle_id, audited_obj_id, event_name, " - + "time_stamp_utc, " - + "DATETOSTRING(time_stamp_utc, 'yyyy-mm-dd hh:mi:ss') " - + "as time_stamp_utc_str " - + "from dm_audittrail_acl " - + "where (event_name='dm_save' or event_name='dm_saveasnew' " - + "or event_name='dm_destroy')"); - - String whereBoundedClause = " and ((time_stamp_utc = " - + "date(''{0}'',''yyyy-mm-dd hh:mi:ss'') and (r_object_id > ''{1}'')) " - + "OR (time_stamp_utc > date(''{0}'',''yyyy-mm-dd hh:mi:ss'')))"; - String whereBoundedClauseDateOnly = " and (time_stamp_utc > " - + "date(''{0}'',''yyyy-mm-dd hh:mi:ss''))"; - - if (Strings.isNullOrEmpty(aclModifiedDate)) { - aclModifiedDate = getNOW(); - logger.log(Level.FINE, "Setting ModifiedDate to now: {0}", - aclModifiedDate); - } - - Object[] arguments = {aclModifiedDate, aclModifyId}; - queryStr.append(MessageFormat.format( - (arguments[1] == null) ? whereBoundedClauseDateOnly - : whereBoundedClause, arguments)); - queryStr.append(" order by time_stamp_utc, r_object_id, event_name"); - logger.log(Level.FINE, "ModifiedDate: {0} ; Modify Id: {1}", arguments); - logger.log(Level.FINER, "UpdateAcl query: {0}", queryStr.toString()); - - IDfQuery query = dmClientX.getQuery(); - query.setDQL(queryStr.toString()); - return query; - } - - private String getNOW() { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - return format.format(new Date()); - } } diff --git a/test/com/google/enterprise/adaptor/documentum/DocumentumAdaptorTest.java b/test/com/google/enterprise/adaptor/documentum/DocumentumAdaptorTest.java index b789020..4ad0a9e 100644 --- a/test/com/google/enterprise/adaptor/documentum/DocumentumAdaptorTest.java +++ b/test/com/google/enterprise/adaptor/documentum/DocumentumAdaptorTest.java @@ -2735,19 +2735,13 @@ private class CollectionMock { public CollectionMock(String query) throws SQLException { stmt = jdbcFixture.getConnection().createStatement(); - query = query.replace("DATETOSTRING(time_stamp_utc, " - + "'yyyy-mm-dd hh:mi:ss') as time_stamp_utc_str", - "time_stamp_utc as time_stamp_utc_str"); - query = query.replace("date", "convert(parseDateTime"); - query = query.replace("'yyyy-mm-dd hh:mi:ss')", "'" - + DocumentumAcls.DATE_FORMAT + "'), timestamp)"); + query = query.replace("DATETOSTRING", "FORMATDATETIME") + .replace("date(", "parseDateTime(") + .replace("yyyy-mm-dd hh:mi:ss", DocumentumAcls.DATE_FORMAT); rs = stmt.executeQuery(query); } public String getString(String colName) throws SQLException { - if (colName.equals("time_stamp_utc_str")) { - colName = "time_stamp_utc"; - } return rs.getString(colName); } @@ -2987,19 +2981,17 @@ private DocumentumAcls getDocumentumAcls() throws DfException { } @Test - public void testUpdateAcls() throws DfException, InterruptedException, - SQLException { + public void testUpdateAcls() throws Exception { createAcl("4501081f80000100"); createAcl("4501081f80000101"); createAcl("4501081f80000102"); - String dateStr = getDateString(5); + String dateStr = getNowPlusMinutes(5); insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr); insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr); insertAclAudit("125", "236", "4501081f80000102", "dm_destroy", dateStr); Map aclMap = getDocumentumAcls().getUpdateAcls(); - assertEquals(3, aclMap.size()); - assertEquals( ImmutableSet.of( + assertEquals(ImmutableSet.of( new DocId("4501081f80000100"), new DocId("4501081f80000101"), new DocId("4501081f80000102")), aclMap.keySet()); @@ -3009,78 +3001,91 @@ public void testUpdateAcls() throws DfException, InterruptedException, } @Test - public void testUpdateAclsWithSameChronicleId() throws DfException, - InterruptedException, SQLException { + public void testUpdateAclsWithSameChronicleId() throws Exception { createAcl("4501081f80000100"); createAcl("4501081f80000101"); createAcl("4501081f80000102"); - String dateStr = getDateString(6); + String dateStr = getNowPlusMinutes(6); insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr); insertAclAudit("124", "234", "4501081f80000101", "dm_saveasnew", dateStr); insertAclAudit("125", "234", "4501081f80000102", "dm_destroy", dateStr); Map aclMap = getDocumentumAcls().getUpdateAcls(); - assertEquals(1, aclMap.size()); assertEquals(ImmutableSet.of(new DocId("4501081f80000100")), aclMap.keySet()); } @Test - public void testPreviouslyUpdatedAcls() throws DfException, - InterruptedException, SQLException { + public void testPreviouslyUpdatedAcls() throws Exception { createAcl("4501081f80000100"); createAcl("4501081f80000101"); createAcl("4501081f80000102"); - String dateStr = getDateString(-10); + String dateStr = getNowPlusMinutes(-10); insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr); insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr); insertAclAudit("125", "236", "4501081f80000102", "dm_destroy", dateStr); Map aclMap = getDocumentumAcls().getUpdateAcls(); - assertEquals(0, aclMap.size()); + assertEquals(ImmutableMap.of(), aclMap); } @Test - public void testMultiUpdateAcls() throws DfException, InterruptedException, - SQLException { + public void testMultiUpdateAcls() throws Exception { DocumentumAcls dctmAcls = getDocumentumAcls(); createAcl("4501081f80000100"); createAcl("4501081f80000101"); createAcl("4501081f80000102"); - String dateStr = getDateString(10); + String dateStr = getNowPlusMinutes(10); insertAclAudit("123", "234", "4501081f80000100", "dm_save", dateStr); insertAclAudit("124", "235", "4501081f80000101", "dm_saveasnew", dateStr); insertAclAudit("125", "236", "4501081f80000102", "dm_saveasnew", dateStr); Map aclMap = dctmAcls.getUpdateAcls(); - assertEquals(3, aclMap.size()); - assertEquals( ImmutableSet.of( + assertEquals(ImmutableSet.of( new DocId("4501081f80000100"), new DocId("4501081f80000101"), new DocId("4501081f80000102")), aclMap.keySet()); - dateStr = getDateString(15); + dateStr = getNowPlusMinutes(15); insertAclAudit("126", "237", "4501081f80000103", "dm_saveasnew", dateStr); insertAclAudit("127", "238", "4501081f80000104", "dm_destroy", dateStr); aclMap = dctmAcls.getUpdateAcls(); - assertEquals(2, aclMap.size()); assertEquals(ImmutableSet.of( new DocId("4501081f80000103"), new DocId("4501081f80000104")), aclMap.keySet()); } + @Test + public void testMultiUpdateAclsWithNoResults() throws Exception { + DocumentumAcls dctmAcls = getDocumentumAcls(); + + createAcl("4501081f80000106"); + createAcl("4501081f80000107"); + String dateStr = getNowPlusMinutes(20); + insertAclAudit("128", "234", "4501081f80000106", "dm_saveasnew", dateStr); + insertAclAudit("129", "235", "4501081f80000107", "dm_saveasnew", dateStr); + + Map aclMap = dctmAcls.getUpdateAcls(); + assertEquals(ImmutableSet.of( + new DocId("4501081f80000106"), + new DocId("4501081f80000107")), aclMap.keySet()); + + aclMap = dctmAcls.getUpdateAcls(); + assertEquals(ImmutableSet.of(), aclMap.keySet()); + } + /** * Returns date string with minutes added to current time. * - * @param min minutes to add. + * @param minutes minutes to add. * @return date in string format. */ - private String getDateString(int min) { + private String getNowPlusMinutes(int minutes) { SimpleDateFormat format = new SimpleDateFormat(DocumentumAcls.DATE_FORMAT); Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.MINUTE, min); + calendar.add(Calendar.MINUTE, minutes); return format.format(calendar.getTime()); } }