From 121429d6af5634e3ba6bce305dcd58f39a5d4695 Mon Sep 17 00:00:00 2001
From: felmer <felmer>
Date: Wed, 5 Nov 2014 07:25:48 +0000
Subject: [PATCH] SSDM-1010: SegmentedStoreUtils refactored. @Test annotation
 added to class SegmentedStoreUtilsTest because it hasn't been executed on CI
 server.

SVN: 32727
---
 .../shared/utils/SegmentedStoreUtils.java     | 50 +++++++++------
 .../shared/utils/SegmentedStoreUtilsTest.java | 61 ++++++++++---------
 2 files changed, 63 insertions(+), 48 deletions(-)

diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
index 058d2f6f7e3..8efadece052 100644
--- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
+++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtils.java
@@ -33,6 +33,7 @@ import java.util.regex.Pattern;
 
 import org.apache.commons.io.FileUtils;
 
+import ch.rinn.restrictions.Private;
 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
 import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
 import ch.systemsx.cisd.base.utilities.OSUtilities;
@@ -80,7 +81,7 @@ public class SegmentedStoreUtils
 
     private static final Pattern SHARE_ID_PATTERN = Pattern.compile("[0-9]+");
 
-    private static final Long MINIMUM_FREE_SCRATCH_SPACE = 1024L * 1024 * 1024;
+    @Private static final Long MINIMUM_FREE_SCRATCH_SPACE = FileUtils.ONE_GB;
 
     private static final Comparator<Share> SHARE_COMPARATOR = new Comparator<Share>()
         {
@@ -116,7 +117,32 @@ public class SegmentedStoreUtils
 
     public static enum FilterOptions
     {
-        ALL, AVAILABLE_FOR_SHUFFLING, ARCHIVING_SCRATCH
+        ALL()
+        {
+            @Override
+            boolean isSelected(Share share)
+            {
+                return true;
+            }
+        },
+        AVAILABLE_FOR_SHUFFLING()
+        {
+            @Override
+            boolean isSelected(Share share)
+            {
+                return false == (share.isIgnoredForShuffling() || share.isUnarchivingScratchShare());
+            }
+        },
+        ARCHIVING_SCRATCH()
+        {
+            @Override
+            boolean isSelected(Share share)
+            {
+                return share.isUnarchivingScratchShare();
+            }
+        };
+
+        abstract boolean isSelected(Share share);
     }
 
     /**
@@ -338,7 +364,8 @@ public class SegmentedStoreUtils
         {
             throw new EnvironmentFailureException("Even after removing all removable data sets from share '"
                     + share.getShareId() + "' there would be still only " + FileUtilities.byteCountToDisplaySize(freeSpace)
-                    + " free space which is not enough as " + FileUtilities.byteCountToDisplaySize(requestedSpace) + " is requested.");
+                    + " free space which is not enough as " + FileUtilities.byteCountToDisplaySize(requestedSpace) 
+                    + " is requested.");
         }
         return dataSetsToRemoveFromShare;
     }
@@ -372,7 +399,7 @@ public class SegmentedStoreUtils
             final Share share =
                     new ShareFactory().createShare(sharesHolder, file, freeSpaceProvider, log);
 
-            if (isShareSelected(share, filterOptions))
+            if (filterOptions.isSelected(share))
             {
                 shares.put(share.getShareId(), share);
             }
@@ -380,21 +407,6 @@ public class SegmentedStoreUtils
         return shares;
     }
 
-    private static boolean isShareSelected(Share share, FilterOptions filterOptions)
-    {
-        switch (filterOptions)
-        {
-            case ALL:
-                return true;
-            case ARCHIVING_SCRATCH:
-                return share.isUnarchivingScratchShare();
-            case AVAILABLE_FOR_SHUFFLING:
-                return false == (share.isIgnoredForShuffling() || share.isUnarchivingScratchShare());
-            default:
-                throw new IllegalStateException("All cases covered");
-        }
-    }
-
     /**
      * Moves the specified data set to the specified share. The data set is folder in the store its name is the data set code. The destination folder
      * is <code>share</code>. Its name is the share id.
diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtilsTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtilsTest.java
index 9f08f3b8dee..c710c1d4ead 100644
--- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtilsTest.java
+++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/utils/SegmentedStoreUtilsTest.java
@@ -59,6 +59,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.SimpleDataSetInformationDTO;
  * @author Franz-Josef Elmer
  */
 @Friend(toClasses = SegmentedStoreUtils.class)
+@Test
 public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
 {
     private static final String MOVED = "Moved";
@@ -136,7 +137,7 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
     @Test
     public void testFreeSpaceForAShareWhichIsNotAnUnarchivingScratchShare()
     {
-        SimpleDataSetInformationDTO ds1 = dataSet(1, 11 * FileUtils.ONE_KB);
+        SimpleDataSetInformationDTO ds1 = dataSet(1, 11 * FileUtils.ONE_GB);
         Share share = new Share(shareFolder, 0, freeSpaceProvider);
 
         try
@@ -156,33 +157,34 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
     @Test
     public void testFreeSpaceNothingToDo()
     {
-        SimpleDataSetInformationDTO ds1 = dataSet(1, 11 * FileUtils.ONE_KB);
+        SimpleDataSetInformationDTO ds1 = dataSet(1, 11 * FileUtils.ONE_GB);
         Share share = new Share(shareFolder, 0, freeSpaceProvider);
         share.setUnarchivingScratchShare(true);
-        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(12L);
+        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(12 * FileUtils.ONE_GB);
 
         SegmentedStoreUtils.freeSpace(share, service, asDatasetDescriptions(ds1), dataSetDirectoryProvider,
                 shareIdManager, log);
 
         assertEquals(shareFolder.getPath(), recordingFileMatcher.recordedObject().getPath());
-        assertEquals("INFO: Free space on unarchiving scratch share '1': 12.00 KB, "
-                + "requested space for unarchiving 1 data sets: 11.00 KB\n", log.toString());
+        assertEquals("INFO: Free space on unarchiving scratch share '1': 13.00 GB, "
+                + "requested space for unarchiving 1 data sets: 11.00 GB\n", log.toString());
     }
 
     @Test
     public void testFreeSpaceRemovingOneDataSet()
     {
-        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_KB);
+        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_GB);
         Share share = new Share(shareFolder, 0, freeSpaceProvider);
         share.setUnarchivingScratchShare(true);
         share.addDataSet(ds5);
         share.addDataSet(ds3);
         share.addDataSet(ds1);
-        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(12L, 22L);
+        RecordingMatcher<HostAwareFile> recordingFileMatcher 
+                = prepareFreeSpace(12 * FileUtils.ONE_GB, 22 * FileUtils.ONE_GB);
         prepareSetArchingStatus(ds1);
         File file = prepareDeleteFromShare(ds1);
         assertEquals(true, file.exists());
@@ -201,24 +203,25 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
                 + "INFO: Data set ds-1 at " + shareFolder + "/abc/ds-1 has been successfully deleted.\n"
                 + "INFO: The following data sets have been successfully removed from share '1' "
                 + "and their archiving status has been successfully set back to ARCHIVED: [ds-1]\n"
-                + "INFO: Free space on unarchiving scratch share '1': 22.00 KB, requested space for "
-                + "unarchiving 2 data sets: 21.00 KB\n", log.toString());
+                + "INFO: Free space on unarchiving scratch share '1': 23.00 GB, requested space for "
+                + "unarchiving 2 data sets: 21.00 GB\n", log.toString());
     }
 
     @Test
     public void testFreeSpaceForThreeDataSetsOneAlreadyInShare()
     {
-        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_KB);
+        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_GB);
         Share share = new Share(shareFolder, 0, freeSpaceProvider);
         share.setUnarchivingScratchShare(true);
         share.addDataSet(ds5);
         share.addDataSet(ds3);
         share.addDataSet(ds1);
