From b7a2ccf47f3b01985dc0d82f8cef46184bb7588f Mon Sep 17 00:00:00 2001
From: buczekp <buczekp>
Date: Tue, 23 Nov 2010 08:45:20 +0000
Subject: [PATCH] [LMS-1858] more tests for material properties

SVN: 18852
---
 .../calculator/BasicPropertyAdaptor.java      |  2 +-
 .../server/dataaccess/PropertyValidator.java  |  5 +-
 .../server/business/bo/AbstractBOTest.java    | 58 +++++++--------
 .../business/bo/PropertyValidatorTest.java    | 72 ++++++++++++++++++-
 .../DynamicPropertyEvaluatorTest.java         | 65 +++++++++++++----
 5 files changed, 156 insertions(+), 46 deletions(-)

diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/calculator/BasicPropertyAdaptor.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/calculator/BasicPropertyAdaptor.java
index 97acf02abb0..fcda8c111be 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/calculator/BasicPropertyAdaptor.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/calculator/BasicPropertyAdaptor.java
@@ -71,7 +71,7 @@ public class BasicPropertyAdaptor implements IEntityPropertyAdaptor
     @Override
     public String toString()
     {
-        return propertyTypeCode() + " " + valueAsString();
+        return "code: " + propertyTypeCode() + ", value: " + valueAsString();
     }
 
 }
diff --git a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/PropertyValidator.java b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/PropertyValidator.java
index 935e4e9a00d..50465466c57 100644
--- a/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/PropertyValidator.java
+++ b/openbis/source/java/ch/systemsx/cisd/openbis/generic/server/dataaccess/PropertyValidator.java
@@ -365,13 +365,14 @@ public final class PropertyValidator implements IPropertyValueValidator
             if (identifierOrNull == null)
             {
                 throw UserFailureException.fromTemplate(
-                        "Material specification '%s' has improper format.", value);
+                        "Material specification '%s' has improper format. "
+                                + "Expected '<CODE> (<TYPE>)'.", value);
             }
             if (materialTypeOrNull != null
                     && identifierOrNull.getTypeCode().equals(materialTypeOrNull.getCode()) == false)
             {
                 throw UserFailureException.fromTemplate(
-                        "Material '%s' is of wrong type. Expected: '%'.", value,
+                        "Material '%s' is of wrong type. Expected: '%s'.", value,
                         materialTypeOrNull.getCode());
             }
             return value;
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 0567816975d..bd1e2238ecf 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
@@ -59,61 +59,61 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.DatabaseInstancePE;
 @Friend(toClasses = ScriptBO.IScriptFactory.class)
 public abstract class AbstractBOTest extends AssertJUnit
 {
-    Mockery context;
+    protected Mockery context;
 
-    IDAOFactory daoFactory;
+    protected IDAOFactory daoFactory;
 
-    IScriptFactory scriptFactory;
+    protected IScriptFactory scriptFactory;
 
-    IGroupDAO groupDAO;
+    protected IGroupDAO groupDAO;
 
-    IScriptDAO scriptDAO;
+    protected IScriptDAO scriptDAO;
 
-    IRelationshipTypeDAO relationshipTypeDAO;
+    protected IRelationshipTypeDAO relationshipTypeDAO;
 
-    IExperimentDAO experimentDAO;
+    protected IExperimentDAO experimentDAO;
 
-    IProjectDAO projectDAO;
+    protected IProjectDAO projectDAO;
 
-    IEntityTypeDAO entityTypeDAO;
+    protected IEntityTypeDAO entityTypeDAO;
 
-    IMaterialDAO materialDAO;
+    protected IMaterialDAO materialDAO;
 
-    IExternalDataDAO externalDataDAO;
+    protected IExternalDataDAO externalDataDAO;
 
-    IDatabaseInstanceDAO databaseInstanceDAO;
+    protected IDatabaseInstanceDAO databaseInstanceDAO;
 
-    ISampleDAO sampleDAO;
+    protected ISampleDAO sampleDAO;
 
-    IEntityPropertyTypeDAO entityPropertyTypeDAO;
+    protected IEntityPropertyTypeDAO entityPropertyTypeDAO;
 
-    IPropertyTypeDAO propertyTypeDAO;
+    protected IPropertyTypeDAO propertyTypeDAO;
 
-    IPersonDAO personDAO;
+    protected IPersonDAO personDAO;
 
-    ISampleTypeDAO sampleTypeDAO;
+    protected ISampleTypeDAO sampleTypeDAO;
 
-    IVocabularyDAO vocabularyDAO;
+    protected IVocabularyDAO vocabularyDAO;
 
-    IVocabularyTermDAO vocabularyTermDAO;
+    protected IVocabularyTermDAO vocabularyTermDAO;
 
-    IEntityPropertiesConverter propertiesConverter;
+    protected IEntityPropertiesConverter propertiesConverter;
 
-    IDataSetTypeDAO dataSetTypeDAO;
+    protected IDataSetTypeDAO dataSetTypeDAO;
 
-    IFileFormatTypeDAO fileFormatTypeDAO;
+    protected IFileFormatTypeDAO fileFormatTypeDAO;
 
-    ILocatorTypeDAO locatorTypeDAO;
+    protected ILocatorTypeDAO locatorTypeDAO;
 
-    IDataStoreDAO dataStoreDAO;
+    protected IDataStoreDAO dataStoreDAO;
 
-    IPermIdDAO permIdDAO;
+    protected IPermIdDAO permIdDAO;
 
-    IEventDAO eventDAO;
+    protected IEventDAO eventDAO;
 
-    IAuthorizationGroupDAO authorizationGroupDAO;
+    protected IAuthorizationGroupDAO authorizationGroupDAO;
 
-    IGridCustomFilterDAO filterDAO;
+    protected IGridCustomFilterDAO filterDAO;
 
     @BeforeMethod
     public void beforeMethod()
@@ -184,6 +184,8 @@ public abstract class AbstractBOTest extends AssertJUnit
                     will(returnValue(personDAO));
                     allowing(daoFactory).getScriptDAO();
                     will(returnValue(scriptDAO));
+                    allowing(daoFactory).getMaterialDAO();
+                    will(returnValue(materialDAO));
                 }
             });
     }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidatorTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidatorTest.java
