RM-1947 New constraint to avoid invalid classification levels.

Create a service locator class to allow the non-Spring constraint to access
the Spring service.

+review RM-21

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@101788 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tom Page
2015-04-14 09:33:51 +00:00
parent 99576a26a3
commit 7d52c49b2b
10 changed files with 514 additions and 246 deletions

View File

@@ -81,6 +81,10 @@
</value> </value>
</property> </property>
</bean> </bean>
<bean id="ClassificationServiceProvider" class="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceProvider">
<constructor-arg ref="ClassificationService" index="0" />
</bean>
<bean id="classificationServiceBootstrap" <bean id="classificationServiceBootstrap"
class="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceBootstrap"> class="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationServiceBootstrap">

View File

@@ -26,7 +26,7 @@
<!-- Model Constraints --> <!-- Model Constraints -->
<constraints> <constraints>
<constraint name="clf:classificationLevelRestriction" type="org.alfresco.module.org_alfresco_module_rm.classification.ClassificationLevelConstraint" />
</constraints> </constraints>
<!-- Types --> <!-- Types -->

View File

@@ -0,0 +1,30 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.caveat;
/**
* A class to hold I18N keys for messages related to constraint validation.
*
* @author tpage
*/
public class RMConstraintMessageKeys
{
public static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string";
public static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value";
}

View File

