From dad2eae38af2cf379a5824a41bf7b77132d9056c Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Wed, 8 Dec 2010 09:51:39 +0000
Subject: [PATCH] LMS-1910 head-less system tests for attachment uploading

SVN: 19049
---
 .../systemtest/AttachmentUploadTest.java      | 114 ++++++++++++++++++
 .../BatchMaterialRegistrationAndUpdate.java   |  16 +--
 .../openbis/systemtest/SystemTestCase.java    |  29 +++++
 3 files changed, 145 insertions(+), 14 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AttachmentUploadTest.java

diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AttachmentUploadTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AttachmentUploadTest.java
new file mode 100644
index 00000000000..2cddc4b315d
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/AttachmentUploadTest.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2010 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.List;
+
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DefaultResultSetConfig;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ResultSet;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SessionContext;
+import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Attachment;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.AttachmentHolderKind;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.AttachmentVersions;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.AttachmentWithContent;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.NewAttachment;
+import ch.systemsx.cisd.openbis.generic.shared.basic.dto.Project;
+
+/**
+ * 
+ *
+ * @author Franz-Josef Elmer
+ */
+@Test(groups = "system test")
+public class AttachmentUploadTest extends SystemTestCase
+{
+    private static final String FILE_CONTENT = "hello world";
+    private static final String FILE_NAME = "hello.txt";
+
+    @Test
+    public void testUploadProjectAttachment()
+    {
+        SessionContext sessionContext = logIntoCommonClientService();
+        ResultSet<Project> projects =
+                commonClientService.listProjects(new DefaultResultSetConfig<String, Project>());
+        Project project = projects.getList().extractOriginalObjects().get(0);
+        TechId projectID = TechId.create(project);
+
+        uploadFile(FILE_NAME, FILE_CONTENT);
+        commonClientService.addAttachment(projectID, SESSION_KEY, AttachmentHolderKind.PROJECT,
+                new NewAttachment(FILE_NAME, "my file", "example file"));
+
+        AttachmentWithContent attachmentWithContent =
+                genericServer.getProjectFileAttachment(sessionContext.getSessionID(), projectID,
+                        FILE_NAME, 1);
+        checkUploadedAttachment(projectID, AttachmentHolderKind.PROJECT, attachmentWithContent);
+    }
+
+    @Test
+    public void testUploadExperimentAttachment()
+    {
+        SessionContext sessionContext = logIntoCommonClientService();
+        TechId experimentID = new TechId(2);
+        
+        uploadFile(FILE_NAME, FILE_CONTENT);
+        commonClientService.addAttachment(experimentID, SESSION_KEY, AttachmentHolderKind.EXPERIMENT,
+                new NewAttachment(FILE_NAME, "my file", "example file"));
+        
+        AttachmentWithContent attachmentWithContent =
+            genericServer.getExperimentFileAttachment(sessionContext.getSessionID(), experimentID,
+                    FILE_NAME, 1);
+        checkUploadedAttachment(experimentID, AttachmentHolderKind.EXPERIMENT, attachmentWithContent);
+    }
+    
+    @Test
+    public void testUploadSampleAttachment()
+    {
+        SessionContext sessionContext = logIntoCommonClientService();
+        TechId sampleID = new TechId(1);
+        
+        uploadFile(FILE_NAME, FILE_CONTENT);
+        commonClientService.addAttachment(sampleID, SESSION_KEY, AttachmentHolderKind.SAMPLE,
+                new NewAttachment(FILE_NAME, "my file", "example file"));
+        
+        AttachmentWithContent attachmentWithContent =
+            genericServer.getSampleFileAttachment(sessionContext.getSessionID(), sampleID,
+                    FILE_NAME, 1);
+        checkUploadedAttachment(sampleID, AttachmentHolderKind.SAMPLE, attachmentWithContent);
+    }
+    
+    private void checkUploadedAttachment(TechId holderID, AttachmentHolderKind holderKind,
+            AttachmentWithContent attachmentWithContent)
+    {
+        ResultSet<AttachmentVersions> attachmentVersions =
+                commonClientService.listAttachmentVersions(holderID, holderKind,
+                        new DefaultResultSetConfig<String, AttachmentVersions>());
+        List<Attachment> attachments =
+                attachmentVersions.getList().get(0).getOriginalObject().getVersions();
+
+        Attachment attachment = attachments.get(0);
+        assertEquals(FILE_NAME, attachment.getFileName());
+        assertEquals("my file", attachment.getTitle());
+        assertEquals("example file", attachment.getDescription());
+        assertEquals(1, attachment.getVersion());
+        assertEquals(FILE_CONTENT, new String(attachmentWithContent.getContent()));
+    }
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdate.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdate.java
index 017bf434ede..69a0e6d69ef 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdate.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/BatchMaterialRegistrationAndUpdate.java
@@ -26,9 +26,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import javax.servlet.http.HttpSession;
 
-import org.springframework.mock.web.MockMultipartFile;
 import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DisplayedOrSelectedIdHolderCriteria;
@@ -36,7 +34,6 @@ import ch.systemsx.cisd.openbis.generic.client.web.client.dto.GridRowModels;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ListMaterialDisplayCriteria;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.ResultSet;
 import ch.systemsx.cisd.openbis.generic.client.web.client.exception.UserFailureException;
-import ch.systemsx.cisd.openbis.generic.client.web.server.UploadedFilesBean;
 import ch.systemsx.cisd.openbis.generic.shared.basic.GridRowModel;
 import ch.systemsx.cisd.openbis.generic.shared.basic.IEntityInformationHolderWithPermId;
 import ch.systemsx.cisd.openbis.generic.shared.basic.TechId;
@@ -54,7 +51,6 @@ import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialType;
 public class BatchMaterialRegistrationAndUpdate extends SystemTestCase
 {
     private static final String MATERIAL_TYPE = "CONTROL";
-    private static final String SESSION_KEY = "session-key";
     private static final Set<String> CODES = new HashSet<String>(Arrays.asList("C1", "C2"));
 
     @Test
@@ -175,7 +171,7 @@ public class BatchMaterialRegistrationAndUpdate extends SystemTestCase
 
     private List<BatchRegistrationResult> registerMaterials(String materialBatchData)
     {
-        uploadFile(materialBatchData);
+        uploadFile("my-file", materialBatchData);
         MaterialType materialType = new MaterialType();
         materialType.setCode(MATERIAL_TYPE);
         return genericClientService.registerMaterials(materialType, SESSION_KEY);
@@ -183,17 +179,9 @@ public class BatchMaterialRegistrationAndUpdate extends SystemTestCase
     
     private List<BatchRegistrationResult> updateMaterials(String materialBatchData, boolean ignoreUnregistered)
     {
-        uploadFile(materialBatchData);
+        uploadFile("my-file", materialBatchData);
         MaterialType materialType = new MaterialType();
         materialType.setCode(MATERIAL_TYPE);
         return genericClientService.updateMaterials(materialType, SESSION_KEY, ignoreUnregistered);
     }
-
-    private void uploadFile(String fileContent)
-    {
-        UploadedFilesBean bean = new UploadedFilesBean();
-        bean.addMultipartFile(new MockMultipartFile("my-file", fileContent.getBytes()));
-        HttpSession session = request.getSession();
-        session.setAttribute(SESSION_KEY, bean);
-    }
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
index ad265853f9f..f1a8c7c38aa 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/systemtest/SystemTestCase.java
@@ -16,8 +16,11 @@
 
 package ch.systemsx.cisd.openbis.systemtest;
 
+import javax.servlet.http.HttpSession;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockMultipartFile;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
 import org.testng.AssertJUnit;
@@ -26,10 +29,12 @@ import org.testng.annotations.BeforeSuite;
 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.client.web.server.UploadedFilesBean;
 import ch.systemsx.cisd.openbis.generic.server.ICommonServerForInternalUse;
 import ch.systemsx.cisd.openbis.generic.server.util.TestInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DisplaySettings;
 import ch.systemsx.cisd.openbis.plugin.generic.client.web.client.IGenericClientService;
+import ch.systemsx.cisd.openbis.plugin.generic.shared.IGenericServer;
 
 /**
  * @author Franz-Josef Elmer
@@ -37,8 +42,12 @@ import ch.systemsx.cisd.openbis.plugin.generic.client.web.client.IGenericClientS
 @ContextConfiguration(locations = "classpath:applicationContext.xml")
 public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
 {
+    protected static final String SESSION_KEY = "session-key";
+
     protected ICommonServerForInternalUse commonServer;
 
+    protected IGenericServer genericServer;
+    
     protected ICommonClientService commonClientService;
 
     protected IGenericClientService genericClientService;
@@ -73,6 +82,18 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
         this.commonServer = commonServer;
     }
 
+    /**
+     * Sets <code>genericServer</code>.
+     * <p>
+     * Will be automatically dependency injected by type.
+     * </p>
+     */
+    @Autowired
+    public final void setGenericServer(final IGenericServer genericServer)
+    {
+        this.genericServer = genericServer;
+    }
+    
     /**
      * Sets <code>commonClientService</code>.
      * <p>
@@ -120,4 +141,12 @@ public abstract class SystemTestCase extends AbstractTestNGSpringContextTests
         }
     }
 
+    protected void uploadFile(String fileName, String fileContent)
+    {
+        UploadedFilesBean bean = new UploadedFilesBean();
+        bean.addMultipartFile(new MockMultipartFile(fileName, fileName, null, fileContent.getBytes()));
+        HttpSession session = request.getSession();
+        session.setAttribute(SESSION_KEY, bean);
+    }
+
 }
-- 
GitLab