From 3b5e9c58d26fb42447cf1846b086648634c420e2 Mon Sep 17 00:00:00 2001
From: tpylak <tpylak>
Date: Mon, 25 May 2009 13:05:17 +0000
Subject: [PATCH] LMS-910 index.tsv file parser + tests

SVN: 11135
---
 rtd_yeastx/.classpath                         |   6 +-
 .../yeastx/etl/DataSetInformationParser.java  | 105 ++++++++++++++++++
 .../yeastx/etl/PlainDataSetInformation.java   |  95 ++++++++++++++++
 .../etl/DataSetInformationParserTest.java     |  81 ++++++++++++++
 rtd_yeastx/sourceTest/java/tests.xml          |  12 ++
 5 files changed, 298 insertions(+), 1 deletion(-)
 create mode 100644 rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParser.java
 create mode 100644 rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/PlainDataSetInformation.java
 create mode 100644 rtd_yeastx/sourceTest/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParserTest.java
 create mode 100644 rtd_yeastx/sourceTest/java/tests.xml

diff --git a/rtd_yeastx/.classpath b/rtd_yeastx/.classpath
index eb162b4b474..879ad6ffbbb 100644
--- a/rtd_yeastx/.classpath
+++ b/rtd_yeastx/.classpath
@@ -9,7 +9,11 @@
 	<classpathentry kind="lib" path="/libraries/postgresql/postgresql.jar" sourcepath="/libraries/postgresql/postgresql-src.zip"/>
 	<classpathentry kind="lib" path="/libraries/eodsql/eodsql.jar" sourcepath="/libraries/eodsql/eodsql_src.zip"/>
 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry combineaccessrules="false" kind="src" path="/dbmigration"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/datastore_server"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/openbis"/>
 	<classpathentry combineaccessrules="false" kind="src" path="/common"/>
+    <classpathentry combineaccessrules="false" kind="src" path="/dbmigration"/>
+	<classpathentry kind="lib" path="/libraries/testng/testng-jdk15.jar" sourcepath="/libraries/testng/src.zip"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/base"/>
 	<classpathentry kind="output" path="targets/classes"/>
 </classpath>
