Moti Asayag has uploaded a new change for review.

Change subject: engine: Support replacements in ValidationResult
......................................................................

engine: Support replacements in ValidationResult

The static validation methods of ValidationResult are enhanced
with the support of replacements, which are already supported
by the ValidationResult instantiation.

Change-Id: I5f2bd85af2d129e0647b100b90908a1314419da2
Signed-off-by: Moti Asayag <[email protected]>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ValidationResult.java
M 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ValidationResultTest.java
2 files changed, 58 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/55/34855/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ValidationResult.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ValidationResult.java
index a729cca..7477735 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ValidationResult.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ValidationResult.java
@@ -166,12 +166,52 @@
     }
 
     /**
+     * Return an error if the following validation is not successful, or a 
valid result if it is.<br>
+     * <br>
+     * For example, if we want to make sure that <b>num doesn't equal 2</b> 
then we would do:
+     *
+     * <pre>
+     * int num = 1;
+     * ValidationResult.error(VdcBllMessages.ERROR_CONST).when(num == 2);
+     * </pre>
+     *
+     * <br>
+     * Which would return a valid result since 1 != 2.<br>
+     * If we were to set <b>num = 2</b> in the example then the desired 
validation error would return.<br>
+     * <br>
+     * Conveniently, we can also check that <b>num equals 2</b> by doing:
+     *
+     * <pre>
+     * int num = 1;
+     * ValidationResult.error(VdcBllMessages.ERROR_CONST, &quot;$COMPARED_NUM 
2&quot;).unless(num == 2);
+     * </pre>
+     *
+     * <br>
+     * This time the desired validation error would return since 1 != 2.<br>
+     * If we were to set <b>num = 2</b> in the example then the result would 
be valid.<br>
+     * In addition, the replacement will contain the substitutions for the 
message.<br>
+     *
+     * @param expectedError
+     *            The error we expect should the validation fail.
+     * @param replacements
+     *            The replacements to be associated with the validation result
+     * @return A helper object that returns the correct validation result 
depending on the condition.
+     */
+    public static ValidationResultBuilder failWith(VdcBllMessages 
expectedError, String... replacements) {
+        return new ValidationResultBuilder(expectedError, replacements);
+    }
+
+    /**
      * Helper class to chain calls that produce a {@link ValidationResult}.
      */
     public static class ValidationResultBuilder {
 
         private ValidationResult expectedValidation;
 
+        private ValidationResultBuilder(VdcBllMessages expectedError, String[] 
replacements) {
+            expectedValidation = new ValidationResult(expectedError, 
replacements);
+        }
+
         private ValidationResultBuilder(VdcBllMessages expectedError) {
             expectedValidation = new ValidationResult(expectedError);
         }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ValidationResultTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ValidationResultTest.java
index 9edf197..ff4763f 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ValidationResultTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/ValidationResultTest.java
@@ -1,8 +1,11 @@
 package org.ovirt.engine.core.bll;
 
+import static org.hamcrest.CoreMatchers.both;
+import static org.hamcrest.CoreMatchers.hasItem;
 import static org.junit.Assert.assertThat;
 import static 
org.ovirt.engine.core.bll.validator.ValidationResultMatchers.failsWith;
 import static 
org.ovirt.engine.core.bll.validator.ValidationResultMatchers.isValid;
+import static 
org.ovirt.engine.core.bll.validator.ValidationResultMatchers.replacements;
 
 import org.junit.Test;
 import org.ovirt.engine.core.common.errors.VdcBllMessages;
@@ -13,6 +16,8 @@
      * Some message to use with the tests, we don't care what it actually is.
      */
     private static final VdcBllMessages ERROR = VdcBllMessages.values()[0];
+    private static final String REPLACEMENT = "replacement";
+    private static final String OTHER_REPLACEMENT = "other_replacement";
 
     @Test
     public void invalidWhenConditionOccurs() throws Exception {
@@ -33,4 +38,17 @@
     public void invalidUnlessConditionDoesntOccur() throws Exception {
         assertThat(ValidationResult.failWith(ERROR).unless(false), 
failsWith(ERROR));
     }
+
+    @Test
+    public void invalidWhenConditionDoesntOccurWithSingleReplacement() throws 
Exception {
+        assertThat(ValidationResult.failWith(ERROR, REPLACEMENT).when(true),
+                
both(failsWith(ERROR)).and(replacements(hasItem(REPLACEMENT))));
+    }
+
+    @Test
+    public void invalidWhenConditionDoesntOccurWithMultipleReplacements() 
throws Exception {
+        assertThat(ValidationResult.failWith(ERROR, REPLACEMENT, 
OTHER_REPLACEMENT).when(true),
+                both(failsWith(ERROR)).and(replacements(hasItem(REPLACEMENT)))
+                        .and(replacements(hasItem(OTHER_REPLACEMENT))));
+    }
 }


-- 
To view, visit http://gerrit.ovirt.org/34855
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5f2bd85af2d129e0647b100b90908a1314419da2
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Moti Asayag <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to