Commits

dloy committed 4489063

Make fixity test optional update base on content update

Comments (0)

Files changed (2)

fixity-src/src/main/java/org/cdlib/mrt/fixity/action/ProcessFixityEntry.java

 import org.cdlib.mrt.fixity.utility.FixityDBUtil;
 import org.cdlib.mrt.fixity.utility.FixityUtil;
 import org.cdlib.mrt.core.DateState;
+import org.cdlib.mrt.core.MessageDigest;
 import org.cdlib.mrt.utility.PropertiesUtil;
 import org.cdlib.mrt.utility.LoggerInf;
 import org.cdlib.mrt.utility.TException;
                         "No matching URL was found for this item:" + entry.getUrl());
             }
             FixityEntry dbEntry = FixityDBUtil.getEntry(connection, id, logger);
+            FixityEntry.StatusType dbStatus = dbEntry.getStatus();
+            DateState dbVerifiedDate = dbEntry.getVerified();
             if (DEBUG) dbEntry.printEntry("IN UPDATE: dbEntry");
             FixityEntry mergeEntry = dbEntry.mergeFromEntry(entry);
             entry = mergeEntry;
             handler = FixityHandlerAbs.getFixityHandler(entry, logger);
             handler.validate();
             log("before runFixity");
-            handler.runFixity();
-            if (entry.getStatus() != FixityEntry.StatusType.verified) {
-                throw new TException.REQUEST_INVALID("Fixity Test fails item not updated:" + entry.getStatus().getItemDBStatus());
+            if (isFixityDiff(dbEntry, mergeEntry)) {
+                handler.runFixity();
+                if (entry.getStatus() != FixityEntry.StatusType.verified) {
+                    throw new TException.REQUEST_INVALID("Fixity Test fails item not updated:" + entry.getStatus().getItemDBStatus());
+                }
+            } else {
+                entry.setStatus(dbStatus);
+                entry.setVerified(dbVerifiedDate);
+                log ("no Fixity diff");
             }
 
             log("runUpdate before replaceEntry");
 
     }
 
+    /**
+     * If either the digest or the file size don't match then a rerun is necessare
+     * @param dbEntry - currently saved version of entry
+     * @param mergeEntry - merge version of entry
+     * @return true=rerun fixity test; false=no changes to primary fixity values
+     * @throws TException
+     */
+    public static boolean isFixityDiff(FixityEntry dbEntry, FixityEntry mergeEntry)
+        throws TException
+    {
+        MessageDigest dbDigest = dbEntry.getDigest();
+        MessageDigest mergeDigest = mergeEntry.getDigest();
+        long dbSize = dbEntry.getSize();
+        long mergeSize = mergeEntry.getSize();
+        FixityEntry.SourceType dbSourceType = dbEntry.getSource();
+        FixityEntry.SourceType mergeSourceType = mergeEntry.getSource();
+
+
+        if ((dbDigest != null) && (mergeDigest != null)) {
+            if (!dbDigest.toString().equals(mergeDigest.toString())) {
+                return true;
+            }
+
+        } else if ((dbDigest == null) && (mergeDigest == null)) {
+            return false;
+
+        } else if ((dbDigest != null) || (mergeDigest != null)) {
+            return true;
+        }
+
+        if ((dbSize <= 0) && (mergeSize <= 0)) {
+            return false;
+
+        }  else if (dbSize != mergeSize) {
+            return true;
+        }
+
+        if ((dbSourceType != null) && (mergeSourceType != null)) {
+            if (dbSourceType != mergeSourceType) {
+                return true;
+            }
+
+        } else if ((dbSourceType == null) && (mergeSourceType == null)) {
+            return false;
+
+        } else if ((dbSourceType != null) || (mergeSourceType != null)) {
+            return true;
+        }
+
+        return false;
+    }
     protected void setRunException(Exception exception)
     {
         Properties entryProp = entry.retrieveProperties();
         return getEntry();
     }
 
-    protected void log(String msg)
+    protected static void log(String msg)
     {
         if (!DEBUG) return;
         System.out.println(MESSAGE + msg);

fixity-src/src/test/java/org/cdlib/mrt/fixity/service/action/ProcessFixityEntryTest.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package org.cdlib.mrt.fixity.service.action;
+
+import java.util.Properties;
+import java.io.File;
+import org.cdlib.mrt.utility.DateUtil;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.cdlib.mrt.fixity.action.ProcessFixityEntry;
+import org.cdlib.mrt.fixity.db.FixityContextEntry;
+import org.cdlib.mrt.utility.FileUtil;
+import org.cdlib.mrt.utility.LoggerAbs;
+import org.cdlib.mrt.utility.LoggerInf;
+import org.cdlib.mrt.utility.PropertiesUtil;
+import org.cdlib.mrt.utility.TException;
+
+
+import java.util.Date;
+import org.cdlib.mrt.fixity.db.FixityEntry;
+
+/**
+ *
+ * @author dloy
+ */
+public class ProcessFixityEntryTest {
+    private LoggerInf logger = null;
+    public ProcessFixityEntryTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    @Before
+    public void setUp() {
+        try {
+            logger = LoggerAbs.getTFileLogger("testFormatter", 10, 10);
+        } catch (Exception ex) {
+            logger = null;
+        }
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+
+
+    @Test
+    public void match()
+    {
+
+        try {
+            Properties propDB = new Properties();
+            propDB.setProperty("source", "web");
+            propDB.setProperty("url", "http://hokusai.cdlib.org:28080/storage/content/1001/ark%3A%2F13030%2Fqt11z1k021/1/system%2Fmrt-ingest.txt");
+            propDB.setProperty("type", "SHA-256");
+            propDB.setProperty("value", "cd64f74c242a36cc7e467e4788cece9a44ef60e18b84f4bd72e36addb4940242");
+            propDB.setProperty("size", "1653");
+            propDB.setProperty("verified", "2011-04-28 08:32:03");
+            propDB.setProperty("created", "2011-04-27 12:55:53");
+            propDB.setProperty("status", "verified");
+            propDB.setProperty("context", "this is an old context");
+            //System.out.println(PropertiesUtil.dumpProperties("propDB ", propDB));
+
+            Properties propDiff = PropertiesUtil.copyProperties(propDB);
+            isDiff("same", propDB, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("context", "this is a new context");
+            isDiff("new context", propDB, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("size", "1653");
+            isDiff("same size change", propDB, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.remove("size");
+            isDiff("one size", propDB, propDiff, true);
+
+            isDiff("no size", propDiff, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("size", "1654");
+            isDiff("bad size", propDB, propDiff, true);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("value", "cd64f74c242a36cc7e467e4788cece9a44ef60e18b84f4bd72e36addb4940242");
+            isDiff("same checksum", propDB, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("value", "aa64f74c242a36cc7e467e4788cece9a44ef60e18b84f4bd72e36addb4940242");
+            isDiff("diff checksum", propDB, propDiff, true);
+            assertTrue(true);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.remove("value");
+            propDiff.remove("type");
+            isDiff("one digest", propDB, propDiff, true);
+
+            isDiff("no digest", propDiff, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("source", "web");
+            isDiff("same source", propDB, propDiff, false);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("source", "merritt");
+            isDiff("diff source", propDB, propDiff, true);
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.setProperty("source", "file");
+            isDiff("diff source", propDB, propDiff, true);
+
+
+            propDiff = PropertiesUtil.copyProperties(propDB);
+            propDiff.remove("source");
+            isDiff("one source", propDB, propDiff, true);
+
+            isDiff("no source", propDiff, propDiff, false);
+            assertTrue(true);
+
+        } catch (Exception ex) {
+            System.out.println("Exception:" + ex);
+            ex.printStackTrace();
+            assertFalse("Exception:" + ex, true);
+        }
+    }
+
+    public void isDiff(String header, Properties propDb, Properties propDiff, boolean diff)
+    {
+        try {
+            System.out.println("------------------->" + header);
+            System.out.println(PropertiesUtil.dumpProperties("propDB ", propDb));
+            FixityEntry dbEntry = new FixityEntry(propDb);
+            System.out.println(PropertiesUtil.dumpProperties("diff   ", propDiff));
+            FixityEntry diffEntry = new FixityEntry(propDiff);
+            FixityContextEntry dbContext = dbEntry.getContextEntry(0);
+            if (dbContext != null) {
+                System.out.println("db   context:" + dbContext.getContext());
+            } else {
+                System.out.println("db   context: NONE");
+            }
+
+            FixityContextEntry diffContext = diffEntry.getContextEntry(0);
+            if (diffContext != null) {
+                System.out.println("diff context:" + diffContext.getContext());
+            } else {
+                System.out.println("diff context: NONE");
+            }
+            boolean matchEntry = ProcessFixityEntry.isFixityDiff(dbEntry, diffEntry);
+            System.out.println("expect=" + diff + " - got=" + matchEntry);
+            assertTrue(diff == matchEntry);
+
+        } catch (Exception ex) {
+            assertFalse("Exception:" + ex, true);
+        }
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.