From 5a129063877908797cb9864f21de455b7aa36ba2 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Mon, 26 Sep 2011 07:06:56 +0000
Subject: [PATCH] LMS-2504 System tests extended to test property history
 feature.

SVN: 23081
---
 ...atchMaterialRegistrationAndUpdateTest.java | 120 +-----------------
 .../openbis/systemtest/HistoryRowMapper.java  |  45 +++++++
 .../openbis/systemtest/PropertyHistory.java   |  88 +++++++++++++
 .../openbis/systemtest/SystemTestCase.java    |  37 ++++++
 ...GeneralInformationChangingServiceTest.java |  13 ++
 5 files changed, 189 insertions(+), 114 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/HistoryRowMapper.java
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/PropertyHistory.java

diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdateTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdateTest.java
index 027dc76a01b..0f9b502ffbf 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdateTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdateTest.java
@@ -19,7 +19,6 @@ package ch.systemsx.cisd.openbis.systemtest;
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertTrue;
 
-import java.sql.SQLException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -29,7 +28,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
 import org.springframework.transaction.annotation.Propagation;
 import org.springframework.transaction.annotation.Transactional;
 import org.testng.annotations.AfterMethod;
@@ -65,94 +63,6 @@ public class BatchMaterialRegistrationAndUpdateTest extends SystemTestCase
 
     private static final Set<String> CODES = new HashSet<String>(Arrays.asList("C1", "C2"));
 
-    private static class PropertyHistory
-    {
-        private String propertyTypeCode;
-
-        private String value;
-
-        private Long termID;
-
-        private Long materialID;
-
-        private Date validUntilTimeStamp;
-
-        public Date getValidUntilTimeStamp()
-        {
-            return validUntilTimeStamp;
-        }
-
-        public void setValidUntilTimeStamp(Date validUntilTimeStamp)
-        {
-            this.validUntilTimeStamp = validUntilTimeStamp;
-        }
-
-        public void setPropertyTypeCode(String propertyTypeCode)
-        {
-            this.propertyTypeCode = propertyTypeCode;
-        }
-
-        public void setValue(String value)
-        {
-            this.value = value;
-        }
-
-        public void setTermID(Long termID)
-        {
-            this.termID = termID;
-        }
-
-        public void setMaterialID(Long materialID)
-        {
-            this.materialID = materialID;
-        }
-
-        @Override
-        public String toString()
-        {
-            StringBuilder builder = new StringBuilder();
-            builder.append(propertyTypeCode).append(":");
-            if (value != null)
-            {
-                builder.append(' ').append(value);
-            }
-            if (termID != null)
-            {
-                builder.append(" term:").append(termID);
-            }
-            if (materialID != null)
-            {
-                builder.append(" material:").append(materialID);
-            }
-            return builder.toString();
-        }
-
-    }
-
-    private static final class HistoryRowMapper implements ParameterizedRowMapper<PropertyHistory>
-    {
-
-        public PropertyHistory mapRow(java.sql.ResultSet rs, int rowNum) throws SQLException
-        {
-            PropertyHistory propertyHistory = new PropertyHistory();
-            propertyHistory.setPropertyTypeCode(rs.getString("code"));
-            propertyHistory.setValue(rs.getString("value"));
-            long id = rs.getLong("cvte_id");
-            if (rs.wasNull() == false)
-            {
-                propertyHistory.setTermID(id);
-            }
-            id = rs.getLong("mate_prop_id");
-            if (rs.wasNull() == false)
-            {
-                propertyHistory.setMaterialID(id);
-            }
-            propertyHistory.setValidUntilTimeStamp(rs.getTimestamp("valid_until_timestamp"));
-            return propertyHistory;
-        }
-
-    }
-
     @AfterMethod
     public void tearDown()
     {
@@ -227,18 +137,19 @@ public class BatchMaterialRegistrationAndUpdateTest extends SystemTestCase
         assertProperties(
                 "[BACTERIUM: BACTERIUM-Y (BACTERIUM), DESCRIPTION: compound 2, GENDER: MALE, SIZE: 43]",
                 "C2");
-        List<PropertyHistory> history = getHistory(getMaterialOrNull("C1").getId());
+        List<PropertyHistory> history =
+                getMaterialPropertiesHistory(getMaterialOrNull("C1").getId());
         assertEquals("[BACTERIUM: material:22, DESCRIPTION: compound 1, GENDER: term:12]",
                 history.toString());
         assertCurrentValidUntilTimeStamp(history.get(0));
-        assertEquals("[BACTERIUM: material:34]", getHistory(getMaterialOrNull("C2").getId())
-                .toString());
+        assertEquals("[BACTERIUM: material:34]",
+                getMaterialPropertiesHistory(getMaterialOrNull("C2").getId()).toString());
 
         updateMaterials("code\tdescription\tgender\tbacterium\n"
                 + "c2\t--DELETE--\tfemale\tbacterium2\n", MATERIAL_TYPE, false);
 
         assertEquals("[BACTERIUM: material:34, BACTERIUM: material:35, GENDER: term:11]",
-                getHistory(getMaterialOrNull("C2").getId()).toString());
+                getMaterialPropertiesHistory(getMaterialOrNull("C2").getId()).toString());
         deleteTestMaterials();
     }
 
