Bug 762: Fix sal-rest-connector unit tests 72/7372/8
authortpantelis <tpanteli@brocade.com>
Sun, 25 May 2014 04:04:21 +0000 (00:04 -0400)
committertpantelis <tpanteli@brocade.com>
Thu, 5 Jun 2014 01:14:40 +0000 (21:14 -0400)
The cnsn-to-json/simple-data-types/xml/data.xml test file used by
CnSnToJsonBasicDataTypesTest actually contains some invalid union type data.
The tests currently pass because union type input data isn't currently
validated. Patch https://git.opendaylight.org/gerrit/#/c/7367/
in yangtools adds validation so the unit tests must be fixed before
that patch is merged.

In addition, the yangtools patch changes some static fields in
TypeDefinitionAwareCodec to private which breaks compilation of
CnSnToXmlTest. This patch modifies CnSnToXmlTest to not use the static
fields.

Change-Id: I020ffacd4a6c96265a191fa74f982de807e336a4
Signed-off-by: tpantelis <tpanteli@brocade.com>
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CodecsExceptionsCatchingTest.java
opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/bad-data.xml [new file with mode: 0644]
opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/data.xml

index 312365585eaa149aaa05125acc4530840451c8e9..4e32e7058ca6cce63f9a38760764b0b0c137ddf7 100644 (file)
@@ -11,49 +11,127 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.IOException;
 import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import javax.ws.rs.WebApplicationException;
+import java.util.Map;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
+import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
 
+import com.google.common.collect.Maps;
 import com.google.gson.stream.JsonReader;
 import com.google.gson.stream.JsonToken;
 
 public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
 
+    static abstract class LeafVerifier {
+
+        Object expectedValue;
+        JsonToken expectedToken;
+
+        LeafVerifier( Object expectedValue, JsonToken expectedToken ) {
+            this.expectedValue = expectedValue;
+            this.expectedToken = expectedToken;
+        }
+
+        abstract Object getActualValue( JsonReader reader ) throws IOException;
+
+        void verify( JsonReader reader, String keyName ) throws IOException {
+            assertEquals( "Json value for key " + keyName, expectedValue, getActualValue( reader ) );
+        }
+
+        JsonToken expectedTokenType() {
+            return expectedToken;
+        }
+    }
+
+    static class BooleanVerifier extends LeafVerifier {
+
+        public BooleanVerifier( boolean expected ) {
+            super( expected, JsonToken.BOOLEAN );
+        }
+
+        @Override
+        Object getActualValue( JsonReader reader ) throws IOException {
+            return reader.nextBoolean();
+        }
+    }
+
+    static class NumberVerifier extends LeafVerifier {
+
+        public NumberVerifier( Number expected ) {
+            super( expected, JsonToken.NUMBER );
+        }
+
+        @Override
+        Object getActualValue( JsonReader reader ) throws IOException {
+            if( expectedValue instanceof Double ) {
+                return reader.nextDouble();
+            }
+            else if( expectedValue instanceof Long ) {
+                return reader.nextLong();
+            }
+            else if( expectedValue instanceof Integer ) {
+                return reader.nextInt();
+            }
+
+            return null;
+        }
+    }
+
+    static class StringVerifier extends LeafVerifier {
+
+        StringVerifier( String expected ) {
+            super( expected, JsonToken.STRING );
+        }
+
+        @Override
+        Object getActualValue( JsonReader reader ) throws IOException {
+            return reader.nextString();
+        }
+    }
+
+    static class EmptyVerifier extends LeafVerifier {
+
+        EmptyVerifier() {
+            super( null, null );
+        }
+
+        @Override
+        Object getActualValue( JsonReader reader ) throws IOException {
+            reader.beginArray();
+            reader.nextNull();
+            reader.endArray();
+            return null;
+        }
+
+    }
+
     @BeforeClass
     public static void initialize() {
         dataLoad("/cnsn-to-json/simple-data-types");
     }
 
     @Test
-    public void simpleYangDataTest() {
+    public void simpleYangDataTest() throws Exception {
 
         CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
                 XmlToCompositeNodeProvider.INSTANCE);
 
-        String jsonOutput = null;
-
         TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
 
-        try {
-            jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
+        String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
                     StructuredDataToJsonProvider.INSTANCE);
-        } catch (WebApplicationException | IOException e) {
-        }
+
         assertNotNull(jsonOutput);
 
         verifyJsonOutput(jsonOutput);
@@ -88,167 +166,84 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader
 
     private void jsonReadContElements(JsonReader jReader) throws IOException {
         jReader.beginObject();
-        List<String> loadedLfs = new ArrayList<>();
-        boolean enumChecked = false;
-        boolean bitsChecked = false;
-        boolean lfdecimal6Checked = false;
-        boolean lfdecimal4Checked = false;
-        boolean lfdecimal3Checked = false;
-        boolean lfdecimal2Checked = false;
-        boolean lfdecimal1Checked = false;
-        boolean lfbool1Checked = false;
-        boolean lfbool2Checked = false;
-        boolean lfstrChecked = false;
-        boolean lfbinaryChecked = false;
-        boolean lfemptyChecked = false;
-        boolean lfstr1Checked = false;
-        boolean lfidentityrefChecked = false;
+
+        Map<String,LeafVerifier> expectedMap = Maps.newHashMap();
+        expectedMap.put( "lfnint8Min", new NumberVerifier( Integer.valueOf( -128 ) ) );
+        expectedMap.put( "lfnint8Max", new NumberVerifier( Integer.valueOf( 127 ) ) );
+        expectedMap.put( "lfnint16Min", new NumberVerifier( Integer.valueOf( -32768 ) ) );
+        expectedMap.put( "lfnint16Max", new NumberVerifier( Integer.valueOf( 32767 ) ) );
+        expectedMap.put( "lfnint32Min", new NumberVerifier( Integer.valueOf( -2147483648 ) ) );
+        expectedMap.put( "lfnint32Max", new NumberVerifier( Long.valueOf( 2147483647 ) ) );
+        expectedMap.put( "lfnint64Min", new NumberVerifier( Long.valueOf( -9223372036854775808L ) ) );
+        expectedMap.put( "lfnint64Max", new NumberVerifier( Long.valueOf( 9223372036854775807L ) ) );
+        expectedMap.put( "lfnuint8Max", new NumberVerifier( Integer.valueOf( 255 ) ) );
+        expectedMap.put( "lfnuint16Max", new NumberVerifier( Integer.valueOf( 65535 ) ) );
+        expectedMap.put( "lfnuint32Max", new NumberVerifier( Long.valueOf( 4294967295L ) ) );
+        expectedMap.put( "lfstr", new StringVerifier( "lfstr" ) );
+        expectedMap.put( "lfstr1", new StringVerifier( "" ) );
+        expectedMap.put( "lfbool1", new BooleanVerifier( true ) );
+        expectedMap.put( "lfbool2", new BooleanVerifier( false ) );
+        expectedMap.put( "lfbool3", new BooleanVerifier( false ) );
+        expectedMap.put( "lfdecimal1", new NumberVerifier( new Double( 43.32 ) ) );
+        expectedMap.put( "lfdecimal2", new NumberVerifier( new Double( -0.43 ) ) );
+        expectedMap.put( "lfdecimal3", new NumberVerifier( new Double( 43 ) ) );
+        expectedMap.put( "lfdecimal4", new NumberVerifier( new Double( 43E3 ) ) );
+        expectedMap.put( "lfdecimal6", new NumberVerifier( new Double( 33.12345 ) ) );
+        expectedMap.put( "lfenum", new StringVerifier( "enum3" ) );
+        expectedMap.put( "lfbits", new StringVerifier( "bit3 bit2" ) );
+        expectedMap.put( "lfbinary", new StringVerifier( "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ) );
+        expectedMap.put( "lfunion1", new StringVerifier( "324" ) );
+        expectedMap.put( "lfunion2", new StringVerifier( "33.3" ) );
+        expectedMap.put( "lfunion3", new StringVerifier( "55" ) );
+        expectedMap.put( "lfunion4", new StringVerifier( "true" ) );
+        expectedMap.put( "lfunion5", new StringVerifier( "true" ) );
+        expectedMap.put( "lfunion6", new StringVerifier( "10" ) );
+        expectedMap.put( "lfunion7", new StringVerifier( "" ) );
+        expectedMap.put( "lfunion8", new StringVerifier( "" ) );
+        expectedMap.put( "lfunion9", new StringVerifier( "" ) );
+        expectedMap.put( "lfunion10", new StringVerifier( "bt1" ) );
+        expectedMap.put( "lfunion11", new StringVerifier( "33" ) );
+        expectedMap.put( "lfunion12", new StringVerifier( "false" ) );
+        expectedMap.put( "lfunion13", new StringVerifier( "b1" ) );
+        expectedMap.put( "lfunion14", new StringVerifier( "zero" ) );
+        expectedMap.put( "lfempty", new EmptyVerifier() );
+        expectedMap.put( "identityref1", new StringVerifier( "simple-data-types:iden" ) );
 
         while (jReader.hasNext()) {
             String keyName = jReader.nextName();
-            JsonToken peek = null;
-            try {
-                peek = jReader.peek();
-            } catch (IOException e) {
-                assertTrue("Key " + keyName + " has incorrect value for specifed type", false);
-            }
+            JsonToken peek = jReader.peek();
 
-            if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                try {
-                    jReader.nextLong();
-                } catch (NumberFormatException e) {
-                    assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false);
-                }
-                loadedLfs.add(keyName.substring(3));
-            } else if (keyName.equals("identityref1")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-                assertEquals("simple-data-types:iden", jReader.nextString());
-                lfidentityrefChecked = true;
-            } else if (keyName.equals("lfstr")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-                assertEquals("lfstr", jReader.nextString());
-                lfstrChecked = true;
-            } else if (keyName.equals("lfstr1")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-                assertEquals("", jReader.nextString());
-                lfstr1Checked = true;
-            } else if (keyName.equals("lfbool1")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
-                assertEquals(true, jReader.nextBoolean());
-                lfbool1Checked = true;
-            } else if (keyName.equals("lfbool2")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
-                assertEquals(false, jReader.nextBoolean());
-                lfbool2Checked = true;
-            } else if (keyName.equals("lfbool3")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek);
-                assertEquals(false, jReader.nextBoolean());
-            } else if (keyName.equals("lfdecimal1")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                assertEquals(new Double(43.32), (Double) jReader.nextDouble());
-                lfdecimal1Checked = true;
-            } else if (keyName.equals("lfdecimal2")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                assertEquals(new Double(-0.43), (Double) jReader.nextDouble());
-                lfdecimal2Checked = true;
-            } else if (keyName.equals("lfdecimal3")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                assertEquals(new Double(43), (Double) jReader.nextDouble());
-                lfdecimal3Checked = true;
-            } else if (keyName.equals("lfdecimal4")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                assertEquals(new Double(43E3), (Double) jReader.nextDouble());
-                lfdecimal4Checked = true;
-            } else if (keyName.equals("lfdecimal6")) {
-                assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek);
-                assertEquals(new Double(33.12345), (Double) jReader.nextDouble());
-                lfdecimal6Checked = true;
-            } else if (keyName.equals("lfenum")) {
-                assertEquals("enum3", jReader.nextString());
-                enumChecked = true;
-            } else if (keyName.equals("lfbits")) {
-                assertEquals("bit3 bit2", jReader.nextString());
-                bitsChecked = true;
-            } else if (keyName.equals("lfbinary")) {
-                assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", jReader.nextString());
-                lfbinaryChecked = true;
-            } else if (keyName.equals("lfempty")) {
-                jReader.beginArray();
-                jReader.nextNull();
-                jReader.endArray();
-                lfemptyChecked = true;
-            } else if (keyName.startsWith("lfunion")) {
-                checkLfUnion(jReader, keyName, peek);
-            } else {
-                assertTrue("Key " + keyName + " doesn't exists in yang file.", false);
+            LeafVerifier verifier = expectedMap.remove( keyName );
+            assertNotNull( "Found unexpected leaf: " + keyName , verifier );
+
+            JsonToken expToken = verifier.expectedTokenType();
+            if( expToken != null ) {
+                assertEquals( "Json token type for key " + keyName, expToken, peek );
             }
 
+            verifier.verify( jReader, keyName );;
+        }
+
+        if( !expectedMap.isEmpty() ) {
+            fail( "Missing leaf nodes in Json output: " +expectedMap.keySet() );
         }
-        Collections.sort(loadedLfs);
-        String expectedLfsStr = "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]";
-        String actualLfsStr = loadedLfs.toString();
-        assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr);
-        assertTrue("Enum wasn't checked", enumChecked);
-        assertTrue("Bits wasn't checked", bitsChecked);
-        assertTrue("Decimal1 wasn't checked", lfdecimal1Checked);
-        assertTrue("Decimal2 wasn't checked", lfdecimal2Checked);
-        assertTrue("Decimal3 wasn't checked", lfdecimal3Checked);
-        assertTrue("Decimal4 wasn't checked", lfdecimal4Checked);
-        assertTrue("Decimal5 wasn't checked", lfdecimal6Checked);
-        assertTrue("lfbool1 wasn't checked", lfbool1Checked);
-        assertTrue("lfbool2 wasn't checked", lfbool2Checked);
-        assertTrue("lfstr wasn't checked", lfstrChecked);
-        assertTrue("lfstr1 wasn't checked", lfstr1Checked);
-        assertTrue("lfbinary wasn't checked", lfbinaryChecked);
-        assertTrue("lfempty wasn't checked", lfemptyChecked);
-        assertTrue("lfidentityref wasn't checked", lfidentityrefChecked);
+
         jReader.endObject();
     }
 
-    private void checkLfUnion(JsonReader jReader, String keyName, JsonToken peek) throws IOException {
-        if (keyName.equals("lfunion1")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("324", jReader.nextString());
-        } else if (keyName.equals("lfunion2")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("33.3", jReader.nextString());
-        } else if (keyName.equals("lfunion3")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("55", jReader.nextString());
-        } else if (keyName.equals("lfunion4")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("true", jReader.nextString());
-        } else if (keyName.equals("lfunion5")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("true", jReader.nextString());
-        } else if (keyName.equals("lfunion6")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("false", jReader.nextString());
-        } else if (keyName.equals("lfunion7")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("", jReader.nextString());
-        } else if (keyName.equals("lfunion8")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("", jReader.nextString());
-        } else if (keyName.equals("lfunion9")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("", jReader.nextString());
-        } else if (keyName.equals("lfunion10")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("bt1", jReader.nextString());
-        } else if (keyName.equals("lfunion11")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("33", jReader.nextString());
-        } else if (keyName.equals("lfunion12")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("false", jReader.nextString());
-        } else if (keyName.equals("lfunion13")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("44", jReader.nextString());
-        } else if (keyName.equals("lfunion14")) {
-            assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek);
-            assertEquals("21", jReader.nextString());
+    @Test
+    public void testBadData() throws Exception {
+
+        try {
+            CompositeNode compositeNode = TestUtils.readInputToCnSn(
+                                               "/cnsn-to-json/simple-data-types/xml/bad-data.xml",
+                                               XmlToCompositeNodeProvider.INSTANCE);
+
+            TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
+            fail( "Expected RestconfDocumentedException" );
+        }
+        catch( RestconfDocumentedException e ) {
+            assertEquals( "getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get( 0 ).getErrorTag() );
         }
     }
 }
index fc54795fcce5a468cb0d6e780f561a3052fe2eeb..155ee9d5908ab7ac6c285278542e976f11374d9a 100644 (file)
@@ -11,12 +11,16 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
+import java.util.List;
 
 import javax.ws.rs.WebApplicationException;
 import javax.xml.transform.TransformerFactoryConfigurationError;
 
 import org.junit.BeforeClass;
 import org.junit.Test;
+
+import static org.mockito.Mockito.*;
+
 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
@@ -26,6 +30,29 @@ import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
+import org.opendaylight.yangtools.yang.model.util.BinaryType;
+import org.opendaylight.yangtools.yang.model.util.BitsType;
+import org.opendaylight.yangtools.yang.model.util.BooleanType;
+import org.opendaylight.yangtools.yang.model.util.EmptyType;
+import org.opendaylight.yangtools.yang.model.util.EnumerationType;
+import org.opendaylight.yangtools.yang.model.util.Int16;
+import org.opendaylight.yangtools.yang.model.util.Int32;
+import org.opendaylight.yangtools.yang.model.util.Int64;
+import org.opendaylight.yangtools.yang.model.util.Int8;
+import org.opendaylight.yangtools.yang.model.util.StringType;
+import org.opendaylight.yangtools.yang.model.util.Uint16;
+import org.opendaylight.yangtools.yang.model.util.Uint32;
+import org.opendaylight.yangtools.yang.model.util.Uint64;
+import org.opendaylight.yangtools.yang.model.util.Uint8;
+import org.opendaylight.yangtools.yang.model.util.UnionType;
+
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
 
 /**
  *
@@ -65,7 +92,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     @Test
     public void snAsYangStringToXmlTest() {
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.STRING_DEFAULT_CODEC.deserialize("lfStr value"),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(StringType.getInstance()).deserialize("lfStr value"),
                         "lfStr"), "<lfStr>lfStr value</lfStr>");
     }
 
@@ -73,7 +100,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangInt8ToXmlTest() {
         String elName = "lfInt8";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.INT8_DEFAULT_CODEC.deserialize("14"), elName), "<"
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int8.getInstance()).deserialize("14"), elName), "<"
                         + elName + ">14</" + elName + ">");
     }
 
@@ -81,7 +108,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangInt16ToXmlTest() {
         String elName = "lfInt16";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.INT16_DEFAULT_CODEC.deserialize("3000"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int16.getInstance()).deserialize("3000"), elName),
                 "<" + elName + ">3000</" + elName + ">");
     }
 
@@ -89,7 +116,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangInt32ToXmlTest() {
         String elName = "lfInt32";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.INT32_DEFAULT_CODEC.deserialize("201234"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int32.getInstance()).deserialize("201234"), elName),
                 "<" + elName + ">201234</" + elName + ">");
     }
 
@@ -97,7 +124,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangInt64ToXmlTest() {
         String elName = "lfInt64";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.INT64_DEFAULT_CODEC.deserialize("5123456789"),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Int64.getInstance()).deserialize("5123456789"),
                         elName), "<" + elName + ">5123456789</" + elName + ">");
     }
 
@@ -105,7 +132,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangUint8ToXmlTest() {
         String elName = "lfUint8";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT8_DEFAULT_CODEC.deserialize("200"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint8.getInstance()).deserialize("200"), elName),
                 "<" + elName + ">200</" + elName + ">");
     }
 
@@ -113,7 +140,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangUint16ToXmlTest() {
         String elName = "lfUint16";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT16_DEFAULT_CODEC.deserialize("4000"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint16.getInstance()).deserialize("4000"), elName),
                 "<" + elName + ">4000</" + elName + ">");
     }
 
@@ -121,7 +148,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangUint32ToXmlTest() {
         String elName = "lfUint32";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT32_DEFAULT_CODEC.deserialize("4123456789"),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint32.getInstance()).deserialize("4123456789"),
                         elName), "<" + elName + ">4123456789</" + elName + ">");
     }
 
@@ -129,7 +156,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangUint64ToXmlTest() {
         String elName = "lfUint64";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT64_DEFAULT_CODEC.deserialize("5123456789"),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(Uint64.getInstance()).deserialize("5123456789"),
                         elName), "<" + elName + ">5123456789</" + elName + ">");
     }
 
@@ -138,25 +165,40 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
         String elName = "lfBinary";
         serializeToXml(
                 prepareCnStructForYangData(
-                        TypeDefinitionAwareCodec.BINARY_DEFAULT_CODEC
-                        .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"),
+                        TypeDefinitionAwareCodec.from(BinaryType.getInstance())
+                                .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"),
                         elName), "<" + elName + ">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567</"
-                                + elName + ">");
+                        + elName + ">");
     }
 
     @Test
     public void snAsYangBitsToXmlTest() {
+        BitsTypeDefinition.Bit mockBit1 = mock( BitsTypeDefinition.Bit.class );
+        when( mockBit1.getName() ).thenReturn( "one" );
+        BitsTypeDefinition.Bit mockBit2 = mock( BitsTypeDefinition.Bit.class );
+        when( mockBit2.getName() ).thenReturn( "two" );
+        List<BitsTypeDefinition.Bit> bitList = Lists.newArrayList( mockBit1, mockBit2 );
+
         String elName = "lfBits";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.BITS_DEFAULT_CODEC.deserialize("one two"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(
+                                BitsType.create( mock( SchemaPath.class ), bitList ) )
+                                                               .deserialize("one two"), elName),
                 "<" + elName + ">one two</" + elName + ">", "<" + elName + ">two one</" + elName + ">");
     }
 
     @Test
     public void snAsYangEnumerationToXmlTest() {
+        EnumTypeDefinition.EnumPair mockEnum = mock( EnumTypeDefinition.EnumPair.class );
+        when( mockEnum.getName() ).thenReturn( "enum2" );
+        List<EnumPair> enumList = Lists.newArrayList( mockEnum );
+
         String elName = "lfEnumeration";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.ENUMERATION_DEFAULT_CODEC.deserialize("enum2"),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(
+                    EnumerationType.create( mock( SchemaPath.class ), enumList,
+                                            Optional.<EnumTypeDefinition.EnumPair>absent() ) )
+                                                                                    .deserialize("enum2"),
                         elName), "<" + elName + ">enum2</" + elName + ">");
     }
 
@@ -164,7 +206,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangEmptyToXmlTest() {
         String elName = "lfEmpty";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.EMPTY_DEFAULT_CODEC.deserialize(null), elName), "<"
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(EmptyType.getInstance()).deserialize(null), elName), "<"
                         + elName + "/>");
     }
 
@@ -172,33 +214,46 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
     public void snAsYangBooleanToXmlTest() {
         String elName = "lfBoolean";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("str"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(BooleanType.getInstance()).deserialize("str"), elName),
                 "<" + elName + ">false</" + elName + ">");
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("true"), elName),
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(BooleanType.getInstance()).deserialize("true"), elName),
                 "<" + elName + ">true</" + elName + ">");
     }
 
     @Test
     public void snAsYangUnionToXmlTest() {
+
+        BitsTypeDefinition.Bit mockBit1 = mock( BitsTypeDefinition.Bit.class );
+        when( mockBit1.getName() ).thenReturn( "first" );
+        BitsTypeDefinition.Bit mockBit2 = mock( BitsTypeDefinition.Bit.class );
+        when( mockBit1.getName() ).thenReturn( "second" );
+        List<BitsTypeDefinition.Bit> bitList = Lists.newArrayList( mockBit1, mockBit2 );
+
+        List<TypeDefinition<?>> types = Lists.<TypeDefinition<?>>newArrayList(
+                                                            Int8.getInstance(),
+                                                            BitsType.create( mock( SchemaPath.class ) , bitList ),
+                                                            BooleanType.getInstance() );
+        UnionType unionType = UnionType.create( types );
+
         String elName = "lfUnion";
         String int8 = "15";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(int8), elName), "<"
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(int8), elName), "<"
                         + elName + ">15</" + elName + ">");
 
         String bits = "first second";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bits), elName), "<"
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(bits), elName), "<"
                         + elName + ">first second</" + elName + ">");
 
         String bool = "str";
         serializeToXml(
-                prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bool), elName), "<"
+                prepareCnStructForYangData(TypeDefinitionAwareCodec.from(unionType).deserialize(bool), elName), "<"
                         + elName + ">str</" + elName + ">");
     }
 
-    private void serializeToXml(final CompositeNode compositeNode, final String... xmlRepresentation)
+    private void serializeToXml(CompositeNode compositeNode, String... xmlRepresentation)
             throws TransformerFactoryConfigurationError {
         String xmlString = "";
         try {
@@ -220,7 +275,7 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader {
 
     }
 
-    private CompositeNode prepareIdentityrefData(final String prefix, final boolean valueAsQName) {
+    private CompositeNode prepareIdentityrefData(String prefix, boolean valueAsQName) {
         MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(
                 TestUtils.buildQName("cont", "basic:module", "2013-12-2"), null, null, ModifyAction.CREATE, null);
         MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(
index 51687e2a1285601e4efff6c3737001a9f12488f3..307abebdd7b1a37ebfb3cc1d716048614c3e44cc 100644 (file)
@@ -56,6 +56,6 @@ public class CodecsExceptionsCatchingTest extends JerseyTest {
         Response response = target("/config/number:cont").request(MediaType.APPLICATION_XML).put(
                 Entity.entity("<cont xmlns=\"number\"><lf>3f</lf></cont>", MediaType.APPLICATION_XML));
         String exceptionMessage = response.readEntity(String.class);
-        assertTrue(exceptionMessage.contains("Incorrect lexical representation of Integer value: 3f"));
+        assertTrue(exceptionMessage.contains("invalid-value"));
     }
 }
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/bad-data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/bad-data.xml
new file mode 100644 (file)
index 0000000..110c323
--- /dev/null
@@ -0,0 +1,3 @@
+<cont>
+    <lfnint8Min>invalid</lfnint8Min>
+</cont>
\ No newline at end of file
index 56872a337d0124be4789789ef8f58427ab378278..f73ce1b65c38a154a8ae2067318c1b261e012c36 100644 (file)
        <lfunion3>55</lfunion3>
        <lfunion4>true</lfunion4>
        <lfunion5>true</lfunion5>
-       <lfunion6>false</lfunion6>
+       <lfunion6>10</lfunion6>
        <lfunion7></lfunion7>
        <lfunion8></lfunion8>
        <lfunion9></lfunion9>
        <lfunion10>bt1</lfunion10>
        <lfunion11>33</lfunion11>
        <lfunion12>false</lfunion12>
-       <lfunion13>44</lfunion13>
-       <lfunion14>21</lfunion14>
-       <lfempty />
+       <lfunion13>b1</lfunion13>
+       <lfunion14>zero</lfunion14>
        <identityref1 xmlns:x="simple:data:types">x:iden</identityref1>
 </cont>
\ No newline at end of file