+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
-import static org.junit.Assert.*;
+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.fail;
+import com.google.common.collect.Maps;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonToken;
import java.io.IOException;
import java.io.StringReader;
-import java.math.BigDecimal;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.*;
-
-import javax.ws.rs.WebApplicationException;
-import javax.xml.bind.DatatypeConverter;
-
+import java.util.Map;
import org.junit.BeforeClass;
+import org.junit.Ignore;
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.common.QName;
-import org.opendaylight.yangtools.yang.data.api.*;
-import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
-
-import com.google.gson.stream.JsonReader;
-import com.google.gson.stream.JsonToken;
+import org.opendaylight.yangtools.yang.data.api.Node;
public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader {
- @BeforeClass
- public static void initialize() {
- dataLoad("/cnsn-to-json/simple-data-types");
+ static abstract class LeafVerifier {
+
+ Object expectedValue;
+ JsonToken expectedToken;
+
+ LeafVerifier(final Object expectedValue, final JsonToken expectedToken) {
+ this.expectedValue = expectedValue;
+ this.expectedToken = expectedToken;
+ }
+
+ abstract Object getActualValue(JsonReader reader) throws IOException;
+
+ void verify(final JsonReader reader, final String keyName) throws IOException {
+ assertEquals("Json value for key " + keyName, expectedValue, getActualValue(reader));
+ }
+
+ JsonToken expectedTokenType() {
+ return expectedToken;
+ }
}
- @Test
- public void simpleYangDataTest() {
+ static class BooleanVerifier extends LeafVerifier {
- CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-data-types/xml/data.xml");
+ public BooleanVerifier(final boolean expected) {
+ super(expected, JsonToken.BOOLEAN);
+ }
- String jsonOutput = null;
+ @Override
+ Object getActualValue(final JsonReader reader) throws IOException {
+ return reader.nextBoolean();
+ }
+ }
- TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont");
+ static class NumberVerifier extends LeafVerifier {
- try {
- jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
- StructuredDataToJsonProvider.INSTANCE);
- } catch (WebApplicationException | IOException e) {
+ public NumberVerifier(final Number expected) {
+ super(expected, JsonToken.NUMBER);
}
- assertNotNull(jsonOutput);
- verifyJsonOutput(jsonOutput);
+ @Override
+ Object getActualValue(final 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;
+ }
}
- private CompositeNode prepareData() {
- MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null,
- ModifyAction.CREATE, null);
-
- List<Node<?>> childNodes = new ArrayList<>();
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Min"), cont, (byte) -128,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Max"), cont, (byte) 127,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Min"), cont, (short) -32768,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Max"), cont, (short) 32767,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Min"), cont,
- (int) -2147483648, ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Max"), cont, (int) 2147483647,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Min"), cont, new Long(
- "-9223372036854775807"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Max"), cont, new Long(
- "9223372036854775807"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint8Max"), cont, (short) 255,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint16Max"), cont, (int) 65535,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint32Max"), cont, new Long(
- "4294967295"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr"), cont, "lfstr",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr1"), cont, "",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool1"), cont, Boolean.TRUE,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool2"), cont, Boolean.FALSE,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal1"), cont, new BigDecimal(
- "43.32"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal2"), cont, new BigDecimal(
- "-0.43"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal3"), cont, new BigDecimal(
- "43"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal4"), cont, new BigDecimal(
- "43E3"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal6"), cont, new BigDecimal(
- "33.12345"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfenum"), cont, "enum3",
- ModifyAction.CREATE, null));
-
- HashSet<String> bits = new HashSet<String>();
- bits.add("bit3");
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbits"), cont, bits,
- ModifyAction.CREATE, null));
-
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbinary"), cont, DatatypeConverter
- .parseBase64Binary("AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"),
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfempty"), cont, null,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion1"), cont, (int) 324,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion2"), cont, new BigDecimal(
- "33.3"), ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion3"), cont, "55",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion4"), cont, Boolean.TRUE,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion5"), cont, "true",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion6"), cont, "false",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion7"), cont, null,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion8"), cont, "",
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion9"), cont, "",
- ModifyAction.CREATE, null));
-
- HashSet<String> bits2 = new HashSet<String>();
- bits2.add("bt1");
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion10"), cont, bits2,
- ModifyAction.CREATE, null));
-
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion11"), cont, (short) 33,
- ModifyAction.CREATE, null));
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion12"), cont, Boolean.FALSE,
- ModifyAction.CREATE, null));
- try {
- childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("identityref1"), cont, new QName(
- new URI("simple:data:types"), "iden"), ModifyAction.CREATE, null));
- } catch (URISyntaxException e) {
+ static class StringVerifier extends LeafVerifier {
+
+ StringVerifier(final String expected) {
+ super(expected, JsonToken.STRING);
+ }
+
+ @Override
+ Object getActualValue(final JsonReader reader) throws IOException {
+ return reader.nextString();
+ }
+ }
+
+ static class EmptyVerifier extends LeafVerifier {
+
+ EmptyVerifier() {
+ super(null, null);
+ }
+
+ @Override
+ Object getActualValue(final JsonReader reader) throws IOException {
+ reader.beginArray();
+ reader.nextNull();
+ reader.endArray();
+ return null;
+ }
+
+ }
+
+ static class ComplexAnyXmlVerifier extends LeafVerifier {
+
+ ComplexAnyXmlVerifier() {
+ super(null, JsonToken.BEGIN_OBJECT);
+ }
+
+ @Override
+ void verify(final JsonReader reader, final String keyName) throws IOException {
+
+ reader.beginObject();
+ final String innerKey = reader.nextName();
+ assertEquals("Json reader child key for " + keyName, "data", innerKey);
+ assertEquals("Json token type for key " + innerKey, JsonToken.BEGIN_OBJECT, reader.peek());
+
+ reader.beginObject();
+ verifyLeaf(reader, innerKey, "leaf1", "leaf1-value");
+ verifyLeaf(reader, innerKey, "leaf2", "leaf2-value");
+
+ String nextName = reader.nextName();
+ assertEquals("Json reader child key for " + innerKey, "leaf-list", nextName);
+ reader.beginArray();
+ assertEquals("Json value for key " + nextName, "leaf-list-value1", reader.nextString());
+ assertEquals("Json value for key " + nextName, "leaf-list-value2", reader.nextString());
+ reader.endArray();
+
+ nextName = reader.nextName();
+ assertEquals("Json reader child key for " + innerKey, "list", nextName);
+ reader.beginArray();
+ verifyNestedLists(reader, 1);
+ verifyNestedLists(reader, 3);
+ reader.endArray();
+
+ reader.endObject();
+ reader.endObject();
+ }
+
+ void verifyNestedLists(final JsonReader reader, int leafNum) throws IOException {
+ reader.beginObject();
+
+ final String nextName = reader.nextName();
+ assertEquals("Json reader next name", "nested-list", nextName);
+
+ reader.beginArray();
+
+ reader.beginObject();
+ verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum++);
+ reader.endObject();
+
+ reader.beginObject();
+ verifyLeaf(reader, "nested-list", "nested-leaf", "nested-value" + leafNum);
+ reader.endObject();
+
+ reader.endArray();
+ reader.endObject();
}
- cont.getChildren().addAll(childNodes);
+ void verifyLeaf(final JsonReader reader, final String parent, final String name, final String value) throws IOException {
+ final String nextName = reader.nextName();
+ assertEquals("Json reader child key for " + parent, name, nextName);
+ assertEquals("Json token type for key " + parent, JsonToken.STRING, reader.peek());
+ assertEquals("Json value for key " + nextName, value, reader.nextString());
+ }
- cont.init();
+ @Override
+ Object getActualValue(final JsonReader reader) throws IOException {
+ return null;
+ }
+ }
- return cont;
+ @BeforeClass
+ public static void initialize() {
+ dataLoad("/cnsn-to-json/simple-data-types");
}
- private void verifyJsonOutput(String jsonOutput) {
- StringReader strReader = new StringReader(jsonOutput);
- JsonReader jReader = new JsonReader(strReader);
+ @Test
+ @Ignore
+ public void simpleYangDataTest() throws Exception {
+
+ final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml",
+ XmlToCompositeNodeProvider.INSTANCE);
+
+ TestUtils.normalizeCompositeNode(node, modules, "simple-data-types:cont");
+
+ final String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(node, modules, dataSchemaNode,
+ StructuredDataToJsonProvider.INSTANCE);
+
+ assertNotNull(jsonOutput);
+
+ verifyJsonOutput(jsonOutput);
+ }
+
+ private void verifyJsonOutput(final String jsonOutput) {
+ final StringReader strReader = new StringReader(jsonOutput);
+ final JsonReader jReader = new JsonReader(strReader);
String exception = null;
try {
jsonReadCont(jReader);
- } catch (IOException e) {
+ } catch (final IOException e) {
exception = e.getMessage();
}
assertNull("Error during reading Json output: " + exception, exception);
}
- private void jsonReadCont(JsonReader jReader) throws IOException {
+ private void jsonReadCont(final JsonReader jReader) throws IOException {
jReader.beginObject();
assertNotNull("cont1 is missing.", jReader.hasNext());
// return dataFromJson;
}
- private void jsonReadContElements(JsonReader jReader) throws IOException {
+ private void jsonReadContElements(final JsonReader jReader) throws IOException {
jReader.beginObject();
- List<String> loadedLfs = new ArrayList<>();
- boolean exceptForDecimal5Raised = false;
- 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 lfref1Checked = false;
- boolean lfemptyChecked = false;
- boolean lfstr1Checked = false;
- boolean lfidentityrefChecked = false;
+
+ final 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"));
+ expectedMap.put("complex-any", new ComplexAnyXmlVerifier());
+ expectedMap.put("simple-any", new StringVerifier("simple"));
+ expectedMap.put("empty-any", new StringVerifier(""));
while (jReader.hasNext()) {
- String keyName = jReader.nextName();
- JsonToken peek = null;
- try {
- peek = jReader.peek();
- } catch (IOException e) {
- if (keyName.equals("lfdecimal5")) {
- exceptForDecimal5Raised = true;
- } else {
- assertTrue("Key " + keyName + " has incorrect value for specifed type", false);
- }
- }
+ final String keyName = jReader.nextName();
+ final JsonToken peek = jReader.peek();
+
+ final LeafVerifier verifier = expectedMap.remove(keyName);
+ assertNotNull("Found unexpected leaf: " + keyName, verifier);
- 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);
+ final JsonToken expToken = verifier.expectedTokenType();
+ if (expToken != null) {
+ assertEquals("Json token type for key " + keyName, expToken, peek);
}
+ verifier.verify(jReader, keyName);
}
- 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);
+
+ if (!expectedMap.isEmpty()) {
+ fail("Missing leaf nodes in Json output: " + expectedMap.keySet());
+ }
+
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 {
+ final Node<?> node = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/bad-data.xml",
+ XmlToCompositeNodeProvider.INSTANCE);
+
+ TestUtils.normalizeCompositeNode(node, modules, "simple-data-types:cont");
+ fail("Expected RestconfDocumentedException");
+ } catch (final RestconfDocumentedException e) {
+ assertEquals("getErrorTag", ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
}
}
}