Created
June 5, 2015 09:36
-
-
Save philipp-spiess/c1673f88e84d6d4e6df2 to your computer and use it in GitHub Desktop.
Validators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sepm.ss15.e1325214.validators; | |
import sepm.ss15.e1325214.models.Jockey; | |
/** | |
* Specific validation behaviour for a jockey object | |
* | |
* @author philipp | |
*/ | |
public class JockeyValidator extends Validator{ | |
private Jockey jockey; | |
public JockeyValidator(Jockey jockey) { | |
this.jockey = jockey; | |
} | |
/** | |
* Overrides the business cirtical validation logic | |
*/ | |
protected void validate() { | |
validateName(jockey.getName()); | |
validateSkill(jockey.getSkill()); | |
validateCountryCode(jockey.getCountryCode()); | |
} | |
/** | |
* Validates a the name attribute | |
* | |
* @param name | |
*/ | |
private void validateName(String name) { | |
validateNotNull(name, "Name"); | |
validatePresence(name, "Name"); | |
validateVarchar(name, "Name"); | |
} | |
/** | |
* Validates the skill attribute | |
* | |
* @param skill | |
*/ | |
public void validateSkill(Long skill) { | |
validateNotNull(skill, "Skill"); | |
} | |
/** | |
* Validates the county code attribute | |
* | |
* @param countryCode | |
*/ | |
public void validateCountryCode(String countryCode) { | |
validateNotNull(countryCode, "Country"); | |
validateInclusionOf(countryCode, "Country", Jockey.COUNTRIES); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sepm.ss15.e1325214.tests.validators; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import sepm.ss15.e1325214.validators.*; | |
import sepm.ss15.e1325214.models.*; | |
/** | |
* Created by philipp on 07.04.15. | |
*/ | |
public class JockeyValidatorTest { | |
private static final Logger LOGGER = LogManager.getLogger(JockeyValidatorTest.class); | |
@Test | |
public void validatesNameIsNotNull() { | |
Jockey jockey = new Jockey(null, (long) 0, "AT"); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
@Test | |
public void validatesNameIsNotEmpty() { | |
Jockey jockey = new Jockey("", (long) 0, "AT"); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
@Test | |
public void validatesNameIsVarcharEmpty() { | |
StringBuilder sb = new StringBuilder(256); | |
for( int i = 0; i < 256; i++ ) { | |
sb.append( 'A' ); | |
} | |
Jockey jockey = new Jockey(sb.toString(), (long) 0, "AT"); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
@Test | |
public void validatesSkillIsNotNull() { | |
Jockey jockey = new Jockey("Valid", null, "AT"); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
@Test | |
public void validatesLanguageCodeIsNotNull() { | |
Jockey jockey = new Jockey("Valid", (long) 0, null); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
@Test | |
public void validatesLanguageCodeInclusion() { | |
Jockey jockey = new Jockey("Valid", (long) 0, "DE"); | |
Validator jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertTrue(jockeyValidator.isValid()); | |
jockey = new Jockey("Valid", (long) 0, "AT"); | |
jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertTrue(jockeyValidator.isValid()); | |
jockey = new Jockey("Valid", (long) 0, "CH"); | |
jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertTrue(jockeyValidator.isValid()); | |
jockey = new Jockey("Valid", (long) 0, "XX"); | |
jockeyValidator = new JockeyValidator(jockey); | |
Assert.assertFalse(jockeyValidator.isValid()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sepm.ss15.e1325214.validators; | |
import java.util.List; | |
/** | |
* A validation exception can occur when the system fails to validate an object. | |
* The Message contains a list of the validation errors | |
* | |
* @author philipp | |
*/ | |
public class ValidationException extends Exception { | |
public ValidationException(List<String> messages) { | |
super(String.join("\n", messages) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sepm.ss15.e1325214.validators; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
import org.apache.logging.log4j.LogManager; | |
import org.apache.logging.log4j.Logger; | |
import sun.reflect.generics.reflectiveObjects.NotImplementedException; | |
/** | |
* Abstract validator. Can be extended to implement model specific behaviour. | |
* A few basic validations are already included. When you need additionaö behaviour you have to add them on your own. | |
* | |
* @author philipp | |
*/ | |
public abstract class Validator { | |
protected static final Logger LOGGER = LogManager.getLogger(Validator.class); | |
protected List<String> messages = new ArrayList<String>(); | |
/** | |
* Chilren need to implements the validate method to mix and match their validation rules for a specific | |
* object. | |
*/ | |
protected void validate() { | |
throw new NotImplementedException(); | |
} | |
/** | |
* Validates the object. Returns true if no validation failed messages were found and false otherwise | |
* | |
* @return | |
*/ | |
public boolean isValid() { | |
this.validate(); | |
if(messages.isEmpty()){ | |
return true; | |
} else { | |
LOGGER.debug("Failed to validate object:" + getMessages()); | |
return false; | |
} | |
} | |
/** | |
* Validates if +value+ is not null. | |
* | |
* @param value | |
* @param name used for error message | |
*/ | |
protected void validateNotNull(Object value, String name) { | |
if(value == null) { | |
messages.add(name + " cannot be null."); | |
} | |
} | |
/** | |
* validates if a sting is present. (e.g. not "") | |
* | |
* @param value | |
* @param name used for error message | |
*/ | |
protected void validatePresence(String value, String name) { | |
if(value == null) { | |
return; | |
} | |
if(value.length() <= 0) { | |
messages.add(name + " cannot be empty."); | |
} | |
} | |
/** | |
* Validates if a string doesn't exceed the varchar max size (255) | |
* | |
* @param value | |
* @param name used for error message | |
*/ | |
protected void validateVarchar(String value, String name) { | |
if(value == null) { | |
return; | |
} | |
if(value.length() > 255) { | |
messages.add(name + " cannot have more than 255 characters."); | |
} | |
} | |
/** | |
* Validates if a string is included in a given set. | |
* | |
* @param value | |
* @param name used for error mes | |
* @param set | |
*/ | |
protected void validateInclusionOf(String value, String name, String[] set) { | |
if(value == null) { | |
return; | |
} | |
if(!Arrays.asList(set).contains(value)) { | |
messages.add(name + " must be one of " + Arrays.asList(set).toString() + "."); | |
} | |
} | |
public List<String> getMessages() { | |
return messages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment