Skip to content
Snippets Groups Projects
Commit 94bb5fef authored by felmer's avatar felmer
Browse files

LMS-761 ExternalDataBOTest created. Preparation of DAOFactory moved to AbstractBOTest

SVN: 9948
parent d244342d
No related branches found
No related tags found
No related merge requests found
......@@ -23,12 +23,15 @@ import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDataSetTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDatabaseInstanceDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IEntityPropertyTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IEntityTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IExperimentDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IExternalDataDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IFileFormatTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IGroupDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.ILocatorTypeDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IMaterialDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IPersonDAO;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.IProcedureDAO;
......@@ -82,6 +85,12 @@ public abstract class AbstractBOTest extends AssertJUnit
IProcedureTypeDAO procedureTypeDAO;
IDataSetTypeDAO dataSetTypeDAO;
IFileFormatTypeDAO fileFormatTypeDAO;
ILocatorTypeDAO locatorTypeDAO;
@BeforeMethod
public void beforeMethod()
{
......@@ -103,7 +112,9 @@ public abstract class AbstractBOTest extends AssertJUnit
procedureDAO = context.mock(IProcedureDAO.class);
procedureTypeDAO = context.mock(IProcedureTypeDAO.class);
materialDAO = context.mock(IMaterialDAO.class);
dataSetTypeDAO = context.mock(IDataSetTypeDAO.class);
fileFormatTypeDAO = context.mock(IFileFormatTypeDAO.class);
locatorTypeDAO = context.mock(ILocatorTypeDAO.class);
context.checking(new Expectations()
{
{
......@@ -111,10 +122,24 @@ public abstract class AbstractBOTest extends AssertJUnit
will(returnValue(databaseInstanceDAO));
allowing(daoFactory).getGroupDAO();
will(returnValue(groupDAO));
allowing(daoFactory).getVocabularyDAO();
will(returnValue(vocabularyDAO));
allowing(daoFactory).getSampleDAO();
will(returnValue(sampleDAO));
allowing(daoFactory).getSampleTypeDAO();
will(returnValue(sampleTypeDAO));
allowing(daoFactory).getDataSetTypeDAO();
will(returnValue(dataSetTypeDAO));
allowing(daoFactory).getFileFormatTypeDAO();
will(returnValue(fileFormatTypeDAO));
allowing(daoFactory).getLocatorTypeDAO();
will(returnValue(locatorTypeDAO));
allowing(daoFactory).getExternalDataDAO();
will(returnValue(externalDataDAO));
allowing(daoFactory).getProcedureTypeDAO();
will(returnValue(procedureTypeDAO));
allowing(daoFactory).getProcedureDAO();
will(returnValue(procedureDAO));
}
});
}
......
/*
* Copyright 2009 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.generic.server.business.bo;
import static ch.systemsx.cisd.openbis.generic.server.business.ManagerTestTool.EXAMPLE_SESSION;
import java.util.Date;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.jmock.Expectations;
import org.testng.annotations.Test;
import ch.systemsx.cisd.common.types.BooleanOrUnknown;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetType;
import ch.systemsx.cisd.openbis.generic.shared.dto.DataSetTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExperimentPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExternalData;
import ch.systemsx.cisd.openbis.generic.shared.dto.ExternalDataPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.FileFormatType;
import ch.systemsx.cisd.openbis.generic.shared.dto.FileFormatTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.GroupPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.LocatorType;
import ch.systemsx.cisd.openbis.generic.shared.dto.LocatorTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ProcedurePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ProcedureTypePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.ProjectPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.SamplePE;
import ch.systemsx.cisd.openbis.generic.shared.dto.SourceType;
import ch.systemsx.cisd.openbis.generic.shared.dto.StorageFormat;
import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyTermPE;
import ch.systemsx.cisd.openbis.generic.shared.dto.types.DataSetTypeCode;
import ch.systemsx.cisd.openbis.generic.shared.dto.types.ProcedureTypeCode;
/**
*
*
* @author Franz-Josef Elmer
*/
public class ExternalDataBOTest extends AbstractBOTest
{
private static final String PARENT_CODE = "parent";
private static final Date PRODUCTION_DATE = new Date(1001);
private static final String DATA_PRODUCER_CODE = "DPC";
private static final String DATA_SET_CODE = "DS1";
private static final String LOCATION = "folder/subfolder/file.fft";
private static final LocatorType LOCATOR_TYPE = new LocatorType("LT");
private static final FileFormatType FILE_FORMAT_TYPE = new FileFormatType("FFT");
private static final DataSetType DATA_SET_TYPE = new DataSetType("DST");
@Test
public void testDefineWithoutParentDataSet()
{
final DataSetTypePE dataSetType = new DataSetTypePE();
final FileFormatTypePE fileFormatType = new FileFormatTypePE();
final VocabularyPE vocabulary = new VocabularyPE();
vocabulary.addTerm(new VocabularyTermPE());
VocabularyTermPE vocabularyTerm = new VocabularyTermPE();
vocabularyTerm.setCode(StorageFormat.PROPRIETARY.toString());
vocabulary.addTerm(vocabularyTerm);
final LocatorTypePE locatorType = new LocatorTypePE();
ProcedurePE procedure = new ProcedurePE();
SamplePE sample = new SamplePE();
prepareDefine(dataSetType, fileFormatType, vocabulary, locatorType);
IExternalDataBO sampleBO = createSampleBO();
sampleBO.define(createData(null), procedure, sample, SourceType.DERIVED);
ExternalDataPE externalData = sampleBO.getExternalData();
assertEquals(DATA_SET_CODE, externalData.getCode());
assertEquals(BooleanOrUnknown.U, externalData.getComplete());
assertEquals(DATA_PRODUCER_CODE, externalData.getDataProducerCode());
assertSame(dataSetType, externalData.getDataSetType());
assertSame(fileFormatType, externalData.getFileFormatType());
assertSame(locatorType, externalData.getLocatorType());
assertEquals(LOCATION, externalData.getLocation());
assertEquals(0, externalData.getParents().size());
assertSame(procedure, externalData.getProcedure());
assertEquals(PRODUCTION_DATE, externalData.getProductionDate());
assertSame(null, externalData.getSampleAcquiredFrom());
assertSame(sample, externalData.getSampleDerivedFrom());
assertEquals(StorageFormat.PROPRIETARY, externalData.getStorageFormat());
assertSame(vocabularyTerm, externalData.getStorageFormatVocabularyTerm());
context.assertIsSatisfied();
}
@Test
public void testDefineWithExistingParentDataSet()
{
final DataSetTypePE dataSetType = new DataSetTypePE();
final FileFormatTypePE fileFormatType = new FileFormatTypePE();
final VocabularyPE vocabulary = new VocabularyPE();
vocabulary.addTerm(new VocabularyTermPE());
VocabularyTermPE vocabularyTerm = new VocabularyTermPE();
vocabularyTerm.setCode(StorageFormat.PROPRIETARY.toString());
vocabulary.addTerm(vocabularyTerm);
final LocatorTypePE locatorType = new LocatorTypePE();
ProcedurePE procedure = new ProcedurePE();
SamplePE sample = new SamplePE();
prepareDefine(dataSetType, fileFormatType, vocabulary, locatorType);
final DataPE data = new DataPE();
context.checking(new Expectations()
{
{
one(externalDataDAO).tryToFindDataSetByCode(PARENT_CODE);
will(returnValue(data));
}
});
IExternalDataBO sampleBO = createSampleBO();
sampleBO.define(createData(PARENT_CODE), procedure, sample, SourceType.MEASUREMENT);
ExternalDataPE externalData = sampleBO.getExternalData();
assertSame(sample, externalData.getSampleAcquiredFrom());
assertSame(null, externalData.getSampleDerivedFrom());
assertEquals(1, externalData.getParents().size());
assertSame(data, externalData.getParents().iterator().next());
context.assertIsSatisfied();
}
@Test
public void testDefineWithNonExistingParentDataSet()
{
final DataSetTypePE dataSetType = new DataSetTypePE();
final FileFormatTypePE fileFormatType = new FileFormatTypePE();
final VocabularyPE vocabulary = new VocabularyPE();
vocabulary.addTerm(new VocabularyTermPE());
VocabularyTermPE vocabularyTerm = new VocabularyTermPE();
vocabularyTerm.setCode(StorageFormat.PROPRIETARY.toString());
vocabulary.addTerm(vocabularyTerm);
final LocatorTypePE locatorType = new LocatorTypePE();
final ProcedurePE procedure = new ProcedurePE();
ProcedureTypePE procedureType = new ProcedureTypePE();
procedureType.setCode(ProcedureTypeCode.UNKNOWN.getCode());
procedure.setProcedureType(procedureType);
procedure.setExperiment(createExperiment());
final SamplePE sample = new SamplePE();
prepareDefine(dataSetType, fileFormatType, vocabulary, locatorType);
final DataSetTypePE dataSetTypeUnknown = new DataSetTypePE();
final DataPE parentData = new DataPE();
parentData.setCode(PARENT_CODE);
parentData.setDataSetType(dataSetTypeUnknown);
parentData.setProcedure(procedure);
parentData.setSampleDerivedFrom(sample);
parentData.setPlaceholder(true);
context.checking(new Expectations()
{
{
one(externalDataDAO).tryToFindDataSetByCode(PARENT_CODE);
will(returnValue(null));
one(dataSetTypeDAO).tryToFindDataSetTypeByCode(ProcedureTypeCode.UNKNOWN.getCode());
will(returnValue(dataSetTypeUnknown));
one(externalDataDAO).createDataSet(parentData);
}
});
IExternalDataBO sampleBO = createSampleBO();
sampleBO.define(createData(PARENT_CODE), procedure, sample, SourceType.MEASUREMENT);
ExternalDataPE externalData = sampleBO.getExternalData();
assertSame(sample, externalData.getSampleAcquiredFrom());
assertSame(null, externalData.getSampleDerivedFrom());
assertEquals(1, externalData.getParents().size());
assertEquals(parentData, externalData.getParents().iterator().next());
context.assertIsSatisfied();
}
@Test
public void testDefineWithNonExistingParentDataSetAndNonExsitingProcedure()
{
final DataSetTypePE dataSetType = new DataSetTypePE();
final FileFormatTypePE fileFormatType = new FileFormatTypePE();
final VocabularyPE vocabulary = new VocabularyPE();
vocabulary.addTerm(new VocabularyTermPE());
VocabularyTermPE vocabularyTerm = new VocabularyTermPE();
vocabularyTerm.setCode(StorageFormat.PROPRIETARY.toString());
vocabulary.addTerm(vocabularyTerm);
final LocatorTypePE locatorType = new LocatorTypePE();
final ProcedurePE procedure = new ProcedurePE();
final ProcedureTypePE procedureType = new ProcedureTypePE();
procedureType.setCode("new");
procedure.setProcedureType(procedureType);
procedure.setExperiment(createExperiment());
final SamplePE sample = new SamplePE();
prepareDefine(dataSetType, fileFormatType, vocabulary, locatorType);
final DataSetTypePE dataSetTypeUnknown = new DataSetTypePE();
final DataPE parentData = new DataPE();
parentData.setCode(PARENT_CODE);
parentData.setDataSetType(dataSetTypeUnknown);
parentData.setProcedure(procedure);
parentData.setSampleDerivedFrom(sample);
parentData.setPlaceholder(true);
final ProcedureTypePE procedureTypeUnknown = new ProcedureTypePE();
context.checking(new Expectations()
{
{
one(externalDataDAO).tryToFindDataSetByCode(PARENT_CODE);
will(returnValue(null));
one(dataSetTypeDAO).tryToFindDataSetTypeByCode(
DataSetTypeCode.UNKNOWN.getCode());
will(returnValue(dataSetTypeUnknown));
one(procedureTypeDAO).tryFindProcedureTypeByCode(
ProcedureTypeCode.UNKNOWN.getCode());
will(returnValue(procedureTypeUnknown));
ProcedurePE newProcedure = new ProcedurePE();
newProcedure.setProcedureType(procedureTypeUnknown);
newProcedure.setExperiment(createExperiment());
one(procedureDAO).createProcedure(newProcedure);
one(externalDataDAO).createDataSet(parentData);
}
});
IExternalDataBO sampleBO = createSampleBO();
sampleBO.define(createData(PARENT_CODE), procedure, sample, SourceType.MEASUREMENT);
ExternalDataPE externalData = sampleBO.getExternalData();
assertSame(sample, externalData.getSampleAcquiredFrom());
assertSame(null, externalData.getSampleDerivedFrom());
assertEquals(1, externalData.getParents().size());
assertEquals(parentData, externalData.getParents().iterator().next());
context.assertIsSatisfied();
}
@Test
public void testSaveNewDataSet()
{
final DataSetTypePE dataSetType = new DataSetTypePE();
final FileFormatTypePE fileFormatType = new FileFormatTypePE();
final VocabularyPE vocabulary = new VocabularyPE();
vocabulary.addTerm(new VocabularyTermPE());
VocabularyTermPE vocabularyTerm = new VocabularyTermPE();
vocabularyTerm.setCode(StorageFormat.PROPRIETARY.toString());
vocabulary.addTerm(vocabularyTerm);
final LocatorTypePE locatorType = new LocatorTypePE();
ProcedurePE procedure = new ProcedurePE();
SamplePE sample = new SamplePE();
prepareDefine(dataSetType, fileFormatType, vocabulary, locatorType);
context.checking(new Expectations()
{
{
one(externalDataDAO).tryToFindDataSetByCode(DATA_SET_CODE);
will(returnValue(null));
one(externalDataDAO).createDataSet(with(new BaseMatcher<DataPE>()
{
public void describeTo(Description description)
{
description.appendText(DATA_SET_CODE);
}
public boolean matches(Object item)
{
if (item instanceof DataPE)
{
DataPE data = (DataPE) item;
return data.getCode().equals(DATA_SET_CODE);
}
return false;
}
}));
}
});
IExternalDataBO sampleBO = createSampleBO();
sampleBO.define(createData(null), procedure, sample, SourceType.DERIVED);
sampleBO.save();
context.assertIsSatisfied();
}
private ExperimentPE createExperiment()
{
ExperimentPE experiment = new ExperimentPE();
experiment.setCode("EXP1");
ProjectPE project = new ProjectPE();
project.setCode("P");
GroupPE group = new GroupPE();
group.setCode("G");
DatabaseInstancePE databaseInstance = new DatabaseInstancePE();
databaseInstance.setCode("DB");
group.setDatabaseInstance(databaseInstance);
project.setGroup(group);
experiment.setProject(project);
return experiment;
}
private void prepareDefine(final DataSetTypePE dataSetType,
final FileFormatTypePE fileFormatType, final VocabularyPE vocabulary,
final LocatorTypePE locatorType)
{
context.checking(new Expectations()
{
{
one(dataSetTypeDAO).tryToFindDataSetTypeByCode(DATA_SET_TYPE.getCode());
will(returnValue(dataSetType));
one(fileFormatTypeDAO).tryToFindFileFormatTypeByCode(FILE_FORMAT_TYPE.getCode());
will(returnValue(fileFormatType));
one(vocabularyDAO).tryFindVocabularyByCode(StorageFormat.VOCABULARY_CODE);
will(returnValue(vocabulary));
one(locatorTypeDAO).tryToFindLocatorTypeByCode(LOCATOR_TYPE.getCode());
will(returnValue(locatorType));
}
});
}
private ExternalData createData(String parentDataSetCodeOrNull)
{
ExternalData data = new ExternalData();
data.setCode(DATA_SET_CODE);
data.setParentDataSetCode(parentDataSetCodeOrNull);
data.setDataProducerCode(DATA_PRODUCER_CODE);
data.setProductionDate(PRODUCTION_DATE);
data.setComplete(BooleanOrUnknown.U);
data.setStorageFormat(StorageFormat.PROPRIETARY);
data.setDataSetType(DATA_SET_TYPE);
data.setFileFormatType(FILE_FORMAT_TYPE);
data.setLocatorType(LOCATOR_TYPE);
data.setLocation(LOCATION);
return data;
}
private final IExternalDataBO createSampleBO()
{
return new ExternalDataBO(daoFactory, EXAMPLE_SESSION);
}
}
......@@ -91,9 +91,6 @@ public final class ExternalDataTableTest extends AbstractBOTest
context.checking(new Expectations()
{
{
allowing(daoFactory).getExternalDataDAO();
will(returnValue(externalDataDAO));
one(databaseInstanceDAO).tryFindDatabaseInstanceByCode(dbCode);
will(returnValue(databaseInstancePE));
......
......@@ -48,9 +48,6 @@ public final class ProcedureBOTest extends AbstractBOTest
context.checking(new Expectations()
{
{
one(daoFactory).getProcedureTypeDAO();
will(returnValue(procedureTypeDAO));
one(procedureTypeDAO).tryFindProcedureTypeByCode(procedureTypeCode);
will(returnValue(procedureType));
}
......@@ -105,9 +102,6 @@ public final class ProcedureBOTest extends AbstractBOTest
context.checking(new Expectations()
{
{
one(daoFactory).getProcedureDAO();
will(returnValue(procedureDAO));
one(procedureDAO).createProcedure(procedure);
}
});
......@@ -129,9 +123,6 @@ public final class ProcedureBOTest extends AbstractBOTest
context.checking(new Expectations()
{
{
one(daoFactory).getProcedureDAO();
will(returnValue(procedureDAO));
one(procedureDAO).createProcedure(procedure);
will(throwException(new DataIntegrityViolationException("data access problem")));
}
......@@ -159,9 +150,6 @@ public final class ProcedureBOTest extends AbstractBOTest
context.checking(new Expectations()
{
{
one(daoFactory).getProcedureTypeDAO();
will(returnValue(procedureTypeDAO));
one(procedureTypeDAO).tryFindProcedureTypeByCode(procedureTypeCode);
will(returnValue(procedureTypeDTO));
}
......
......@@ -189,9 +189,6 @@ public final class PropertyTypeBOTest extends AbstractBOTest
one(propertyTypeDAO).getDataTypeByCode(EntityDataType.CONTROLLEDVOCABULARY);
will(returnValue(dataTypePE));
allowing(daoFactory).getVocabularyDAO();
will(returnValue(vocabularyDAO));
one(vocabularyDAO).tryFindVocabularyByCode(vocabulary.getCode());
will(returnValue(new VocabularyPE()));
}
......
......@@ -141,15 +141,13 @@ public final class VocabularyBOTest extends AbstractBOTest
one(daoFactory).getHomeDatabaseInstance();
will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE));
one(daoFactory).getVocabularyDAO();
will(returnValue(vocabularyDAO));
one(vocabularyDAO).createVocabulary(with(aNonNull(VocabularyPE.class)));
}
});
final Vocabulary vocabulary = createVocabulary();
vocabularyBO.define(vocabulary);
vocabularyBO.save();
context.assertIsSatisfied();
}
@Test
......@@ -162,9 +160,6 @@ public final class VocabularyBOTest extends AbstractBOTest
one(daoFactory).getHomeDatabaseInstance();
will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE));
one(daoFactory).getVocabularyDAO();
will(returnValue(vocabularyDAO));
one(vocabularyDAO).createVocabulary(with(aNonNull(VocabularyPE.class)));
will(throwException(new DataIntegrityViolationException(null)));
}
......@@ -179,5 +174,6 @@ public final class VocabularyBOTest extends AbstractBOTest
{
// Nothing to do here.
}
context.assertIsSatisfied();
}
}
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