@@ -1,4 +1,4 @@
/* /*
* Copyright (C) 2005-2014 Alfresco Software Limited. * Copyright (C) 2005-2014 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
@@ -14,239 +14,237 @@
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/ */
package org.alfresco.module.org_alfresco_module_rm.caveat; package org.alfresco.module.org_alfresco_module_rm.caveat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint; import org.alfresco.repo.dictionary.constraint.ListOfValuesConstraint;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.dictionary.ConstraintException; import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.i18n.MessageLookup; import org.alfresco.service.cmr.i18n.MessageLookup;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException; import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.extensions.surf.util.I18NUtil;
/** /**
* RM Constraint implementation that ensures the value is one of a constrained * RM Constraint implementation that ensures the value is one of a constrained
* <i>list of values</i>. By default, this constraint is case-sensitive. * <i>list of values</i>. By default, this constraint is case-sensitive.
* *
* @see #setAllowedValues(List) * @see #setAllowedValues(List)
* @see #setCaseSensitive(boolean) * @see #setCaseSensitive(boolean)
* *
* @author janv * @author janv
*/ */
public class RMListOfValuesConstraint extends ListOfValuesConstraint public class RMListOfValuesConstraint extends ListOfValuesConstraint
{ {
private static final String ERR_NON_STRING = "d_dictionary.constraint.string_length.non_string"; private static final String LOV_CONSTRAINT_VALUE = "listconstraint";
private static final String ERR_INVALID_VALUE = "d_dictionary.constraint.list_of_values.invalid_value"; private List<String> allowedValues;
private static final String LOV_CONSTRAINT_VALUE = "listconstraint"; private List<String> allowedValuesUpper;
private List<String> allowedValues; // defined match logic used by caveat matching (default = "AND")
private List<String> allowedValuesUpper; private MatchLogic matchLogic = MatchLogic.AND;
// defined match logic used by caveat matching (default = "AND")
private MatchLogic matchLogic = MatchLogic.AND; public enum MatchLogic
{
public enum MatchLogic // closed marking - all values must match
{ AND,
// closed marking - all values must match // open marking - at least one value must match
AND, OR;
// open marking - at least one value must match }
OR;
} // note: alternative to static init could be to use 'registered' constraint
private static RMCaveatConfigService caveatConfigService;
// note: alternative to static init could be to use 'registered' constraint
private static RMCaveatConfigService caveatConfigService; public void setCaveatConfigService(RMCaveatConfigService caveatConfigService)
{
public void setCaveatConfigService(RMCaveatConfigService caveatConfigService) RMListOfValuesConstraint.caveatConfigService = caveatConfigService;
{ }
RMListOfValuesConstraint.caveatConfigService = caveatConfigService;
}
@Override
public String toString()
@Override {
public String toString() StringBuilder sb = new StringBuilder(80);
{ sb.append("RMListOfValuesConstraint")
StringBuilder sb = new StringBuilder(80); .append("[allowedValues=").append(getAllowedValues())
sb.append("RMListOfValuesConstraint") .append(", caseSensitive=").append(isCaseSensitive())
.append("[allowedValues=").append(getAllowedValues()) .append(", sorted=").append(isSorted())
.append(", caseSensitive=").append(isCaseSensitive()) .append(", matchLogic=").append(getMatchLogic())
.append(", sorted=").append(isCaseSensitive()) .append("]");
.append(", matchLogic=").append(getMatchLogic()) return sb.toString();
.append("]"); }
return sb.toString();
} public RMListOfValuesConstraint()
{
public RMListOfValuesConstraint() super();
{
super(); // Set RM list of value constraints to be sorted by default
sorted = true;
// Set RM list of value constraints to be sorted by default }
sorted = true;
} /**
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
/** * represent non-<tt>String</tt> values. It is up to the caller to distinguish.
* Get the allowed values. Note that these are <tt>String</tt> instances, but may *
* represent non-<tt>String</tt> values. It is up to the caller to distinguish. * @return Returns the values allowed
* */
* @return Returns the values allowed @Override
*/ public List<String> getRawAllowedValues()
@Override {
public List<String> getRawAllowedValues() String runAsUser = AuthenticationUtil.getRunAsUser();
{ if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
String runAsUser = AuthenticationUtil.getRunAsUser(); {
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null)) // get allowed values for current user
{ List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getShortName());
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getShortName()); List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
List<String> filteredList = new ArrayList<String>(allowedForUser.size()); {
for (String allowed : allowedForUser) if (this.allowedValues.contains(allowed))
{ {
if (this.allowedValues.contains(allowed)) filteredList.add(allowed);
{ }
filteredList.add(allowed); }
}
} return filteredList;
}
return filteredList; else
} {
else return this.allowedValues;
{ }
return this.allowedValues; }
}
} public String getDisplayLabel(String constraintAllowableValue, MessageLookup messageLookup)
{
public String getDisplayLabel(String constraintAllowableValue, MessageLookup messageLookup) if (!this.allowedValues.contains(constraintAllowableValue))
{ {
if (!this.allowedValues.contains(constraintAllowableValue)) return null;
{ }
return null;
} String key = LOV_CONSTRAINT_VALUE;
key += "." + this.getShortName();
String key = LOV_CONSTRAINT_VALUE; key += "." + constraintAllowableValue;
key += "." + this.getShortName(); key = StringUtils.replace(key, ":", "_");
key += "." + constraintAllowableValue;
key = StringUtils.replace(key, ":", "_"); String message = messageLookup.getMessage(key, I18NUtil.getLocale());
return message == null ? constraintAllowableValue : message;
String message = messageLookup.getMessage(key, I18NUtil.getLocale()); }
return message == null ? constraintAllowableValue : message;
} private List<String> getAllowedValuesUpper()
{
private List<String> getAllowedValuesUpper() String runAsUser = AuthenticationUtil.getRunAsUser();
{ if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null))
String runAsUser = AuthenticationUtil.getRunAsUser(); {
if ((runAsUser != null) && (! runAsUser.equals(AuthenticationUtil.getSystemUserName())) && (caveatConfigService != null)) // get allowed values for current user
{ List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getType());
// get allowed values for current user
List<String> allowedForUser = caveatConfigService.getRMAllowedValues(getType()); List<String> filteredList = new ArrayList<String>(allowedForUser.size());
for (String allowed : allowedForUser)
List<String> filteredList = new ArrayList<String>(allowedForUser.size()); {
for (String allowed : allowedForUser) if (this.allowedValuesUpper.contains(allowed.toUpperCase()))
{ {
if (this.allowedValuesUpper.contains(allowed.toUpperCase())) filteredList.add(allowed);
{ }
filteredList.add(allowed); }
}
} return filteredList;
}
return filteredList; else
} {
else return this.allowedValuesUpper;
{ }
return this.allowedValuesUpper; }
} /**
} * Set the values that are allowed by the constraint.
/** *
* Set the values that are allowed by the constraint. * @param values a list of allowed values
* */
* @param values a list of allowed values @SuppressWarnings({ "unchecked", "rawtypes" })
*/ @Override
@SuppressWarnings({ "unchecked", "rawtypes" }) public void setAllowedValues(List allowedValues)
@Override {
public void setAllowedValues(List allowedValues) if (allowedValues == null)
{ {
if (allowedValues == null) allowedValues = new ArrayList<String>(0);
{ }
allowedValues = new ArrayList<String>(0); int valueCount = allowedValues.size();
} this.allowedValues = Collections.unmodifiableList(allowedValues);
int valueCount = allowedValues.size();
this.allowedValues = Collections.unmodifiableList(allowedValues); // make the upper case versions
this.allowedValuesUpper = new ArrayList<String>(valueCount);
// make the upper case versions for (String allowedValue : this.allowedValues)
this.allowedValuesUpper = new ArrayList<String>(valueCount); {
for (String allowedValue : this.allowedValues) allowedValuesUpper.add(allowedValue.toUpperCase());
{ }
allowedValuesUpper.add(allowedValue.toUpperCase()); }
}
} @Override
public void initialize()
@Override {
public void initialize() checkPropertyNotNull("allowedValues", allowedValues);
{ }
checkPropertyNotNull("allowedValues", allowedValues);
} @Override
public Map<String, Object> getParameters()
@Override {
public Map<String, Object> getParameters() Map<String, Object> params = new HashMap<String, Object>(2);
{
Map<String, Object> params = new HashMap<String, Object>(2); params.put("caseSensitive", isCaseSensitive());
params.put("allowedValues", getAllowedValues());
params.put("caseSensitive", isCaseSensitive()); params.put("sorted", isSorted());
params.put("allowedValues", getAllowedValues()); params.put("matchLogic", getMatchLogic());
params.put("sorted", isSorted());
params.put("matchLogic", getMatchLogic()); return params;
}
return params;
} public MatchLogic getMatchLogicEnum()
{
public MatchLogic getMatchLogicEnum() return matchLogic;
{ }
return matchLogic;
} public String getMatchLogic()
{
public String getMatchLogic() return matchLogic.toString();
{ }
return matchLogic.toString();
} public void setMatchLogic(String matchLogicStr)
{
public void setMatchLogic(String matchLogicStr) this.matchLogic = MatchLogic.valueOf(matchLogicStr);
{ }
this.matchLogic = MatchLogic.valueOf(matchLogicStr);
} @Override
protected void evaluateSingleValue(Object value)
@Override {
protected void evaluateSingleValue(Object value) // convert the value to a String
{ String valueStr = null;
// convert the value to a String try
String valueStr = null; {
try valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
{ }
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value); catch (TypeConversionException e)
} {
catch (TypeConversionException e) throw new ConstraintException(RMConstraintMessageKeys.ERR_NON_STRING, value, e);
{ }
throw new ConstraintException(ERR_NON_STRING, value, e); // check that the value is in the set of allowed values
} if (isCaseSensitive())
// check that the value is in the set of allowed values {
if (isCaseSensitive()) if (!getAllowedValues().contains(valueStr))
{ {
if (!getAllowedValues().contains(valueStr)) throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
{ }
throw new ConstraintException(ERR_INVALID_VALUE, value); }
} else
} {
else if (!getAllowedValuesUpper().contains(valueStr.toUpperCase()))
{ {
if (!getAllowedValuesUpper().contains(valueStr.toUpperCase())) throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
{ }
throw new ConstraintException(ERR_INVALID_VALUE, value); }
} }
} }
}
}

