From 6eb461f54fe6705b07f34b6cee197ea968de4991 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Tue, 17 Dec 2013 14:36:44 +0100 Subject: [PATCH] More test for improving of code coverage + test refactoring Added tests to sal-rest-connector to improve code coverage. Added test for leafref. Some methods in TestUtils were refactored. Change-Id: I245e9e7a55e02c542aab7f835f09081b040afa41 Signed-off-by: Jozef Gloncak --- .../controller/sal/rest/impl/JsonMapper.java | 37 +- .../sal/restconf/impl/RestCodec.java | 44 +- ...t.java => CnSnJsonBasicYangTypesTest.java} | 41 +- ...eTest.java => CnSnJsonChoiceCaseTest.java} | 8 +- ...java => CnSnToJsonBasicDataTypesTest.java} | 29 +- ...st.java => CnSnToJsonIdentityrefTest.java} | 34 +- .../test/CnSnToJsonIncorrectTopLevelTest.java | 160 +++++++ .../to/json/test/CnSnToJsonLeafrefType.java | 102 +++++ .../CnSnToJsonNotExistingLeafTypeTest.java | 72 ++++ ...st.java => CnSnToJsonWithAugmentTest.java} | 27 +- .../cnsn/to/json/test/ToJsonLeafrefType.java | 161 ------- .../CnSnToXmlNotExistingLeafTypeTest.java | 69 +++ .../impl/cnsn/to/xml/test/CnSnToXmlTest.java | 101 +++-- .../to/xml/test/CnSnToXmlWithChoiceTest.java | 64 +++ .../cnsn/test/JsonIdentityrefToCnSnTest.java | 74 ++++ .../to/cnsn/test/JsonLeafrefToCnSnTest.java | 48 +++ .../json/to/cnsn/test/JsonToCnSnTest.java | 126 +++--- .../sal/restconf/impl/test/DummyType.java | 64 +++ .../impl/test/InvokeRpcMethodTest.java | 2 +- .../impl/test/ReadConfAndOperDataTest.java | 26 +- .../impl/test/RestCodecExceptionsTest.java | 33 ++ .../restconf/impl/test/RestconfImplTest.java | 4 +- .../sal/restconf/impl/test/TestUtils.java | 309 ++++---------- .../test/YangAndXmlAndDataSchemaLoader.java | 14 +- .../to/cnsn/test/XmlLeafrefToCnSnTest.java | 392 ++++++++++++++++++ .../impl/xml/to/cnsn/test/XmlToCnSnTest.java | 388 +---------------- .../invalid-top-level-element.yang | 13 + .../cnsn-to-json/list/list-types-module | 274 ++++++++++++ .../choice/module-with-choice.yang | 25 ++ .../cnsn-to-xml/yang/basic-module.yang | 6 + .../json-to-cnsn/leafref/json/data.json | 6 + .../json-to-cnsn/leafref/leafref-module | 19 + .../{ => 1}/simple-list1.yang | 0 .../{ => 2}/simple-list2.yang | 0 .../json-to-cnsn/unsupported-json-format.json | 1 + .../xml-to-cnsn/leafref/leafref-module | 19 + .../xml-to-cnsn/leafref/xml/data.xml | 4 + 37 files changed, 1840 insertions(+), 956 deletions(-) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/{ToJsonBasicYangTypesTest.java => CnSnJsonBasicYangTypesTest.java} (93%) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/{ToJsonChoiceCaseTest.java => CnSnJsonChoiceCaseTest.java} (90%) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/{ToJsonBasicDataTypesTest.java => CnSnToJsonBasicDataTypesTest.java} (96%) rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/{ToJsonIdentityrefTest.java => CnSnToJsonIdentityrefTest.java} (56%) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonNotExistingLeafTypeTest.java rename opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/{ToJsonWithAugmentTest.java => CnSnToJsonWithAugmentTest.java} (53%) delete mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonLeafrefType.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonIdentityrefToCnSnTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyType.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/invalid-top-level-element/invalid-top-level-element.yang create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/list/list-types-module create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/choice/module-with-choice.yang create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module rename opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/{ => 1}/simple-list1.yang (100%) rename opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/{ => 2}/simple-list2.yang (100%) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/unsupported-json-format.json create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/leafref-module create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/xml/data.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java index fb7872f8bc..121f697827 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java @@ -4,35 +4,16 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; import java.net.URI; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import javax.activation.UnsupportedDataTypeException; -import org.opendaylight.controller.sal.restconf.impl.ControllerContext; -import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO; +import org.opendaylight.controller.sal.restconf.impl.*; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; -import org.opendaylight.controller.sal.restconf.impl.RestCodec; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.Node; -import org.opendaylight.yangtools.yang.data.api.SimpleNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode; -import org.opendaylight.yangtools.yang.model.api.ChoiceNode; -import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode; -import org.opendaylight.yangtools.yang.model.api.ListSchemaNode; -import org.opendaylight.yangtools.yang.model.api.TypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.model.api.*; +import org.opendaylight.yangtools.yang.model.api.type.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,7 +24,7 @@ class JsonMapper { private final Set foundLeafLists = new HashSet<>(); private final Set foundLists = new HashSet<>(); - private final Logger logger = LoggerFactory.getLogger(JsonMapper.class); + private final Logger logger = LoggerFactory.getLogger(JsonMapper.class); public void write(JsonWriter writer, CompositeNode data, DataNodeContainer schema) throws IOException { Preconditions.checkNotNull(writer); @@ -189,11 +170,13 @@ class JsonMapper { if (node.getValue() instanceof QName) { IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue()); IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0); - String moduleName = ControllerContext.getInstance().findModuleByNamespace(URI.create(valueFromDTO.getNamespace())); + String moduleName = ControllerContext.getInstance().findModuleByNamespace( + URI.create(valueFromDTO.getNamespace())); writer.value(moduleName + ":" + valueFromDTO.getValue()); } else { logger.debug("Value of " + baseType.getQName().getNamespace() + ":" - + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is " + node.getValue().getClass()); + + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is " + + node.getValue().getClass()); writer.value(String.valueOf(node.getValue())); } } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java index 450ba02b56..40fba88356 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestCodec.java @@ -19,16 +19,16 @@ public class RestCodec { private RestCodec() { } - + public static final Codec from(TypeDefinition typeDefinition) { return new ObjectCodec(typeDefinition); } - + @SuppressWarnings("rawtypes") public static final class ObjectCodec implements Codec { private final Logger logger = LoggerFactory.getLogger(RestCodec.class); - + public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl(); public static final Codec LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl(); @@ -37,7 +37,7 @@ public class RestCodec { private ObjectCodec(TypeDefinition typeDefinition) { type = RestUtil.resolveBaseTypeFrom(typeDefinition); } - + @SuppressWarnings("unchecked") @Override public Object deserialize(Object input) { @@ -47,16 +47,21 @@ public class RestCodec { } else if (type instanceof LeafrefTypeDefinition) { return LEAFREF_DEFAULT_CODEC.deserialize(input); } else { - TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec.from(type); + TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec + .from(type); if (typeAwarecodec != null) { return typeAwarecodec.deserialize(String.valueOf(input)); } else { - logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet."); + logger.debug("Codec for type \"" + type.getQName().getLocalName() + + "\" is not implemented yet."); return null; } } - } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs - logger.error("ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e); + } catch (ClassCastException e) { // TODO remove this catch when + // everyone use codecs + logger.error( + "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), + e); return input; } } @@ -70,22 +75,27 @@ public class RestCodec { } else if (type instanceof LeafrefTypeDefinition) { return LEAFREF_DEFAULT_CODEC.serialize(input); } else { - TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec.from(type); + TypeDefinitionAwareCodec> typeAwarecodec = TypeDefinitionAwareCodec + .from(type); if (typeAwarecodec != null) { return typeAwarecodec.serialize(input); } else { - logger.debug("Codec for type \"" + type.getQName().getLocalName() + "\" is not implemented yet."); + logger.debug("Codec for type \"" + type.getQName().getLocalName() + + "\" is not implemented yet."); return null; } } - } catch (ClassCastException e) { // TODO remove this catch when everyone use codecs - logger.error("ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), e); + } catch (ClassCastException e) { // TODO remove this catch when + // everyone use codecs + logger.error( + "ClassCastException was thrown when codec is invoked with parameter " + String.valueOf(input), + e); return input; } } - + } - + public static class IdentityrefCodecImpl implements IdentityrefCodec { @Override @@ -105,7 +115,7 @@ public class RestCodec { } } - + public static class LeafrefCodecImpl implements LeafrefCodec { @Override @@ -117,7 +127,7 @@ public class RestCodec { public Object deserialize(String data) { return data; } - + } - + } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java similarity index 93% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java index b01d4104b2..3e3adb3efa 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java @@ -7,8 +7,13 @@ import java.io.StringReader; import java.util.Map; import java.util.Set; +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; +import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader; import org.opendaylight.controller.sal.restconf.impl.test.structures.*; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; @@ -16,7 +21,12 @@ import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; -public class ToJsonBasicYangTypesTest { +public class CnSnJsonBasicYangTypesTest extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialize() { + dataLoad("/cnsn-to-json/simple-yang-types", 1, "simple-yang-types", "cont1"); + } /** * Test of json output when as input are specified composite node with empty @@ -24,9 +34,16 @@ public class ToJsonBasicYangTypesTest { */ @Test public void compositeNodeAndYangWithJsonReaderEmptyDataTest() { - String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(prepareCompositeNodeWithEmpties(), - "/cnsn-to-json/simple-yang-types", "/cnsn-to-json/simple-yang-types/xml", "simple-yang-types", "cont1"); - verifyJsonOutputForEmpty(jsonOutput); + CompositeNode compositeNode = prepareCompositeNodeWithEmpties(); + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + String jsonOutput = null; + try { + jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + + verifyJsonOutputForEmptyData(jsonOutput); } /** @@ -35,13 +52,20 @@ public class ToJsonBasicYangTypesTest { */ @Test public void xmlAndYangTypesWithJsonReaderTest() { - String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/simple-yang-types/xml/data.xml"), - "/cnsn-to-json/simple-yang-types", "/cnsn-to-json/simple-yang-types/xml", "simple-yang-types", "cont1"); + CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-yang-types/xml/data.xml"); + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + String jsonOutput = null; + try { + jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + verifyJsonOutput(jsonOutput); } - private void verifyJsonOutputForEmpty(String jsonOutput) { + private void verifyJsonOutputForEmptyData(String jsonOutput) { + assertNotNull(jsonOutput); StringReader strReader = new StringReader(jsonOutput); JsonReader jReader = new JsonReader(strReader); @@ -60,6 +84,7 @@ public class ToJsonBasicYangTypesTest { } private void verifyJsonOutput(String jsonOutput) { + assertNotNull(jsonOutput); StringReader strReader = new StringReader(jsonOutput); JsonReader jReader = new JsonReader(strReader); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonChoiceCaseTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java similarity index 90% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonChoiceCaseTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java index b745411bf4..15122cfe3a 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonChoiceCaseTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java @@ -8,11 +8,12 @@ import javax.ws.rs.WebApplicationException; import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; 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; -public class ToJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { +public class CnSnJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { @@ -112,9 +113,10 @@ public class ToJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { private void testWrapper(String xmlPath, String pathToSchemaNode) { CompositeNode compNode = TestUtils.loadCompositeNode(xmlPath); - TestUtils.normalizeCompositeNode(compNode, modules, dataSchemaNode, pathToSchemaNode); + TestUtils.normalizeCompositeNode(compNode, modules, pathToSchemaNode); try { - TestUtils.writeCompNodeWithSchemaContextToJson(compNode, modules, dataSchemaNode); + TestUtils.writeCompNodeWithSchemaContextToOutput(compNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); } catch (WebApplicationException | IOException e) { // shouldn't end here assertTrue(false); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicDataTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java similarity index 96% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicDataTypesTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java index 73d0c82521..f71e2eeaa6 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicDataTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java @@ -12,40 +12,41 @@ import java.util.*; import javax.ws.rs.WebApplicationException; import javax.xml.bind.DatatypeConverter; +import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; 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 org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; -public class ToJsonBasicDataTypesTest { +public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialize() { + dataLoad("/cnsn-to-json/simple-data-types"); + } @Test public void simpleYangDataTest() { - String jsonOutput = ""; + CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-data-types/xml/data.xml"); - Set modules = TestUtils.resolveModules("/cnsn-to-json/simple-data-types"); - assertEquals(1, modules.size()); - Module module = TestUtils.resolveModule(null, modules); - assertNotNull(module); - DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); - assertNotNull(dataSchemaNode); + String jsonOutput = null; - TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "simple-data-types:cont"); + TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont"); try { - jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(compositeNode, modules, dataSchemaNode); + jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); } catch (WebApplicationException | IOException e) { - assertTrue(false); // shouldn't get here } + assertNotNull(jsonOutput); - System.out.println(jsonOutput); verifyJsonOutput(jsonOutput); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIdentityrefTest.java similarity index 56% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIdentityrefTest.java index 6d30559ccd..6920b0f1ff 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIdentityrefTest.java @@ -11,12 +11,14 @@ import javax.ws.rs.WebApplicationException; import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; 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; -public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { +public class CnSnToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { @@ -27,7 +29,9 @@ public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { public void identityrefToJsonTest() { String json = null; try { - json = TestUtils.writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), modules, dataSchemaNode); + QName valueAsQname = TestUtils.buildQName("name_test", "identityref:module", "2013-12-2"); + json = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(valueAsQname), modules, + dataSchemaNode, StructuredDataToJsonProvider.INSTANCE); } catch (WebApplicationException | IOException e) { // shouldn't end here assertTrue(false); @@ -40,15 +44,35 @@ public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { assertTrue(mtch.matches()); } - private CompositeNode prepareCompositeNode() { + @Test + public void identityrefToJsonWithoutQNameTest() { + String json = null; + try { + String value = "not q name value"; + json = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(value), modules, + dataSchemaNode, StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + // shouldn't end here + assertTrue(false); + } + System.out.println(json); + assertNotNull(json); + Pattern ptrn = Pattern.compile(".*\"lf1\"\\p{Space}*:\\p{Space}*\"not q name value\".*", Pattern.DOTALL); + Matcher mtch = ptrn.matcher(json); + + assertTrue(mtch.matches()); + } + + private CompositeNode prepareCompositeNode(Object value) { MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, ModifyAction.CREATE, null); MutableCompositeNode cont1 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont1"), cont, null, ModifyAction.CREATE, null); cont.getChildren().add(cont1); - MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont1, - TestUtils.buildQName("name_test", "identityref:module", "2013-12-2"), ModifyAction.CREATE, null); + MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1"), cont1, value, + ModifyAction.CREATE, null); + cont1.getChildren().add(lf1); cont1.init(); cont.init(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java new file mode 100644 index 0000000000..47653ae296 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java @@ -0,0 +1,160 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.List; +import java.util.Set; + +import javax.activation.UnsupportedDataTypeException; +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; +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.CompositeNode; +import org.opendaylight.yangtools.yang.model.api.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CnSnToJsonIncorrectTopLevelTest extends YangAndXmlAndDataSchemaLoader { + + private static final Logger LOG = LoggerFactory.getLogger(CnSnToJsonIncorrectTopLevelTest.class); + + @BeforeClass + public static void initialize() { + dataLoad("/cnsn-to-json/simple-data-types"); + } + + private class IncorrectDataSchema implements DataSchemaNode, DataNodeContainer { + + @Override + public String getDescription() { + // TODO Auto-generated method stub + return null; + } + + @Override + public SchemaPath getPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public QName getQName() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getReference() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Status getStatus() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getUnknownSchemaNodes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getChildNodes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public DataSchemaNode getDataChildByName(QName arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public DataSchemaNode getDataChildByName(String arg0) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getGroupings() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set> getTypeDefinitions() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Set getUses() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ConstraintDefinition getConstraints() { + // TODO Auto-generated method stub + return null; + } + + @Override + public YangNode getParent() { + // TODO Auto-generated method stub + return null; + } + + @Override + public boolean isAddedByUses() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isAugmenting() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isConfiguration() { + // TODO Auto-generated method stub + return false; + } + + } + + @Test + public void incorrectTopLevelElementTest() { + + CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-data-types/xml/data.xml"); + DataSchemaNode incorrectDataSchema = null; + incorrectDataSchema = new IncorrectDataSchema(); + + TestUtils.normalizeCompositeNode(compositeNode, modules, "simple-data-types:cont"); + + boolean exceptionRaised = false; + try { + TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, incorrectDataSchema, + StructuredDataToJsonProvider.INSTANCE); + } catch (UnsupportedDataTypeException e) { + exceptionRaised = true; + } catch (WebApplicationException | IOException e) { + LOG.error("WebApplicationException or IOException was raised"); + } + + assertTrue(exceptionRaised); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java new file mode 100644 index 0000000000..6a92ebdf1b --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java @@ -0,0 +1,102 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; +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; + +/** + * + * All tests are commented now because leafref isn't supported now + * + */ + +public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialization() { + dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont"); + } + + @Test + public void leafrefAbsolutePathToExistingLeafTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml"); + validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json); + } + + @Test + public void leafrefRelativePathToExistingLeafTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml"); + validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json); + } + + /** + * Tests case when reference to not existing element is present. In this + * case value from single node is printed as string. + */ + @Test + public void leafrefToNonExistingLeafTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml"); + validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json); + } + + /** + * Tests case when non leaf element is referenced. In this case value from + * single node is printed as string. + */ + @Test + public void leafrefToNotLeafTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml"); + validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json); + } + + /** + * Tests case when leaflist element is refers to leaf. + */ + @Test + public void leafrefFromLeafListToLeafTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"); + validateJson( + ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*", + json); + } + + /** + * Tests case when leaflist element is refers to leaf. + */ + @Test + public void leafrefFromLeafrefToLeafrefTest() { + String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml"); + validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json); + } + + private void validateJson(String regex, String value) { + assertNotNull(value); + Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL); + Matcher mtch = ptrn.matcher(value); + assertTrue(mtch.matches()); + } + + private String toJson(String xmlDataPath) { + try { + CompositeNode compositeNode = TestUtils.loadCompositeNode(xmlDataPath); + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + return ""; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonNotExistingLeafTypeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonNotExistingLeafTypeTest.java new file mode 100644 index 0000000000..e5a317e2d5 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonNotExistingLeafTypeTest.java @@ -0,0 +1,72 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Collections; +import java.util.Set; + +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; +import org.opendaylight.controller.sal.restconf.impl.test.*; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.data.impl.NodeFactory; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CnSnToJsonNotExistingLeafTypeTest extends YangAndXmlAndDataSchemaLoader { + + private static final Logger LOG = LoggerFactory.getLogger(CnSnToJsonNotExistingLeafTypeTest.class); + + @BeforeClass + public static void initialize() { + dataLoad("/cnsn-to-json/simple-data-types"); + } + + @Test + public void incorrectTopLevelElementTest() { + + String jsonOutput = null; + try { + jsonOutput = TestUtils + .writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(), + (Set) Collections.EMPTY_SET, prepareDataSchemaNode(), + StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + LOG.error("WebApplicationException or IOException was raised"); + } + assertNotNull(jsonOutput); + assertTrue(jsonOutput.contains("\"lf1\": \"\"")); + } + + private CompositeNode prepareCompositeNode() { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode( + TestUtils.buildQName("cont", "simple:uri", "2012-12-17"), null, null, ModifyAction.CREATE, null); + MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode( + TestUtils.buildQName("lf1", "simple:uri", "2012-12-17"), cont, "any value", ModifyAction.CREATE, null); + cont.getChildren().add(lf1); + cont.init(); + return cont; + } + + private DataSchemaNode prepareDataSchemaNode() { + ContainerSchemaNodeBuilder contBuild = new ContainerSchemaNodeBuilder("module", 1, TestUtils.buildQName("cont", + "simple:uri", "2012-12-17"), null); + LeafSchemaNodeBuilder leafBuild = new LeafSchemaNodeBuilder("module", 2, TestUtils.buildQName("lf1", + "simple:uri", "2012-12-17"), null); + leafBuild.setType(new DummyType()); + leafBuild.setConfiguration(true); + + contBuild.addChildNode(leafBuild); + return contBuild.build(null); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java similarity index 53% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java index 73bd178ff3..878ed39ecf 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java @@ -1,12 +1,25 @@ package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import java.io.IOException; + +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider; 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; -public class ToJsonWithAugmentTest { +public class CnSnToJsonWithAugmentTest extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialize() { + dataLoad("/cnsn-to-json/augmentation", 5, "yang", "cont"); + } /** * Test of json output when as input are specified composite node with empty @@ -14,10 +27,16 @@ public class ToJsonWithAugmentTest { */ @Test public void augmentedElementsToJson() { - CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/augmentation/xml/data.xml"); - String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(compositeNode, - "/cnsn-to-json/augmentation", "/cnsn-to-json/augmentation/xml", "yang", "cont"); + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + + String jsonOutput = null; + try { + jsonOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToJsonProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + assertNotNull(jsonOutput); assertTrue(jsonOutput.contains("\"augment-leaf:lf2\": \"lf2\"")); assertTrue(jsonOutput.contains("\"augment-container:cont1\": {")); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonLeafrefType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonLeafrefType.java deleted file mode 100644 index 1ac81a332f..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonLeafrefType.java +++ /dev/null @@ -1,161 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.util.regex.Matcher; - -import javax.ws.rs.WebApplicationException; - -import org.junit.*; -import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; -import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader; - -/** - * - * All tests are commented now because leafref isn't supported now - * - */ - -public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { - - @BeforeClass - public static void initialization() { - dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont"); - } - - @Ignore - @Test - public void leafrefAbsolutePathToExistingLeafTest() { - String json = null; - try { - json = TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml"), - modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf3\":\\p{Blank}*true.*", - java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - - @Ignore - @Test - public void leafrefRelativePathToExistingLeafTest() { - String json = null; - try { - json = TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml"), - modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf2\":\\p{Blank}*121.*", - java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - - /** - * Tests case when reference to not existing element is present. In this - * case value from single node is printed as string. - */ - @Ignore - @Test - public void leafrefToNonExistingLeafTest() { - String json = null; - try { - json = TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml"), - modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf5\":\\p{Blank}*\"137\".*", - java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - - /** - * Tests case when non leaf element is referenced. In this case value from - * single node is printed as string. - */ - @Ignore - @Test - public void leafrefToNotLeafTest() { - String json = null; - try { - json = TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml"), modules, - dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile( - ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", - java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - - /** - * Tests case when leaflist element is refers to leaf. - */ - @Ignore - @Test - public void leafrefFromLeafListToLeafTest() { - String json = null; - try { - json = TestUtils - .writeCompNodeWithSchemaContextToJson( - TestUtils - .loadCompositeNode("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"), - modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern - .compile( - ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*345,\\p{Space}*346,\\p{Space}*347.*", - java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - - /** - * Tests case when leaflist element is refers to leaf. - */ - @Ignore - @Test - public void leafrefFromLeafrefToLeafrefTest() { - String json = null; - try { - json = TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml"), modules, - dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - assertNotNull(json); - java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile( - ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*200.*", java.util.regex.Pattern.DOTALL); - Matcher mtch = ptrn.matcher(json); - assertTrue(mtch.matches()); - } - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java new file mode 100644 index 0000000000..a2fc138b51 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java @@ -0,0 +1,69 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.Collections; +import java.util.Set; + +import javax.ws.rs.WebApplicationException; + +import org.junit.Ignore; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; +import org.opendaylight.controller.sal.restconf.impl.test.DummyType; +import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.data.impl.NodeFactory; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.parser.builder.impl.ContainerSchemaNodeBuilder; +import org.opendaylight.yangtools.yang.parser.builder.impl.LeafSchemaNodeBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CnSnToXmlNotExistingLeafTypeTest { + + private static final Logger LOG = LoggerFactory.getLogger(CnSnToXmlNotExistingLeafTypeTest.class); + + @Ignore + @Test + public void incorrectTopLevelElementTest() { + + String xmlOutput = null; + try { + xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(), + (Set) Collections.EMPTY_SET, prepareDataSchemaNode(), StructuredDataToXmlProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + LOG.error("WebApplicationException or IOException was raised"); + } + assertNotNull(xmlOutput); + assertTrue(xmlOutput.contains("any value")); + + } + + private CompositeNode prepareCompositeNode() { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode( + TestUtils.buildQName("cont", "simple:uri", "2012-12-17"), null, null, ModifyAction.CREATE, null); + MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode( + TestUtils.buildQName("lf1", "simple:uri", "2012-12-17"), cont, "any value", ModifyAction.CREATE, null); + cont.getChildren().add(lf1); + cont.init(); + return cont; + } + + private DataSchemaNode prepareDataSchemaNode() { + ContainerSchemaNodeBuilder contBuild = new ContainerSchemaNodeBuilder("module", 1, TestUtils.buildQName("cont", + "simple:uri", "2012-12-17"), null); + LeafSchemaNodeBuilder leafBuild = new LeafSchemaNodeBuilder("module", 2, TestUtils.buildQName("lf1", + "simple:uri", "2012-12-17"), null); + leafBuild.setType(new DummyType()); + leafBuild.setConfiguration(true); + + contBuild.addChildNode(leafBuild); + return contBuild.build(null); + + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java index d04337865a..1079d843f0 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java @@ -1,24 +1,20 @@ package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import java.io.StringWriter; -import java.util.Set; +import java.io.IOException; -import javax.activation.UnsupportedDataTypeException; -import javax.xml.transform.*; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; +import javax.ws.rs.WebApplicationException; +import javax.xml.transform.TransformerFactoryConfigurationError; -import org.junit.BeforeClass; -import org.junit.Test; -import org.opendaylight.controller.sal.rest.impl.XmlMapper; +import org.junit.*; +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; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; -import org.opendaylight.yangtools.yang.model.api.*; -import org.w3c.dom.Document; /** * @@ -27,25 +23,33 @@ import org.w3c.dom.Document; * XML file * */ -public class CnSnToXmlTest { - - private static Set modules; - private static DataSchemaNode dataSchemaNode; - +public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/cnsn-to-xml/yang"); - assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("basic-module", modules); - assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont"); - assertNotNull(dataSchemaNode); - + dataLoad("/cnsn-to-xml/yang", 2, "basic-module", "cont"); } @Test public void snAsYangIdentityrefToXMLTest() { - serializeToXml(prepareIdentityrefData(), "x:iden"); + serializeToXml(prepareIdentityrefData(null, true), "x:iden"); + } + + @Test + public void snAsYangIdentityrefWithQNamePrefixToXMLTest() { + serializeToXml(prepareIdentityrefData("prefix", true), + "prefix:iden"); + } + + @Ignore + @Test + public void snAsYangIdentityrefWithPrefixToXMLTest() { + serializeToXml(prepareIdentityrefData("prefix", false), "no qname value"); + } + + @Ignore + @Test + public void snAsYangLeafrefWithPrefixToXMLTest() { + serializeToXml(prepareLeafrefData(), "true", "true"); } @Test @@ -186,22 +190,13 @@ public class CnSnToXmlTest { private void serializeToXml(CompositeNode compositeNode, String... xmlRepresentation) throws TransformerFactoryConfigurationError { - XmlMapper xmlMapper = new XmlMapper(); - String xmlString = null; - if (dataSchemaNode instanceof DataNodeContainer) { - try { - Document doc = xmlMapper.write(compositeNode, (DataNodeContainer) dataSchemaNode); - DOMSource domSource = new DOMSource(doc); - StringWriter writer = new StringWriter(); - StreamResult result = new StreamResult(writer); - TransformerFactory tf = TransformerFactory.newInstance(); - Transformer transformer = tf.newTransformer(); - transformer.transform(domSource, result); - xmlString = writer.toString(); - } catch (UnsupportedDataTypeException | TransformerException e) { - } + String xmlString = ""; + try { + xmlString = TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, + StructuredDataToXmlProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { } - assertNotNull(xmlMapper); + assertNotNull(xmlString); boolean containSearchedStr = false; String strRepresentation = ""; for (String searchedStr : xmlRepresentation) { @@ -215,16 +210,21 @@ public class CnSnToXmlTest { } - private CompositeNode prepareIdentityrefData() { + 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( TestUtils.buildQName("cont1", "basic:module", "2013-12-2"), cont, null, ModifyAction.CREATE, null); cont.getChildren().add(cont1); + Object value = null; + if (valueAsQName) { + value = TestUtils.buildQName("iden", "referenced:module", "2013-12-2", prefix); + } else { + value = "no qname value"; + } MutableSimpleNode lf11 = NodeFactory.createMutableSimpleNode( - TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, - TestUtils.buildQName("iden", "referenced:module", "2013-12-2"), ModifyAction.CREATE, null); + TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, value, ModifyAction.CREATE, null); cont1.getChildren().add(lf11); cont1.init(); cont.init(); @@ -244,4 +244,19 @@ public class CnSnToXmlTest { return cont; } + private CompositeNode prepareLeafrefData() { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, + ModifyAction.CREATE, null); + + MutableSimpleNode lfBoolean = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfBoolean"), + cont, Boolean.TRUE, ModifyAction.CREATE, null); + MutableSimpleNode lfLfref = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfLfref"), cont, + "true", ModifyAction.CREATE, null); + cont.getChildren().add(lfBoolean); + cont.getChildren().add(lfLfref); + cont.init(); + + return cont; + } + } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java new file mode 100644 index 0000000000..a23501cbe6 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlWithChoiceTest.java @@ -0,0 +1,64 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; + +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import javax.ws.rs.WebApplicationException; + +import org.junit.BeforeClass; +import org.junit.Test; +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; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.data.impl.NodeFactory; + +/** + * + * CnSn = Composite node and Simple node data structure Class contains test of + * serializing simple nodes data values according data types from YANG schema to + * XML file + * + */ +public class CnSnToXmlWithChoiceTest extends YangAndXmlAndDataSchemaLoader { + @BeforeClass + public static void initialization() { + dataLoad("/cnsn-to-xml/choice", 1, "module-with-choice", "cont"); + } + + @Test + public void cnSnToXmlWithYangChoice() { + String xmlOutput = ""; + try { + xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput( + prepareCnStructForYangData("lf1", "String data1"), modules, dataSchemaNode, + StructuredDataToXmlProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + + assertTrue(xmlOutput.contains("String data1")); + + try { + xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput( + prepareCnStructForYangData("lf2", "String data2"), modules, dataSchemaNode, + StructuredDataToXmlProvider.INSTANCE); + } catch (WebApplicationException | IOException e) { + } + assertTrue(xmlOutput.contains("String data2")); + + } + + private CompositeNode prepareCnStructForYangData(String lfName, Object data) { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, + ModifyAction.CREATE, null); + + MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName(lfName), cont, data, + ModifyAction.CREATE, null); + cont.getChildren().add(lf1); + cont.init(); + + return cont; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonIdentityrefToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonIdentityrefToCnSnTest.java new file mode 100644 index 0000000000..e9b1dbe1a5 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonIdentityrefToCnSnTest.java @@ -0,0 +1,74 @@ +package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; +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.*; + +public class JsonIdentityrefToCnSnTest extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialize() { + dataLoad("/json-to-cnsn/identityref", 2, "identityref-module", "cont"); + } + + @Test + public void jsonIdentityrefToCompositeNode() { + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false, + JsonToCompositeNodeProvider.INSTANCE); + assertNotNull(compositeNode); + + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + + assertEquals("cont", compositeNode.getNodeType().getLocalName()); + + List> childs = compositeNode.getChildren(); + assertEquals(1, childs.size()); + Node nd = childs.iterator().next(); + assertTrue(nd instanceof CompositeNode); + assertEquals("cont1", nd.getNodeType().getLocalName()); + + childs = ((CompositeNode) nd).getChildren(); + assertEquals(4, childs.size()); + SimpleNode lf11 = null; + SimpleNode lf12 = null; + SimpleNode lf13 = null; + SimpleNode lf14 = null; + for (Node child : childs) { + assertTrue(child instanceof SimpleNode); + if (child.getNodeType().getLocalName().equals("lf11")) { + lf11 = (SimpleNode) child; + } else if (child.getNodeType().getLocalName().equals("lf12")) { + lf12 = (SimpleNode) child; + } else if (child.getNodeType().getLocalName().equals("lf13")) { + lf13 = (SimpleNode) child; + } else if (child.getNodeType().getLocalName().equals("lf14")) { + lf14 = (SimpleNode) child; + } + } + + assertTrue(lf11.getValue() instanceof QName); + assertEquals("iden", ((QName) lf11.getValue()).getLocalName()); + assertEquals("identity:module", ((QName) lf11.getValue()).getNamespace().toString()); + + assertTrue(lf12.getValue() instanceof QName); + assertEquals("iden_local", ((QName) lf12.getValue()).getLocalName()); + assertEquals("identityref:module", ((QName) lf12.getValue()).getNamespace().toString()); + + assertTrue(lf13.getValue() instanceof QName); + assertEquals("iden_local", ((QName) lf13.getValue()).getLocalName()); + assertEquals("identityref:module", ((QName) lf13.getValue()).getNamespace().toString()); + + assertTrue(lf14.getValue() instanceof QName); + assertEquals("iden_local", ((QName) lf14.getValue()).getLocalName()); + assertEquals("identity:module", ((QName) lf14.getValue()).getNamespace().toString()); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java new file mode 100644 index 0000000000..2bb42d903a --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonLeafrefToCnSnTest.java @@ -0,0 +1,48 @@ +package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; + +import static org.junit.Assert.*; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; +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.*; + +public class JsonLeafrefToCnSnTest extends YangAndXmlAndDataSchemaLoader { + + @BeforeClass + public static void initialize() { + dataLoad("/json-to-cnsn/leafref"); + } + + /** + * JSON values which represents leafref are always loaded to simple node as + * string + */ + @Test + public void jsonIdentityrefToCompositeNode() { + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/leafref/json/data.json", false, + JsonToCompositeNodeProvider.INSTANCE); + assertNotNull(compositeNode); + TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); + + assertEquals("cont", compositeNode.getNodeType().getLocalName()); + + SimpleNode lf2 = null; + for (Node childNode : compositeNode.getChildren()) { + if (childNode instanceof SimpleNode) { + if (childNode.getNodeType().getLocalName().equals("lf2")) { + lf2 = (SimpleNode) childNode; + break; + } + } + } + + assertNotNull(lf2); + assertTrue(lf2.getValue() instanceof String); + assertEquals("121", (String) lf2.getValue()); + + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java index b02ea9a3a2..e7421a1776 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java @@ -2,16 +2,15 @@ package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; import static org.junit.Assert.*; -import java.io.*; -import java.net.URISyntaxException; -import java.util.List; -import java.util.Set; +import java.io.FileNotFoundException; +import java.util.*; import javax.ws.rs.WebApplicationException; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; +import org.opendaylight.controller.sal.restconf.impl.ResponseException; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.*; @@ -28,7 +27,7 @@ public class JsonToCnSnTest { @Test public void simpleListTest() { - simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang", "lst", "simple:list:yang1", + simpleTest("/json-to-cnsn/simple-list.json", "/json-to-cnsn/simple-list-yang/1", "lst", "simple:list:yang1", "simple-list-yang1"); } @@ -43,7 +42,8 @@ public class JsonToCnSnTest { */ @Test public void multipleItemsInLeafList() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/multiple-leaflist-items.json", true); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-leaflist-items.json", true, + JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); assertEquals(3, compositeNode.getChildren().size()); @@ -76,9 +76,10 @@ public class JsonToCnSnTest { */ @Test public void multipleItemsInListTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/multiple-items-in-list.json", true); - assertNotNull(compositeNode); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/multiple-items-in-list.json", true, + JsonToCompositeNodeProvider.INSTANCE); + assertNotNull(compositeNode); assertEquals("lst", compositeNode.getNodeType().getLocalName()); verityMultipleItemsInList(compositeNode); @@ -86,7 +87,8 @@ public class JsonToCnSnTest { @Test public void nullArrayToSimpleNodeWithNullValueTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/array-with-null.json", true); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/array-with-null.json", true, + JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); assertEquals("cont", compositeNode.getNodeType().getLocalName()); @@ -103,7 +105,8 @@ public class JsonToCnSnTest { public void incorrectTopLevelElementsTest() { Throwable cause1 = null; try { - compositeContainerFromJson("/json-to-cnsn/wrong-top-level1.json", true); + TestUtils + .readInputToCnSn("/json-to-cnsn/wrong-top-level1.json", true, JsonToCompositeNodeProvider.INSTANCE); } catch (WebApplicationException e) { cause1 = e; } @@ -117,7 +120,8 @@ public class JsonToCnSnTest { Throwable cause2 = null; try { - compositeContainerFromJson("/json-to-cnsn/wrong-top-level2.json", true); + TestUtils + .readInputToCnSn("/json-to-cnsn/wrong-top-level2.json", true, JsonToCompositeNodeProvider.INSTANCE); } catch (WebApplicationException e) { cause2 = e; } @@ -126,7 +130,8 @@ public class JsonToCnSnTest { Throwable cause3 = null; try { - compositeContainerFromJson("/json-to-cnsn/wrong-top-level3.json", true); + TestUtils + .readInputToCnSn("/json-to-cnsn/wrong-top-level3.json", true, JsonToCompositeNodeProvider.INSTANCE); } catch (WebApplicationException e) { cause3 = e; } @@ -145,7 +150,8 @@ public class JsonToCnSnTest { */ @Test public void emptyDataReadTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/empty-data.json", true); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/empty-data.json", true, + JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); @@ -158,7 +164,7 @@ public class JsonToCnSnTest { String reason = null; try { - compositeContainerFromJson("/json-to-cnsn/empty-data1.json", true); + TestUtils.readInputToCnSn("/json-to-cnsn/empty-data1.json", true, JsonToCompositeNodeProvider.INSTANCE); } catch (JsonSyntaxException e) { reason = e.getMessage(); } @@ -176,24 +182,23 @@ public class JsonToCnSnTest { @Test public void notSupplyNamespaceIfAlreadySupplied() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/simple-list.json"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/simple-list.json", false, + JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - DataSchemaNode dataSchemaNode1 = null; - DataSchemaNode dataSchemaNode2 = null; + // supplement namespaces according to first data schema - + // "simple:data:types1" + Set modules1 = new HashSet<>(); + Set modules2 = new HashSet<>(); try { - dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-cnsn/simple-list-yang", "simple-list-yang1"); - dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-cnsn/simple-list-yang", "simple-list-yang2"); + modules1 = TestUtils.loadModules(TestUtils.class.getResource("/json-to-cnsn/simple-list-yang/1").getPath()); + modules2 = TestUtils.loadModules(TestUtils.class.getResource("/json-to-cnsn/simple-list-yang/2").getPath()); } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); } - assertNotNull(dataSchemaNode1); - assertNotNull(dataSchemaNode2); + assertNotNull(modules1); + assertNotNull(modules2); - // supplement namespaces according to first data schema - - // "simple:data:types1" - TestUtils.supplementNamespace(dataSchemaNode1, compositeNode); + TestUtils.normalizeCompositeNode(compositeNode, modules1, "simple-list-yang1:lst"); assertTrue(compositeNode instanceof CompositeNodeWrapper); CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(); @@ -201,26 +206,31 @@ public class JsonToCnSnTest { assertEquals("lst", compNode.getNodeType().getLocalName()); verifyCompositeNode(compNode, "simple:list:yang1"); - // dataSchemaNode2 should't be taken into account, because compNode - // isn't CompositeNodeWrapper - TestUtils.supplementNamespace(dataSchemaNode2, compNode); - verifyCompositeNode(compNode, "simple:list:yang1"); + String exceptionMessage = ""; + try { + TestUtils.normalizeCompositeNode(compositeNode, modules2, "simple-list-yang2:lst"); + } catch (ResponseException e) { + exceptionMessage = String.valueOf(e.getResponse().getEntity()); + } + assertTrue(exceptionMessage + .contains("Data has bad format\nIf data is in XML format then namespace for lst should be simple:list:yang2.\n If data is in Json format then module name for lst should be simple-list-yang2.")); } @Test public void jsonIdentityrefToCompositeNode() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-cnsn/identityref/json/data.json"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/json-to-cnsn/identityref/json/data.json", false, + JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - Set modules = TestUtils.resolveModules("/json-to-cnsn/identityref"); + Set modules = TestUtils.resolveModulesFrom("/json-to-cnsn/identityref"); assertEquals(2, modules.size()); Module module = TestUtils.resolveModule("identityref-module", modules); assertNotNull(module); - DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); + DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module); assertNotNull(dataSchemaNode); - TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "identityref-module:cont"); + TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont"); assertEquals("cont", compositeNode.getNodeType().getLocalName()); @@ -268,19 +278,20 @@ public class JsonToCnSnTest { private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace, String moduleName) { - CompositeNode compositeNode = compositeContainerFromJson(jsonPath); + CompositeNode compositeNode = TestUtils.readInputToCnSn(jsonPath, false, JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - DataSchemaNode dataSchemaNode = null; + Set modules = null; try { - dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName); + modules = TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); } catch (FileNotFoundException e) { LOG.error(e.getMessage()); assertTrue(false); } - assertNotNull(dataSchemaNode); + assertNotNull(modules); - TestUtils.supplementNamespace(dataSchemaNode, compositeNode); + TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName); + // TestUtils.supplementNamespace(dataSchemaNode, compositeNode); assertTrue(compositeNode instanceof CompositeNodeWrapper); CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(); @@ -331,7 +342,8 @@ public class JsonToCnSnTest { boolean lflst1_2Found = false; boolean lf1Found = false; - assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString()); + // assertEquals(namespace, + // compositeNode.getNodeType().getNamespace().toString()); for (Node node : compositeNode.getChildren()) { if (node.getNodeType().getLocalName().equals("cont1")) { @@ -369,34 +381,16 @@ public class JsonToCnSnTest { assertTrue(lf1Found); } - private CompositeNode compositeContainerFromJson(String jsonPath) { - return compositeContainerFromJson(jsonPath, false); - } - - private CompositeNode compositeContainerFromJson(String jsonPath, boolean dummyNamespaces) - throws WebApplicationException { - - JsonToCompositeNodeProvider jsonToCompositeNodeProvider = JsonToCompositeNodeProvider.INSTANCE; - InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath); + @Test + public void unsupportedDataFormatTest() { + String exceptionMessage = ""; try { - CompositeNode compositeNode = jsonToCompositeNodeProvider - .readFrom(null, null, null, null, null, jsonStream); - assertTrue(compositeNode instanceof CompositeNodeWrapper); - if (dummyNamespaces) { - try { - TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); - return ((CompositeNodeWrapper) compositeNode).unwrap(); - } catch (URISyntaxException e) { - LOG.error(e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - return compositeNode; - } catch (IOException e) { - LOG.error(e.getMessage()); - assertTrue(e.getMessage(), false); + TestUtils.readInputToCnSn("/json-to-cnsn/unsupported-json-format.json", true, + JsonToCompositeNodeProvider.INSTANCE); + } catch (WebApplicationException e) { + exceptionMessage = e.getCause().getMessage(); } - return null; + assertTrue(exceptionMessage.contains("Root element of Json has to be Object")); } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyType.java new file mode 100644 index 0000000000..0876584cab --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyType.java @@ -0,0 +1,64 @@ +package org.opendaylight.controller.sal.restconf.impl.test; + +import java.util.List; + +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.*; + +public class DummyType implements TypeDefinition { + QName dummyQName = TestUtils.buildQName("dummy type", "simple:uri", "2012-12-17"); + + @Override + public QName getQName() { + return dummyQName; + } + + @Override + public SchemaPath getPath() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getDescription() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getReference() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Status getStatus() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getUnknownSchemaNodes() { + // TODO Auto-generated method stub + return null; + } + + @Override + public DummyType getBaseType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getUnits() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Object getDefaultValue() { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java index d58b7e9dab..f181c02bbb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java @@ -33,7 +33,7 @@ public class InvokeRpcMethodTest { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/invoke-rpc"); + modules = TestUtils.resolveModulesFrom("/invoke-rpc"); assertEquals(1, modules.size()); Module module = TestUtils.resolveModule("invoke-rpc-module", modules); assertNotNull(module); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java index 0bb03cb0ad..e912b733f3 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java @@ -14,21 +14,15 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.LogRecord; -import javax.ws.rs.core.Application; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; +import javax.ws.rs.core.*; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.TestProperties; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; -import org.opendaylight.controller.sal.restconf.impl.ControllerContext; -import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; +import org.opendaylight.controller.sal.restconf.impl.*; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.Module; @@ -69,33 +63,33 @@ public class ReadConfAndOperDataTest extends JerseyTest { String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml"); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml", true); when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); assertEquals(200, response.getStatus()); - + uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example"); when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null); - + response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); assertEquals(404, response.getStatus()); } + @Ignore @Test public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException { String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0"); - - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml"); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml"); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); assertEquals(200, response.getStatus()); - + uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example"); when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null); - + response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); assertEquals(404, response.getStatus()); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java new file mode 100644 index 0000000000..fcc4c02a6f --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestCodecExceptionsTest.java @@ -0,0 +1,33 @@ +package org.opendaylight.controller.sal.restconf.impl.test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +import org.junit.Test; +import org.opendaylight.controller.sal.restconf.impl.RestCodec; +import org.opendaylight.yangtools.concepts.Codec; +import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.BitsType; + +public class RestCodecExceptionsTest { + + @Test + public void serializeExceptionTest() { + Codec codec = RestCodec.from(new BitsType(null)); + String serializedValue = (String) codec.serialize("incorrect value"); // set + // expected + assertEquals("incorrect value", serializedValue); + } + + @Test + public void deserializeExceptionTest() { + IdentityrefTypeDefinition mockedIidentityrefType = mock(IdentityrefTypeDefinition.class); + + Codec codec = RestCodec.from(mockedIidentityrefType); + String serializedValue = (String) codec.deserialize("incorrect value"); // IdentityValuesDTO + // object + // expected + assertEquals("incorrect value", serializedValue); + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java index 41cc0ddb51..64c4bee3a6 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java @@ -6,7 +6,6 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.FileNotFoundException; -import java.io.InputStream; import java.util.Set; import org.junit.BeforeClass; @@ -33,8 +32,7 @@ public class RestconfImplTest { @Test public void testExample() throws FileNotFoundException { - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder(xmlStream); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml"); BrokerFacade brokerFacade = mock(BrokerFacade.class); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); assertEquals(loadedCompositeNode, brokerFacade.readOperationalData(null)); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index 366d99dbcb..25488457b2 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -10,9 +11,10 @@ import java.net.URI; import java.net.URISyntaxException; import java.sql.Date; import java.util.*; -import java.util.concurrent.Future; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.ext.MessageBodyReader; +import javax.ws.rs.ext.MessageBodyWriter; import javax.xml.parsers.*; import javax.xml.stream.XMLStreamException; import javax.xml.transform.*; @@ -20,12 +22,13 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; -import org.opendaylight.controller.sal.rest.impl.*; +import org.opendaylight.controller.sal.rest.impl.UnsupportedFormatException; +import org.opendaylight.controller.sal.rest.impl.XmlReader; import org.opendaylight.controller.sal.restconf.impl.*; +import org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test.JsonToCnSnTest; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.*; -import org.opendaylight.yangtools.yang.data.impl.XmlTreeBuilder; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.*; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; @@ -38,7 +41,7 @@ import com.google.common.base.Preconditions; public final class TestUtils { - private static final Logger logger = LoggerFactory.getLogger(TestUtils.class); + private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class); private final static YangModelParser parser = new YangParserImpl(); @@ -77,15 +80,13 @@ public final class TestUtils { return result; } - - public static Document loadDocumentFrom(InputStream inputStream) { try { DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); return docBuilder.parse(inputStream); } catch (SAXException | IOException | ParserConfigurationException e) { - logger.error("Error during loading Document from XML", e); + LOG.error("Error during loading Document from XML", e); return null; } } @@ -107,239 +108,108 @@ public final class TestUtils { return new String(charData, "UTF-8"); } catch (IOException | TransformerException e) { String msg = "Error during transformation of Document into String"; - logger.error(msg, e); + LOG.error(msg, e); return msg; } } - public static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, - String outputPath, String searchedModuleName, String searchedDataSchemaName) { - Set modules = resolveModules(yangPath); - Module module = resolveModule(searchedModuleName, modules); - DataSchemaNode dataSchemaNode = resolveDataSchemaNode(module, searchedDataSchemaName); - - normalizeCompositeNode(compositeNode, modules, dataSchemaNode, searchedModuleName + ":" - + searchedDataSchemaName); - - try { - return writeCompNodeWithSchemaContextToJson(compositeNode, modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return null; - - } - - public static void normalizeCompositeNode(CompositeNode compositeNode, Set modules, - DataSchemaNode dataSchemaNode, String schemaNodePath) { + /** + * + * Fill missing data (namespaces) and build correct data type in + * {@code compositeNode} according to {@code dataSchemaNode}. The method + * {@link RestconfImpl#createConfigurationData createConfigurationData} is + * used because it contains calling of method {code normalizeNode} + */ + public static void normalizeCompositeNode(CompositeNode compositeNode, Set modules, String schemaNodePath) { RestconfImpl restconf = RestconfImpl.getInstance(); ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); - TestUtils.prepareMockForRestconfBeforeNormalization(modules, dataSchemaNode, restconf); + prepareMockForRestconfBeforeNormalization(modules, restconf); restconf.createConfigurationData(schemaNodePath, compositeNode); } public static Module resolveModule(String searchedModuleName, Set modules) { assertNotNull("modules can't be null.", modules); - Module module = null; if (searchedModuleName != null) { for (Module m : modules) { if (m.getName().equals(searchedModuleName)) { - module = m; - break; + return m; } } } else if (modules.size() == 1) { - module = modules.iterator().next(); + return modules.iterator().next(); } - return module; + return null; } - public static Set resolveModules(String yangPath) { - Set modules = null; - + public static Set resolveModulesFrom(String yangPath) { try { - modules = TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); + return TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); } catch (FileNotFoundException e) { - e.printStackTrace(); + LOG.error("Yang files at path: " + yangPath + " weren't loaded."); } - return modules; + return null; } - public static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) { - assertNotNull("Module is missing", module); + public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) { + assertNotNull("Module can't be null", module); - DataSchemaNode dataSchemaNode = null; if (searchedDataSchemaName != null) { for (DataSchemaNode dsn : module.getChildNodes()) { if (dsn.getQName().getLocalName().equals(searchedDataSchemaName)) { - dataSchemaNode = dsn; + return dsn; } } } else if (module.getChildNodes().size() == 1) { - dataSchemaNode = module.getChildNodes().iterator().next(); + return module.getChildNodes().iterator().next(); } - return dataSchemaNode; + return null; } - public static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, Set modules, - DataSchemaNode dataSchemaNode) throws IOException, WebApplicationException { - String jsonResult; - - assertNotNull(dataSchemaNode); - assertNotNull("Composite node can't be null", compositeNode); - ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); - - ControllerContext.getInstance().setSchemas(loadSchemaContext(modules)); - - StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; - structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, - null, byteArrayOS); - - jsonResult = byteArrayOS.toString(); - - return jsonResult; + public static CompositeNode loadCompositeNode(String xmlDataPath) { + return loadCompositeNode(xmlDataPath, false); } - public static CompositeNode loadCompositeNode(String xmlDataPath) { + public static CompositeNode loadCompositeNode(String xmlDataPath, boolean addDumyNamespace) { InputStream xmlStream = TestUtils.class.getResourceAsStream(xmlDataPath); CompositeNode compositeNode = null; try { XmlReader xmlReader = new XmlReader(); compositeNode = xmlReader.read(xmlStream); - } catch (UnsupportedFormatException | XMLStreamException e) { - e.printStackTrace(); + LOG.error(e.getMessage()); } - return compositeNode; - } - - static void outputToFile(ByteArrayOutputStream outputStream, String outputDir) throws IOException { - FileOutputStream fileOS = null; - try { - String path = TestUtils.class.getResource(outputDir).getPath(); - File outFile = new File(path + "/data.json"); - fileOS = new FileOutputStream(outFile); + if (addDumyNamespace) { try { - fileOS.write(outputStream.toByteArray()); - } catch (IOException e) { - e.printStackTrace(); + addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); + } catch (URISyntaxException e) { + LOG.error(e.getMessage()); } - fileOS.close(); - } catch (FileNotFoundException e1) { - e1.printStackTrace(); } + return compositeNode; } - static String readJsonFromFile(String path, boolean removeWhiteChars) { - FileReader fileReader = getFileReader(path); - - StringBuilder strBuilder = new StringBuilder(); - char[] buffer = new char[1000]; - - while (true) { - int loadedCharNum; - try { - loadedCharNum = fileReader.read(buffer); - } catch (IOException e) { - break; - } - if (loadedCharNum == -1) { - break; - } - strBuilder.append(buffer, 0, loadedCharNum); - } - try { - fileReader.close(); - } catch (IOException e) { - System.out.println("The file wasn't closed"); - } - String rawStr = strBuilder.toString(); - if (removeWhiteChars) { - rawStr = rawStr.replace("\n", ""); - rawStr = rawStr.replace("\r", ""); - rawStr = rawStr.replace("\t", ""); - rawStr = removeSpaces(rawStr); - } - - return rawStr; - } - - private static FileReader getFileReader(String path) { - String fullPath = TestUtils.class.getResource(path).getPath(); - assertNotNull("Path to file can't be null.", fullPath); - File file = new File(fullPath); - assertNotNull("File can't be null", file); - FileReader fileReader = null; - try { - fileReader = new FileReader(file); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } - assertNotNull("File reader can't be null.", fileReader); - return fileReader; - } - - private static String removeSpaces(String rawStr) { - StringBuilder strBuilder = new StringBuilder(); - int i = 0; - int quoteCount = 0; - while (i < rawStr.length()) { - if (rawStr.substring(i, i + 1).equals("\"")) { - quoteCount++; - } - - if (!rawStr.substring(i, i + 1).equals(" ") || (quoteCount % 2 == 1)) { - strBuilder.append(rawStr.charAt(i)); - } - i++; - } - - return strBuilder.toString(); - } - - public static QName buildQName(String name, String uri, String date) { + public static QName buildQName(String name, String uri, String date, String prefix) { try { URI u = new URI(uri); Date dt = null; if (date != null) { dt = Date.valueOf(date); } - return new QName(u, dt, name); + return new QName(u, dt, prefix, name); } catch (URISyntaxException e) { return null; } } - public static QName buildQName(String name) { - return buildQName(name, "", null); + public static QName buildQName(String name, String uri, String date) { + return buildQName(name, uri, date, null); } - public static void supplementNamespace(DataSchemaNode dataSchemaNode, CompositeNode compositeNode) { - RestconfImpl restconf = RestconfImpl.getInstance(); - - InstanceIdWithSchemaNode instIdAndSchema = new InstanceIdWithSchemaNode(mock(InstanceIdentifier.class), - dataSchemaNode); - - ControllerContext controllerContext = mock(ControllerContext.class); - BrokerFacade broker = mock(BrokerFacade.class); - - RpcResult rpcResult = new DummyRpcResult.Builder().result( - TransactionStatus.COMMITED).build(); - Future> future = DummyFuture.builder().rpcResult(rpcResult).build(); - when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema); - when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn( - future); - - restconf.setControllerContext(controllerContext); - restconf.setBroker(broker); - - // method is called only because it contains call of method which - // supplement namespaces to compositeNode - restconf.createConfigurationData("something", compositeNode); + public static QName buildQName(String name) { + return buildQName(name, "", null); } public static DataSchemaNode obtainSchemaFromYang(String yangFolder) throws FileNotFoundException { @@ -385,7 +255,6 @@ public final class TestUtils { } DataSchemaNode dataSchemaNode = moduleRes.getChildNodes().iterator().next(); return dataSchemaNode; - } public static void addDummyNamespaceToAllNodes(NodeWrapper wrappedNode) throws URISyntaxException { @@ -397,56 +266,62 @@ public final class TestUtils { } } - public static void prepareMockForRestconfBeforeNormalization(Set modules, DataSchemaNode dataSchemaNode, - RestconfImpl restconf) { - ControllerContext instance = ControllerContext.getInstance(); - instance.setSchemas(TestUtils.loadSchemaContext(modules)); - restconf.setControllerContext(ControllerContext.getInstance()); + private static void prepareMockForRestconfBeforeNormalization(Set modules, RestconfImpl restconf) { + ControllerContext controllerContext = ControllerContext.getInstance(); BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class); + + controllerContext.setSchemas(TestUtils.loadSchemaContext(modules)); + when(mockedBrokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) .thenReturn( new DummyFuture.Builder().rpcResult( new DummyRpcResult.Builder().result(TransactionStatus.COMMITED) .build()).build()); + + restconf.setControllerContext(ControllerContext.getInstance()); restconf.setBroker(mockedBrokerFacade); } - - static CompositeNode loadCompositeNodeWithXmlTreeBuilder(String xmlDataPath) { - InputStream xmlStream = TestUtils.class.getResourceAsStream(xmlDataPath); - CompositeNode compositeNode = null; + + public static CompositeNode readInputToCnSn(String jsonPath, boolean dummyNamespaces, + MessageBodyReader reader) throws WebApplicationException { + + InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath); try { - compositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder(xmlStream); - } catch (FileNotFoundException e) { - e.printStackTrace(); + CompositeNode compositeNode = reader.readFrom(null, null, null, null, null, jsonStream); + assertTrue(compositeNode instanceof CompositeNodeWrapper); + if (dummyNamespaces) { + try { + TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); + return ((CompositeNodeWrapper) compositeNode).unwrap(); + } catch (URISyntaxException e) { + LOG.error(e.getMessage()); + assertTrue(e.getMessage(), false); + } + } + return compositeNode; + } catch (IOException e) { + LOG.error(e.getMessage()); + assertTrue(e.getMessage(), false); } - return compositeNode; - - - + return null; + } + + public static String writeCompNodeWithSchemaContextToOutput(CompositeNode compositeNode, Set modules, + DataSchemaNode dataSchemaNode, MessageBodyWriter messageBodyWriter) throws IOException, + WebApplicationException { + + assertNotNull(dataSchemaNode); + assertNotNull("Composite node can't be null", compositeNode); + ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream(); + + ControllerContext.getInstance().setSchemas(loadSchemaContext(modules)); + + messageBodyWriter.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, null, + byteArrayOS); + + return byteArrayOS.toString(); + } - - - public static CompositeNode loadCompositeNodeWithXmlTreeBuilder(InputStream xmlInputStream) throws FileNotFoundException { - if (xmlInputStream == null) { - throw new IllegalArgumentException(); - } - Node dataTree; - try { - dataTree = XmlTreeBuilder.buildDataTree(xmlInputStream); - } catch (XMLStreamException e) { - logger.error("Error during building data tree from XML", e); - return null; - } - if (dataTree == null) { - logger.error("data tree is null"); - return null; - } - if (dataTree instanceof SimpleNode) { - logger.error("RPC XML was resolved as SimpleNode"); - return null; - } - return (CompositeNode) dataTree; - } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java index 7e3da0e4b4..dd3f367f49 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java @@ -4,24 +4,32 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.util.Set; -import org.opendaylight.yangtools.yang.model.api.*; + +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; public abstract class YangAndXmlAndDataSchemaLoader { protected static Set modules; protected static DataSchemaNode dataSchemaNode; + protected static String searchedModuleName; + protected static String searchedDataSchemaName; + protected static String schemaNodePath; protected static void dataLoad(String yangPath) { dataLoad(yangPath, 1, null, null); } protected static void dataLoad(String yangPath, int modulesNumber, String moduleName, String dataSchemaName) { - modules = TestUtils.resolveModules(yangPath); + modules = TestUtils.resolveModulesFrom(yangPath); assertEquals(modulesNumber, modules.size()); Module module = TestUtils.resolveModule(moduleName, modules); + searchedModuleName = module == null ? "" : module.getName(); assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, dataSchemaName); + dataSchemaNode = TestUtils.resolveDataSchemaNode(dataSchemaName, module); + searchedDataSchemaName = dataSchemaNode == null ? "" : dataSchemaNode.getQName().getLocalName(); assertNotNull(dataSchemaNode); + schemaNodePath = searchedModuleName + ":" + searchedDataSchemaName; } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java new file mode 100644 index 0000000000..894b4c2bab --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java @@ -0,0 +1,392 @@ +package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test; + +import static org.junit.Assert.*; + +import java.io.*; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Set; + +import javax.ws.rs.WebApplicationException; + +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; +import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; +import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class XmlLeafrefToCnSnTest { + private static final Logger LOG = LoggerFactory.getLogger(XmlLeafrefToCnSnTest.class); + + /** + * top level element represents container. second level element is list with + * two elements. + */ + @Test + public void testXmlDataContainer() { + CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false); + assertNotNull(compNode); + Set modules = null; + try { + modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-container-yang").getPath()); + } catch (FileNotFoundException e) { + LOG.error(e.getMessage()); + assertTrue(false); + } + + assertNotNull(modules); + TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont"); + + String nameSpace = "data:container:yang"; + assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString()); + + verifyNullAndEmptyStringSingleNode(compNode, nameSpace); + verifyCommonPartAOfXml(compNode, "", nameSpace); + } + + private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) { + assertEquals("cont", compNode.getNodeType().getLocalName()); + + SimpleNode lf2 = null; + SimpleNode lf3 = null; + int found = 0; + for (Node child : compNode.getChildren()) { + if (found == 0x3) + break; + if (child instanceof SimpleNode) { + SimpleNode childSimple = (SimpleNode) child; + if (childSimple.getNodeType().getLocalName().equals("lf3")) { + lf3 = childSimple; + found = found | (1 << 0); + } else if (childSimple.getNodeType().getLocalName().equals("lf2")) { + lf2 = childSimple; + found = found | (1 << 1); + } + } + assertEquals(nameSpace, child.getNodeType().getNamespace().toString()); + } + + assertEquals("", lf2.getValue()); + assertEquals(null, lf3.getValue()); + } + + @Test + public void testXmlDataList() { + CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false); + assertNotNull(compNode); + + Set modules = null; + try { + modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-list-yang").getPath()); + } catch (FileNotFoundException e) { + } + assertNotNull(modules); + + TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont"); + + String nameSpaceList = "data:list:yang"; + String nameSpaceCont = "data:container:yang"; + assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString()); + assertEquals("cont", compNode.getNodeType().getLocalName()); + assertEquals(3, compNode.getChildren().size()); + CompositeNode lst1_1 = null; + CompositeNode lst1_2 = null; + int loopCount = 0; + for (Node node : compNode.getChildren()) { + if (node.getNodeType().getLocalName().equals("lf1")) { + assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString()); + assertTrue(node instanceof SimpleNode); + assertEquals("lf1", node.getValue()); + } else { + assertTrue(node instanceof CompositeNode); + switch (loopCount++) { + case 0: + lst1_1 = (CompositeNode) node; + break; + case 1: + lst1_2 = (CompositeNode) node; + break; + } + assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString()); + } + } + // lst1_1 + verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont); + // :lst1_1 + + // lst1_2 + SimpleNode lflst11 = null; + CompositeNode cont11 = null; + for (Node node : lst1_2.getChildren()) { + String nodeName = node.getNodeType().getLocalName(); + if (nodeName.equals("lflst11")) { + assertTrue(node instanceof SimpleNode); + lflst11 = (SimpleNode) node; + + } else if (nodeName.equals("cont11")) { + assertTrue(node instanceof CompositeNode); + cont11 = (CompositeNode) node; + } + assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString()); + } + assertEquals("221", lflst11.getValue()); + + assertEquals(1, cont11.getChildren().size()); + assertTrue(cont11.getChildren().get(0) instanceof SimpleNode); + SimpleNode cont11_lf111 = (SimpleNode) cont11.getChildren().get(0); + assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString()); + assertEquals("lf111", cont11_lf111.getNodeType().getLocalName()); + assertEquals((short) 100, cont11_lf111.getValue()); + // :lst1_2 + + } + + @Test + public void testXmlEmptyData() { + CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/empty-data.xml", true); + assertEquals("cont", compNode.getNodeType().getLocalName()); + SimpleNode lf1 = null; + SimpleNode lflst1_1 = null; + SimpleNode lflst1_2 = null; + CompositeNode lst1 = null; + int lflst1Count = 0; + for (Node node : compNode.getChildren()) { + if (node.getNodeType().getLocalName().equals("lf1")) { + assertTrue(node instanceof SimpleNode); + lf1 = (SimpleNode) node; + } else if (node.getNodeType().getLocalName().equals("lflst1")) { + assertTrue(node instanceof SimpleNode); + + switch (lflst1Count++) { + case 0: + lflst1_1 = (SimpleNode) node; + break; + case 1: + lflst1_2 = (SimpleNode) node; + break; + } + } else if (node.getNodeType().getLocalName().equals("lst1")) { + assertTrue(node instanceof CompositeNode); + lst1 = (CompositeNode) node; + } + } + + assertNotNull(lf1); + assertNotNull(lflst1_1); + assertNotNull(lflst1_2); + assertNotNull(lst1); + + assertEquals("", lf1.getValue()); + assertEquals("", lflst1_1.getValue()); + assertEquals("", lflst1_2.getValue()); + assertEquals(1, lst1.getChildren().size()); + assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName()); + + assertTrue(lst1.getChildren().get(0) instanceof SimpleNode); + assertEquals("", lst1.getChildren().get(0).getValue()); + + } + + /** + * Test case like this x:identity + */ + @Test + public void testIdentityrefNmspcInElement() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-element.xml", "/xml-to-cnsn/identityref", + "identityref-module", "cont", 2, "iden", "identity:module"); + } + + /** + * + * Test case like identity + */ + + @Test + public void testIdentityrefDefaultNmspcInElement() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-element.xml", + "/xml-to-cnsn/identityref/yang-augments", "general-module", "cont", 3, "iden", "identityref:module"); + } + + /** + * + * Test case like identity + */ + @Test + public void testIdentityrefDefaultNmspcInParrentElement() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-parrent-element.xml", + "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module"); + } + + /** + * + * Test case like + * x:identity + */ + @Test + public void testIdentityrefNmspcInParrentElement() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-parrent-element.xml", + "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "z:namespace"); + + } + + /** + * + * Test case like (without namespace in xml) x:identity + * + */ + @Test + public void testIdentityrefNoNmspcValueWithPrefix() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-with-prefix.xml", + "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "x:iden", "identityref:module"); + } + + /** + * + * Test case like (without namespace in xml) identity + * + */ + @Test + public void testIdentityrefNoNmspcValueWithoutPrefix() { + testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-without-prefix.xml", + "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module"); + } + + private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) { + SimpleNode lf1suf = null; + SimpleNode lflst1suf_1 = null; + SimpleNode lflst1suf_2 = null; + SimpleNode lflst1suf_3 = null; + CompositeNode cont1suf = null; + CompositeNode lst1suf = null; + + int lflstCount = 0; + + for (Node node : compNode.getChildren()) { + String localName = node.getNodeType().getLocalName(); + if (localName.equals("lf1" + suf)) { + assertTrue(node instanceof SimpleNode); + lf1suf = (SimpleNode) node; + } else if (localName.equals("lflst1" + suf)) { + assertTrue(node instanceof SimpleNode); + switch (lflstCount++) { + case 0: + lflst1suf_1 = (SimpleNode) node; + break; + case 1: + lflst1suf_2 = (SimpleNode) node; + break; + case 2: + lflst1suf_3 = (SimpleNode) node; + break; + } + } else if (localName.equals("lst1" + suf)) { + assertTrue(node instanceof CompositeNode); + lst1suf = (CompositeNode) node; + } else if (localName.equals("cont1" + suf)) { + assertTrue(node instanceof CompositeNode); + cont1suf = (CompositeNode) node; + } + assertEquals(nameSpace, node.getNodeType().getNamespace().toString()); + } + + assertNotNull(lf1suf); + assertNotNull(lflst1suf_1); + assertNotNull(lflst1suf_2); + assertNotNull(lflst1suf_3); + assertNotNull(lst1suf); + assertNotNull(cont1suf); + + assertEquals("str0", lf1suf.getValue()); + assertEquals("121", lflst1suf_1.getValue()); + assertEquals("131", lflst1suf_2.getValue()); + assertEquals("str1", lflst1suf_3.getValue()); + + assertEquals(1, lst1suf.getChildren().size()); + + assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode); + SimpleNode lst11_lf11 = (SimpleNode) lst1suf.getChildren().get(0); + assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString()); + assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName()); + assertEquals("str2", lst11_lf11.getValue()); + + assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode); + SimpleNode cont1_lf11 = (SimpleNode) cont1suf.getChildren().get(0); + assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString()); + assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName()); + assertEquals((short) 100, cont1_lf11.getValue()); + } + + private CompositeNode compositeNodeFromXml(String xmlPath, boolean dummyNamespaces) { + XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE; + try { + InputStream xmlStream = XmlLeafrefToCnSnTest.class.getResourceAsStream(xmlPath); + CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream); + if (dummyNamespaces) { + try { + TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); + return ((CompositeNodeWrapper) compositeNode).unwrap(); + } catch (URISyntaxException e) { + LOG.error(e.getMessage()); + assertTrue(e.getMessage(), false); + } + } + return compositeNode; + + } catch (WebApplicationException | IOException e) { + LOG.error(e.getMessage()); + assertTrue(false); + } + return null; + } + + private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName, + int moduleCount, String resultLocalName, String resultNamespace) { + CompositeNode compositeNode = compositeNodeFromXml(xmlPath, false); + assertNotNull(compositeNode); + + Set modules = TestUtils.resolveModulesFrom(yangPath); + assertEquals(moduleCount, modules.size()); + Module module = TestUtils.resolveModule(moduleName, modules); + assertNotNull(module); + DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module); + assertNotNull(dataSchemaNode); + + TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + schemaName); + + SimpleNode lf11 = getLf11(compositeNode); + assertTrue(lf11.getValue() instanceof QName); + QName qName = (QName) lf11.getValue(); + assertEquals(resultLocalName, qName.getLocalName()); + assertEquals(resultNamespace, qName.getNamespace().toString()); + + } + + private SimpleNode getLf11(CompositeNode compositeNode) { + assertEquals("cont", compositeNode.getNodeType().getLocalName()); + + List> childs = compositeNode.getChildren(); + assertEquals(1, childs.size()); + Node nd = childs.iterator().next(); + assertTrue(nd instanceof CompositeNode); + assertEquals("cont1", nd.getNodeType().getLocalName()); + + childs = ((CompositeNode) nd).getChildren(); + SimpleNode lf11 = null; + for (Node child : childs) { + assertTrue(child instanceof SimpleNode); + if (child.getNodeType().getLocalName().equals("lf11")) { + lf11 = (SimpleNode) child; + } + } + assertNotNull(lf11); + return lf11; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlToCnSnTest.java index 50ab0857b7..7c7df56133 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlToCnSnTest.java @@ -2,391 +2,43 @@ package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test; import static org.junit.Assert.*; -import java.io.*; -import java.net.URISyntaxException; -import java.util.List; -import java.util.Set; - -import javax.ws.rs.WebApplicationException; - +import org.junit.BeforeClass; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; -import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader; import org.opendaylight.yangtools.yang.data.api.*; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class XmlToCnSnTest { - private static final Logger LOG = LoggerFactory.getLogger(XmlToCnSnTest.class); - - /** - * top level element represents container. second level element is list with - * two elements. - */ - @Test - public void testXmlDataContainer() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false); - assertNotNull(compNode); - DataSchemaNode dataSchemaNode = null; - try { - dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-cnsn/data-container-yang"); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } - - assertNotNull(dataSchemaNode); - TestUtils.supplementNamespace(dataSchemaNode, compNode); - - String nameSpace = "data:container:yang"; - assertEquals(nameSpace, compNode.getNodeType().getNamespace().toString()); - - verifyNullAndEmptyStringSingleNode(compNode, nameSpace); - verifyCommonPartAOfXml(compNode, "", nameSpace); - } - - private void verifyNullAndEmptyStringSingleNode(CompositeNode compNode, String nameSpace) { - assertEquals("cont", compNode.getNodeType().getLocalName()); - SimpleNode lf2 = null; - SimpleNode lf3 = null; - int found = 0; - for (Node child : compNode.getChildren()) { - if (found == 0x3) - break; - if (child instanceof SimpleNode) { - SimpleNode childSimple = (SimpleNode) child; - if (childSimple.getNodeType().getLocalName().equals("lf3")) { - lf3 = childSimple; - found = found | (1 << 0); - } else if (childSimple.getNodeType().getLocalName().equals("lf2")) { - lf2 = childSimple; - found = found | (1 << 1); - } - } - assertEquals(nameSpace, child.getNodeType().getNamespace().toString()); - } +public class XmlToCnSnTest extends YangAndXmlAndDataSchemaLoader { - assertEquals("", lf2.getValue()); - assertEquals(null, lf3.getValue()); + @BeforeClass + public static void initialize() { + dataLoad("/xml-to-cnsn/leafref"); } @Test - public void testXmlDataList() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false); - assertNotNull(compNode); - - DataSchemaNode dataSchemaNode = null; - try { - dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-cnsn/data-list-yang", "data-container-yang"); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - } + public void testXmlLeafrefToCnSn() { + CompositeNode compositeNode = TestUtils.readInputToCnSn("/xml-to-cnsn/leafref/xml/data.xml", false, + XmlToCompositeNodeProvider.INSTANCE); + assertNotNull(compositeNode); assertNotNull(dataSchemaNode); - TestUtils.supplementNamespace(dataSchemaNode, compNode); - - String nameSpaceList = "data:list:yang"; - String nameSpaceCont = "data:container:yang"; - assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString()); - assertEquals("cont", compNode.getNodeType().getLocalName()); - assertEquals(3, compNode.getChildren().size()); - CompositeNode lst1_1 = null; - CompositeNode lst1_2 = null; - int loopCount = 0; - for (Node node : compNode.getChildren()) { - if (node.getNodeType().getLocalName().equals("lf1")) { - assertEquals(nameSpaceList, node.getNodeType().getNamespace().toString()); - assertTrue(node instanceof SimpleNode); - assertEquals("lf1", node.getValue()); - } else { - assertTrue(node instanceof CompositeNode); - switch (loopCount++) { - case 0: - lst1_1 = (CompositeNode) node; - break; - case 1: - lst1_2 = (CompositeNode) node; - break; - } - assertEquals(nameSpaceCont, node.getNodeType().getNamespace().toString()); - } - } - // lst1_1 - verifyCommonPartAOfXml(lst1_1, "1", nameSpaceCont); - // :lst1_1 - - // lst1_2 - SimpleNode lflst11 = null; - CompositeNode cont11 = null; - for (Node node : lst1_2.getChildren()) { - String nodeName = node.getNodeType().getLocalName(); - if (nodeName.equals("lflst11")) { - assertTrue(node instanceof SimpleNode); - lflst11 = (SimpleNode) node; - - } else if (nodeName.equals("cont11")) { - assertTrue(node instanceof CompositeNode); - cont11 = (CompositeNode) node; - } - assertEquals(nameSpaceCont, compNode.getNodeType().getNamespace().toString()); - } - assertEquals("221", lflst11.getValue()); - - assertEquals(1, cont11.getChildren().size()); - assertTrue(cont11.getChildren().get(0) instanceof SimpleNode); - SimpleNode cont11_lf111 = (SimpleNode) cont11.getChildren().get(0); - assertEquals(nameSpaceCont, cont11_lf111.getNodeType().getNamespace().toString()); - assertEquals("lf111", cont11_lf111.getNodeType().getLocalName()); - assertEquals((short) 100, cont11_lf111.getValue()); - // :lst1_2 - - } - - @Test - public void testXmlEmptyData() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/empty-data.xml", true); - assertEquals("cont", compNode.getNodeType().getLocalName()); - SimpleNode lf1 = null; - SimpleNode lflst1_1 = null; - SimpleNode lflst1_2 = null; - CompositeNode lst1 = null; - int lflst1Count = 0; - for (Node node : compNode.getChildren()) { - if (node.getNodeType().getLocalName().equals("lf1")) { - assertTrue(node instanceof SimpleNode); - lf1 = (SimpleNode) node; - } else if (node.getNodeType().getLocalName().equals("lflst1")) { - assertTrue(node instanceof SimpleNode); - - switch (lflst1Count++) { - case 0: - lflst1_1 = (SimpleNode) node; - break; - case 1: - lflst1_2 = (SimpleNode) node; - break; - } - } else if (node.getNodeType().getLocalName().equals("lst1")) { - assertTrue(node instanceof CompositeNode); - lst1 = (CompositeNode) node; - } - } - - assertNotNull(lf1); - assertNotNull(lflst1_1); - assertNotNull(lflst1_2); - assertNotNull(lst1); - - assertEquals("", lf1.getValue()); - assertEquals("", lflst1_1.getValue()); - assertEquals("", lflst1_2.getValue()); - assertEquals(1, lst1.getChildren().size()); - assertEquals("lf11", lst1.getChildren().get(0).getNodeType().getLocalName()); - - assertTrue(lst1.getChildren().get(0) instanceof SimpleNode); - assertEquals("", lst1.getChildren().get(0).getValue()); - - } - - /** - * Test case like this x:identity - */ - @Test - public void testIdentityrefNmspcInElement() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-element.xml", "/xml-to-cnsn/identityref", - "identityref-module", "cont", 2, "iden", "identity:module"); - } + TestUtils.normalizeCompositeNode(compositeNode, modules, schemaNodePath); - /** - * - * Test case like identity - */ - - @Test - public void testIdentityrefDefaultNmspcInElement() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-element.xml", - "/xml-to-cnsn/identityref/yang-augments", "general-module", "cont", 3, "iden", "identityref:module"); - } - - /** - * - * Test case like identity - */ - @Test - public void testIdentityrefDefaultNmspcInParrentElement() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-default-nmspc-in-parrent-element.xml", - "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module"); - } - - /** - * - * Test case like - * x:identity - */ - @Test - public void testIdentityrefNmspcInParrentElement() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-nmspc-in-parrent-element.xml", - "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "z:namespace"); - - } - - /** - * - * Test case like (without namespace in xml) x:identity - * - */ - @Test - public void testIdentityrefNoNmspcValueWithPrefix() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-with-prefix.xml", - "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "x:iden", "identityref:module"); - } - - /** - * - * Test case like (without namespace in xml) identity - * - */ - @Test - public void testIdentityrefNoNmspcValueWithoutPrefix() { - testIdentityrefToCnSn("/xml-to-cnsn/identityref/xml/data-no-nmspc-value-without-prefix.xml", - "/xml-to-cnsn/identityref", "identityref-module", "cont", 2, "iden", "identityref:module"); - } - - private void verifyCommonPartAOfXml(CompositeNode compNode, String suf, String nameSpace) { - SimpleNode lf1suf = null; - SimpleNode lflst1suf_1 = null; - SimpleNode lflst1suf_2 = null; - SimpleNode lflst1suf_3 = null; - CompositeNode cont1suf = null; - CompositeNode lst1suf = null; - - int lflstCount = 0; + assertEquals("cont", compositeNode.getNodeType().getLocalName()); - for (Node node : compNode.getChildren()) { - String localName = node.getNodeType().getLocalName(); - if (localName.equals("lf1" + suf)) { - assertTrue(node instanceof SimpleNode); - lf1suf = (SimpleNode) node; - } else if (localName.equals("lflst1" + suf)) { - assertTrue(node instanceof SimpleNode); - switch (lflstCount++) { - case 0: - lflst1suf_1 = (SimpleNode) node; - break; - case 1: - lflst1suf_2 = (SimpleNode) node; - break; - case 2: - lflst1suf_3 = (SimpleNode) node; + SimpleNode lf2 = null; + for (Node childNode : compositeNode.getChildren()) { + if (childNode instanceof SimpleNode) { + if (childNode.getNodeType().getLocalName().equals("lf2")) { + lf2 = (SimpleNode) childNode; break; } - } else if (localName.equals("lst1" + suf)) { - assertTrue(node instanceof CompositeNode); - lst1suf = (CompositeNode) node; - } else if (localName.equals("cont1" + suf)) { - assertTrue(node instanceof CompositeNode); - cont1suf = (CompositeNode) node; } - assertEquals(nameSpace, node.getNodeType().getNamespace().toString()); } - assertNotNull(lf1suf); - assertNotNull(lflst1suf_1); - assertNotNull(lflst1suf_2); - assertNotNull(lflst1suf_3); - assertNotNull(lst1suf); - assertNotNull(cont1suf); - - assertEquals("str0", lf1suf.getValue()); - assertEquals("121", lflst1suf_1.getValue()); - assertEquals("131", lflst1suf_2.getValue()); - assertEquals("str1", lflst1suf_3.getValue()); - - assertEquals(1, lst1suf.getChildren().size()); - - assertTrue(lst1suf.getChildren().get(0) instanceof SimpleNode); - SimpleNode lst11_lf11 = (SimpleNode) lst1suf.getChildren().get(0); - assertEquals(nameSpace, lst11_lf11.getNodeType().getNamespace().toString()); - assertEquals("lf11" + suf, lst11_lf11.getNodeType().getLocalName()); - assertEquals("str2", lst11_lf11.getValue()); - - assertTrue(cont1suf.getChildren().get(0) instanceof SimpleNode); - SimpleNode cont1_lf11 = (SimpleNode) cont1suf.getChildren().get(0); - assertEquals(nameSpace, cont1_lf11.getNodeType().getNamespace().toString()); - assertEquals("lf11" + suf, cont1_lf11.getNodeType().getLocalName()); - assertEquals((short) 100, cont1_lf11.getValue()); - } - - private CompositeNode compositeNodeFromXml(String xmlPath, boolean dummyNamespaces) { - XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE; - try { - InputStream xmlStream = XmlToCnSnTest.class.getResourceAsStream(xmlPath); - CompositeNode compositeNode = xmlToCompositeNodeProvider.readFrom(null, null, null, null, null, xmlStream); - if (dummyNamespaces) { - try { - TestUtils.addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); - return ((CompositeNodeWrapper) compositeNode).unwrap(); - } catch (URISyntaxException e) { - LOG.error(e.getMessage()); - assertTrue(e.getMessage(), false); - } - } - return compositeNode; - - } catch (WebApplicationException | IOException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } - return null; - } - - private void testIdentityrefToCnSn(String xmlPath, String yangPath, String moduleName, String schemaName, - int moduleCount, String resultLocalName, String resultNamespace) { - CompositeNode compositeNode = compositeNodeFromXml(xmlPath, false); - assertNotNull(compositeNode); - - Set modules = TestUtils.resolveModules(yangPath); - assertEquals(moduleCount, modules.size()); - Module module = TestUtils.resolveModule(moduleName, modules); - assertNotNull(module); - DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); - assertNotNull(dataSchemaNode); - - TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, moduleName + ":" + schemaName); - - SimpleNode lf11 = getLf11(compositeNode); - assertTrue(lf11.getValue() instanceof QName); - QName qName = (QName) lf11.getValue(); - assertEquals(resultLocalName, qName.getLocalName()); - assertEquals(resultNamespace, qName.getNamespace().toString()); - - } - - private SimpleNode getLf11(CompositeNode compositeNode) { - assertEquals("cont", compositeNode.getNodeType().getLocalName()); - - List> childs = compositeNode.getChildren(); - assertEquals(1, childs.size()); - Node nd = childs.iterator().next(); - assertTrue(nd instanceof CompositeNode); - assertEquals("cont1", nd.getNodeType().getLocalName()); - - childs = ((CompositeNode) nd).getChildren(); - SimpleNode lf11 = null; - for (Node child : childs) { - assertTrue(child instanceof SimpleNode); - if (child.getNodeType().getLocalName().equals("lf11")) { - lf11 = (SimpleNode) child; - } - } - assertNotNull(lf11); - return lf11; + assertNotNull(lf2); + assertTrue(lf2.getValue() instanceof String); + assertEquals("121", (String) lf2.getValue()); } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/invalid-top-level-element/invalid-top-level-element.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/invalid-top-level-element/invalid-top-level-element.yang new file mode 100644 index 0000000000..a9df486b13 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/invalid-top-level-element/invalid-top-level-element.yang @@ -0,0 +1,13 @@ +module invalid-top-level-element { + namespace "invalid:top:level:element"; + + prefix "intoleel"; + revision 2013-12-17 { + } + + + leaf lf { + type string; + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/list/list-types-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/list/list-types-module new file mode 100644 index 0000000000..9bdea81579 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/list/list-types-module @@ -0,0 +1,274 @@ +module simple-data-types { + namespace "simple:data:types"; + + prefix "smpdtp"; + revision 2013-11-12 { + } + + identity iden { + } + + typedef tpdfempty { + type empty; + } + + typedef tpdfbit { + type bits { + bit b1; + bit b2; + bit b3; + } + } + + typedef tpdfun4 { + type boolean; + } + + typedef tpdfun3 { + type union { + type tpdfbit; + type tpdfempty; + } + } + + typedef tpdfun2 { + type union { + type tpdfun3; + type tpdfun4; + } + } + + typedef tpdfun1 { + type union { + type uint8; + type decimal64 { + fraction-digits 2; + } + } + } + + container cont { + leaf lfnint8Min { + type int8; + } + leaf lfnint8Max { + type int8; + } + leaf lfnint16Min { + type int16; + } + leaf lfnint16Max { + type int16; + } + leaf lfnint32Min { + type int32; + } + leaf lfnint32Max { + type int32; + } + leaf lfnint64Min { + type int64; + } + leaf lfnint64Max { + type int64; + } + + leaf lfnuint8Max { + type uint8; + } + leaf lfnuint16Max { + type uint16; + } + leaf lfnuint32Max { + type uint32; + } + leaf lfuint64Max { + type uint64; + } + leaf lfstr { + type string; + } + leaf lfstr1 { + type string; + } + leaf lfbool1 { + type boolean; + } + leaf lfbool2 { + type boolean; + } + leaf lfbool3 { + type boolean; + } + leaf lfdecimal1 { + type decimal64 { + fraction-digits 2; + } + } + leaf lfdecimal2 { + type decimal64 { + fraction-digits 2; + } + } + leaf lfdecimal3 { + type decimal64 { + fraction-digits 2; + } + } + + leaf lfdecimal4 { + type decimal64 { + fraction-digits 2; + } + } + + + leaf lfdecimal6 { + type decimal64 { + fraction-digits 2; + } + } + + leaf lfenum { + type enumeration { + enum enum1; + enum enum2; + enum enum3; + enum enum4; + } + } + + leaf lfbits { + type bits { + bit bit1; + bit bit2; + bit bit3; + bit bit4; + } + } + + leaf lfbinary { + type binary; + } + + leaf lfref1 { //reference to string type + type leafref { + path "../lfstr"; + } + } + + leaf lfref2 { //reference to number type + type leafref { + path "../lfnint8Max"; + } + } + + leaf lfempty { + type empty; + } + + leaf lfunion1 { + type union { + type uint16; + type string; + } + } + leaf lfunion2 { + type union { + type decimal64 { + fraction-digits 2; + } + type string; + } + } + + leaf lfunion3 { + type union { + type empty; + type string; + } + } + + leaf lfunion4 { + type union { + type boolean; + type string; + } + } + + leaf lfunion5 { + type union { + type uint16; + type string; + } + } + + leaf lfunion6 { + type union { + type uint16; + type empty; + } + } + + leaf lfunion7 { + type tpdfun3; + } + + leaf lfunion8 { + type union { + type uint16; + type string; + } + } + + leaf lfunion9 { + type union { + type uint16; + type boolean; + } + } + + leaf lfunion10 { + type union { + type bits { + bit bt1; + bit bt2; + } + type boolean; + } + } + + leaf lfunion11 { + type union { + type tpdfun1; + type tpdfun2; + } + } + + leaf lfunion12 { + type tpdfun2; + } + + leaf lfunion13 { + type tpdfbit; + } + + leaf lfunion14 { + type union { + type enumeration { + enum zero; + enum one; + } + type uint16; + } + } + + leaf identityref1 { + type identityref { + base iden; + } + } + + + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/choice/module-with-choice.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/choice/module-with-choice.yang new file mode 100644 index 0000000000..84547847ee --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/choice/module-with-choice.yang @@ -0,0 +1,25 @@ +module module-with-choice { + namespace "module:with:choice"; + + prefix "mowicho"; + + revision 2013-12-18 { + } + + + container cont { + choice choA { + case caA1 { + leaf lf1 { + type string; + } + } + case caA2 { + leaf lf2 { + type string; + } + } + } + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/basic-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/basic-module.yang index 7023c94f2f..81d77329c6 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/basic-module.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/basic-module.yang @@ -91,6 +91,12 @@ module basic-module { } } + leaf lfLfref { + type leafref { + path "/cont/lfBoolean"; + } + } + } } \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json new file mode 100644 index 0000000000..235666eed4 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/json/data.json @@ -0,0 +1,6 @@ +{ + "cont":{ + "lf1":121, + "lf2":121 + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module new file mode 100644 index 0000000000..8ca9f09096 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/leafref/leafref-module @@ -0,0 +1,19 @@ +module leafref-module { + namespace "leafref:module"; + + prefix "lfrfmo"; + revision 2013-11-18 { + } + + container cont { + leaf lf1 { + type int32; + } + leaf lf2 { + type leafref { + path "/cont/lf1"; + } + } + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/simple-list1.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/1/simple-list1.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/simple-list1.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/1/simple-list1.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/simple-list2.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/2/simple-list2.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/simple-list2.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/simple-list-yang/2/simple-list2.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/unsupported-json-format.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/unsupported-json-format.json new file mode 100644 index 0000000000..abc626739e --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-cnsn/unsupported-json-format.json @@ -0,0 +1 @@ +fffff \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/leafref-module b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/leafref-module new file mode 100644 index 0000000000..8ca9f09096 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/leafref-module @@ -0,0 +1,19 @@ +module leafref-module { + namespace "leafref:module"; + + prefix "lfrfmo"; + revision 2013-11-18 { + } + + container cont { + leaf lf1 { + type int32; + } + leaf lf2 { + type leafref { + path "/cont/lf1"; + } + } + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/xml/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/xml/data.xml new file mode 100644 index 0000000000..06200a69b5 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-cnsn/leafref/xml/data.xml @@ -0,0 +1,4 @@ + + 121 + 121 + \ No newline at end of file -- 2.36.6