Bug 2816: Changed BooleanStringCodec deserialization 12/20412/7
authorDebalina Ghosh <debalina.ghosh@hp.com>
Thu, 14 May 2015 16:59:57 +0000 (09:59 -0700)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 4 Jun 2015 11:09:24 +0000 (11:09 +0000)
Added validate() to deserialize that checks whether input string is true or false.
If some other string is given, IllegalArgumentException is thrown.
Unit test is changed accordingly.

Change-Id: I51e7da19bd4cce85aa707d7741ae3956cc3e8890
Signed-off-by: Debalina Ghosh <debalina.ghosh@hp.com>
yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/codec/BooleanStringCodec.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/test/codecs/BooleanCodecStringTest.java

index 54bd3372deaaba57106e8fcea4a9e4415741d55f..28212224f63dec3a8f6289107036558574ac2545 100644 (file)
@@ -8,6 +8,8 @@
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+
 import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
 
@@ -25,9 +27,19 @@ class BooleanStringCodec extends TypeDefinitionAwareCodec<Boolean, BooleanTypeDe
 
     @Override
     public final Boolean deserialize(final String stringRepresentation) {
+        if (stringRepresentation == null) {
+            return null;
+        }
+        validate(stringRepresentation);
         return Boolean.valueOf(stringRepresentation);
     }
 
+    private void validate(final String string) {
+        Preconditions.checkArgument(string.toLowerCase().equals("true")
+                || string.toLowerCase().equals("false"),
+                "Invalid value '%s' for boolean type. Allowed values are true and false", string);
+    }
+
     static TypeDefinitionAwareCodec<?,BooleanTypeDefinition> from(final BooleanTypeDefinition normalizedType) {
         return new BooleanStringCodec(Optional.fromNullable(normalizedType));
     }
index 860357b142d7ae6f2421699bc4d27ba67a0dfdc0..7bcde74390652e9b91f327513845fc5acaee7a3b 100644 (file)
@@ -11,7 +11,10 @@ package org.opendaylight.yangtools.yang.data.impl.test.codecs;
 import static org.junit.Assert.*;
 
 import org.junit.Test;
+
+import static org.opendaylight.yangtools.yang.data.impl.test.codecs.TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx;
 import static org.opendaylight.yangtools.yang.data.impl.test.codecs.TypeDefinitionAwareCodecTestHelper.getCodec;
+
 import org.opendaylight.yangtools.yang.data.api.codec.BooleanCodec;
 import org.opendaylight.yangtools.yang.model.util.BooleanType;
 
@@ -41,8 +44,8 @@ public class BooleanCodecStringTest {
         assertEquals( "deserialize", Boolean.TRUE, codec.deserialize( "TRUE" ) );
         assertEquals( "deserialize", Boolean.FALSE, codec.deserialize( "FALSE" ) );
         assertEquals( "deserialize", Boolean.FALSE, codec.deserialize( "false" ) );
-        assertEquals( "deserialize", Boolean.FALSE, codec.deserialize( "foo" ) );
-        assertEquals( "deserialize", Boolean.FALSE, codec.deserialize( "" ) );
-        assertEquals( "deserialize", Boolean.FALSE, codec.deserialize( null ) );
+        assertEquals( "deserialize", null, codec.deserialize( null ) );
+        deserializeWithExpectedIllegalArgEx(codec, "foo");
+        deserializeWithExpectedIllegalArgEx(codec, "");
     }
 }