Last active
December 11, 2015 20:59
-
-
Save sveinung/4659141 to your computer and use it in GitHub Desktop.
Forslag til validering i Jerseyressursar. Spesielt av former o.l.
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
public class ConcreteFormValidator extends FormValidator<FormDTO> { | |
private NumericStringValidator numericStringValidator; | |
public ConcreteFormValidator(NumericStringValidator numericStringValidator) { | |
this.numericStringValidator = numericStringValidator; | |
} | |
@Override | |
public List<Message> validate(FormDTO form) { | |
return collectMessages(this.numericStringValidator.validate(form.getA(), "first"), | |
this.numericStringValidator.validate(form.getB(), "second")); | |
} | |
} |
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
public class ConcreteFormValidatorTest { | |
private ConcreteFormValidator formValidator; | |
@Before | |
public void setUp() { | |
formValidator = new ConcreteFormValidator(new NumericStringValidator()); | |
} | |
@Test | |
public void should_validate() { | |
List<Message> messages = formValidator.validate(new FormDTO("a", "1")); | |
assertEquals(messages.size(), 1); | |
assertEquals(messages.get(0).getField(), "first"); | |
assertEquals(messages.get(0).getKey(), "some.error.string"); | |
assertEquals(messages.get(0).getValue(), "a"); | |
assertEquals(messages.get(0).getType(), MessageType.FAILED); | |
} | |
@Test | |
public void should_throw() { | |
try { | |
formValidator.validateAndThrow(new FormDTO("a", "1")); | |
fail(); | |
} catch (ResourceValidationException e) { | |
List<Message> messages = e.getMessages(); | |
assertEquals(messages.size(), 1); | |
assertEquals(messages.get(0).getField(), "first"); | |
assertEquals(messages.get(0).getKey(), "some.error.string"); | |
assertEquals(messages.get(0).getValue(), "a"); | |
assertEquals(messages.get(0).getType(), MessageType.FAILED); | |
} | |
} | |
@Test | |
public void should_pass() { | |
formValidator.validateAndThrow(new FormDTO("1", "3")); | |
} | |
} |
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
public class FormDTO { | |
private String a; | |
private String b; | |
public FormDTO(String a, String b) { | |
this.a = a; | |
this.b = b; | |
} | |
public String getA() { | |
return a; | |
} | |
public String getB() { | |
return b; | |
} | |
} |
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
public abstract class FormValidator<T> { | |
public abstract List<Message> validate(T t); | |
public void validateAndThrow(T t) { | |
List<Message> messages = validate(t); | |
if (!messages.isEmpty()) { | |
throw new ResourceValidationException(messages); | |
} | |
} | |
protected List<Message> collectMessages(Message... messages) { | |
List<Message> collected = new ArrayList<Message>(); | |
for (Message message : messages) { | |
if (message != null) { | |
collected.add(message); | |
} | |
} | |
return collected; | |
} | |
} |
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
public class Message { | |
private String field; | |
private String key; | |
private String value; | |
private MessageType type; | |
public Message(String field, String key, String value, MessageType type) { | |
this.field = field; | |
this.key = key; | |
this.value = value; | |
this.type = type; | |
} | |
public String getField() { | |
return field; | |
} | |
public String getKey() { | |
return key; | |
} | |
public String getValue() { | |
return value; | |
} | |
public MessageType getType() { | |
return type; | |
} | |
} |
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
public enum MessageType { | |
SUCCESS, FAILED | |
} |
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
public class NumericStringValidator extends PrimitiveValidator<String> { | |
@Override | |
public Message validate(String numericString, String field) { | |
if (!isNumeric(numericString)) { | |
return new Message(field, "some.error.string", numericString, MessageType.FAILED); | |
} | |
return null; | |
} | |
} |
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
public abstract class PrimitiveValidator<T> { | |
public abstract Message validate(T t, String field); | |
} |
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
public class ResourceValidationException extends RuntimeException { | |
private List<Message> messages; | |
public ResourceValidationException(List<Message> messages) { | |
this.messages = messages; | |
} | |
public List<Message> getMessages() { | |
return messages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment