From c0c051d7e372b38ba3c49b741835a708489f3875 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Wed, 20 Aug 2014 06:55:27 +0000
Subject: [PATCH] SSDM-768: Introducing ILogMonitoringStopCondition

SVN: 32252
---
 .../FinishedPostRegistrationCondition.java    |  38 +++++
 .../ILogMonitoringStopCondition.java          |  27 ++++
 .../systemtests/ParsedLogEntry.java           |  67 +++++++++
 .../systemtests/SystemTestCase.java           | 136 +++---------------
 .../FeatureVectorsDropboxTest.java            |   9 +-
 .../systemtests/ImageBase64EncodingTest.java  |  11 +-
 .../TransformedImageRepresentationsTest.java  |   8 +-
 7 files changed, 162 insertions(+), 134 deletions(-)
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/FinishedPostRegistrationCondition.java
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ILogMonitoringStopCondition.java
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ParsedLogEntry.java

diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/FinishedPostRegistrationCondition.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/FinishedPostRegistrationCondition.java
new file mode 100644
index 00000000000..94758de7eda
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/FinishedPostRegistrationCondition.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 ETH Zuerich, SIS
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.datastoreserver.systemtests;
+
+import java.util.regex.Pattern;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class FinishedPostRegistrationCondition implements ILogMonitoringStopCondition
+{
+    public static final ILogMonitoringStopCondition INSTANCE = new FinishedPostRegistrationCondition();
+    
+    private static Pattern PATTERN = Pattern.compile(".*Post registration of (\\d*). of \\1 data sets: (.*)");
+    
+    @Override
+    public boolean stopConditionFulfilled(ParsedLogEntry logEntry)
+    {
+        return PATTERN.matcher(logEntry.getLogMessage()).matches();
+    }
+
+}
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ILogMonitoringStopCondition.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ILogMonitoringStopCondition.java
new file mode 100644
index 00000000000..97a35e121e0
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ILogMonitoringStopCondition.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2014 ETH Zuerich, SIS
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.datastoreserver.systemtests;
+
+/**
+ * Interface for a stop condition in monitoring logs.
+ *
+ * @author Franz-Josef Elmer
+ */
+public interface ILogMonitoringStopCondition
+{
+    public boolean stopConditionFulfilled(ParsedLogEntry logEntry);
+}
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ParsedLogEntry.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ParsedLogEntry.java
new file mode 100644
index 00000000000..389c7b781c0
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/ParsedLogEntry.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2014 ETH Zuerich, SIS
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.openbis.datastoreserver.systemtests;
+
+import java.text.MessageFormat;
+import java.util.Date;
+
+import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
+
+public final class ParsedLogEntry
+{
+    private static final String FORMAT_TEMPLATE = "{0,date," + BasicConstant.DATE_WITHOUT_TIMEZONE_PATTERN + "} {1} [{2}] {3}";
+    
+    private Date timestamp;
+    private String logLevel;
+    private String threadName;
+    private String logMessage;
+
+    ParsedLogEntry(Date timestamp, String logLevel, String threadName, String logMessage)
+    {
+        this.timestamp = timestamp;
+        this.logLevel = logLevel;
+        this.threadName = threadName;
+        this.logMessage = logMessage;
+    }
+
+    public Date getTimestamp()
+    {
+        return timestamp;
+    }
+
+    public String getLogLevel()
+    {
+        return logLevel;
+    }
+
+    public String getThreadName()
+    {
+        return threadName;
+    }
+
+    public String getLogMessage()
+    {
+        return logMessage;
+    }
+
+    @Override
+    public String toString()
+    {
+        return new MessageFormat(FORMAT_TEMPLATE).format(new Object[] {timestamp, logLevel, threadName, logMessage});
+    }
+    
+}
\ No newline at end of file
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SystemTestCase.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SystemTestCase.java
index 0605eb25b83..3fc696f52fd 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SystemTestCase.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/datastoreserver/systemtests/SystemTestCase.java
@@ -77,42 +77,6 @@ import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
  */
 public abstract class SystemTestCase extends AssertJUnit
 {
-    private static final class LogEntry
-    {
-        private Date timestamp;
-        private String logLevel;
-        private String threadName;
-        private String logMessage;
-
-        LogEntry(Date timestamp, String logLevel, String threadName, String logMessage)
-        {
-            this.timestamp = timestamp;
-            this.logLevel = logLevel;
-            this.threadName = threadName;
-            this.logMessage = logMessage;
-        }
-
-        public Date getTimestamp()
-        {
-            return timestamp;
-        }
-
-        public String getLogLevel()
-        {
-            return logLevel;
-        }
-
-        public String getThreadName()
-        {
-            return threadName;
-        }
-
-        public String getLogMessage()
-        {
-            return logMessage;
-        }
-    }
-    
     private Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, getClass());
 
     private static final Pattern PATTERN = Pattern.compile("::(\\d+-\\d+);.*");