@@ -282,7 +193,7 @@ public class BatchMaterialRegistrationAndUpdateTest extends SystemTestCase
         for (Material deletedMaterial : materialsToBeDeleted)
         {
             assertEquals("Deleted material: " + deletedMaterial, 0,
-                    getHistory(deletedMaterial.getId()).size());
+                    getMaterialPropertiesHistory(deletedMaterial.getId()).size());
         }
     }
 
@@ -356,23 +267,4 @@ public class BatchMaterialRegistrationAndUpdateTest extends SystemTestCase
                         - System.currentTimeMillis()) < 10000);
     }
 
-    private List<PropertyHistory> getHistory(long materialID)
-    {
-        List<PropertyHistory> list =
-                simpleJdbcTemplate
-                        .query("select t.code, h.value, h.cvte_id, h.mate_prop_id, valid_until_timestamp"
-                                + " from material_properties_history as h "
-                                + " join material_type_property_types as etpt on h.mtpt_id = etpt.id"
-                                + " join property_types as t on etpt.prty_id = t.id where h.mate_id = ?",
-                                new HistoryRowMapper(), materialID);
-        Collections.sort(list, new Comparator<PropertyHistory>()
-            {
-                public int compare(PropertyHistory o1, PropertyHistory o2)
-                {
-                    return o1.toString().compareTo(o2.toString());
-                }
-            });
-        return list;
-    }
-
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/HistoryRowMapper.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/HistoryRowMapper.java
new file mode 100644
index 00000000000..ab84bbdebff
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/HistoryRowMapper.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2011 ETH Zuerich, CISD
+ *
+ * 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.systemtest;
+
+import java.sql.SQLException;
+
+import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
+
+final class HistoryRowMapper implements ParameterizedRowMapper<PropertyHistory>
+{
+
+    public PropertyHistory mapRow(java.sql.ResultSet rs, int rowNum) throws SQLException
+    {
+        PropertyHistory propertyHistory = new PropertyHistory();
+        propertyHistory.setPropertyTypeCode(rs.getString("code"));
+        propertyHistory.setValue(rs.getString("value"));
+        long id = rs.getLong("cvte_id");
+        if (rs.wasNull() == false)
+        {
+            propertyHistory.setTermID(id);
+        }
+        id = rs.getLong("mate_prop_id");
+        if (rs.wasNull() == false)
+        {
+            propertyHistory.setMaterialID(id);
+        }
+        propertyHistory.setValidUntilTimeStamp(rs.getTimestamp("valid_until_timestamp"));
+        return propertyHistory;
+    }
+
+}
\ No newline at end of file
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/PropertyHistory.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/PropertyHistory.java
new file mode 100644
index 00000000000..5f2a6082dc5
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/PropertyHistory.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2011 ETH Zuerich, CISD
+ *
+ * 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.systemtest;
+
+import java.util.Date;
+
+/**
+ * Helper class with property history.
+ * 
+ * @author Franz-Josef Elmer
+ */
+public class PropertyHistory
+{
+    private String propertyTypeCode;
+
+    private String value;
+
+    private Long termID;
+
+    private Long materialID;
+
+    private Date validUntilTimeStamp;
+
+    public Date getValidUntilTimeStamp()
+    {
+        return validUntilTimeStamp;
+    }
+
+    public void setValidUntilTimeStamp(Date validUntilTimeStamp)
+    {
+        this.validUntilTimeStamp = validUntilTimeStamp;
+    }
+
+    public void setPropertyTypeCode(String propertyTypeCode)
+    {
+        this.propertyTypeCode = propertyTypeCode;
+    }
+
+    public void setValue(String value)
+    {
+        this.value = value;
+    }
+
+    public void setTermID(Long termID)
+    {
+        this.termID = termID;
+    }
+
+    public void setMaterialID(Long materialID)
+    {
+        this.materialID = materialID;
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder builder = new StringBuilder();
+        builder.append(propertyTypeCode).append(":");
+        if (value != null)
+        {
+            builder.append(' ').append(value);
+        }
+        if (termID != null)
+        {
+            builder.append(" term:").append(termID);
+        }
+        if (materialID != null)
+        {
+            builder.append(" material:").append(materialID);
+        }
+        return builder.toString();
+    }
+
+}
\ No newline at end of file
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
index f87e1de81f2..8168bbce17e 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
@@ -243,4 +243,41 @@ public abstract class SystemTestCase extends AbstractTransactionalTestNGSpringCo
         AssertJUnit.fail("No property " + key + " found in " + propertyCodes);
     }
 
