From dc8d40c61b97e8c2c38c992a0c05604c0c283fc1 Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Fri, 20 May 2011 07:48:37 +0000
Subject: [PATCH] [LMS-2104] tests for creation of virtual content by content
 provider

SVN: 21405
---
 .../HierarchicalContentProviderTest.java      | 108 ++++++++++++++++++
 .../shared/basic/dto/ContainerDataSet.java    |  12 ++
 .../generic/shared/basic/dto/DataSet.java     |   9 +-
 .../shared/basic/dto/ExternalData.java        |  20 +---
 4 files changed, 132 insertions(+), 17 deletions(-)

diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/HierarchicalContentProviderTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/HierarchicalContentProviderTest.java
index 2f581e5fb97..81e1c417473 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/HierarchicalContentProviderTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/HierarchicalContentProviderTest.java
@@ -17,6 +17,8 @@
 package ch.systemsx.cisd.openbis.dss.generic.shared;
 
 import java.io.File;
+import java.util.Arrays;
+import java.util.List;
 
 import org.jmock.Expectations;
 import org.jmock.Mockery;
@@ -25,9 +27,12 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import ch.systemsx.cisd.common.io.IHierarchicalContent;
 import ch.systemsx.cisd.common.io.IHierarchicalContentFactory;
+import ch.systemsx.cisd.common.io.IHierarchicalContentNode;
 import ch.systemsx.cisd.common.test.RecordingMatcher;
 import ch.systemsx.cisd.common.utilities.IDelegatedAction;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.ContainerDataSet;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSet;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.IDatasetLocation;
 
@@ -119,6 +124,77 @@ public class HierarchicalContentProviderTest extends AssertJUnit
         context.assertIsSatisfied();
     }
 
+    @Test
+    void testAsContentFromContainerCode()
+    {
+        final String containerCode = "CONTAINER_CODE";
+        final ContainerDataSet container = new ContainerDataSet();
+        container.setCode(containerCode);
+
+        final DataSet dataSet1 = new DataSet();
+        final DataSet dataSet2 = new DataSet();
+        final String componentCode1 = "DS_CODE_1";
+        final String componentCode2 = "DS_CODE_2";
+        dataSet1.setCode(componentCode1);
+        dataSet2.setCode(componentCode2);
+        final File dataSetRootFile1 = new File("DS_FILE_1");
+        final File dataSetRootFile2 = new File("DS_FILE_2");
+
+        container.getContainedDataSets().add(dataSet1);
+        container.getContainedDataSets().add(dataSet2);
+
+        final RecordingMatcher<IDelegatedAction> actionMatcher = RecordingMatcher.create();
+        context.checking(new Expectations()
+            {
+                {
+                    one(openbisService).tryGetDataSet(containerCode);
+                    will(returnValue(container));
+
+                    one(shareIdManager).lock(componentCode1);
+                    one(directoryProvider).getDataSetDirectory(dataSet1);
+                    will(returnValue(dataSetRootFile1));
+
+                    one(shareIdManager).lock(componentCode2);
+                    one(directoryProvider).getDataSetDirectory(dataSet2);
+                    will(returnValue(dataSetRootFile2));
+
+                    IHierarchicalContent content1 = new DummyHierarchicalContent();
+                    IHierarchicalContent content2 = new DummyHierarchicalContent();
+                    one(hierarchicalContentFactory).asHierarchicalContent(
+                            with(same(dataSetRootFile1)), with(actionMatcher));
+                    will(returnValue(content1));
+                    one(hierarchicalContentFactory).asHierarchicalContent(
+                            with(same(dataSetRootFile2)), with(actionMatcher));
+                    will(returnValue(content2));
+
+                    one(hierarchicalContentFactory).asVirtualHierarchicalContent(
+                            Arrays.asList(content1, content2));
+                }
+            });
+
+        hierarchicalContentProvider.asContent(containerCode);
+        context.assertIsSatisfied();
+
+        // check that locks are released on execution of recorded actions
+        assertEquals(2, actionMatcher.getRecordedObjects().size());
+        context.checking(new Expectations()
+            {
+                {
+                    one(shareIdManager).releaseLock(componentCode1);
+                }
+            });
+        actionMatcher.getRecordedObjects().get(0).execute();
+        context.checking(new Expectations()
+            {
+                {
+                    one(shareIdManager).releaseLock(componentCode2);
+                }
+            });
+        actionMatcher.getRecordedObjects().get(1).execute();
+
+        context.assertIsSatisfied();
+    }
+
     @Test
     void testAsContentFromFakeDataSetCodeFails()
     {
@@ -212,4 +288,36 @@ public class HierarchicalContentProviderTest extends AssertJUnit
 
     }
 