@@ -291,27 +255,43 @@ public abstract class SystemTestCase extends AssertJUnit
     }
 
     protected void waitUntilDataSetImported() throws Exception
+    {
+        waitUntilDataSetImported(new ILogMonitoringStopCondition()
+            {
+                @Override
+                public boolean stopConditionFulfilled(ParsedLogEntry logEntry)
+                {
+                    String logMessage = logEntry.getLogMessage();
+                    return logMessage.contains(DATA_SET_IMPORTED_LOG_MARKER)
+                            || logMessage.contains(REGISTRATION_FINISHED_LOG_MARKER);
+                }
+            });
+    }
+
+    protected void waitUntilDataSetImported(ILogMonitoringStopCondition stopCondition) throws Exception
     {
         final int maxLoops = dataSetImportWaitDurationInSeconds();
 
         for (int loops = 0; loops < maxLoops; loops++)
         {
             Thread.sleep(1000);
-            List<LogEntry> logEntries = getLogEntries();
-            for (LogEntry logEntry : logEntries)
+            List<ParsedLogEntry> logEntries = getLogEntries();
+            for (ParsedLogEntry logEntry : logEntries)
             {
-                if (checkLogContentForFinishedDataSetRegistration(logEntry.getLogMessage()))
+                if (stopCondition.stopConditionFulfilled(logEntry))
                 {
+                    operationLog.info("Monitoring log stop after this log entry: " + logEntry);
                     return;
                 }
             }
         }
+        fail("Log monitoring stop condition (" + stopCondition + ") never fulfilled after " + maxLoops + " seconds.");
 
     }
     
-    private List<LogEntry> getLogEntries()
+    private List<ParsedLogEntry> getLogEntries()
     {
-        List<LogEntry> result = new ArrayList<LogEntry>();
+        List<ParsedLogEntry> result = new ArrayList<ParsedLogEntry>();
         String[] logLines = getLogAppender().getLogContent().split("\n");
         Pattern pattern = Pattern.compile("^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}),\\d{3} ([^ ]*) \\[(.*)\\] (.*)$");
         SimpleDateFormat dateFormat = new SimpleDateFormat(BasicConstant.DATE_WITHOUT_TIMEZONE_PATTERN);
@@ -326,7 +306,7 @@ public abstract class SystemTestCase extends AssertJUnit
                     String logLevel = matcher.group(2);
                     String threadName = matcher.group(3);
                     String logMessage = matcher.group(4);
-                    result.add(new LogEntry(timestamp, logLevel, threadName, logMessage));
+                    result.add(new ParsedLogEntry(timestamp, logLevel, threadName, logMessage));
                 } catch (ParseException ex)
                 {
                     throw CheckedExceptionTunnel.wrapIfNecessary(ex);
@@ -353,39 +333,7 @@ public abstract class SystemTestCase extends AssertJUnit
 
         fail("Failed to determine whether data set import was executed with error");
     }
