From 1fb1513defb0a8897e963f0cda8f62b5cf93d25a Mon Sep 17 00:00:00 2001
From: ribeaudc <ribeaudc>
Date: Tue, 2 Dec 2008 10:07:05 +0000
Subject: [PATCH] change: - 'SampleBOTest' no longer in the 'broken' group.
 add: - Unit test for 'EntityPropertiesConverter'.

SVN: 9160
---
 .../bo/EntityPropertiesConverter.java         |  17 +-
 .../generic/server/business/bo/SampleBO.java  |  10 +-
 .../bo/EntityPropertiesConverterTest.java     | 198 ++++++++++++++++++
 .../server/business/bo/SampleBOTest.java      |  13 +-
 4 files changed, 226 insertions(+), 12 deletions(-)
 create mode 100644 openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java
index 34d10a9078a..3aaaa7e5404 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverter.java
@@ -17,6 +17,7 @@
 package ch.systemsx.cisd.openbis.generic.server.business.bo;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import ch.systemsx.cisd.common.collections.IKeyExtractor;
@@ -46,7 +47,6 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
  * 
  * @author Christian Ribeaud
  */
-// TODO 2008-12-02, Christian Ribeaud: Test missing for this class.
 public final class EntityPropertiesConverter implements IEntityPropertiesConverter
 {
     private final IDAOFactory daoFactory;
@@ -63,6 +63,9 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert
 
     public EntityPropertiesConverter(final EntityKind entityKind, final IDAOFactory daoFactory)
     {
+        assert entityKind != null : "Unspecified entity kind.";
+        assert daoFactory != null : "Unspecified DAO factory.";
+
         this.daoFactory = daoFactory;
         this.entityKind = entityKind;
     }
@@ -175,15 +178,19 @@ public final class EntityPropertiesConverter implements IEntityPropertiesConvert
             final EntityProperty<ET, ETPT>[] properties, final String entityTypeCode,
             final PersonPE registrator)
     {
+        assert entityTypeCode != null : "Unspecified entity type code.";
+        assert registrator != null : "Unspecified registrator";
+        assert properties != null : "Unspecified entity properties";
+        if (properties.length == 0)
+        {
+            return Collections.emptyList();
+        }
         final EntityTypePE entityTypePE = getEntityType(entityTypeCode);
         final List<T> list = new ArrayList<T>();
         for (final EntityProperty<ET, ETPT> property : properties)
         {
             final T convertedProperty = convertProperty(registrator, entityTypePE, property);
-            if (convertedProperty != null)
-            {
-                list.add(convertedProperty);
-            }
+            list.add(convertedProperty);
         }
         return list;
     }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java
index f377be24f18..eed1fe0e132 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBO.java
@@ -20,6 +20,7 @@ import java.util.List;
 
 import org.springframework.dao.DataAccessException;
 
+import ch.rinn.restrictions.Private;
 import ch.systemsx.cisd.common.exceptions.UserFailureException;
 import ch.systemsx.cisd.common.utilities.ParameterChecker;
 import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleProperty;
@@ -47,9 +48,16 @@ public final class SampleBO extends AbstractSampleIdentifierBusinessObject imple
     private boolean dataChanged;
 
     public SampleBO(final IDAOFactory daoFactory, final Session session)
