diff --git a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPlugin.java b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPlugin.java index af49533128b86e547a0c65262ac439130b686e97..c5fc51ac5fce95f2a7e961bf07c77457a37f2e41 100644 --- a/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPlugin.java +++ b/datastore_server/source/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPlugin.java @@ -462,6 +462,11 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore private void asyncUpdateStatuses(List<String> dataSetCodes, DataSetArchivingStatus newStatus, boolean presentInArchive) { + if (dataSetCodes.isEmpty()) + { + return; + } + if (statusUpdater == null) { statusUpdater = new IDataSetStatusUpdater() @@ -572,6 +577,12 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore return service; } + @Private + void setStatusUpdater(IDataSetStatusUpdater statusUpdater) + { + this.statusUpdater = statusUpdater; + } + /** * @author Franz-Josef Elmer */ diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPluginTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPluginTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0f5f49dd7d5ff8754bdd02daf0f5e0959c0b3f35 --- /dev/null +++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/AbstractArchiverProcessingPluginTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 2011 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.dss.generic.server.plugins.standard; + +import java.util.Arrays; +import java.util.List; +import java.util.Properties; + +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.remoting.RemoteAccessException; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase; +import ch.systemsx.cisd.openbis.dss.generic.server.plugins.demo.DemoArchiver; +import ch.systemsx.cisd.openbis.dss.generic.shared.ArchiverTaskContext; +import ch.systemsx.cisd.openbis.dss.generic.shared.IConfigProvider; +import ch.systemsx.cisd.openbis.dss.generic.shared.IDataSetStatusUpdater; +import ch.systemsx.cisd.openbis.dss.generic.shared.IEncapsulatedOpenBISService; +import ch.systemsx.cisd.openbis.dss.generic.shared.ServiceProviderTestWrapper; +import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetArchivingStatus; +import ch.systemsx.cisd.openbis.generic.shared.dto.DatasetDescription; +import ch.systemsx.cisd.openbis.generic.shared.dto.builders.DatasetDescriptionBuilder; + +/** + * @author Kaloyan Enimanev + */ +public class AbstractArchiverProcessingPluginTest extends AbstractFileSystemTestCase +{ + + private static final String DATA_STORE_CODE = "DSS1"; + + private IEncapsulatedOpenBISService service; + + private IConfigProvider configProvider; + + private Mockery context; + + @BeforeMethod + public void beforeMethod() + { + context = new Mockery(); + + final BeanFactory beanFactory = context.mock(BeanFactory.class); + ServiceProviderTestWrapper.setApplicationContext(beanFactory); + + service = ServiceProviderTestWrapper.mock(context, IEncapsulatedOpenBISService.class); + configProvider = ServiceProviderTestWrapper.mock(context, IConfigProvider.class); + + context.checking(new Expectations() + { + { + allowing(configProvider).getDataStoreCode(); + will(returnValue(DATA_STORE_CODE)); + } + }); + + } + + @AfterMethod + public void afterMethod() + { + context.assertIsSatisfied(); + ServiceProviderTestWrapper.restoreApplicationContext(); + } + + @Test + public void testStatusRestoredIfRemoteCallFails() + { + AbstractArchiverProcessingPlugin archiver = + new DemoArchiver(new Properties(), workingDirectory); + final IDataSetStatusUpdater statusUpdater = context.mock(IDataSetStatusUpdater.class); + archiver.setStatusUpdater(statusUpdater); + + DatasetDescriptionBuilder builder = new DatasetDescriptionBuilder("ds1"); + List<DatasetDescription> datasets = Arrays.asList(builder.getDatasetDescription()); + ArchiverTaskContext archiverContext = new ArchiverTaskContext(null); + + context.checking(new Expectations() + { + { + one(service).listDataSets(); + will(throwException(new RemoteAccessException("service offline"))); + + one(statusUpdater).update(Arrays.asList("ds1"), DataSetArchivingStatus.ARCHIVED , true); + } + }); + + archiver.unarchive(datasets, archiverContext); + + } + +} diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/RsyncArchiverTest.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/RsyncArchiverTest.java index e33927cd413c99f9e58c7165e6848630e73c41ef..4cbd358d132c9fb3bb787a1e8b36a8496369354c 100644 --- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/RsyncArchiverTest.java +++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/server/plugins/standard/RsyncArchiverTest.java @@ -218,8 +218,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase one(statusUpdater).update(Arrays.asList("ds1"), DataSetArchivingStatus.ARCHIVED, true); - one(statusUpdater).update(Arrays.<String> asList(), - DataSetArchivingStatus.AVAILABLE, false); } }); @@ -255,8 +253,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase one(statusUpdater).update(Arrays.asList("ds1"), DataSetArchivingStatus.AVAILABLE, true); - one(statusUpdater).update(Arrays.<String> asList(), - DataSetArchivingStatus.AVAILABLE, false); } }); @@ -308,8 +304,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase one(statusUpdater).update(Arrays.asList("ds1"), DataSetArchivingStatus.AVAILABLE, true); - one(statusUpdater).update(Arrays.<String> asList(), - DataSetArchivingStatus.ARCHIVED, true); } }); @@ -365,8 +359,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase one(shareIdManager).getShareId("ds1"); will(returnValue("2")); - one(statusUpdater).update(Arrays.<String>asList(), - DataSetArchivingStatus.AVAILABLE, true); one(statusUpdater).update(Arrays.asList("ds1", "ds2"), DataSetArchivingStatus.ARCHIVED, true); } diff --git a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProviderTestWrapper.java b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProviderTestWrapper.java index fa141f65de08793dc537fd7dbab199c990b392cc..c2124b6a7f0773916764efce9917189babbdf8f4 100644 --- a/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProviderTestWrapper.java +++ b/datastore_server/sourceTest/java/ch/systemsx/cisd/openbis/dss/generic/shared/ServiceProviderTestWrapper.java @@ -16,6 +16,11 @@ package ch.systemsx.cisd.openbis.dss.generic.shared; +import java.util.HashMap; +import java.util.Map; + +import org.jmock.Expectations; +import org.jmock.Mockery; import org.springframework.beans.factory.BeanFactory; /** @@ -28,8 +33,18 @@ import org.springframework.beans.factory.BeanFactory; public class ServiceProviderTestWrapper { + private static BeanFactory mockApplicationContext; private static BeanFactory cachedApplicationContext; + private final static Map<String /* classname */, String /* bean name */> classNameToBeanName; + static + { + classNameToBeanName = new HashMap<String, String>(); + classNameToBeanName.put(IEncapsulatedOpenBISService.class.getName(), "openBIS-service"); + classNameToBeanName.put(IShareIdManager.class.getName(), "share-id-manager"); + classNameToBeanName.put(IConfigProvider.class.getName(), "config-provider"); + } + /** * caches the existing application context and replaces it temporarily with another one for test * purposes. @@ -41,6 +56,7 @@ public class ServiceProviderTestWrapper { cachedApplicationContext = ServiceProvider.tryGetApplicationContext(false); } + mockApplicationContext = applicationContext; ServiceProvider.setBeanFactory(applicationContext); } @@ -53,6 +69,26 @@ public class ServiceProviderTestWrapper { ServiceProvider.setBeanFactory(cachedApplicationContext); cachedApplicationContext = null; + mockApplicationContext = null; + } + + /** + * A helper method for test cases that creates a mock instance and sets it up within the mocked + * application context of ServiceProvider. + */ + public static <T> T mock(Mockery mockery, final Class<T> clazz) + { + final T mock = mockery.mock(clazz); + mockery.checking(new Expectations() + { + { + String beanName = classNameToBeanName.get(clazz.getName()); + allowing(mockApplicationContext).getBean(beanName); + will(returnValue(mock)); + } + }); + + return mock; } }