index eda85c2ca83..2af19bbf8ef 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidatorTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/PropertyValidatorTest.java
@@ -30,6 +30,7 @@ import ch.systemsx.cisd.openbis.generic.server.dataaccess.PropertyValidator;
 import ch.systemsx.cisd.openbis.generic.server.dataaccess.PropertyValidator.SupportedDatePattern;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
 import ch.systemsx.cisd.openbis.generic.shared.dto.DataTypePE;
+import ch.systemsx.cisd.openbis.generic.shared.dto.MaterialTypePE;
 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.VocabularyTermPE;
@@ -216,14 +217,13 @@ public final class PropertyValidatorTest extends AbstractBOTest
     }
 
     //
-    // Controlled Vocabulary with DAO access
+    // Controlled Vocabulary
     //
 
     private final static PropertyTypePE createControlledVocabularyPropertyType()
     {
         final PropertyTypePE propertyType = createPropertyType(DataTypeCode.CONTROLLEDVOCABULARY);
         final VocabularyPE vocabularyPE = new VocabularyPE();
-        // terms list is currently not used in validation but lets keep it here
         final List<VocabularyTermPE> terms = new ArrayList<VocabularyTermPE>();
         terms.add(createVocabularyTerm("GOODVALUE"));
         vocabularyPE.setTerms(terms);
@@ -255,4 +255,72 @@ public final class PropertyValidatorTest extends AbstractBOTest
             // Nothing to do here.
         }
     }
+
+    //
+    // Material
+    //
+
+    private final static PropertyTypePE createMaterialPropertyType(MaterialTypePE materialType)
+    {
+        final PropertyTypePE propertyType = createPropertyType(DataTypeCode.MATERIAL);
+        propertyType.setMaterialType(materialType);
+        return propertyType;
+    }
+
+    @Test
+    public final void testValidateMaterialPropertyValueNoType()
+    {
+        final PropertyTypePE propertyType = createMaterialPropertyType(null);
+        final String value = "code (type)";
+        final PropertyValidator propertyValidator = createPropertyValidator();
+        propertyValidator.validatePropertyValue(propertyType, value);
+    }
+
+    @Test
+    public final void testValidateMaterialPropertyValueWithType()
+    {
+        final MaterialTypePE materialType = new MaterialTypePE();
+        materialType.setCode("t1");
+        final PropertyTypePE propertyType = createMaterialPropertyType(materialType);
+        final String value = "code (" + materialType.getCode() + ")";
+        final PropertyValidator propertyValidator = createPropertyValidator();
+        propertyValidator.validatePropertyValue(propertyType, value);
+    }
+
+    @Test
+    public final void testValidateMaterialPropertyValueNoTypeFailed()
+    {
+        PropertyTypePE propertyType = createMaterialPropertyType(null);
+        final String value = "noType";
+        final PropertyValidator propertyValidator = createPropertyValidator();
+        try
+        {
+            propertyValidator.validatePropertyValue(propertyType, value);
+            fail(String.format("'%s' expected.", UserFailureException.class.getSimpleName()));
+        } catch (final UserFailureException ex)
+        {
+            assertEquals("Material specification '" + value
+                    + "' has improper format. Expected '<CODE> (<TYPE>)'.", ex.getMessage());
+        }
+    }
+
+    @Test
+    public final void testValidateMaterialPropertyValueWithTypeFailed()
+    {
+        final MaterialTypePE materialType = new MaterialTypePE();
+        materialType.setCode("t1");
+        PropertyTypePE propertyType = createMaterialPropertyType(materialType);
+        final String value = "wrongType (t2)";
+        final PropertyValidator propertyValidator = createPropertyValidator();
+        try
+        {
+            propertyValidator.validatePropertyValue(propertyType, value);
+            fail(String.format("'%s' expected.", UserFailureException.class.getSimpleName()));
+        } catch (final UserFailureException ex)
+        {
+            assertEquals(
+                    "Material '" + value + "' is of wrong type. Expected: '"
+                            + materialType.getCode() + "'.", ex.getMessage());
+        }
+    }
 }