+    {
+        this(daoFactory, session, new EntityPropertiesConverter(EntityKind.SAMPLE, daoFactory));
+    }
+
+    @Private
+    SampleBO(final IDAOFactory daoFactory, final Session session,
+            final IEntityPropertiesConverter entityPropertiesConverter)
     {
         super(daoFactory, session);
-        propertiesConverter = new EntityPropertiesConverter(EntityKind.SAMPLE, daoFactory);
+        propertiesConverter = entityPropertiesConverter;
         this.dataChanged = false;
     }
 
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java
new file mode 100644
index 00000000000..da398ccc316
--- /dev/null
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/EntityPropertiesConverterTest.java
@@ -0,0 +1,198 @@
+/*
+ * Copyright 2008 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 org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+
+import java.util.Collections;
+import java.util.List;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+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.SampleProperty;
+import ch.systemsx.cisd.openbis.generic.client.web.client.dto.SampleTypePropertyType;
+import ch.systemsx.cisd.openbis.generic.server.business.ManagerTestTool;
+import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
+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.IPropertyTypeDAO;
+import ch.systemsx.cisd.openbis.generic.shared.dto.EntityPropertyPE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.PropertyTypePE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SampleTypePE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.SampleTypePropertyTypePE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.properties.EntityKind;
+
+/**
+ * Test cases for corresponding {@link EntityPropertiesConverter} class.
+ * 
+ * @author Christian Ribeaud
+ */
+public final class EntityPropertiesConverterTest
+{
+    private static final String VARCHAR_PROPERTY_TYPE_CODE = "color";
+
+    private static final String SAMPLE_TYPE_CODE = "MASTER_PLATE";
+
+    private Mockery context;
+
+    private IDAOFactory daoFactory;
+
+    private IEntityPropertyTypeDAO entityPropertyTypeDAO;
+
+    private IEntityTypeDAO entityTypeDAO;
+
+    private IPropertyTypeDAO propertyTypeDAO;
+
+    @BeforeMethod
+    public void setUp()
+    {
+        context = new Mockery();
+        daoFactory = context.mock(IDAOFactory.class);
+        entityPropertyTypeDAO = context.mock(IEntityPropertyTypeDAO.class);
+        entityTypeDAO = context.mock(IEntityTypeDAO.class);
+        propertyTypeDAO = context.mock(IPropertyTypeDAO.class);
+    }
+
+    @AfterMethod
+    public void tearDown()
+    {
+        // 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();
+    }
+
+    private final IEntityPropertiesConverter createEntityPropertiesConverter(
+            final EntityKind entityKind)
+    {
+        return new EntityPropertiesConverter(entityKind, daoFactory);
+    }
+
+    private void prepareForConvertion(final Expectations exp)
+    {
+        final SampleTypePE sampleType = createSampleType(SAMPLE_TYPE_CODE);
+        final SampleTypePropertyTypePE sampleTypePropertyTypePE = new SampleTypePropertyTypePE();
+        sampleTypePropertyTypePE.setEntityType(sampleType);
+        final PropertyTypePE propertyType = new PropertyTypePE();
+        propertyType.setCode(VARCHAR_PROPERTY_TYPE_CODE);
+        sampleTypePropertyTypePE.setPropertyType(propertyType);
+
+        exp.allowing(daoFactory).getEntityPropertyTypeDAO(EntityKind.SAMPLE);
+        exp.will(Expectations.returnValue(entityPropertyTypeDAO));
+
+        exp.allowing(daoFactory).getEntityTypeDAO(EntityKind.SAMPLE);
+        exp.will(Expectations.returnValue(entityTypeDAO));
+
+        exp.allowing(daoFactory).getPropertyTypeDAO();
+        exp.will(Expectations.returnValue(propertyTypeDAO));
+
+        exp.one(entityTypeDAO).listEntityTypes();
+        exp.will(Expectations.returnValue(Collections.singletonList(sampleType)));
+
+        exp.one(entityPropertyTypeDAO).listEntityPropertyTypes(sampleType);
+        exp.will(Expectations.returnValue(Collections.singletonList(sampleTypePropertyTypePE)));
+    }
+
+    private final static SampleTypePE createSampleType(final String sampleTypeCode)
+    {
+        final SampleTypePE sampleType = new SampleTypePE();
+        sampleType.setCode(sampleTypeCode);
+        sampleType.setDatabaseInstance(ManagerTestTool.EXAMPLE_DATABASE_INSTANCE);
+        return sampleType;
+    }
+
+    private final static SampleProperty createVarcharSampleProperty()
+    {
+        final SampleProperty sampleProperty = new SampleProperty();
+        sampleProperty.setValue("blue");
+        final SampleTypePropertyType sampleTypePropertyType = new SampleTypePropertyType();
+        final PropertyType propertyType = new PropertyType();
+        propertyType.setLabel(VARCHAR_PROPERTY_TYPE_CODE);
+        propertyType.setCode(VARCHAR_PROPERTY_TYPE_CODE);
+        final DataType dataType = new DataType();
+        dataType.setCode(DataTypeCode.VARCHAR);
+        propertyType.setDataType(dataType);
+        sampleTypePropertyType.setPropertyType(propertyType);
+        sampleProperty.setEntityTypePropertyType(sampleTypePropertyType);
+        return sampleProperty;
+    }
+
+    private final SampleProperty[] createSampleProperties()
+    {
+        return new SampleProperty[]
+            { createVarcharSampleProperty() };
+    }
+
+    @Test
+    public final void testConvertPropertiesFailed()
+    {
+        final IEntityPropertiesConverter entityPropertiesConverter =
+                createEntityPropertiesConverter(EntityKind.MATERIAL);
+        boolean fail = true;
+        try
+        {
+            entityPropertiesConverter.convertProperties(null, null, null);
+        } catch (final AssertionError e)
+        {
+            fail = false;
+        }
+        assertFalse(fail);
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public final void testConvertPropertiesWithEmptyProperties()
+    {
+        final IEntityPropertiesConverter entityPropertiesConverter =
+                createEntityPropertiesConverter(EntityKind.SAMPLE);
+        final List<EntityPropertyPE> properties =
+                entityPropertiesConverter.convertProperties(SampleProperty.EMPTY_ARRAY,
+                        SAMPLE_TYPE_CODE, ManagerTestTool.EXAMPLE_PERSON);
+        assertEquals(0, properties.size());
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public final void testConvertProperties()
+    {
+        final IEntityPropertiesConverter entityPropertiesConverter =
+                createEntityPropertiesConverter(EntityKind.SAMPLE);
+        final PropertyTypePE propertyTypePE = new PropertyTypePE();
+        propertyTypePE.setCode(VARCHAR_PROPERTY_TYPE_CODE);
+        context.checking(new Expectations()
+            {
+                {
+                    prepareForConvertion(this);
+
+                    one(propertyTypeDAO).tryFindPropertyTypeByCode(VARCHAR_PROPERTY_TYPE_CODE);
+                    will(returnValue(propertyTypePE));
+                }
+            });
+        final SampleProperty[] properties = createSampleProperties();
+        final List<EntityPropertyPE> convertedProperties =
+                entityPropertiesConverter.convertProperties(properties, SAMPLE_TYPE_CODE,
+                        ManagerTestTool.EXAMPLE_PERSON);
+        assertEquals(1, convertedProperties.size());
+    }
+}
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBOTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBOTest.java
index 1f5b6291e93..8b29ceb9f49 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBOTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/SampleBOTest.java
@@ -36,6 +36,7 @@ import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
+import ch.rinn.restrictions.Friend;
 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;
@@ -63,7 +64,7 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.types.SampleTypeCode;
  * 
  * @author Franz-Josef Elmer
  */
-@Test(groups = "broken")
+@Friend(toClasses = SampleBO.class)
 public final class SampleBOTest
 {
     private static final String DB = "DB";
@@ -155,6 +156,11 @@ public final class SampleBOTest
         return new SampleIdentifier(IdentifierHelper.createIdentifier(EXAMPLE_GROUP), code);
     }
 
+    private final SampleBO createSampleBO()
+    {
+        return new SampleBO(daoFactory, EXAMPLE_SESSION, propertiesConverter);
+    }
+
     @Test
     public void testGetUndefinedSample()
     {
@@ -458,9 +464,4 @@ public final class SampleBOTest
         context.assertIsSatisfied();
     }
 
-    private SampleBO createSampleBO()
-    {
-        return new SampleBO(daoFactory, EXAMPLE_SESSION);
-    }
-
 }
-- 
GitLab