From d00e343118fc2cf00a4a704f115a8aa75db474b3 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 1 Jul 2011 08:18:31 +0000
Subject: [PATCH] [LMS-2301] tests for reporting/processing plugins using
 scripts part2

SVN: 21945
---
 .../JythonBasedProcessingPluginTest.java      | 211 +++++++++++++++++
 .../JythonBasedReportingPluginTest.java       | 215 ++++++++++++++++++
 2 files changed, 426 insertions(+)
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedProcessingPluginTest.java
 create mode 100644 datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedReportingPluginTest.java

diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedProcessingPluginTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedProcessingPluginTest.java
new file mode 100644
index 00000000000..8b3dbad41ab
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedProcessingPluginTest.java
@@ -0,0 +1,211 @@
+/*
+ * 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.openbis.dss.generic.server.plugins.jython;
+
+import java.io.File;
+import java.util.Arrays;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
+import ch.systemsx.cisd.common.evaluator.EvaluatorException;
+import ch.systemsx.cisd.common.exceptions.Status;
+import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContent;
+import ch.systemsx.cisd.common.logging.LogInitializer;
+import ch.systemsx.cisd.common.test.RecordingMatcher;
+import ch.systemsx.cisd.openbis.dss.generic.server.plugins.jython.api.IDataSet;
+import ch.systemsx.cisd.openbis.dss.generic.shared.DataSetProcessingContext;
+import ch.systemsx.cisd.openbis.dss.generic.shared.IHierarchicalContentProvider;
+import ch.systemsx.cisd.openbis.dss.generic.shared.ProcessingStatus;
+import ch.systemsx.cisd.openbis.generic.shared.dto.DatasetDescription;
+
+/**
+ * Tests for {@link JythonBasedProcessingPlugin} class.
+ * 
+ * @author Piotr Buczek
+ */
+public class JythonBasedProcessingPluginTest extends AbstractFileSystemTestCase
+{
+    private Mockery context;
+
+    private IHierarchicalContentProvider hierarchicalContentProvider;
+
+    private IPluginScriptRunnerFactory scriptRunnerFactory;
+
+    private IProcessingPluginScriptRunner processingPluginScriptRunner;
+
+    private DataSetProcessingContext processingContext;
+
+    private File store;
+
+    private DatasetDescription datasetDescription1;
+
+    private DatasetDescription datasetDescription2;
+
+    private IHierarchicalContent content1;
+
+    private IHierarchicalContent content2;
+
+    @BeforeMethod
+    public void beforeMethod()
+    {
+        LogInitializer.init();
+        store = new File(workingDirectory, "store");
+        store.mkdirs();
+
+        datasetDescription1 = createDatasetDescription(1);
+        datasetDescription2 = createDatasetDescription(2);
+        context = new Mockery();
+        scriptRunnerFactory = context.mock(IPluginScriptRunnerFactory.class);
+        processingPluginScriptRunner = context.mock(IProcessingPluginScriptRunner.class);
+        hierarchicalContentProvider = context.mock(IHierarchicalContentProvider.class);
+        content1 = context.mock(IHierarchicalContent.class, "content1");
+        content2 = context.mock(IHierarchicalContent.class, "content2");
+    }
+
+    @AfterMethod
+    public void afterTest()
+    {
+        context.assertIsSatisfied();
+    }
+
+    private JythonBasedProcessingPlugin createPlugin()
+    {
+        return new JythonBasedProcessingPlugin(scriptRunnerFactory, hierarchicalContentProvider);
+    }
+
+    @Test
+    public void testCreateReportHappyCase()
+    {
+        final JythonBasedProcessingPlugin plugin = createPlugin();
+        final RecordingMatcher<IDataSet> iDataSetMatcher = new RecordingMatcher<IDataSet>();
+        commonPrepare();
+        context.checking(new Expectations()
+            {
+                {
+                    exactly(2).of(processingPluginScriptRunner).process(with(iDataSetMatcher));
+                    will(returnValue(Status.OK));
+                }
+            });
+        ProcessingStatus status =
+                plugin.process(Arrays.asList(datasetDescription1, datasetDescription2),
+                        processingContext);
+        assertEquals("[code1, code2]", status.getDatasetsByStatus(Status.OK).toString());
+        // verify data sets
+        assertEquals(2, iDataSetMatcher.getRecordedObjects().size());
+        verifyDataset(content1, 1, iDataSetMatcher.getRecordedObjects().get(0));
+        verifyDataset(content2, 2, iDataSetMatcher.getRecordedObjects().get(1));
+
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public void testCreateReportErrorHandling()
+    {
+        final JythonBasedProcessingPlugin plugin = createPlugin();
+        // same as testCreateReport() but with an EvaluatorException expected in describe method
+        commonPrepare();
+        final RecordingMatcher<IDataSet> iDataSetMatcher = new RecordingMatcher<IDataSet>();
+        context.checking(new Expectations()
+            {
+                {
+                    one(processingPluginScriptRunner).process(with(iDataSetMatcher));
+                    will(throwException(new EvaluatorException("blabla")));
+                }
+            });
+        try
+        {
+            plugin.process(Arrays.asList(datasetDescription1, datasetDescription2),
+                    processingContext);
+            fail("Expected EvaluatorException");
+        } catch (EvaluatorException ex)
+        {
+            assertEquals("blabla", ex.getMessage());
+        }
+
+        context.assertIsSatisfied();
+    }
+
+    private void commonPrepare()
+    {
+        context.checking(new Expectations()
+            {
+                {
+                    one(scriptRunnerFactory).createProcessingPluginRunner(processingContext);
+                    will(returnValue(processingPluginScriptRunner));
+
+                    one(hierarchicalContentProvider).asContent("code1");
+                    will(returnValue(content1));
+                    one(hierarchicalContentProvider).asContent("code2");
+                    will(returnValue(content2));
+
+                    // in the end all contents should be closed automatically
+                    // (both for success and failure)
+                    one(content1).close();
+                    one(content2).close();
+                }
+            });
+    }
+
+    private static DatasetDescription createDatasetDescription(int nr)
+    {
+        DatasetDescription result = new DatasetDescription();
+        result.setDatabaseInstanceCode("databaseInstanceCode" + nr);
+        result.setDataSetCode("code" + nr);
+        result.setDataSetLocation("dataSetLocation" + nr);
+        result.setDataSetSize(new Long(nr));
+        result.setDatasetTypeCode("datasetTypeCode" + nr);
+        result.setExperimentCode("experimentCode" + nr);
+        result.setExperimentIdentifier("experimentIdentifier" + nr);
+        result.setExperimentTypeCode("experimentTypeCode" + nr);
+        result.setMainDataSetPath("mainDataSetPath" + nr);
+        result.setMainDataSetPattern("mainDataSetPattern" + nr);
+        result.setProjectCode("projectCode" + nr);
+        result.setSampleCode("sampleCode" + nr);
+        result.setSampleIdentifier("sampleIdentifier" + nr);
+        result.setSampleTypeCode("sampleTypeCode" + nr);
+        result.setSpaceCode("spaceCode" + nr);
+        result.setSpeedHint(nr);
+        return result;
+    }
+
+    private static void verifyDataset(IHierarchicalContent expectedContent, int expectedNr,
+            IDataSet dataSet)
+    {
+        assertSame(expectedContent, dataSet.getContent());
+        assertEquals("databaseInstanceCode" + expectedNr, dataSet.getDatabaseInstanceCode());
+        assertEquals("code" + expectedNr, dataSet.getDataSetCode());
+        assertEquals("dataSetLocation" + expectedNr, dataSet.getDataSetLocation());
+        assertEquals(new Long(expectedNr), dataSet.getDataSetSize());
+        assertEquals("datasetTypeCode" + expectedNr, dataSet.getDataSetTypeCode());
+        assertEquals("experimentCode" + expectedNr, dataSet.getExperimentCode());
+        assertEquals("experimentIdentifier" + expectedNr, dataSet.getExperimentIdentifier());
+        assertEquals("experimentTypeCode" + expectedNr, dataSet.getExperimentTypeCode());
+        assertEquals("mainDataSetPath" + expectedNr, dataSet.getMainDataSetPath());
+        assertEquals("mainDataSetPattern" + expectedNr, dataSet.getMainDataSetPattern());
+        assertEquals("projectCode" + expectedNr, dataSet.getProjectCode());
+        assertEquals("sampleCode" + expectedNr, dataSet.getSampleCode());
+        assertEquals("sampleIdentifier" + expectedNr, dataSet.getSampleIdentifier());
+        assertEquals("sampleTypeCode" + expectedNr, dataSet.getSampleTypeCode());
+        assertEquals("spaceCode" + expectedNr, dataSet.getSpaceCode());
+        assertEquals(expectedNr, dataSet.getSpeedHint());
+    }
+}
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedReportingPluginTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedReportingPluginTest.java
new file mode 100644
index 00000000000..9d625ee4157
--- /dev/null
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/jython/JythonBasedReportingPluginTest.java
@@ -0,0 +1,215 @@
+/*
+ * 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.openbis.dss.generic.server.plugins.jython;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
+import ch.systemsx.cisd.common.evaluator.EvaluatorException;
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
+import ch.systemsx.cisd.common.io.hierarchical_content.api.IHierarchicalContent;
+import ch.systemsx.cisd.common.logging.LogInitializer;
+import ch.systemsx.cisd.common.test.RecordingMatcher;
+import ch.systemsx.cisd.openbis.dss.generic.server.plugins.jython.api.IDataSet;
+import ch.systemsx.cisd.openbis.dss.generic.shared.DataSetProcessingContext;
+import ch.systemsx.cisd.openbis.dss.generic.shared.IHierarchicalContentProvider;
+import ch.systemsx.cisd.openbis.generic.shared.dto.DatasetDescription;
+import ch.systemsx.cisd.openbis.generic.shared.managed_property.api.ISimpleTableModelBuilderAdaptor;
+
+/**
+ * Tests for {@link JythonBasedReportingPlugin} class.
+ * 
+ * @author Piotr Buczek
+ */
+public class JythonBasedReportingPluginTest extends AbstractFileSystemTestCase
+{
+    private Mockery context;
+
+    private IHierarchicalContentProvider hierarchicalContentProvider;
+
+    private IPluginScriptRunnerFactory scriptRunnerFactory;
+
+    private IReportingPluginScriptRunner reportingPluginScriptRunner;
+
+    private DataSetProcessingContext processingContext;
+
+    private File store;
+
+    private DatasetDescription datasetDescription1;
+
+    private DatasetDescription datasetDescription2;
+
+    private IHierarchicalContent content1;
+
+    private IHierarchicalContent content2;
+
+    @BeforeMethod
+    public void beforeMethod()
+    {
+        LogInitializer.init();
+        store = new File(workingDirectory, "store");
+        store.mkdirs();
+
+        datasetDescription1 = createDatasetDescription(1);
+        datasetDescription2 = createDatasetDescription(2);
+        context = new Mockery();
+        scriptRunnerFactory = context.mock(IPluginScriptRunnerFactory.class);
+        reportingPluginScriptRunner = context.mock(IReportingPluginScriptRunner.class);
+        hierarchicalContentProvider = context.mock(IHierarchicalContentProvider.class);
+        content1 = context.mock(IHierarchicalContent.class, "content1");
+        content2 = context.mock(IHierarchicalContent.class, "content2");
+    }
+
+    @AfterMethod
+    public void afterTest()
+    {
+        context.assertIsSatisfied();
+    }
+
+    private JythonBasedReportingPlugin createPlugin()
+    {
+        return new JythonBasedReportingPlugin(new Properties(), store, scriptRunnerFactory,
+                hierarchicalContentProvider);
+    }
+
+    @Test
+    public void testCreateReportHappyCase()
+    {
+        final JythonBasedReportingPlugin plugin = createPlugin();
+        final RecordingMatcher<List<IDataSet>> iDataSetsMatcher =
+                new RecordingMatcher<List<IDataSet>>();
+        commonPrepare();
+        context.checking(new Expectations()
+            {
+                {
+                    one(reportingPluginScriptRunner).describe(with(iDataSetsMatcher),
+                            with(any(ISimpleTableModelBuilderAdaptor.class)));
+                }
+            });
+        plugin.createReport(Arrays.asList(datasetDescription1, datasetDescription2),
+                processingContext);
+        // verify data sets
+        assertEquals(2, iDataSetsMatcher.recordedObject().size());
+        verifyDataset(content1, 1, iDataSetsMatcher.recordedObject().get(0));
+        verifyDataset(content2, 2, iDataSetsMatcher.recordedObject().get(1));
+
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public void testCreateReportErrorHandling()
+    {
+        final JythonBasedReportingPlugin plugin = createPlugin();
+        // same as testCreateReport() but with an EvaluatorException expected in describe method
+        commonPrepare();
+        final RecordingMatcher<List<IDataSet>> iDataSetsMatcher =
+                new RecordingMatcher<List<IDataSet>>();
+        context.checking(new Expectations()
+            {
+                {
+                    one(reportingPluginScriptRunner).describe(with(iDataSetsMatcher),
+                            with(any(ISimpleTableModelBuilderAdaptor.class)));
+                    will(throwException(new EvaluatorException("blabla")));
+                }
+            });
+        try
+        {
+            plugin.createReport(Arrays.asList(datasetDescription1, datasetDescription2),
+                    processingContext);
+            fail("Expected UserFailureException");
+        } catch (UserFailureException ex)
+        {
+            assertEquals("Chosen plugin failed to create a report.", ex.getMessage());
+        }
+
+        context.assertIsSatisfied();
+    }
+
+    private void commonPrepare()
+    {
+        context.checking(new Expectations()
+            {
+                {
+                    one(scriptRunnerFactory).createReportingPluginRunner(processingContext);
+                    will(returnValue(reportingPluginScriptRunner));
+
+                    one(hierarchicalContentProvider).asContent("code1");
+                    will(returnValue(content1));
+                    one(hierarchicalContentProvider).asContent("code2");
+                    will(returnValue(content2));
+
+                    // in the end all contents should be closed automatically
+                    // (both for success and failure)
+                    one(content1).close();
+                    one(content2).close();
+                }
+            });
+    }
+
+    private static DatasetDescription createDatasetDescription(int nr)
+    {
+        DatasetDescription result = new DatasetDescription();
+        result.setDatabaseInstanceCode("databaseInstanceCode" + nr);
+        result.setDataSetCode("code" + nr);
+        result.setDataSetLocation("dataSetLocation" + nr);
+        result.setDataSetSize(new Long(nr));
+        result.setDatasetTypeCode("datasetTypeCode" + nr);
+        result.setExperimentCode("experimentCode" + nr);
+        result.setExperimentIdentifier("experimentIdentifier" + nr);
+        result.setExperimentTypeCode("experimentTypeCode" + nr);
+        result.setMainDataSetPath("mainDataSetPath" + nr);
+        result.setMainDataSetPattern("mainDataSetPattern" + nr);
+        result.setProjectCode("projectCode" + nr);
+        result.setSampleCode("sampleCode" + nr);
+        result.setSampleIdentifier("sampleIdentifier" + nr);
+        result.setSampleTypeCode("sampleTypeCode" + nr);
+        result.setSpaceCode("spaceCode" + nr);
+        result.setSpeedHint(nr);
+        return result;
+    }
+
+    private static void verifyDataset(IHierarchicalContent expectedContent, int expectedNr,
+            IDataSet dataSet)
+    {
+        assertSame(expectedContent, dataSet.getContent());
+        assertEquals("databaseInstanceCode" + expectedNr, dataSet.getDatabaseInstanceCode());
+        assertEquals("code" + expectedNr, dataSet.getDataSetCode());
+        assertEquals("dataSetLocation" + expectedNr, dataSet.getDataSetLocation());
+        assertEquals(new Long(expectedNr), dataSet.getDataSetSize());
+        assertEquals("datasetTypeCode" + expectedNr, dataSet.getDataSetTypeCode());
+        assertEquals("experimentCode" + expectedNr, dataSet.getExperimentCode());
+        assertEquals("experimentIdentifier" + expectedNr, dataSet.getExperimentIdentifier());
+        assertEquals("experimentTypeCode" + expectedNr, dataSet.getExperimentTypeCode());
+        assertEquals("mainDataSetPath" + expectedNr, dataSet.getMainDataSetPath());
+        assertEquals("mainDataSetPattern" + expectedNr, dataSet.getMainDataSetPattern());
+        assertEquals("projectCode" + expectedNr, dataSet.getProjectCode());
+        assertEquals("sampleCode" + expectedNr, dataSet.getSampleCode());
+        assertEquals("sampleIdentifier" + expectedNr, dataSet.getSampleIdentifier());
+        assertEquals("sampleTypeCode" + expectedNr, dataSet.getSampleTypeCode());
+        assertEquals("spaceCode" + expectedNr, dataSet.getSpaceCode());
+        assertEquals(expectedNr, dataSet.getSpeedHint());
+    }
+}
-- 
GitLab