From a60066d32c25ee16db5ff1e52d78c0dbf0c2c44f Mon Sep 17 00:00:00 2001
From: pkupczyk <piotr.kupczyk@id.ethz.ch>
Date: Thu, 14 Mar 2024 12:54:47 +0100
Subject: [PATCH] SSDM-13578 : 2PT : Database and V3 Implementation -
 integration tests

---
 build/build.gradle                            |   1 +
 build/settings.gradle                         |   2 +-
 test-integration/build.gradle                 |   6 -
 .../systemtests/Integration2PCTest.java       | 137 +++++++++++++++---
 .../common/AbstractIntegrationTest.java       |  37 +++--
 .../common/IntegrationTestLogger.java         |  11 --
 .../common/IntegrationTestOpenBIS.java        |  15 --
 7 files changed, 149 insertions(+), 60 deletions(-)
 delete mode 100644 test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestLogger.java
 delete mode 100644 test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestOpenBIS.java

diff --git a/build/build.gradle b/build/build.gradle
index 406c4f0f450..c0994e9bd6a 100644
--- a/build/build.gradle
+++ b/build/build.gradle
@@ -15,6 +15,7 @@ evaluationDependsOn(':test-api-openbis-javascript')
 evaluationDependsOn(':lib-image-readers')
 evaluationDependsOn(':server-external-data-store')
 evaluationDependsOn(':api-openbis-typescript')
+evaluationDependsOn(':test-integration')
 apply from: '../build/javaproject.gradle'
 
 
diff --git a/build/settings.gradle b/build/settings.gradle
index 80da04f2d8e..a949a8e6637 100644
--- a/build/settings.gradle
+++ b/build/settings.gradle
@@ -3,4 +3,4 @@ includeFlat 'lib-commonbase', 'lib-common', 'api-openbis-java', 'lib-openbis-com
         'core-plugin-openbis', 'app-openbis-installer', 'lib-image-readers', 'test-ui-core',
         'test-api-openbis-javascript', 'server-external-data-store', 'ui-admin', 'lib-microservice-server',
         'lib-transactional-file-system', 'server-data-store', 'ui-eln-lims', 'api-openbis-javascript',
-        'lib-json', 'api-data-store-server-java', 'api-data-store-server-javascript', 'api-openbis-typescript'
\ No newline at end of file
+        'lib-json', 'api-data-store-server-java', 'api-data-store-server-javascript', 'api-openbis-typescript', 'test-integration'
\ No newline at end of file
diff --git a/test-integration/build.gradle b/test-integration/build.gradle
index 788ad2c6a4f..b54bf1ea81d 100644
--- a/test-integration/build.gradle
+++ b/test-integration/build.gradle
@@ -7,9 +7,3 @@ dependencies {
             project(path: ':server-application-server', configuration: 'tests'),
             project(':server-data-store')
 }
-
-test {
-    // log4j1.xml is used by the application server (config location is taken from system property below)
-    // log4j2.xml is used by the afs server (config location taken from afs server configuration)
-    jvmArgs += ['-Dlog4j.configuration=etc/log4j1.xml', '-Dlog4j.configurationFile=etc/log4j1.xml']
-}
\ No newline at end of file
diff --git a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/Integration2PCTest.java b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/Integration2PCTest.java
index c2017cb92cc..56e48f0e281 100644
--- a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/Integration2PCTest.java
+++ b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/Integration2PCTest.java
@@ -1,61 +1,97 @@
 package ch.ethz.sis.openbis.systemtests;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
 import static org.testng.Assert.fail;
 
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
+import java.util.Collections;
 import java.util.List;
+import java.util.UUID;
 
 import org.testng.annotations.Test;
 
 import ch.ethz.sis.openbis.generic.OpenBIS;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.entitytype.id.EntityTypePermId;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.Experiment;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.create.ExperimentCreation;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.fetchoptions.ExperimentFetchOptions;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.experiment.id.ExperimentPermId;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.Project;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.create.ProjectCreation;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.fetchoptions.ProjectFetchOptions;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.project.id.ProjectPermId;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.Space;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.create.SpaceCreation;
+import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.fetchoptions.SpaceFetchOptions;
 import ch.ethz.sis.openbis.generic.asapi.v3.dto.space.id.SpacePermId;
 import ch.ethz.sis.openbis.systemtests.common.AbstractIntegrationTest;