diff --git a/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParser.java b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParser.java
new file mode 100644
index 00000000000..43535be058f
--- /dev/null
+++ b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParser.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2008 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.yeastx.etl;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+
+import ch.systemsx.cisd.common.parser.AbstractParserObjectFactory;
+import ch.systemsx.cisd.common.parser.IParserObjectFactory;
+import ch.systemsx.cisd.common.parser.IParserObjectFactoryFactory;
+import ch.systemsx.cisd.common.parser.IPropertyMapper;
+import ch.systemsx.cisd.common.parser.IPropertyModel;
+import ch.systemsx.cisd.common.parser.ParserException;
+import ch.systemsx.cisd.common.parser.TabFileLoader;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+
+/**
+ * A {@link AbstractParserObjectFactory} extension for creating {@link NewProperty}.
+ * 
+ * @author Tomasz Pylak
+ */
+final class DataSetInformationParser
+{
+    public static List<PlainDataSetInformation> parse(File indexFile)
+    {
+        TabFileLoader<PlainDataSetInformation> tabFileLoader =
+                new TabFileLoader<PlainDataSetInformation>(
+                        new IParserObjectFactoryFactory<PlainDataSetInformation>()
+                            {
+
+                                public IParserObjectFactory<PlainDataSetInformation> createFactory(
+                                        IPropertyMapper propertyMapper) throws ParserException
+                                {
+                                    return new NewPropertyParserObjectFactory(propertyMapper);
+                                }
+                            });
+        // TODO 2009-05-25, Tomasz Pylak: consider handling exception similar to BisTabFileLoader
+        return tabFileLoader.load(indexFile);
+    }
+
+    private static final class NewPropertyParserObjectFactory extends
+            AbstractParserObjectFactory<PlainDataSetInformation>
+    {
+
+        private NewPropertyParserObjectFactory(final IPropertyMapper propertyMapper)
+        {
+            super(PlainDataSetInformation.class, propertyMapper);
+        }
+
+        private final void setProperties(final PlainDataSetInformation dataset,
+                final String[] lineTokens)
+        {
+            final List<NewProperty> properties = new ArrayList<NewProperty>();
+            for (final String unmatchedProperty : getUnmatchedProperties())
+            {
+                final IPropertyModel propertyModel = tryGetPropertyModel(unmatchedProperty);
+                final String propertyValue = getPropertyValue(lineTokens, propertyModel);
+                if (StringUtils.isEmpty(propertyValue) == false)
+                {
+                    final NewProperty property = new NewProperty();
+                    property.setPropertyCode(propertyModel.getCode());
+                    property.setValue(propertyValue);
+                    properties.add(property);
+                }
+            }
+            dataset.setProperties(properties);
+        }
+
+        //
+        // AbstractParserObjectFactory
+        //
+
+        @Override
+        protected final boolean ignoreUnmatchedProperties()
+        {
+            return true;
+        }
+
+        @Override
+        public final PlainDataSetInformation createObject(final String[] lineTokens)
+                throws ParserException
+        {
+            final PlainDataSetInformation dataset = super.createObject(lineTokens);
+            setProperties(dataset, lineTokens);
+            return dataset;
+        }
+    }
+}
\ No newline at end of file
diff --git a/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/PlainDataSetInformation.java b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/PlainDataSetInformation.java
new file mode 100644
index 00000000000..9904932890c
--- /dev/null
+++ b/rtd_yeastx/source/java/ch/systemsx/cisd/yeastx/etl/PlainDataSetInformation.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2009 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.yeastx.etl;
+
+import java.util.List;
+
+import org.apache.commons.lang.StringUtils;
+
+import ch.systemsx.cisd.common.annotation.BeanProperty;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+
+/**
+ * @author Tomasz Pylak
+ */
+public class PlainDataSetInformation
+{
+    private String fileName;
+
+    private String sampleCodeOrLabel;
+
+    private String experimentCode;
+
+    private String conversion;
+
+    private List<NewProperty> properties;
+
+    public String getFileName()
+    {
+        return fileName;
+    }
+
+    @BeanProperty(label = "file_name")
+    public void setFileName(String fileName)
+    {
+        this.fileName = fileName;
+    }
+
+    public String getSampleCodeOrLabel()
+    {
+        return sampleCodeOrLabel;
+    }
+
+    @BeanProperty(label = "sample")
+    public void setSampleCodeOrLabel(String sampleCodeOrLabel)
+    {
+        this.sampleCodeOrLabel = StringUtils.trimToNull(sampleCodeOrLabel);
+    }
+
+    public String getExperimentCode()
+    {
+        return experimentCode;
+    }
+
+    @BeanProperty(label = "experiment", optional = true)
+    public void setExperimentCode(String experimentCode)
+    {
+        this.experimentCode = experimentCode;
+    }
+
+    public String getConversion()
+    {
+        return conversion;
+    }
+
+    @BeanProperty(label = "conversion", optional = true)
+    public void setConversion(String conversion)
+    {
+        this.conversion = conversion;
+    }
+
+    public List<NewProperty> getProperties()
+    {
+        return properties;
+    }
+
+    public void setProperties(List<NewProperty> dataSetProperties)
+    {
+        this.properties = dataSetProperties;
+    }
+
+}
diff --git a/rtd_yeastx/sourceTest/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParserTest.java b/rtd_yeastx/sourceTest/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParserTest.java
new file mode 100644
index 00000000000..8dde97baed4
--- /dev/null
+++ b/rtd_yeastx/sourceTest/java/ch/systemsx/cisd/yeastx/etl/DataSetInformationParserTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2009 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.yeastx.etl;
+
+import java.io.File;
+import java.util.List;
+
+import org.testng.AssertJUnit;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
+import ch.systemsx.cisd.common.filesystem.FileUtilities;
+import ch.systemsx.cisd.common.parser.MandatoryPropertyMissingException;
+import ch.systemsx.cisd.openbis.generic.shared.dto.NewProperty;
+
+/**
+ * @author Tomasz Pylak
+ */
+public class DataSetInformationParserTest extends AbstractFileSystemTestCase
+{
+    private static final String HEADER =
+            "file_name sample experiment conversion dataset_property_1 dataset_property_2\n";
+
+    private static final String TAB = "\t";
+
+    @Test
+    public void testLoadIndexFile()
+    {
+        File indexFile = writeFile(HEADER + "data.txt sample1 experiment1 conversion1 v1 v2");
+        List<PlainDataSetInformation> list = DataSetInformationParser.parse(indexFile);
+        AssertJUnit.assertEquals(1, list.size());
+        PlainDataSetInformation elem = list.get(0);
+        AssertJUnit.assertEquals("data.txt", elem.getFileName());
+        AssertJUnit.assertEquals(2, elem.getProperties().size());
+        NewProperty prop1 = elem.getProperties().get(0);
+        AssertJUnit.assertEquals("v1", prop1.getValue());
+        AssertJUnit.assertEquals("dataset_property_1", prop1.getPropertyCode());
+    }
+
+    // TODO 2009-05-25, Tomasz Pylak: remove from broken after LMS-914 is fixed
+    @Test(expectedExceptions = MandatoryPropertyMissingException.class, groups = "broken")
+    public void testLoadIndexFileWithMissingFieldValueFails()
+    {
+        File indexFile = writeFile(HEADER + TAB + TAB + TAB + TAB + TAB);
+        DataSetInformationParser.parse(indexFile);
+    }
+
+    @Test(expectedExceptions = MandatoryPropertyMissingException.class)
+    public void testLoadIndexFileWithMissingFieldHeaderFails()
+    {
+        File indexFile = writeFile("xxx");
+        DataSetInformationParser.parse(indexFile);
+    }
+
+    private File writeFile(String content)
+    {
+        String contentWithTabs = spacesToTabs(content);
+        File indexFile = new File(workingDirectory, "index.tsv");
+        FileUtilities.writeToFile(indexFile, contentWithTabs);
+        return indexFile;
+    }
+
+    private static String spacesToTabs(String text)
+    {
+        return text.replaceAll(" ", TAB);
+    }
+}
diff --git a/rtd_yeastx/sourceTest/java/tests.xml b/rtd_yeastx/sourceTest/java/tests.xml
new file mode 100644
index 00000000000..f186f91b1ea
--- /dev/null
+++ b/rtd_yeastx/sourceTest/java/tests.xml
@@ -0,0 +1,12 @@
+<suite name="All" verbose="2" >
+  <test name="All" annotations="JDK">
+    <groups>
+      <run>
+        <exclude name="broken" />
+      </run>
+    </groups>
+    <packages>
+      <package name="ch.systemsx.cisd.yeastx.*" />
+   </packages>
+ </test>
+</suite>
-- 
GitLab