diff --git a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/DynamicPropertyEvaluatorTest.java b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/DynamicPropertyEvaluatorTest.java
index badb6018f83..e805db65610 100644
--- a/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/DynamicPropertyEvaluatorTest.java
+++ b/openbis/sourceTest/java/ch/systemsx/cisd/openbis/generic/server/business/bo/dynamic_property/DynamicPropertyEvaluatorTest.java
@@ -22,17 +22,16 @@ import java.util.HashSet;
 import java.util.LinkedHashSet;
 import java.util.Set;
 
-import org.jmock.Mockery;
-import org.testng.AssertJUnit;
+import org.jmock.Expectations;
 import org.testng.annotations.BeforeMethod;
 import org.testng.annotations.Test;
 
 import ch.systemsx.cisd.common.logging.LogInitializer;
+import ch.systemsx.cisd.openbis.generic.server.business.bo.AbstractBOTest;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.dynamic_property.calculator.AbstractEntityAdaptor;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.dynamic_property.calculator.BasicPropertyAdaptor;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.dynamic_property.calculator.IEntityAdaptor;
 import ch.systemsx.cisd.openbis.generic.server.business.bo.dynamic_property.calculator.IEntityPropertyAdaptor;
-import ch.systemsx.cisd.openbis.generic.server.dataaccess.IDAOFactory;
 import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataTypeCode;
 import ch.systemsx.cisd.openbis.generic.shared.basic.dto.MaterialIdentifier;
@@ -53,18 +52,15 @@ import ch.systemsx.cisd.openbis.generic.shared.dto.VocabularyTermPE;
 /**
  * @author Piotr Buczek
  */
-public class DynamicPropertyEvaluatorTest extends AssertJUnit
+public class DynamicPropertyEvaluatorTest extends AbstractBOTest
 {
 
     private DynamicPropertyEvaluator evaluator;
 
-    private IDAOFactory daoFactory;
-
     @BeforeMethod
     public void setUp()
     {
         LogInitializer.init();
-        daoFactory = new Mockery().mock(IDAOFactory.class);
         evaluator = new DynamicPropertyEvaluator(daoFactory);
     }
 
@@ -201,19 +197,19 @@ public class DynamicPropertyEvaluatorTest extends AssertJUnit
         assertEquals(null, dpVocabularyError.getVocabularyTerm());
     }
 