-import ch.ethz.sis.openbis.systemtests.common.IntegrationTestOpenBIS;
 import ch.systemsx.cisd.common.test.AssertionUtil;
 
 public class Integration2PCTest extends AbstractIntegrationTest
 {
 
+    private static final String CODE_PREFIX = "TRANSACTION_TEST_";
+
+    private static final String OWNER_PREFIX = "test-owner-";
+
+    private static final String SOURCE_PREFIX = "test-source-";
+
+    private static final String CONTENT = "test-content";
+
+    @Test
+    public void testTransactionCommit()
+    {
+        testTransaction(false);
+    }
+
     @Test
-    public void test()
+    public void testTransactionRollback()
     {
-        OpenBIS openBIS = new IntegrationTestOpenBIS();
-        openBIS.setInteractiveSessionKey(TEST_INTERACTIVE_SESSION_KEY);
+        testTransaction(true);
+    }
+
+    private void testTransaction(boolean rollback)
+    {
+        OpenBIS openBISWithTr = createOpenBIS();
+        OpenBIS openBISWithNoTr = createOpenBIS();
+
+        openBISWithTr.setInteractiveSessionKey(TEST_INTERACTIVE_SESSION_KEY);
 
-        openBIS.login("test", "admin");
+        openBISWithTr.login(USER, PASSWORD);
+        openBISWithNoTr.login(USER, PASSWORD);
 
-        openBIS.beginTransaction();
+        openBISWithTr.beginTransaction();
 
-        byte[] bytes = "Hello World!".getBytes(StandardCharsets.UTF_8);
-        openBIS.getAfsServerFacade().write("another-sample", "anotherdir/anotherfile", 0L, bytes, calculateMD5(bytes));
+        String owner = OWNER_PREFIX + UUID.randomUUID();
+        String source = SOURCE_PREFIX + UUID.randomUUID();
+        byte[] bytesToWrite = CONTENT.getBytes(StandardCharsets.UTF_8);
+
+        openBISWithTr.getAfsServerFacade().write(owner, source, 0L, bytesToWrite, calculateMD5(bytesToWrite));
 
         SpaceCreation spaceCreation = new SpaceCreation();
-        spaceCreation.setCode("2PT_TEST");
+        spaceCreation.setCode(CODE_PREFIX + UUID.randomUUID());
 
-        SpacePermId spaceId = openBIS.createSpaces(List.of(spaceCreation)).get(0);
+        SpacePermId spaceId = openBISWithTr.createSpaces(List.of(spaceCreation)).get(0);
 
         ProjectCreation projectCreation = new ProjectCreation();
         projectCreation.setSpaceId(spaceId);
-        projectCreation.setCode("2PT_TEST");
+        projectCreation.setCode(CODE_PREFIX + UUID.randomUUID());
 
-        ProjectPermId projectId = openBIS.createProjects(List.of(projectCreation)).get(0);
+        ProjectPermId projectId = openBISWithTr.createProjects(List.of(projectCreation)).get(0);
 
         ExperimentCreation experimentCreation = new ExperimentCreation();
         experimentCreation.setProjectId(projectId);
-        experimentCreation.setCode("2PT_TEST");
+        experimentCreation.setCode(CODE_PREFIX + UUID.randomUUID());
 
         try
         {
-            openBIS.createExperiments(List.of(experimentCreation));
+            openBISWithTr.createExperiments(List.of(experimentCreation));
             fail();
-        }catch(Exception e){
+        } catch (Exception e)
+        {
             assertEquals(e.getMessage(), "Operation 'createExperiments' failed.");
             AssertionUtil.assertContains("Type id cannot be null", e.getCause().getMessage());
         }
@@ -63,11 +99,76 @@ public class Integration2PCTest extends AbstractIntegrationTest
         ExperimentCreation experimentCreation2 = new ExperimentCreation();
         experimentCreation2.setTypeId(new EntityTypePermId("UNKNOWN"));
         experimentCreation2.setProjectId(projectId);
-        experimentCreation2.setCode("2PT_TEST");
+        experimentCreation2.setCode(CODE_PREFIX + UUID.randomUUID());
+
+        ExperimentPermId experimentId = openBISWithTr.createExperiments(List.of(experimentCreation2)).get(0);
 
-        openBIS.createExperiments(List.of(experimentCreation2));
+        // the transaction session sees created entities before they are committed (except for afs changes with are not visible until commit)
+        Space trSpaceBefore = openBISWithTr.getSpaces(Collections.singletonList(spaceId), new SpaceFetchOptions()).get(spaceId);
+        Project trProjectBefore = openBISWithTr.getProjects(Collections.singletonList(projectId), new ProjectFetchOptions()).get(projectId);
+        Experiment trExperimentBefore =
+                openBISWithTr.getExperiments(Collections.singletonList(experimentId), new ExperimentFetchOptions()).get(experimentId);
 
-        openBIS.commitTransaction();
+        assertNotNull(trSpaceBefore);
+        assertNotNull(trProjectBefore);
+        assertNotNull(trExperimentBefore);
+
+        // the non-transaction session does not see created entities before they are committed
+        Space noTrSpaceBefore = openBISWithNoTr.getSpaces(Collections.singletonList(spaceId), new SpaceFetchOptions()).get(spaceId);
+        Project noTrProjectBefore = openBISWithNoTr.getProjects(Collections.singletonList(projectId), new ProjectFetchOptions()).get(projectId);
+        Experiment noTrExperimentBefore =
+                openBISWithNoTr.getExperiments(Collections.singletonList(experimentId), new ExperimentFetchOptions()).get(experimentId);
+
+        assertNull(noTrSpaceBefore);
+        assertNull(noTrProjectBefore);
+        assertNull(noTrExperimentBefore);
+
+        if (rollback)
+        {
+            openBISWithTr.rollbackTransaction();
+
+        } else
+        {
+            openBISWithTr.commitTransaction();
+        }
+
+        Space trSpaceAfter = openBISWithTr.getSpaces(Collections.singletonList(spaceId), new SpaceFetchOptions()).get(spaceId);
+        Project trProjectAfter = openBISWithTr.getProjects(Collections.singletonList(projectId), new ProjectFetchOptions()).get(projectId);
+        Experiment trExperimentAfter =
+                openBISWithTr.getExperiments(Collections.singletonList(experimentId), new ExperimentFetchOptions()).get(experimentId);
+
+        Space noTrSpaceAfter = openBISWithNoTr.getSpaces(Collections.singletonList(spaceId), new SpaceFetchOptions()).get(spaceId);
+        Project noTrProjectAfter = openBISWithNoTr.getProjects(Collections.singletonList(projectId), new ProjectFetchOptions()).get(projectId);
+        Experiment noTrExperimentAfter =
+                openBISWithNoTr.getExperiments(Collections.singletonList(experimentId), new ExperimentFetchOptions()).get(experimentId);
+
+        if (rollback)
+        {
+            // neither the transaction session nor the non-transaction session see the created entities after the rollback
+            assertNull(trSpaceAfter);
+            assertNull(trProjectAfter);
+            assertNull(trExperimentAfter);
+
+            assertNull(noTrSpaceAfter);
+            assertNull(noTrProjectAfter);
+            assertNull(noTrExperimentAfter);
+        } else
+        {
+            // both the transaction session and the non-transaction session see the created entities after the commit
+            assertNotNull(trSpaceAfter);
+            assertNotNull(trProjectAfter);
+            assertNotNull(trExperimentAfter);
+
+            assertNotNull(noTrSpaceAfter);
+            assertNotNull(noTrProjectAfter);
+            assertNotNull(noTrExperimentAfter);
+
+            byte[] trBytesRead = openBISWithTr.getAfsServerFacade().read(owner, source, 0L, bytesToWrite.length);
+            byte[] noTrBytesRead = openBISWithNoTr.getAfsServerFacade().read(owner, source, 0L, bytesToWrite.length);
+
+            assertEquals(new String(trBytesRead, StandardCharsets.UTF_8), CONTENT);
+            assertEquals(new String(noTrBytesRead, StandardCharsets.UTF_8), CONTENT);
+        }
     }
 
     public static byte[] calculateMD5(byte[] data)
diff --git a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/AbstractIntegrationTest.java b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/AbstractIntegrationTest.java
index e5b0e7adad1..0dc7cdaf249 100644
--- a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/AbstractIntegrationTest.java
+++ b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/AbstractIntegrationTest.java
@@ -42,10 +42,10 @@ import org.testng.annotations.BeforeSuite;
 import ch.ethz.sis.afs.manager.TransactionConnection;
 import ch.ethz.sis.afsserver.server.observer.impl.DummyServerObserver;
 import ch.ethz.sis.afsserver.startup.AtomicFileSystemServerParameter;
+import ch.ethz.sis.openbis.generic.OpenBIS;
 import ch.ethz.sis.openbis.generic.server.asapi.v3.TransactionConfiguration;
 import ch.ethz.sis.shared.startup.Configuration;
 import ch.systemsx.cisd.common.filesystem.FileUtilities;
-import ch.systemsx.cisd.common.logging.LogInitializer;
 import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
 
 /**
@@ -57,18 +57,20 @@ public abstract class AbstractIntegrationTest
 
     public static final String TEST_INTERACTIVE_SESSION_KEY = "integration-test-interactive-session-key";
 
+    public static final String USER = "test";
+
+    public static final String PASSWORD = "password";
+
     private static Server applicationServer;
 
     private static ch.ethz.sis.afsserver.server.Server<TransactionConnection, Object> afsServer;
 
     protected static GenericWebApplicationContext applicationServerSpringContext;
 
-    protected static IntegrationTestLogger logger = new IntegrationTestLogger();
-
     @BeforeSuite
     public void beforeSuite() throws Exception
     {
-        LogInitializer.init();
+        initLogging();
 
         cleanupApplicationServerFolders();
         cleanupAfsServerFolders();
@@ -87,25 +89,30 @@ public abstract class AbstractIntegrationTest
     private void shutdownApplicationServer()
     {
         applicationServer.setStopAtShutdown(true);
-        logger.log("Shut down application server.");
+        log("Shut down application server.");
     }
 
     private void shutdownAfsServer() throws Exception
     {
         afsServer.shutdown(false);
-        logger.log("Shut down afs server.");
+        log("Shut down afs server.");
     }
 
     @BeforeMethod
     public void beforeTest(Method method)
     {
-        logger.log("BEFORE " + method.getDeclaringClass().getName() + "." + method.getName());
+        log("BEFORE " + method.getDeclaringClass().getName() + "." + method.getName());
     }
 
     @AfterMethod
     public void afterTest(Method method)
     {
-        logger.log("AFTER  " + method.getDeclaringClass().getName() + "." + method.getName());
+        log("AFTER  " + method.getDeclaringClass().getName() + "." + method.getName());
+    }
+
+    private void initLogging(){
+        System.setProperty("log4j.configuration", "etc/log4j1.xml");
+        System.setProperty("log4j.configurationFile", "etc/log4j1.xml");
     }
 
     private void cleanupApplicationServerFolders() throws Exception
@@ -150,7 +157,7 @@ public abstract class AbstractIntegrationTest
         } else
         {
             FileUtilities.deleteRecursively(new File(folderPath));
-            logger.log("Deleted folder: " + new File(folderPath).getAbsolutePath());
+            log("Deleted folder: " + new File(folderPath).getAbsolutePath());
         }
     }
 
@@ -232,4 +239,16 @@ public abstract class AbstractIntegrationTest
         return configuration;
     }
 
+    public static OpenBIS createOpenBIS()
+    {
+        return new OpenBIS(TestInstanceHostUtils.getOpenBISUrl() + TestInstanceHostUtils.getOpenBISPath(),
+                TestInstanceHostUtils.getDSSUrl() + TestInstanceHostUtils.getDSSPath(),
+                TestInstanceHostUtils.getAFSUrl() + TestInstanceHostUtils.getAFSPath());
+    }
+
+    public void log(String message)
+    {
+        System.out.println("[TEST] " + message);
+    }
+
 }
diff --git a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestLogger.java b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestLogger.java
deleted file mode 100644
index 0b101441199..00000000000
--- a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestLogger.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package ch.ethz.sis.openbis.systemtests.common;
-
-public class IntegrationTestLogger
-{
-
-    public void log(String message)
-    {
-        System.out.println("[TEST] " + message);
-    }
-
-}
diff --git a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestOpenBIS.java b/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestOpenBIS.java
deleted file mode 100644
index b35381dde89..00000000000
--- a/test-integration/sourceTest/java/ch/ethz/sis/openbis/systemtests/common/IntegrationTestOpenBIS.java
+++ /dev/null
@@ -1,15 +0,0 @@
-package ch.ethz.sis.openbis.systemtests.common;
-
-import ch.ethz.sis.openbis.generic.OpenBIS;
-import ch.systemsx.cisd.openbis.generic.shared.util.TestInstanceHostUtils;
-
-public class IntegrationTestOpenBIS extends OpenBIS
-{
-
-    public IntegrationTestOpenBIS()
-    {
-        super(TestInstanceHostUtils.getOpenBISUrl() + TestInstanceHostUtils.getOpenBISPath(),
-                TestInstanceHostUtils.getDSSUrl() + TestInstanceHostUtils.getDSSPath(),
-                TestInstanceHostUtils.getAFSUrl() + TestInstanceHostUtils.getAFSPath());
-    }
-}
-- 
GitLab