+    private static class DummyHierarchicalContent implements IHierarchicalContent
+    {
+
+        public IHierarchicalContentNode getRootNode()
+        {
+            return null;
+        }
+
+        public IHierarchicalContentNode getNode(String relativePath)
+                throws IllegalArgumentException
+        {
+            return null;
+        }
+
+        public List<IHierarchicalContentNode> listMatchingNodes(String relativePathPattern)
+        {
+            return null;
+        }
+
+        public List<IHierarchicalContentNode> listMatchingNodes(String startingPath,
+                String fileNamePattern)
+        {
+            return null;
+        }
+
+        public void close()
+        {
+
+        }
+
+    }
+
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ContainerDataSet.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ContainerDataSet.java
index c12e211c8ed..17dda2141d4 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ContainerDataSet.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ContainerDataSet.java
@@ -31,6 +31,18 @@ public class ContainerDataSet extends ExternalData
 
     private List<ExternalData> containedDataSets = new ArrayList<ExternalData>();
 
+    @Override
+    public boolean isContainer()
+    {
+        return true;
+    }
+
+    @Override
+    public ContainerDataSet tryGetAsContainerDataSet()
+    {
+        return this;
+    }
+
     public List<ExternalData> getContainedDataSets()
     {
         return containedDataSets;
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DataSet.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DataSet.java
index d690ac0cfbe..ec71fd0100d 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DataSet.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/DataSet.java
@@ -16,7 +16,6 @@
 
 package ch.systemsx.cisd.openbis.generic.shared.basic.dto;
 
-
 /**
  * A simple (non-container aka non-virtual) data set.
  * 
@@ -28,7 +27,7 @@ public class DataSet extends ExternalData implements IDatasetLocation
     private static final long serialVersionUID = ServiceVersionHolder.VERSION;
 
     private Boolean complete;
-    
+
     private String shareId;
 
     private String location;
@@ -41,6 +40,12 @@ public class DataSet extends ExternalData implements IDatasetLocation
 
     private FileFormatType fileFormatType;
 
+    @Override
+    public DataSet tryGetAsDataSet()
+    {
+        return this;
+    }
+
     @Override
     public Boolean getComplete()
     {
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ExternalData.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ExternalData.java
index 92a20655689..0a0acf8490c 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ExternalData.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/shared/basic/dto/ExternalData.java
@@ -97,33 +97,23 @@ public class ExternalData extends CodeWithRegistration<ExternalData> implements
      */
     public boolean isContainer()
     {
-        return dataSetType != null && dataSetType.isContainerType();
+        return false; // overriden in subclasses
     }
 
     /**
-     * Tries to cast the current object to {@link DataSet}. Will return non-null values for plain
-     * non-container data sets.
+     * Will return non-null values for plain non-container data sets.
      */
     public DataSet tryGetAsDataSet()
     {
-        if (this instanceof DataSet)
-        {
-            return (DataSet) this;
-        }
-        return null;
+        return null; // overriden in subclasses
     }
 
     /**
-     * Tries to cast the current object to {@link ContainerDataSet}. Returns null if the data set is
-     * not a container data set.
+     * Returns null if the data set is not a container data set, otherwise returns the container.
      */
     public ContainerDataSet tryGetAsContainerDataSet()
     {
-        if (this instanceof ContainerDataSet)
-        {
-            return (ContainerDataSet) this;
-        }
-        return null;
+        return null; // overriden in subclasses
     }
 
     public String getPermlink()
-- 
GitLab