-    @Test(groups = "broken")
+    @Test
     public void testEvaluateMaterialProperties()
     {
         // check handling of material properties
         Set<SamplePropertyPE> properties = new HashSet<SamplePropertyPE>();
 
         PropertyTypePE materialPropertyType = createPropertyType("mp", DataTypeCode.MATERIAL);
-        String materialTypeCode = "M_TYPE";
+        final String materialTypeCode = "M_TYPE";
         MaterialTypePE materialType = new MaterialTypePE();
         materialType.setCode(materialTypeCode);
         materialPropertyType.setMaterialType(materialType);
 
-        MaterialPE material = new MaterialPE();
+        final MaterialPE material = new MaterialPE();
         material.setCode("M_CODE");
         material.setMaterialType(materialType);
 
@@ -222,27 +218,58 @@ public class DynamicPropertyEvaluatorTest extends AssertJUnit
 
         // create dynamic properties
         final ScriptPE script1 = createScript("s1", "entity.propertyValue('mp')");
+
         final SamplePropertyPE dpVarchar = createDynamicSampleProperty("dpVarchar", script1);
+
         final PropertyTypePE dynamicPropertyType =
                 createPropertyType("dpMaterial", DataTypeCode.MATERIAL);
         dynamicPropertyType.setMaterialType(materialType);
         final SamplePropertyPE dpMaterial =
                 createDynamicSampleProperty(dynamicPropertyType, script1);
+
         final ScriptPE scriptError1 = createScript("se1", "'fake_material'");
         final PropertyTypePE dynamicPropertyTypeError1 =
                 createPropertyType("dpMaterialError1", DataTypeCode.MATERIAL);
         dynamicPropertyTypeError1.setMaterialType(materialType);
         final SamplePropertyPE dpMaterialError1 =
                 createDynamicSampleProperty(dynamicPropertyTypeError1, scriptError1);
-        final ScriptPE scriptError2 = createScript("se2", "'fake_material (fake_type)'");
+
+        final MaterialIdentifier fakeTypeIdentifier = new MaterialIdentifier("fake", "fake_type");
+        final ScriptPE scriptError2 =
+                createScript("se2", "'" + fakeTypeIdentifier.toString() + "'");
         final PropertyTypePE dynamicPropertyTypeError2 =
                 createPropertyType("dpMaterialError2", DataTypeCode.MATERIAL);
+        dynamicPropertyTypeError2.setMaterialType(materialType);
         final SamplePropertyPE dpMaterialError2 =
                 createDynamicSampleProperty(dynamicPropertyTypeError2, scriptError2);
+
+        final MaterialIdentifier fakeMaterialIdentifier =
+                new MaterialIdentifier("fake", materialTypeCode);
+        final ScriptPE scriptError3 =
+                createScript("se3", "'" + fakeMaterialIdentifier.toString() + "'");
+        final PropertyTypePE dynamicPropertyTypeError3 =
+                createPropertyType("dpMaterialError3", DataTypeCode.MATERIAL);
+        dynamicPropertyTypeError3.setMaterialType(materialType);
+        final SamplePropertyPE dpMaterialError3 =
+                createDynamicSampleProperty(dynamicPropertyTypeError3, scriptError3);
+
         properties.add(dpVarchar);
         properties.add(dpMaterial);
         properties.add(dpMaterialError1);
         properties.add(dpMaterialError2);
+        properties.add(dpMaterialError3);
+
+        context.checking(new Expectations()
+            {
+                {
+                    one(materialDAO).tryFindMaterial(
+                            new MaterialIdentifier(material.getCode(), materialTypeCode));
+                    will(returnValue(material));
+
+                    one(materialDAO).tryFindMaterial(fakeMaterialIdentifier);
+                    will(returnValue(null));
+                }
+            });
 
         // create sample with all properties created above and evaluate dynamic properties
         final SamplePE sample = createSample("s1", properties);
@@ -253,15 +280,27 @@ public class DynamicPropertyEvaluatorTest extends AssertJUnit
                 MaterialIdentifier.print(material.getCode(), materialTypeCode);
         assertEquals(materialIdentifier, dpVarchar.getValue());
         assertEquals(null, dpVarchar.getMaterialValue());
+
         assertEquals(null, dpMaterial.getValue());
         assertEquals(material, dpMaterial.getMaterialValue());
+
         final String expectedDpMaterialError1Value =
-                expectedErrorMessage("Material specification 'fake_material' has improper format.");
+                expectedErrorMessage("Material specification 'fake_material' has improper format. "
+                        + "Expected '<CODE> (<TYPE>)'.");
         assertEquals(expectedDpMaterialError1Value, dpMaterialError1.getValue());
         assertEquals(null, dpMaterialError1.getVocabularyTerm());
-        final String expectedDpMaterialError2Value = expectedErrorMessage("");
+
+        final String expectedDpMaterialError2Value =
+                expectedErrorMessage("Material '" + fakeTypeIdentifier.toString()
+                        + "' is of wrong type. Expected: '" + materialTypeCode + "'.");
         assertEquals(expectedDpMaterialError2Value, dpMaterialError2.getValue());
         assertEquals(null, dpMaterialError2.getVocabularyTerm());
+
+        final String expectedDpMaterialError3Value =
+                expectedErrorMessage("No material could be found for identifier '"
+                        + fakeMaterialIdentifier.toString() + "'.");
+        assertEquals(expectedDpMaterialError3Value, dpMaterialError3.getValue());
+        assertEquals(null, dpMaterialError3.getVocabularyTerm());
     }
 
     @Test
-- 
GitLab