From 451a0a7ff01de16a579ed44074bd3376e548d52c Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Tue, 13 Oct 2009 13:09:53 +0000
Subject: [PATCH] LMS-1222 FilterSystemTest

SVN: 12933
---
 .../server/dataaccess/db/AbstractDAOTest.java |  13 +--
 .../dataaccess/db/HibernateSearchDAOTest.java |   3 +-
 .../generic/server/util/TestInitializer.java  |  42 ++++++++
 .../systemtest/AuthenticationSystemTest.java  |  45 ++++++++
 .../openbis/systemtest/FilterSystemTest.java  | 101 ++++++++++++++++++
 .../openbis/systemtest/SystemTestCase.java    |  93 ++++++++++++++++
 6 files changed, 285 insertions(+), 12 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/util/TestInitializer.java
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AuthenticationSystemTest.java
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/FilterSystemTest.java
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java

diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAOTest.java
index 4b85b5961a4..7a33f905a4b 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAOTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/AbstractDAOTest.java
@@ -38,10 +38,10 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 
 import ch.rinn.restrictions.Friend;
-import ch.systemsx.cisd.common.logging.LogInitializer;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IExternalDataDAO;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.HibernateSearchContext;
+import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.dto.AuthorizationGroupPE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DataTypePE;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
@@ -75,18 +75,9 @@ import ch.systemsx.cisd.openbis.generic.shared.util.UuidUtil;
     { AbstractDAO.class })
 public abstract class AbstractDAOTest extends AbstractTransactionalTestNGSpringContextTests
 {
-    static final String LUCENE_INDEX_PATH = "targets/lucene/indices";
-
     static
     {
-        LogInitializer.init();
-        System.setProperty("database.create-from-scratch", "true");
-        System.setProperty("database.kind", "test");
-        System.setProperty("script-folder", "sourceTest");
-        System.setProperty("hibernate.search.index-mode", "NO_INDEX");
-        System.setProperty("hibernate.search.index-base", LUCENE_INDEX_PATH);
-        System.setProperty("hibernate.search.worker.execution", "sync");
-        System.setProperty("mass-upload-folder", "sourceTest/sql/postgresql");
+        TestInitializer.init();
     }
 
     static final Long ANOTHER_DATABASE_INSTANCE_ID = new Long(2);
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/HibernateSearchDAOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/HibernateSearchDAOTest.java
index f03c9a9d876..0a224a1486e 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/HibernateSearchDAOTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/db/HibernateSearchDAOTest.java
@@ -44,6 +44,7 @@ import ch.systemsx.cisd.common.filesystem.FileUtilities;
 import ch.systemsx.cisd.common.test.AssertionUtil;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.IHibernateSearchDAO;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.search.FullTextIndexerRunnable;
+import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetAttributeSearchFieldKind;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DetailedSearchCriteria;
@@ -84,7 +85,7 @@ public final class HibernateSearchDAOTest extends AbstractDAOTest
     // create a fresh copy of the Lucene index
     private static void restoreSearchIndex()
     {
-        File targetPath = new File(LUCENE_INDEX_PATH);
+        File targetPath = new File(TestInitializer.LUCENE_INDEX_PATH);
         FileUtilities.deleteRecursively(targetPath);
         targetPath.mkdirs();
         File srcPath = new File(LUCENE_INDEX_TEMPLATE_PATH);
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/util/TestInitializer.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/util/TestInitializer.java
new file mode 100644
index 00000000000..7b13d6e5a6d
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/util/TestInitializer.java
@@ -0,0 +1,42 @@
+/*
+ * 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.generic.server.util;
+
+import ch.systemsx.cisd.common.logging.LogInitializer;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class TestInitializer
+{
+    public static final String LUCENE_INDEX_PATH = "targets/lucene/indices";
+    
+    public static void init()
+    {
+        LogInitializer.init();
+        System.setProperty("database.create-from-scratch", "true");
+        System.setProperty("database.kind", "test");
+        System.setProperty("script-folder", "sourceTest");
+        System.setProperty("hibernate.search.index-mode", "NO_INDEX");
+        System.setProperty("hibernate.search.index-base", LUCENE_INDEX_PATH);
+        System.setProperty("hibernate.search.worker.execution", "sync");
+        System.setProperty("mass-upload-folder", "sourceTest/sql/postgresql");
+    }
+
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AuthenticationSystemTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AuthenticationSystemTest.java
new file mode 100644
index 00000000000..515eb7c7d3c
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AuthenticationSystemTest.java
@@ -0,0 +1,45 @@
+/*
+ * 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.systemtest;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.fail;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.openbis.generic.client.web.client.exception.UserFailureException;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class AuthenticationSystemTest extends SystemTestCase
+{
+    @Test
+    public void testTryToAuthenticateWithMissingPassword()
+    {
+        try
+        {
+            commonClientService.tryToLogin("test", "");
+            fail("UserFailureException expected");
+        } catch (UserFailureException ex)
+        {
+            assertEquals("No 'password' specified.", ex.getMessage());
+        }
+    }
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/FilterSystemTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/FilterSystemTest.java
new file mode 100644
index 00000000000..79ad8a3db8d
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/FilterSystemTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.systemtest;
+
+import static org.testng.AssertJUnit.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.openbis.generic.client.web.client.application.util.lang.StringEscapeUtils;
+import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.GridCustomFilter;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewColumnOrFilter;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+public class FilterSystemTest extends SystemTestCase
+{
+    private static final String GRID_ID = "blabla";
+
+    @Test
+    public void testRegisterAndDeleteFilter()
+    {
+        logIntoCommonClientService();
+        List<GridCustomFilter> filters = commonClientService.listFilters(GRID_ID);
+        assertEquals(0, filters.size());
+        
+        NewColumnOrFilter filter = createFilter();
+        commonClientService.registerFilter(filter);
+        
+        filters = commonClientService.listFilters(GRID_ID);
+        assertEquals(1, filters.size());
+        assertEquals(filter.getName(), filters.get(0).getName());
+        assertEquals(filter.getDescription(), filters.get(0).getDescription());
+        assertEquals(filter.getExpression(), StringEscapeUtils.unescapeHtml(filters.get(0).getExpression()));
+        assertEquals(filter.isPublic(), filters.get(0).isPublic());
+        assertEquals("[threshold]", filters.get(0).getParameters().toString());
+        
+        commonClientService.deleteFilters(Arrays.asList(new TechId(filters.get(0).getId())));
+        
+        assertEquals(0, commonClientService.listFilters(GRID_ID).size());
+    }
+    
+    @Test
+    public void testEditFilter()
+    {
+        logIntoCommonClientService();
+        commonClientService.registerFilter(createFilter());
+        GridCustomFilter filter = commonClientService.listFilters(GRID_ID).get(0);
+        filter.setName(filter.getName() + "2");
+        filter.setDescription(filter.getDescription() + " (updated)");
+        filter.setExpression(filter.getExpression() + " * ${factor}");
+        filter.setPublic(filter.isPublic() == false);
+        
+        commonClientService.updateFilter(filter);
+        
+        List<GridCustomFilter> filters = commonClientService.listFilters(GRID_ID);
+        assertEquals(1, filters.size());
+        assertEquals(filter.getName(), filters.get(0).getName());
+        assertEquals(filter.getDescription(), filters.get(0).getDescription());
+        assertEquals(filter.getExpression(), StringEscapeUtils.unescapeHtml(filters.get(0).getExpression()));
+        assertEquals(filter.isPublic(), filters.get(0).isPublic());
+        List<String> parameters = new ArrayList<String>(filters.get(0).getParameters());
+        Collections.sort(parameters);
+        assertEquals("[factor, threshold]", parameters.toString());
+        
+        commonClientService.deleteFilters(Arrays.asList(new TechId(filters.get(0).getId())));
+    }
+
+    private NewColumnOrFilter createFilter()
+    {
+        NewColumnOrFilter filter = new NewColumnOrFilter();
+        filter.setGridId(GRID_ID);
+        filter.setName("my filter");
+        filter.setDescription("A test filter");
+        filter.setExpression("${threshold} < 42");
+        filter.setPublic(true);
+        return filter;
+    }
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
new file mode 100644
index 00000000000..fa6f8cba62b
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
@@ -0,0 +1,93 @@
+/*
+ * 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.systemtest;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
+import org.testng.AssertJUnit;
+
+import ch.systemsx.cisd.common.servlet.SpringRequestContextProvider;
+import ch.systemsx.cisd.openbis.generic.client.web.client.ICommonClientService;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SessionContext;
+import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer;
+import ch.systemsx.cisd.openbis.generic.shared.ICommonServer;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DisplaySettings;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+@ContextConfiguration(locations = "classpath:applicationContext.xml")
+public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
+{
+    protected ICommonServer commonServer;
+    protected ICommonClientService commonClientService;
+    
+    static
+    {
+        TestInitializer.init();
+    }
+
+    /**
+     * Sets a {@link MockHttpServletRequest} for the specified context provider
+     */
+    @Autowired
+    public final void setRequestContextProvider(final SpringRequestContextProvider contextProvider)
+    {
+        contextProvider.setRequest(new MockHttpServletRequest());
+    }
+    
+    /**
+     * Sets <code>commonServer</code>.
+     * <p>
+     * Will be automatically dependency injected by type.
+     * </p>
+     */
+    @Autowired
+    public final void setCommonServer(final ICommonServer commonServer)
+    {
+        this.commonServer = commonServer;
+    }
+    
+    /**
+     * Sets <code>commonClientService</code>.
+     * <p>
+     * Will be automatically dependency injected by type.
+     * </p>
+     */
+    @Autowired
+    public final void setCommonClientService(final ICommonClientService commonClientService)
+    {
+        this.commonClientService = commonClientService;
+    }
+    
+    protected SessionContext logIntoCommonClientService()
+    {
+        SessionContext context = commonClientService.tryToLogin("test", "a");
+        AssertJUnit.assertNotNull(context);
+        return context;
+    }
+
+    protected void logOutFromCommonClientService()
+    {
+        commonClientService.logout(new DisplaySettings());
+    }
+
+}
-- 
GitLab