View File

@@ -35,7 +35,7 @@ public final class ClassificationLevel implements Serializable
{ {
/** serial version uid */ /** serial version uid */
private static final long serialVersionUID = -3375064867090476422L; private static final long serialVersionUID = -3375064867090476422L;
private final String id; private final String id;
private final String displayLabelKey; private final String displayLabelKey;
@@ -50,7 +50,10 @@ public final class ClassificationLevel implements Serializable
/** Returns the unique identifier for this classification level. */ /** Returns the unique identifier for this classification level. */
public String getId() { return this.id; } public String getId() { return this.id; }
/** Returns the localised (current locale) display label for this classification level. */ /**
* Returns the localised (current locale) display label for this classification level. If no translation is found
* then return the key instead.
*/
public String getDisplayLabel() public String getDisplayLabel()
{ {
String message = I18NUtil.getMessage(displayLabelKey); String message = I18NUtil.getMessage(displayLabelKey);

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.module.org_alfresco_module_rm.caveat.RMConstraintMessageKeys;
import org.alfresco.repo.dictionary.constraint.AbstractConstraint;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.repository.datatype.TypeConversionException;
/**
* Check that a value is a valid {@link ClassificationLevel} by checking the {@link ClassificationService}.
*
* @author tpage
*/
public class ClassificationLevelConstraint extends AbstractConstraint
{
/** The classification service provides access to the valid classification levels. */
private ClassificationService classificationService;
/** Constraints must use a default constructor. */
public ClassificationLevelConstraint()
{
super();
this.classificationService = ClassificationServiceProvider.getClassificationService();
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
// TODO Decide whether calling getAllowedValues() is a good idea.
sb.append("ClassificationLevelConstraint")
.append("[allowedValues=").append(getAllowedValues())
.append("]");
return sb.toString();
}
/**
* Get the allowed values. Note that these are <tt>String</tt> instances, but may
* represent non-<tt>String</tt> values. It is up to the caller to distinguish.
*
* @return Returns the values allowed
*/
private List<String> getAllowedValues()
{
List<ClassificationLevel> classificationLevels = classificationService.getClassificationLevels();
List<String> values = new ArrayList<String>();
for (ClassificationLevel classificationLevel : classificationLevels)
{
values.add(classificationLevel.getId());
}
return values;
}
/** {@inheritDoc} */
@Override
protected void evaluateSingleValue(Object value)
{
// convert the value to a String
String valueStr = null;
try
{
valueStr = DefaultTypeConverter.INSTANCE.convert(String.class, value);
}
catch (TypeConversionException e)
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_NON_STRING, value);
}
// Check that the classification level is one of the configured levels.
if (!getAllowedValues().contains(valueStr))
{
throw new ConstraintException(RMConstraintMessageKeys.ERR_INVALID_VALUE, value);
}
}
}

View File

@@ -42,7 +42,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
private static final Serializable[] REASONS_KEY = new String[] { "org.alfresco", private static final Serializable[] REASONS_KEY = new String[] { "org.alfresco",
"module.org_alfresco_module_rm", "module.org_alfresco_module_rm",
"classification.reasons" }; "classification.reasons" };
private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceImpl.class); private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceImpl.class);
private AttributeService attributeService; // TODO What about other code (e.g. REST API) accessing the AttrService? private AttributeService attributeService; // TODO What about other code (e.g. REST API) accessing the AttrService?
private ClassificationServiceDAO classificationServiceDao; private ClassificationServiceDAO classificationServiceDao;
@@ -80,7 +80,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
this.configuredLevels = allPersistedLevels; this.configuredLevels = allPersistedLevels;
} }
} }
void initConfiguredClassificationReasons() void initConfiguredClassificationReasons()
{ {
final List<ClassificationReason> persistedReasons = getPersistedReasons(); final List<ClassificationReason> persistedReasons = getPersistedReasons();
@@ -143,7 +143,7 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
{ {
return classificationServiceDao.getConfiguredLevels(); return classificationServiceDao.getConfiguredLevels();
} }
/** /**
* Gets the list of classification reasons as persisted in the system. * Gets the list of classification reasons as persisted in the system.
* @return the list of classification reasons if they have been persisted, else {@code null}. * @return the list of classification reasons if they have been persisted, else {@code null}.
@@ -186,7 +186,8 @@ public class ClassificationServiceImpl extends ServiceBaseImpl
@Override @Override
public List<ClassificationLevel> getClassificationLevels() public List<ClassificationLevel> getClassificationLevels()
{ {
if (configuredLevels == null) { if (configuredLevels == null)
{
return Collections.emptyList(); return Collections.emptyList();
} }
// FIXME Currently assume user has highest security clearance, this should be fixed as part of RM-2112. // FIXME Currently assume user has highest security clearance, this should be fixed as part of RM-2112.

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Spring class used to provide the {@link ClassificationService} to non-Spring classes.
*
* @author tpage
*/
public class ClassificationServiceProvider
{
/** Logging utility for the class. */
private static final Logger LOGGER = LoggerFactory.getLogger(ClassificationServiceProvider.class);
/** The Spring application context. */
private static final AtomicReference<ClassificationService> CLASSIFICATION_SERVICE_REF = new AtomicReference<>();
/** Constructor that takes the classification service and makes it available statically. */
public ClassificationServiceProvider(ClassificationService classificationService)
{
ClassificationService oldClassificationService = CLASSIFICATION_SERVICE_REF.getAndSet(classificationService);
if (oldClassificationService != null)
{
LOGGER.debug("Unexpected instantiation of ClassificationServiceProvider has updated reference to classification service.");
}
}
/**
* Get the <code>ClassificationService</code> as defined in the Spring context.
*
* @return The service bean.
*/
public static ClassificationService getClassificationService()
{
return CLASSIFICATION_SERVICE_REF.get();
}
}

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2005-2015 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.module.org_alfresco_module_rm.classification;
import static org.mockito.Mockito.doReturn;
import java.util.Arrays;
import java.util.List;
import org.alfresco.service.cmr.dictionary.ConstraintException;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Unit tests for the {@link ClassificationLevelConstraint}.
*
* @author tpage
*/
public class ClassificationLevelConstraintUnitTest
{
private static final ClassificationLevel LEVEL_ONE = new ClassificationLevel("id1", "DisplayKey1");
private static final ClassificationLevel LEVEL_TWO = new ClassificationLevel("id2", "DisplayKey2");
private static final List<ClassificationLevel> DEFAULT_LEVELS = Arrays.asList(LEVEL_ONE, LEVEL_TWO);
@InjectMocks ClassificationLevelConstraint classificationLevelConstraint;
@Mock ClassificationService mockClassificationService;
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
// Currently this list of levels suffices for all the tests.
doReturn(DEFAULT_LEVELS).when(mockClassificationService).getClassificationLevels();
}
/** Check that evaluateSingleValue throws no exceptions when an id is found. */
@Test
public void evaluateSingleValue_valid()
{
classificationLevelConstraint.evaluateSingleValue("id1");
}
/** Check that evaluateSingleValue throws an exception when an id is not found. */
@Test(expected = ConstraintException.class)
public void evaluateSingleValue_stringNotFound()
{
classificationLevelConstraint.evaluateSingleValue("non-existant id");
}
/** Check that evaluateSingleValue throws an exception when supplied with something that isn't a String. */
@Test(expected = ConstraintException.class)
public void evaluateSingleValue_notString()
{
classificationLevelConstraint.evaluateSingleValue(Integer.valueOf(123));
}
}

View File

@@ -28,8 +28,9 @@ import org.junit.runners.Suite;
@RunWith(Suite.class) @RunWith(Suite.class)
@Suite.SuiteClasses( @Suite.SuiteClasses(
{ {
ClassificationServiceImplUnitTest.class, ClassificationLevelConstraintUnitTest.class,
ClassificationServiceDAOUnitTest.class ClassificationServiceDAOUnitTest.class,
ClassificationServiceImplUnitTest.class
}) })
public class ClassificationSuite public class ClassificationSuite
{ {