Skip to content
Snippets Groups Projects
Commit 23a08398 authored by kaloyane's avatar kaloyane
Browse files

[LMS-2250] added a unit test

SVN: 21471
parent 509dcae5
No related branches found
No related tags found
No related merge requests found
...@@ -462,6 +462,11 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore ...@@ -462,6 +462,11 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore
private void asyncUpdateStatuses(List<String> dataSetCodes, DataSetArchivingStatus newStatus, private void asyncUpdateStatuses(List<String> dataSetCodes, DataSetArchivingStatus newStatus,
boolean presentInArchive) boolean presentInArchive)
{ {
if (dataSetCodes.isEmpty())
{
return;
}
if (statusUpdater == null) if (statusUpdater == null)
{ {
statusUpdater = new IDataSetStatusUpdater() statusUpdater = new IDataSetStatusUpdater()
...@@ -572,6 +577,12 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore ...@@ -572,6 +577,12 @@ public abstract class AbstractArchiverProcessingPlugin extends AbstractDatastore
return service; return service;
} }
@Private
void setStatusUpdater(IDataSetStatusUpdater statusUpdater)
{
this.statusUpdater = statusUpdater;
}
/** /**
* @author Franz-Josef Elmer * @author Franz-Josef Elmer
*/ */
......
/*
* 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);
}
}
...@@ -218,8 +218,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase ...@@ -218,8 +218,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase
one(statusUpdater).update(Arrays.asList("ds1"), one(statusUpdater).update(Arrays.asList("ds1"),
DataSetArchivingStatus.ARCHIVED, true); DataSetArchivingStatus.ARCHIVED, true);
one(statusUpdater).update(Arrays.<String> asList(),
DataSetArchivingStatus.AVAILABLE, false);
} }
}); });
...@@ -255,8 +253,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase ...@@ -255,8 +253,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase
one(statusUpdater).update(Arrays.asList("ds1"), one(statusUpdater).update(Arrays.asList("ds1"),
DataSetArchivingStatus.AVAILABLE, true); DataSetArchivingStatus.AVAILABLE, true);
one(statusUpdater).update(Arrays.<String> asList(),
DataSetArchivingStatus.AVAILABLE, false);
} }
}); });
...@@ -308,8 +304,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase ...@@ -308,8 +304,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase
one(statusUpdater).update(Arrays.asList("ds1"), one(statusUpdater).update(Arrays.asList("ds1"),
DataSetArchivingStatus.AVAILABLE, true); DataSetArchivingStatus.AVAILABLE, true);
one(statusUpdater).update(Arrays.<String> asList(),
DataSetArchivingStatus.ARCHIVED, true);
} }
}); });
...@@ -365,8 +359,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase ...@@ -365,8 +359,6 @@ public class RsyncArchiverTest extends AbstractFileSystemTestCase
one(shareIdManager).getShareId("ds1"); one(shareIdManager).getShareId("ds1");
will(returnValue("2")); will(returnValue("2"));
one(statusUpdater).update(Arrays.<String>asList(),
DataSetArchivingStatus.AVAILABLE, true);
one(statusUpdater).update(Arrays.asList("ds1", "ds2"), DataSetArchivingStatus.ARCHIVED, one(statusUpdater).update(Arrays.asList("ds1", "ds2"), DataSetArchivingStatus.ARCHIVED,
true); true);
} }
......
...@@ -16,6 +16,11 @@ ...@@ -16,6 +16,11 @@
package ch.systemsx.cisd.openbis.dss.generic.shared; 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; import org.springframework.beans.factory.BeanFactory;
/** /**
...@@ -28,8 +33,18 @@ import org.springframework.beans.factory.BeanFactory; ...@@ -28,8 +33,18 @@ import org.springframework.beans.factory.BeanFactory;
public class ServiceProviderTestWrapper public class ServiceProviderTestWrapper
{ {
private static BeanFactory mockApplicationContext;
private static BeanFactory cachedApplicationContext; 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 * caches the existing application context and replaces it temporarily with another one for test
* purposes. * purposes.
...@@ -41,6 +56,7 @@ public class ServiceProviderTestWrapper ...@@ -41,6 +56,7 @@ public class ServiceProviderTestWrapper
{ {
cachedApplicationContext = ServiceProvider.tryGetApplicationContext(false); cachedApplicationContext = ServiceProvider.tryGetApplicationContext(false);
} }
mockApplicationContext = applicationContext;
ServiceProvider.setBeanFactory(applicationContext); ServiceProvider.setBeanFactory(applicationContext);
} }
...@@ -53,6 +69,26 @@ public class ServiceProviderTestWrapper ...@@ -53,6 +69,26 @@ public class ServiceProviderTestWrapper
{ {
ServiceProvider.setBeanFactory(cachedApplicationContext); ServiceProvider.setBeanFactory(cachedApplicationContext);
cachedApplicationContext = null; 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;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment