From 841e1c86f9ce697c4cf659d9df7c838d8581ad2c Mon Sep 17 00:00:00 2001
From: tpylak <tpylak>
Date: Wed, 17 Dec 2008 09:05:44 +0000
Subject: [PATCH] LMS-680 System and unit tests for sample export

SVN: 9405
---
 .../web/server/resultset/CacheManager.java    | 21 -----
 .../web/server/AbstractClientServiceTest.java |  4 +-
 .../web/server/CommonClientServiceTest.java   | 67 ++++++++++++--
 .../web/server/util/TSVRendererTest.java      | 89 +++++++++++++++++++
 .../web/server/GenericClientServiceTest.java  |  2 +-
 5 files changed, 150 insertions(+), 33 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/util/TSVRendererTest.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CacheManager.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CacheManager.java
index b22beeb18a8..9cdc013eddc 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CacheManager.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/client/web/server/resultset/CacheManager.java
@@ -83,27 +83,6 @@ public class CacheManager<K, T>
         }
     }
 
-    public static final class CounterBasedResultSetKeyGenerator implements
-            IResultSetKeyGenerator<Integer>
-    {
-        private static final long serialVersionUID = 1L;
-
-        private int counter;
-
-        public CounterBasedResultSetKeyGenerator()
-        {
-        }
-
-        //
-        // IResultSetKeyProvider
-        //
-
-        public final Integer createKey()
-        {
-            return counter++;
-        }
-    }
-
     public static final class TokenBasedResultSetKeyGenerator implements
             IResultSetKeyGenerator<String>
     {
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/AbstractClientServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/AbstractClientServiceTest.java
index 4126c2e1167..0e7b3ed2b1a 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/AbstractClientServiceTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/AbstractClientServiceTest.java
@@ -52,7 +52,7 @@ public abstract class AbstractClientServiceTest
 
     protected Session session;
 
-    protected final void prepareGetSession(final Expectations expectations)
+    protected final void prepareGetHttpSession(final Expectations expectations)
     {
         expectations.allowing(requestContextProvider).getHttpServletRequest();
         expectations.will(Expectations.returnValue(servletRequest));
@@ -75,7 +75,7 @@ public abstract class AbstractClientServiceTest
 
     protected final void prepareGetSessionToken(final Expectations expectations)
     {
-        prepareGetSession(expectations);
+        prepareGetHttpSession(expectations);
 
         expectations.allowing(httpSession).getAttribute(
                 SessionConstants.OPENBIS_SESSION_ATTRIBUTE_KEY);
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/CommonClientServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/CommonClientServiceTest.java
index db51b23de40..c20c2740e16 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/CommonClientServiceTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/CommonClientServiceTest.java
@@ -32,8 +32,12 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyType;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ResultSet;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Sample;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleType;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.TableExportCriteria;
+import ch.systemsx.cisd.openbis.generic.client.web.server.resultset.CacheManager;
 import ch.systemsx.cisd.openbis.generic.client.web.server.resultset.DefaultResultSet;
 import ch.systemsx.cisd.openbis.generic.client.web.server.resultset.IOriginalDataProvider;
+import ch.systemsx.cisd.openbis.generic.client.web.server.resultset.IResultSetKeyGenerator;
+import ch.systemsx.cisd.openbis.generic.server.SessionConstants;
 import ch.systemsx.cisd.openbis.generic.shared.ICommonServer;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DataTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityDataType;
@@ -102,21 +106,14 @@ public final class CommonClientServiceTest extends AbstractClientServiceTest
         context.checking(new Expectations()
             {
                 {
-                    prepareGetSession(this);
+                    prepareGetHttpSession(this);
                     prepareGetSessionToken(this);
                     prepareGetResultSetManager(this);
 
                     one(resultSetManager).getResultSet(with(listCriteria),
-                            getOriginalDataProvider());
+                            getAnyOriginalDataProvider(this));
                     will(returnValue(defaultResultSet));
                 }
-
-                @SuppressWarnings("unchecked")
-                private final IOriginalDataProvider<Sample> getOriginalDataProvider()
-                {
-                    return with(any(IOriginalDataProvider.class));
-                }
-
             });
         final ResultSet<Sample> resultSet = commonClientService.listSamples(listCriteria);
         assertEquals(0, resultSet.getList().size());
@@ -146,6 +143,58 @@ public final class CommonClientServiceTest extends AbstractClientServiceTest
         context.assertIsSatisfied();
     }
 
+    @Test
+    public final void testPrepareExportSamples()
+    {
+        final TableExportCriteria<Sample> criteria = new TableExportCriteria<Sample>();
+        final CacheManager<String, TableExportCriteria<Sample>> manager = createCacheManager();
+        context.checking(new Expectations()
+            {
+                {
+                    prepareGetSessionToken(this);
+                    prepareGetCacheManager(this, manager);
+                }
+            });
+        final String key = commonClientService.prepareExportSamples(criteria);
+        assertEquals("" + CounterBasedResultSetKeyGenerator.INIT_VALUE, key);
+        assertEquals(criteria, manager.tryGetData(key));
+        context.assertIsSatisfied();
+    }
+
+    private void prepareGetCacheManager(Expectations exp,
+            CacheManager<String, TableExportCriteria<Sample>> manager)
+    {
+        prepareGetHttpSession(exp);
+        exp.allowing(httpSession).getAttribute(SessionConstants.OPENBIS_EXPORT_MANAGER);
+        exp.will(Expectations.returnValue(manager));
+    }
+
+    @SuppressWarnings("unchecked")
+    private final IOriginalDataProvider<Sample> getAnyOriginalDataProvider(Expectations exp)
+    {
+        return exp.with(Expectations.any(IOriginalDataProvider.class));
+    }
+
+    private <T> CacheManager<String, T> createCacheManager()
+    {
+        return new CacheManager<String, T>(new CounterBasedResultSetKeyGenerator());
+    }
+
+    private static final class CounterBasedResultSetKeyGenerator implements
+            IResultSetKeyGenerator<String>
+    {
+        public static final int INIT_VALUE = 123;
+
+        private static final long serialVersionUID = 1L;
+
+        private int counter = INIT_VALUE;
+
+        public final String createKey()
+        {
+            return "" + counter++;
+        }
+    }
+
     @Test
     public final void testRegisterPropertyType()
     {
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/util/TSVRendererTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/util/TSVRendererTest.java
new file mode 100644
index 00000000000..b3db5f9261f
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/client/web/server/util/TSVRendererTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.openbis.generic.client.web.server.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.IColumnDefinition;
+
+/**
+ * Tests of {@link TSVRenderer}
+ * 
+ * @author Tomasz Pylak
+ */
+public class TSVRendererTest
+{
+    @Test
+    public void testRenderer()
+    {
+        List<IColumnDefinition<String[]>> columnDefs = createColumnDefs(2);
+        List<String[]> entities = new ArrayList<String[]>();
+        entities.add(new String[]
+            { "x", "y" });
+        entities.add(new String[]
+            { "a", "b" });
+        String content = TSVRenderer.createTable(entities, columnDefs);
+        Assert.assertEquals("h0\th1\nx\ty\na\tb\n", content);
+    }
+
+    private static List<IColumnDefinition<String[]>> createColumnDefs(int colNum)
+    {
+        List<IColumnDefinition<String[]>> columnDefs = new ArrayList<IColumnDefinition<String[]>>();
+        for (int i = 0; i < colNum; i++)
+        {
+            columnDefs.add(createColDef(i));
+        }
+        return columnDefs;
+    }
+
+    @Test
+    public void testRendererNoRows()
+    {
+        List<IColumnDefinition<String[]>> columnDefs = createColumnDefs(2);
+        List<String[]> entities = new ArrayList<String[]>();
+        String content = TSVRenderer.createTable(entities, columnDefs);
+        Assert.assertEquals("h0\th1\n", content);
+    }
+
+    private static IColumnDefinition<String[]> createColDef(final int colIx)
+    {
+        return new IColumnDefinition<String[]>()
+            {
+
+                public String getHeader()
+                {
+                    return "h" + colIx;
+                }
+
+                public String getIdentifier()
+                {
+                    return null;
+                }
+
+                public String getValue(String[] rowModel)
+                {
+                    return rowModel[colIx];
+                }
+
+            };
+    }
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientServiceTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientServiceTest.java
index a2f20ff6bba..d3f052b3174 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientServiceTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/plugin/generic/client/web/server/GenericClientServiceTest.java
@@ -144,7 +144,7 @@ public final class GenericClientServiceTest extends AbstractClientServiceTest
         context.checking(new Expectations()
             {
                 {
-                    prepareGetSession(this);
+                    prepareGetHttpSession(this);
                     prepareGetSessionToken(this);
 
                     allowing(httpSession).getAttribute(sessionKey);
-- 
GitLab