-
-    protected boolean checkLogContentForFinishedDataSetRegistration(String logContent)
-    {
-        return logContent.contains(DATA_SET_IMPORTED_LOG_MARKER)
-                || logContent.contains(REGISTRATION_FINISHED_LOG_MARKER);
-    }
-
-    private Set<String> getSuccessfullyRegisteredDataSets(String logContent)
-    {
-        BufferedReader reader = new BufferedReader(new StringReader(logContent));
-        Set<String> codes = new TreeSet<String>();
-        try
-        {
-            String line;
-            String simpleClassName = getClass().getSimpleName();
-            while ((line = reader.readLine()) != null)
-            {
-                int indexOfSignature = line.indexOf(DATA_SET_IMPORTED_LOG_MARKER);
-                if (line.startsWith(simpleClassName) && indexOfSignature > 0)
-                {
-                    codes.addAll(extractDataSetCodes(line.substring(indexOfSignature)));
-                }
-            }
-            return codes;
-        } catch (IOException ex)
-        {
-            throw CheckedExceptionTunnel.wrapIfNecessary(ex);
-        } finally
-        {
-            IOUtils.closeQuietly(reader);
-        }
-    }
-
+    
     protected void waitUntilIndexedByLucene(Class<?> entityPeClass, Long entityId) throws Exception
     {
         operationLog.info("Waiting for " + entityPeClass.getName() + " with id: " + entityId + " to be indexed by Lucene");
@@ -406,21 +354,6 @@ public abstract class SystemTestCase extends AssertJUnit
         fail("Failed to determine whether enity: " + entityPeClass.getName() + " with id: " + entityId + " was indexed by Lucene");
     }
 
-    private Set<String> extractDataSetCodes(String logLineExtract)
-    {
-        Set<String> result = new HashSet<String>();
-        String[] splittedExtract = logLineExtract.split("Data Set Code");
-        for (String term : splittedExtract)
-        {
-            Matcher matcher = PATTERN.matcher(term);
-            if (matcher.matches())
-            {
-                result.add(matcher.group(1));
-            }
-        }
-        return result;
-    }
-
     /**
      * Time to wait to determine if a data set has been registered or not. Subclasses may override.
      */
@@ -439,29 +372,6 @@ public abstract class SystemTestCase extends AssertJUnit
         FileUtils.moveDirectoryToDirectory(exampleDataSet, getIncomingDirectory(), false);
     }
 
