From 94ccda93adebb4f662b3460e87905d0d5765f395 Mon Sep 17 00:00:00 2001 From: ribeaudc <ribeaudc> Date: Sat, 20 Dec 2008 20:51:02 +0000 Subject: [PATCH] [LMS-709] add: - Unit tests for '*BO'. SVN: 9450 --- .../server/business/bo/IPropertyTypeBO.java | 5 + .../server/business/bo/PropertyTypeBO.java | 6 + .../server/business/bo/VocabularyBO.java | 1 + .../server/business/bo/AbstractBOTest.java | 10 +- .../business/bo/PropertyTypeBOTest.java | 215 ++++++++++++++++++ .../server/business/bo/VocabularyBOTest.java | 156 +++++++++++++ 6 files changed, 387 insertions(+), 6 deletions(-) diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/IPropertyTypeBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/IPropertyTypeBO.java index 8d67ca19c57..fe552be0b76 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/IPropertyTypeBO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/IPropertyTypeBO.java @@ -39,4 +39,9 @@ public interface IPropertyTypeBO extends IBusinessObject */ public void define(final PropertyType propertyType) throws UserFailureException; + /** + * Returns the loaded {@link PropertyTypePE}. + */ + public PropertyTypePE getPropertyType(); + } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBO.java index c04f94d23c8..ccbcb926d12 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBO.java @@ -47,6 +47,7 @@ public final class PropertyTypeBO extends VocabularyBO implements IPropertyTypeB public final void define(final PropertyType propertyType) throws UserFailureException { + assert propertyType != null : "Unspecified property type."; propertyTypePE = new PropertyTypePE(); propertyTypePE.setDatabaseInstance(getHomeDatabaseInstance()); propertyTypePE.setCode(propertyType.getCode()); @@ -94,4 +95,9 @@ public final class PropertyTypeBO extends VocabularyBO implements IPropertyTypeB } } + public final PropertyTypePE getPropertyType() + { + assert propertyTypePE != null : "Property type not defined."; + return propertyTypePE; + } } diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java index 7648e8cdb48..35b0b5fb715 100644 --- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java +++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBO.java @@ -46,6 +46,7 @@ public class VocabularyBO extends AbstractBusinessObject implements IVocabularyB public final void define(final Vocabulary vocabulary) throws UserFailureException { + assert vocabulary != null : "Unspecified vocabulary."; vocabularyPE = new VocabularyPE(); vocabularyPE.setCode(vocabulary.getCode()); vocabularyPE.setDescription(vocabulary.getDescription()); diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractBOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractBOTest.java index ceced47f37c..d2553d244ee 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractBOTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/AbstractBOTest.java @@ -16,13 +16,11 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo; -import org.apache.log4j.Level; import org.jmock.Mockery; import org.testng.AssertJUnit; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; -import ch.systemsx.cisd.common.logging.BufferedAppender; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDatabaseInstanceDAO; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IEntityPropertyTypeDAO; @@ -35,6 +33,7 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.IProjectDAO; import ch.systemsx.cisd.openbis.generic.server.dataaccess.IPropertyTypeDAO; import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISampleDAO; import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISampleTypeDAO; +import ch.systemsx.cisd.openbis.generic.server.dataaccess.IVocabularyDAO; /** * An <i>abstract</i> test for <i>Business Object</i>. @@ -43,8 +42,6 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.ISampleTypeDAO; */ public abstract class AbstractBOTest extends AssertJUnit { - BufferedAppender logRecorder; - Mockery context; IDAOFactory daoFactory; @@ -71,6 +68,8 @@ public abstract class AbstractBOTest extends AssertJUnit ISampleTypeDAO sampleTypeDAO; + IVocabularyDAO vocabularyDAO; + IEntityPropertiesConverter propertiesConverter; @BeforeMethod @@ -90,13 +89,12 @@ public abstract class AbstractBOTest extends AssertJUnit sampleTypeDAO = context.mock(ISampleTypeDAO.class); propertyTypeDAO = context.mock(IPropertyTypeDAO.class); entityPropertyTypeDAO = context.mock(IEntityPropertyTypeDAO.class); - logRecorder = new BufferedAppender("%m%n", Level.DEBUG); + vocabularyDAO = context.mock(IVocabularyDAO.class); } @AfterMethod public void afterMethod() { - logRecorder.reset(); // To 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(); diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBOTest.java index b4830458bc4..750bfc45d81 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBOTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyTypeBOTest.java @@ -16,12 +16,227 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo; +import org.jmock.Expectations; +import org.springframework.dao.DataIntegrityViolationException; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DataType; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.DataTypeCode; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.PropertyType; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Vocabulary; +import ch.systemsx.cisd.openbis.generic.server.business.ManagerTestTool; +import ch.systemsx.cisd.openbis.generic.shared.dto.DataTypePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE; +import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyPE; +import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityDataType; + /** * Test cases for corresponding {@link PropertyTypeBO} class. * * @author Christian Ribeaud */ public final class PropertyTypeBOTest extends AbstractBOTest + { + private static final String DATA_TYPE_DESCRIPTION = "A string data type."; + + private static final String PROPERTY_TYPE_LABEL = "Color"; + + private static final String PROPERTY_TYPE_DESCRIPTION = "A color."; + + private static final String PROPERTY_TYPE_CODE = "USER.COLOR"; + + private final PropertyTypeBO createPropertyTypeBO() + { + return new PropertyTypeBO(daoFactory, ManagerTestTool.EXAMPLE_SESSION); + } + + static final PropertyType createPropertyType(final DataTypeCode dataTypeCode) + { + final PropertyType propertyType = new PropertyType(); + propertyType.setCode(PROPERTY_TYPE_CODE); + propertyType.setDescription(PROPERTY_TYPE_DESCRIPTION); + propertyType.setDataType(createDataType(dataTypeCode)); + propertyType.setLabel(PROPERTY_TYPE_LABEL); + return propertyType; + } + + final static DataType createDataType(final DataTypeCode dataTypeCode) + { + final DataType dataType = new DataType(); + dataType.setCode(dataTypeCode); + dataType.setDescription(DATA_TYPE_DESCRIPTION); + return dataType; + } + + final static void assertPropertyTypeEquals(final PropertyType propertyType, + final PropertyTypePE propertyTypePE) + { + assertEquals(propertyType.getCode(), propertyTypePE.getCode()); + assertEquals(propertyType.getLabel(), propertyTypePE.getLabel()); + assertEquals(propertyType.getDescription(), propertyTypePE.getDescription()); + assertEquals(propertyType.getDescription(), propertyTypePE.getDescription()); + assertEquals(propertyType.getDataType().getCode().name(), propertyTypePE.getType() + .getCode().name()); + } + + @Test + public final void testWithNull() + { + boolean fail = true; + try + { + createPropertyTypeBO().define((PropertyType) null); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + fail = true; + try + { + createPropertyTypeBO().save(); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + context.assertIsSatisfied(); + } + + @Test + public final void testDefineAndSave() + { + final DataTypePE dataTypePE = new DataTypePE(); + dataTypePE.setCode(EntityDataType.VARCHAR); + context.checking(new Expectations() + { + { + one(daoFactory).getHomeDatabaseInstance(); + will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE)); + + allowing(daoFactory).getPropertyTypeDAO(); + will(returnValue(propertyTypeDAO)); + + one(propertyTypeDAO).getDataTypeByCode(EntityDataType.VARCHAR); + will(returnValue(dataTypePE)); + + one(propertyTypeDAO).createPropertyType(with(aNonNull(PropertyTypePE.class))); + } + }); + final PropertyTypeBO propertyTypeBO = createPropertyTypeBO(); + final PropertyType propertyType = createPropertyType(DataTypeCode.VARCHAR); + propertyTypeBO.define(propertyType); + final PropertyTypePE propertyTypePE = propertyTypeBO.getPropertyType(); + assertPropertyTypeEquals(propertyType, propertyTypePE); + propertyTypeBO.save(); + context.assertIsSatisfied(); + } + + @Test + public final void testDefineAndSaveWithException() + { + final DataTypePE dataTypePE = new DataTypePE(); + dataTypePE.setCode(EntityDataType.VARCHAR); + context.checking(new Expectations() + { + { + one(daoFactory).getHomeDatabaseInstance(); + will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE)); + + allowing(daoFactory).getPropertyTypeDAO(); + will(returnValue(propertyTypeDAO)); + + one(propertyTypeDAO).getDataTypeByCode(EntityDataType.VARCHAR); + will(returnValue(dataTypePE)); + + one(propertyTypeDAO).createPropertyType(with(aNonNull(PropertyTypePE.class))); + will(throwException(new DataIntegrityViolationException(null))); + } + }); + final PropertyTypeBO propertyTypeBO = createPropertyTypeBO(); + final PropertyType propertyType = createPropertyType(DataTypeCode.VARCHAR); + propertyTypeBO.define(propertyType); + final PropertyTypePE propertyTypePE = propertyTypeBO.getPropertyType(); + assertPropertyTypeEquals(propertyType, propertyTypePE); + try + { + propertyTypeBO.save(); + fail(String.format("'%s' expected.", UserFailureException.class.getSimpleName())); + } catch (final UserFailureException ex) + { + // Nothing to do here. + } + context.assertIsSatisfied(); + } + + @Test + public final void testDefineWithExistingVocabulary() + { + final DataTypePE dataTypePE = new DataTypePE(); + dataTypePE.setCode(EntityDataType.CONTROLLEDVOCABULARY); + final Vocabulary vocabulary = VocabularyBOTest.createVocabulary(); + context.checking(new Expectations() + { + { + allowing(daoFactory).getHomeDatabaseInstance(); + will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE)); + + one(daoFactory).getPropertyTypeDAO(); + will(returnValue(propertyTypeDAO)); + + one(propertyTypeDAO).getDataTypeByCode(EntityDataType.CONTROLLEDVOCABULARY); + will(returnValue(dataTypePE)); + + allowing(daoFactory).getVocabularyDAO(); + will(returnValue(vocabularyDAO)); + + one(vocabularyDAO).tryFindVocabularyByCode(vocabulary.getCode()); + will(returnValue(new VocabularyPE())); + } + }); + final PropertyTypeBO propertyTypeBO = createPropertyTypeBO(); + final PropertyType propertyType = createPropertyType(DataTypeCode.CONTROLLEDVOCABULARY); + propertyType.setVocabulary(vocabulary); + propertyTypeBO.define(propertyType); + final PropertyTypePE propertyTypePE = propertyTypeBO.getPropertyType(); + assertPropertyTypeEquals(propertyType, propertyTypePE); + context.assertIsSatisfied(); + } + + @Test + public final void testDefineWithNewVocabulary() + { + final DataTypePE dataTypePE = new DataTypePE(); + dataTypePE.setCode(EntityDataType.CONTROLLEDVOCABULARY); + final Vocabulary vocabulary = VocabularyBOTest.createVocabulary(); + context.checking(new Expectations() + { + { + allowing(daoFactory).getHomeDatabaseInstance(); + will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE)); + + one(daoFactory).getPropertyTypeDAO(); + will(returnValue(propertyTypeDAO)); + + one(propertyTypeDAO).getDataTypeByCode(EntityDataType.CONTROLLEDVOCABULARY); + will(returnValue(dataTypePE)); + + allowing(daoFactory).getVocabularyDAO(); + will(returnValue(vocabularyDAO)); + + one(vocabularyDAO).tryFindVocabularyByCode(vocabulary.getCode()); + one(vocabularyDAO).createVocabulary(with(aNonNull(VocabularyPE.class))); + } + }); + final PropertyTypeBO propertyTypeBO = createPropertyTypeBO(); + final PropertyType propertyType = createPropertyType(DataTypeCode.CONTROLLEDVOCABULARY); + propertyType.setVocabulary(vocabulary); + propertyTypeBO.define(propertyType); + final PropertyTypePE propertyTypePE = propertyTypeBO.getPropertyType(); + assertPropertyTypeEquals(propertyType, propertyTypePE); + context.assertIsSatisfied(); + } } diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java index 55cf86b4587..6b1b4268236 100644 --- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java +++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/VocabularyBOTest.java @@ -16,6 +16,19 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo; +import java.util.ArrayList; +import java.util.List; + +import org.jmock.Expectations; +import org.springframework.dao.DataIntegrityViolationException; +import org.testng.annotations.Test; + +import ch.systemsx.cisd.common.exceptions.UserFailureException; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.Vocabulary; +import ch.systemsx.cisd.openbis.generic.client.web.client.dto.VocabularyTerm; +import ch.systemsx.cisd.openbis.generic.server.business.ManagerTestTool; +import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyPE; + /** * Test cases for corresponding {@link VocabularyBO} class. * @@ -23,5 +36,148 @@ package ch.systemsx.cisd.openbis.generic.server.business.bo; */ public final class VocabularyBOTest extends AbstractBOTest { + static final String VOCABULARY_CODE = "USER.COLOR"; + + static final String VOCABULARY_DESCRIPTION = "Some predefined colors"; + + private final VocabularyBO createVocabularyBO() + { + return new VocabularyBO(daoFactory, ManagerTestTool.EXAMPLE_SESSION); + } + + static final Vocabulary createVocabulary() + { + final Vocabulary vocabulary = new Vocabulary(); + vocabulary.setCode(VOCABULARY_CODE); + vocabulary.setDescription(VOCABULARY_DESCRIPTION); + vocabulary.setTerms(createTerms()); + return vocabulary; + } + + final static List<VocabularyTerm> createTerms() + { + final List<VocabularyTerm> terms = new ArrayList<VocabularyTerm>(); + terms.add(createVocabularyTerm("RED")); + terms.add(createVocabularyTerm("YELLOW")); + terms.add(createVocabularyTerm("WHITE")); + return terms; + } + + final static VocabularyTerm createVocabularyTerm(final String code) + { + final VocabularyTerm term = new VocabularyTerm(); + term.setCode(code); + return term; + } + + static void assertVocabularyEquals(final Vocabulary vocabulary, final VocabularyPE vocabularyPE) + { + assertEquals(vocabulary.getCode(), vocabularyPE.getCode()); + assertEquals(vocabulary.getDescription(), vocabularyPE.getDescription()); + final List<VocabularyTerm> terms = vocabulary.getTerms(); + assertNotNull(terms); + assertEquals(terms.size(), vocabularyPE.getTerms().size()); + } + + @Test + public final void testWithNull() + { + boolean fail = true; + try + { + createVocabularyBO().define(null); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + fail = true; + try + { + createVocabularyBO().save(); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + fail = true; + try + { + createVocabularyBO().getVocabulary(); + } catch (final AssertionError e) + { + fail = false; + } + assertFalse(fail); + context.assertIsSatisfied(); + } + + @Test + public final void testDefine() + { + final VocabularyBO vocabularyBO = createVocabularyBO(); + context.checking(new Expectations() + { + { + one(daoFactory).getHomeDatabaseInstance(); + will(returnValue(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE)); + } + }); + final Vocabulary vocabulary = createVocabulary(); + vocabularyBO.define(vocabulary); + final VocabularyPE vocabularyPE = vocabularyBO.getVocabulary(); + assertVocabularyEquals(vocabulary, vocabularyPE); + assertFalse(vocabularyPE.isInternalNamespace()); + context.assertIsSatisfied(); + } + + @Test + public final void testSave() + { + final VocabularyBO vocabularyBO = createVocabularyBO(); + context.checking(new Expectations() + { + { + 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(); + } + + @Test + public final void testSaveWithException() + { + final VocabularyBO vocabularyBO = createVocabularyBO(); + context.checking(new Expectations() + { + { + 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))); + } + }); + final Vocabulary vocabulary = createVocabulary(); + vocabularyBO.define(vocabulary); + try + { + vocabularyBO.save(); + fail(String.format("'%s' expected.", UserFailureException.class.getSimpleName())); + } catch (final UserFailureException ex) + { + // Nothing to do here. + } + } } -- GitLab