Skip to content
Snippets Groups Projects
Commit 8ab77b11 authored by mpukhliak's avatar mpukhliak Committed by felmer
Browse files

make RequestArchivingPostRegistrationTask idempotent.

parent 77a74064
No related branches found
No related tags found
No related merge requests found
......@@ -16,9 +16,10 @@
package ch.systemsx.cisd.etlserver.postregistration;
import java.util.Arrays;
import java.util.Properties;
import java.util.*;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.AbstractExternalData;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.PhysicalDataSet;
import org.apache.log4j.Logger;
import ch.ethz.sis.openbis.generic.asapi.v3.dto.dataset.id.DataSetPermId;
......@@ -46,10 +47,26 @@ public class RequestArchivingPostRegistrationTask extends AbstractPostRegistrati
{
return new IPostRegistrationTaskExecutor()
{
private boolean isAlreadyArchived() {
List<String> codeAsList = Collections.singletonList(dataSetCode);
List<AbstractExternalData> dataList = service.listDataSetsByCode(codeAsList);
if (dataList == null || dataList.isEmpty()) {
return false;
}
AbstractExternalData dataSet = dataList.get(0);
return dataSet instanceof PhysicalDataSet && ((PhysicalDataSet) dataSet).isPresentInArchive();
}
@Override
public void execute()
{
if (isAlreadyArchived()) {
operationLog.info("DataSet " + dataSetCode + " is already in archive.");
return;
}
DataSetUpdate dataSetUpdate = new DataSetUpdate();
dataSetUpdate.setDataSetId(new DataSetPermId(dataSetCode));
PhysicalDataUpdate physicalDataUpdate = new PhysicalDataUpdate();
......
......@@ -18,11 +18,18 @@ package ch.systemsx.cisd.etlserver.postregistration;
import static org.testng.Assert.assertEquals;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import ch.systemsx.cisd.common.logging.BufferedAppender;
import ch.systemsx.cisd.common.test.AssertionUtil;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.PhysicalDataSet;
import ch.systemsx.cisd.openbis.util.LogRecordingUtils;
import org.apache.log4j.Level;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
......@@ -44,24 +51,50 @@ public class RequestArchivingPostRegistrationTaskTest
private IApplicationServerApi v3api;
private BufferedAppender logRecorder;
@BeforeMethod
public void setUp()
{
logRecorder = LogRecordingUtils.createRecorder("%-5p %c - %m%n", Level.INFO);
context = new Mockery();
service = context.mock(IEncapsulatedOpenBISService.class);
v3api = context.mock(IApplicationServerApi.class);
}
@AfterMethod(alwaysRun = true)
public void afterMethod()
{
if (logRecorder != null)
{
logRecorder.reset();
}
if (context != null)
{
// The following line of code should also be called at the end of each test method.
// Otherwise one do not known which test failed.
context.assertIsSatisfied();
}
}
@Test
public void test()
public void testNotArchivedDataSet()
{
// Given
Properties properties = new Properties();
IPostRegistrationTask task = createTask(properties);
RecordingMatcher<List<DataSetUpdate>> recordedUpdates = new RecordingMatcher<List<DataSetUpdate>>();
final PhysicalDataSet physicalDataset = new PhysicalDataSet();
physicalDataset.setCode("ds1");
physicalDataset.setPresentInArchive(false);
context.checking(new Expectations()
{
{
one(service).listDataSetsByCode(with(Collections.singletonList("ds1")));
will(returnValue(Collections.singletonList(physicalDataset)));
one(service).getSessionToken();
will(returnValue(SESSION_TOKEN));
one(v3api).updateDataSets(with(SESSION_TOKEN), with(recordedUpdates));
......@@ -81,6 +114,35 @@ public class RequestArchivingPostRegistrationTaskTest
context.assertIsSatisfied();
}
@Test
public void testArchivedDataSet()
{
// Given
Properties properties = new Properties();
IPostRegistrationTask task = createTask(properties);
RecordingMatcher<List<DataSetUpdate>> recordedUpdates = new RecordingMatcher<List<DataSetUpdate>>();
final PhysicalDataSet physicalDataset = new PhysicalDataSet();
physicalDataset.setCode("ds1");
physicalDataset.setPresentInArchive(true);
context.checking(new Expectations()
{
{
one(service).listDataSetsByCode(with(Collections.singletonList("ds1")));
will(returnValue(Collections.singletonList(physicalDataset)));
}
});
// When
task.createExecutor("ds1", false).execute();
// Then
AssertionUtil.assertContains("DataSet ds1 is already in archive.", logRecorder.getLogContent());
context.assertIsSatisfied();
}
private IPostRegistrationTask createTask(Properties properties)
{
return new RequestArchivingPostRegistrationTask(properties, service)
......
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