-    private boolean checkForFinalPostRegistrationLogEntry(String logContent,
-            Set<String> registeredDataSets)
-    {
-        Pattern pattern = Pattern.compile(".*Post registration of (\\d*). of \\1 data sets: (.*)");
-        String[] lines = logContent.split("\\n");
-        for (String line : lines)
-        {
-            Matcher matcher = pattern.matcher(line);
-            if (matcher.matches() && registeredDataSets.contains(matcher.group(2)))
-            {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    protected boolean checkOnFinishedPostRegistration(String logContent)
-    {
-        Pattern pattern = Pattern.compile(".*Post registration of (\\d*). of \\1 data sets: (.*)");
-        Matcher matcher = pattern.matcher(logContent);
-        return matcher.matches();
-    }
-
     private BufferedAppender getLogAppender()
     {
         if (logAppender == null)
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/FeatureVectorsDropboxTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/FeatureVectorsDropboxTest.java
index ca89ad58c90..b146dabec49 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/FeatureVectorsDropboxTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/FeatureVectorsDropboxTest.java
@@ -32,6 +32,7 @@ import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.servlet.SpringRequestContextProvider;
+import ch.systemsx.cisd.openbis.datastoreserver.systemtests.FinishedPostRegistrationCondition;
 import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
 import ch.systemsx.cisd.openbis.plugin.screening.client.api.v1.IScreeningOpenbisServiceFacade;
 import ch.systemsx.cisd.openbis.plugin.screening.client.api.v1.ScreeningOpenbisServiceFacade;
@@ -66,13 +67,7 @@ public class FeatureVectorsDropboxTest extends AbstractScreeningSystemTestCase
     {
         File exampleDataSet = createTestDataContents();
         moveFileToIncoming(exampleDataSet);
-        waitUntilDataSetImported();
-    }
-
-    @Override
-    protected boolean checkLogContentForFinishedDataSetRegistration(String logContent)
-    {
-        return checkOnFinishedPostRegistration(logContent);
+        waitUntilDataSetImported(FinishedPostRegistrationCondition.INSTANCE);
     }
 
     @BeforeMethod
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/ImageBase64EncodingTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/ImageBase64EncodingTest.java
index 4f1586e296b..75c45dbf62c 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/ImageBase64EncodingTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/ImageBase64EncodingTest.java
@@ -43,6 +43,7 @@ import com.googlecode.jsonrpc4j.ProxyUtil;
 import ch.systemsx.cisd.common.collection.IModifiable;
 import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.servlet.SpringRequestContextProvider;
+import ch.systemsx.cisd.openbis.datastoreserver.systemtests.FinishedPostRegistrationCondition;
 import ch.systemsx.cisd.openbis.dss.screening.shared.api.v1.IDssServiceRpcScreening;
 import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
 import ch.systemsx.cisd.openbis.plugin.screening.client.api.v1.IScreeningOpenbisServiceFacade;
@@ -82,7 +83,7 @@ public class ImageBase64EncodingTest extends AbstractScreeningSystemTestCase
     {
         File exampleDataSet = createTestDataContents();
         moveFileToIncoming(exampleDataSet);
-        waitUntilDataSetImported();
+        waitUntilDataSetImported(FinishedPostRegistrationCondition.INSTANCE);
     }
 
     @BeforeMethod
@@ -190,13 +191,7 @@ public class ImageBase64EncodingTest extends AbstractScreeningSystemTestCase
     @Override
     protected int dataSetImportWaitDurationInSeconds()
     {
-        return 6000;
-    }
-
-    @Override
-    protected boolean checkLogContentForFinishedDataSetRegistration(String logContent)
-    {
-        return checkOnFinishedPostRegistration(logContent);
+        return 600;
     }
 
     private static class PlateImageReferenceList extends ArrayList<PlateImageReference> implements
diff --git a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/TransformedImageRepresentationsTest.java b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/TransformedImageRepresentationsTest.java
index 7eed1c445f8..aba9fe9b05e 100644
--- a/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/TransformedImageRepresentationsTest.java
+++ b/screening/sourceTest/java/ch/systemsx/cisd/openbis/screening/systemtests/TransformedImageRepresentationsTest.java
@@ -39,6 +39,7 @@ import com.googlecode.jsonrpc4j.ProxyUtil;
 
 import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.servlet.SpringRequestContextProvider;
+import ch.systemsx.cisd.openbis.datastoreserver.systemtests.FinishedPostRegistrationCondition;
 import ch.systemsx.cisd.openbis.dss.client.api.v1.IDataSetDss;
 import ch.systemsx.cisd.openbis.dss.screening.shared.api.v1.IDssServiceRpcScreening;
 import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
@@ -80,7 +81,7 @@ public class TransformedImageRepresentationsTest extends AbstractScreeningSystem
     {
         File exampleDataSet = createTestDataContents();
         moveFileToIncoming(exampleDataSet);
-        waitUntilDataSetImported();
+        waitUntilDataSetImported(FinishedPostRegistrationCondition.INSTANCE);
     }
 
     @BeforeMethod
@@ -250,9 +251,4 @@ public class TransformedImageRepresentationsTest extends AbstractScreeningSystem
         return 6000;
     }
 
-    @Override
-    protected boolean checkLogContentForFinishedDataSetRegistration(String logContent)
-    {
-        return checkOnFinishedPostRegistration(logContent);
-    }
 }
-- 
GitLab