+    protected List<PropertyHistory> getMaterialPropertiesHistory(long materialID)
+    {
+        List<PropertyHistory> list =
+                simpleJdbcTemplate
+                        .query("select t.code, h.value, h.cvte_id, h.mate_prop_id, valid_until_timestamp"
+                                + " from material_properties_history as h "
+                                + " join material_type_property_types as etpt on h.mtpt_id = etpt.id"
+                                + " join property_types as t on etpt.prty_id = t.id where h.mate_id = ?",
+                                new HistoryRowMapper(), materialID);
+        sort(list);
+        return list;
+    }
+
+    protected List<PropertyHistory> getSamplePropertiesHistory(long sampleID)
+    {
+        List<PropertyHistory> list =
+                simpleJdbcTemplate
+                        .query("select t.code, h.value, h.cvte_id, h.mate_prop_id, valid_until_timestamp"
+                                + " from sample_properties_history as h "
+                                + " join sample_type_property_types as etpt on h.stpt_id = etpt.id"
+                                + " join property_types as t on etpt.prty_id = t.id where h.samp_id = ?",
+                                new HistoryRowMapper(), sampleID);
+        sort(list);
+        return list;
+    }
+
+    private void sort(List<PropertyHistory> list)
+    {
+        Collections.sort(list, new Comparator<PropertyHistory>()
+            {
+                public int compare(PropertyHistory o1, PropertyHistory o2)
+                {
+                    return o1.toString().compareTo(o2.toString());
+                }
+            });
+    }
+
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/api/v1/GeneralInformationChangingServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/api/v1/GeneralInformationChangingServiceTest.java
index cf743e5b138..05ad3c74001 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/api/v1/GeneralInformationChangingServiceTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/api/v1/GeneralInformationChangingServiceTest.java
@@ -16,7 +16,10 @@
 
 package ch.systemsx.cisd.openbis.systemtest.api.v1;
 
+import static org.testng.AssertJUnit.assertEquals;
+
 import java.util.HashMap;
+import java.util.List;
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.testng.annotations.AfterMethod;
@@ -29,6 +32,7 @@ import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService
 import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.EntityKind;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewETPTAssignment;
+import ch.systemsx.cisd.openbis.systemtest.PropertyHistory;
 import ch.systemsx.cisd.openbis.systemtest.SystemTestCase;
 
 /**
@@ -61,6 +65,11 @@ public class GeneralInformationChangingServiceTest extends SystemTestCase
         generalInformationService.logout(sessionToken);
     }
 
+    @Test
+    public void testUpdateExperimentProperties()
+    {
+    }
+
     @Test
     public void testUpdateSampleProperties()
     {
@@ -86,5 +95,9 @@ public class GeneralInformationChangingServiceTest extends SystemTestCase
                 + "COMMENT: extremely simple stuff, DESCRIPTION: hello example, GENDER: FEMALE, "
                 + "ORGANISM: DOG, SIZE: 42]", commonServer.getSampleInfo(sessionToken, id)
                 .getParent());
+
+        List<PropertyHistory> history = getSamplePropertiesHistory(id.getId());
+        assertEquals("[ANY_MATERIAL: material:41, ORGANISM: term:9, SIZE: 321]", history.toString());
+
     }
 }
-- 
GitLab