-        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(12L, 24L);
+        RecordingMatcher<HostAwareFile> recordingFileMatcher 
+                = prepareFreeSpace(12 * FileUtils.ONE_GB, 24 * FileUtils.ONE_GB);
         prepareSetArchingStatus(ds3);
         File file = prepareDeleteFromShare(ds3);
         assertEquals(true, file.exists());
@@ -237,24 +240,24 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
                 + "INFO: Data set ds-3 at " + shareFolder + "/abc/ds-3 has been successfully deleted.\n"
                 + "INFO: The following data sets have been successfully removed from share '1' "
                 + "and their archiving status has been successfully set back to ARCHIVED: [ds-3]\n"
-                + "INFO: Free space on unarchiving scratch share '1': 24.00 KB, requested space for "
-                + "unarchiving 2 data sets: 21.00 KB\n", log.toString());
+                + "INFO: Free space on unarchiving scratch share '1': 25.00 GB, requested space for "
+                + "unarchiving 2 data sets: 21.00 GB\n", log.toString());
     }
 
     @Test
     public void testFreeSpaceRemovingDataSetsButStillNotEnoughFreeSpace()
     {
-        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_KB);
-        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_KB);
+        SimpleDataSetInformationDTO ds1 = dataSet(1, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds2 = dataSet(2, 10 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds3 = dataSet(3, 12 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds4 = dataSet(4, 11 * FileUtils.ONE_GB);
+        SimpleDataSetInformationDTO ds5 = dataSet(5, 14 * FileUtils.ONE_GB);
         Share share = new Share(shareFolder, 0, freeSpaceProvider);
         share.setUnarchivingScratchShare(true);
         share.addDataSet(ds3);
         share.addDataSet(ds2);
         share.addDataSet(ds1);
-        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(2L);
+        RecordingMatcher<HostAwareFile> recordingFileMatcher = prepareFreeSpace(2 * FileUtils.ONE_GB);
 
         try
         {
@@ -264,7 +267,7 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
         } catch (EnvironmentFailureException ex)
         {
             assertEquals("Even after removing all removable data sets from share '1' there would be "
-                    + "still only 24.00 KB free space which is less than the requested 25.00 KB.", ex.getMessage());
+                    + "still only 25.00 GB free space which is not enough as 25.00 GB is requested.", ex.getMessage());
         }
 
         assertEquals(shareFolder.getPath(), recordingFileMatcher.recordedObject().getPath());
@@ -813,7 +816,7 @@ public class SegmentedStoreUtilsTest extends AbstractFileSystemTestCase
                         for (long freeSpace : freeSpaceValues)
                         {
                             one(freeSpaceProvider).freeSpaceKb(with(recorder));
-                            will(returnValue(freeSpace));
+                            will(returnValue((freeSpace + SegmentedStoreUtils.MINIMUM_FREE_SCRATCH_SPACE) / 1024));
                             inSequence(sequence);
                         }
                     } catch (IOException ex)
-- 
GitLab