From: Martin Sunal Date: Wed, 18 Dec 2013 09:24:12 +0000 (+0100) Subject: Switched codecs infrastructure to use yang-data-impl codecs X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~172^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=refs%2Fchanges%2F49%2F3749%2F8 Switched codecs infrastructure to use yang-data-impl codecs - Codecs are used for tranlation of JSON/XML values to SimpleNode values and vice versa - Leafref is encoded as String Change-Id: Ibe5b4a23d5dad6373a2dcf7d8723a7dff135cf2a Signed-off-by: Martin Sunal Signed-off-by: Jozef Gloncak Signed-off-by: Tony Tkacik --- 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 ea793e913a..fb7872f8bc 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,7 +4,6 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; import java.net.URI; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -12,9 +11,9 @@ import java.util.Set; import javax.activation.UnsupportedDataTypeException; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; -import org.opendaylight.controller.sal.restconf.impl.RestCodec; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO; 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; @@ -32,10 +31,7 @@ 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.InstanceIdentifierTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; -import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -186,13 +182,12 @@ class JsonMapper { private void writeValueOfNodeByType(JsonWriter writer, SimpleNode node, TypeDefinition type, DataSchemaNode schema) throws IOException { - String value = String.valueOf(node.getValue()); - TypeDefinition baseType = resolveBaseTypeFrom(type); + TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(type); - // TODO check InstanceIdentifierTypeDefinition, + // TODO check InstanceIdentifierTypeDefinition if (baseType instanceof IdentityrefTypeDefinition) { if (node.getValue() instanceof QName) { - IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(type).serialize(node.getValue()); + IdentityValuesDTO valueDTO = (IdentityValuesDTO) RestCodec.from(baseType).serialize(node.getValue()); IdentityValue valueFromDTO = valueDTO.getValuesWithNamespaces().get(0); String moduleName = ControllerContext.getInstance().findModuleByNamespace(URI.create(valueFromDTO.getNamespace())); writer.value(moduleName + ":" + valueFromDTO.getValue()); @@ -201,102 +196,28 @@ class JsonMapper { + baseType.getQName().getLocalName() + " is not instance of " + QName.class + " but is " + node.getValue().getClass()); writer.value(String.valueOf(node.getValue())); } - } else if (baseType instanceof LeafrefTypeDefinition) { - ControllerContext contContext = ControllerContext.getInstance(); - LeafSchemaNode lfSchemaNode = contContext.resolveTypeFromLeafref((LeafrefTypeDefinition) baseType, schema); - if (lfSchemaNode != null) { - writeValueOfNodeByType(writer, node, lfSchemaNode.getType(), lfSchemaNode); - } else { - writer.value(value); - } - } else if (baseType instanceof InstanceIdentifierTypeDefinition) { - writer.value(((InstanceIdentifierTypeDefinition) baseType).getPathStatement().toString()); - } else if (baseType instanceof UnionTypeDefinition) { - processTypeIsUnionType(writer, (UnionTypeDefinition) baseType, value); } else if (baseType instanceof DecimalTypeDefinition || baseType instanceof IntegerTypeDefinition || baseType instanceof UnsignedIntegerTypeDefinition) { - writer.value(new NumberForJsonWriter(value)); + writer.value(new NumberForJsonWriter((String) RestCodec.from(baseType).serialize(node.getValue()))); } else if (baseType instanceof BooleanTypeDefinition) { - writer.value(Boolean.parseBoolean(value)); + writer.value(Boolean.parseBoolean((String) RestCodec.from(baseType).serialize(node.getValue()))); } else if (baseType instanceof EmptyTypeDefinition) { writeEmptyDataTypeToJson(writer); } else { + String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue())); + if (value == null) { + value = String.valueOf(node.getValue()); + } writer.value(value.equals("null") ? "" : value); } } - private void processTypeIsUnionType(JsonWriter writer, UnionTypeDefinition unionType, String value) - throws IOException { - if (value == null) { - writeEmptyDataTypeToJson(writer); - } else if ((isNumber(value)) - && containsType(unionType, UnsignedIntegerTypeDefinition.class, IntegerTypeDefinition.class, - DecimalTypeDefinition.class)) { - writer.value(new NumberForJsonWriter(value)); - } else if (isBoolean(value) && containsType(unionType, BooleanTypeDefinition.class)) { - writer.value(Boolean.parseBoolean(value)); - } else { - writer.value(value); - } - } - - private boolean isBoolean(String value) { - if (value.equals("true") || value.equals("false")) { - return true; - } - return false; - } - private void writeEmptyDataTypeToJson(JsonWriter writer) throws IOException { writer.beginArray(); writer.nullValue(); writer.endArray(); } - private boolean isNumber(String value) { - try { - Double.valueOf(value); - } catch (NumberFormatException e) { - return false; - } - return true; - } - - private boolean containsType(UnionTypeDefinition unionType, Class... searchedTypes) { - List> allUnionSubtypes = resolveAllUnionSubtypesFrom(unionType); - - for (TypeDefinition unionSubtype : allUnionSubtypes) { - for (Class searchedType : searchedTypes) { - if (searchedType.isInstance(unionSubtype)) { - return true; - } - } - } - return false; - } - - private List> resolveAllUnionSubtypesFrom(UnionTypeDefinition inputType) { - List> result = new ArrayList<>(); - for (TypeDefinition subtype : inputType.getTypes()) { - TypeDefinition resolvedSubtype = subtype; - - resolvedSubtype = resolveBaseTypeFrom(subtype); - - if (resolvedSubtype instanceof UnionTypeDefinition) { - List> subtypesFromRecursion = resolveAllUnionSubtypesFrom((UnionTypeDefinition) resolvedSubtype); - result.addAll(subtypesFromRecursion); - } else { - result.add(resolvedSubtype); - } - } - - return result; - } - - private TypeDefinition resolveBaseTypeFrom(TypeDefinition type) { - return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type; - } - private void writeName(Node node, DataSchemaNode schema, JsonWriter writer) throws IOException { String nameForOutput = node.getNodeType().getLocalName(); if (schema.isAugmenting()) { diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestUtil.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestUtil.java new file mode 100644 index 0000000000..6e70b9bddf --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestUtil.java @@ -0,0 +1,15 @@ +package org.opendaylight.controller.sal.rest.impl; + +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; + +public final class RestUtil { + + public final static TypeDefinition resolveBaseTypeFrom(TypeDefinition type) { + TypeDefinition superType = type; + while (superType.getBaseType() != null) { + superType = superType.getBaseType(); + } + return superType; + } + +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java index f580b3216c..a3d658e7bf 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java @@ -7,9 +7,9 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.opendaylight.controller.sal.restconf.impl.RestCodec; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO; 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; @@ -64,9 +64,9 @@ public class XmlMapper { Element itemEl = doc.createElementNS(dataType.getNamespace().toString(), dataType.getLocalName()); if (data instanceof SimpleNode) { if (schema instanceof LeafListSchemaNode) { - writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafListSchemaNode) schema).getType()); + writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafListSchemaNode) schema).getType(), (DataSchemaNode) schema); } else if (schema instanceof LeafSchemaNode) { - writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafSchemaNode) schema).getType()); + writeValueOfNodeByType(itemEl, (SimpleNode) data, ((LeafSchemaNode) schema).getType(), (DataSchemaNode) schema); } else { Object value = data.getValue(); if (value != null) { @@ -91,9 +91,9 @@ public class XmlMapper { return itemEl; } - private void writeValueOfNodeByType(Element element, SimpleNode node, TypeDefinition type) { + private void writeValueOfNodeByType(Element element, SimpleNode node, TypeDefinition type, DataSchemaNode schema) { - TypeDefinition baseType = resolveBaseTypeFrom(type); + TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(type); if (baseType instanceof IdentityrefTypeDefinition) { if (node.getValue() instanceof QName) { @@ -111,9 +111,12 @@ public class XmlMapper { element.setTextContent(String.valueOf(node.getValue())); } } else { - Object value = node.getValue(); - if (value != null) { - element.setTextContent(String.valueOf(value)); + if (node.getValue() != null) { + String value = String.valueOf(RestCodec.from(baseType).serialize(node.getValue())); + if (value.equals("null")) { + value = String.valueOf(node.getValue()); + } + element.setTextContent(value); } } } @@ -136,8 +139,4 @@ public class XmlMapper { return null; } - private TypeDefinition resolveBaseTypeFrom(TypeDefinition type) { - return type.getBaseType() != null ? resolveBaseTypeFrom(type.getBaseType()) : type; - } - } diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend index 406480df37..eec2d452a1 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.xtend @@ -12,6 +12,7 @@ import java.util.Map import java.util.concurrent.ConcurrentHashMap import javax.ws.rs.core.Response import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener +import org.opendaylight.controller.sal.rest.impl.RestUtil import org.opendaylight.controller.sal.rest.impl.RestconfProvider import org.opendaylight.yangtools.yang.common.QName import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier @@ -29,10 +30,7 @@ import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode import org.opendaylight.yangtools.yang.model.api.ListSchemaNode import org.opendaylight.yangtools.yang.model.api.RpcDefinition import org.opendaylight.yangtools.yang.model.api.SchemaContext -import org.opendaylight.yangtools.yang.model.api.SchemaNode import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition -import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition -import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil import org.slf4j.LoggerFactory import static com.google.common.base.Preconditions.* @@ -313,10 +311,7 @@ class ControllerContext implements SchemaServiceListener { var decoded = TypeDefinitionAwareCodec.from(typedef)?.deserialize(urlDecoded) if(decoded === null) { - var baseType = typedef - while (baseType.baseType !== null) { - baseType = baseType.baseType; - } + var baseType = RestUtil.resolveBaseTypeFrom(typedef) if(baseType instanceof IdentityrefTypeDefinition) { decoded = toQName(urlDecoded) } @@ -366,35 +361,4 @@ class ControllerContext implements SchemaServiceListener { } } - /** - * Resolve target type from leafref type. - * - * According to RFC 6020 referenced element has to be leaf (chapter 9.9). - * Therefore if other element is referenced then null value is returned. - * - * Currently only cases without path-predicate are supported. - * - * @param leafRef - * @param schemaNode - * data schema node which contains reference - * @return type if leaf is referenced and it is possible to find referenced - * node in schema context. In other cases null value is returned - */ - def LeafSchemaNode resolveTypeFromLeafref(LeafrefTypeDefinition leafRef, DataSchemaNode schemaNode) { - val xPath = leafRef.getPathStatement(); - val module = SchemaContextUtil.findParentModule(schemas, schemaNode); - - var SchemaNode foundSchemaNode - if (xPath.isAbsolute()) { - foundSchemaNode = SchemaContextUtil.findDataSchemaNode(schemas, module, xPath); - } else { - foundSchemaNode = SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemas, module, schemaNode, xPath); - } - - if (foundSchemaNode instanceof LeafSchemaNode) { - return foundSchemaNode as LeafSchemaNode; - } - - return null; - } } 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 6452b72f1e..450ba02b56 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 @@ -2,19 +2,21 @@ package org.opendaylight.controller.sal.restconf.impl; import java.net.URI; +import org.opendaylight.controller.sal.rest.impl.RestUtil; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; import org.opendaylight.yangtools.concepts.Codec; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec; +import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec; import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class RestCodec { - - @SuppressWarnings("rawtypes") - public static final Codec IDENTITYREF_DEFAULT_CODEC = new IdentityrefCodecImpl(); - + private RestCodec() { } @@ -22,31 +24,63 @@ public class RestCodec { return new ObjectCodec(typeDefinition); } + @SuppressWarnings("rawtypes") public static final class ObjectCodec implements Codec { - private TypeDefinition type; + 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(); + + private TypeDefinition type; + private ObjectCodec(TypeDefinition typeDefinition) { - type = typeDefinition; + type = RestUtil.resolveBaseTypeFrom(typeDefinition); } @SuppressWarnings("unchecked") @Override public Object deserialize(Object input) { - if (type instanceof IdentityrefTypeDefinition) { - return IDENTITYREF_DEFAULT_CODEC.deserialize(input); - } else { - return TypeDefinitionAwareCodec.from(type).deserialize(String.valueOf(input)); + try { + if (type instanceof IdentityrefTypeDefinition) { + return IDENTITYREF_DEFAULT_CODEC.deserialize(input); + } else if (type instanceof LeafrefTypeDefinition) { + return LEAFREF_DEFAULT_CODEC.deserialize(input); + } else { + 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."); + 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); + return input; } } @SuppressWarnings("unchecked") @Override public Object serialize(Object input) { - if (type instanceof IdentityrefTypeDefinition) { - return IDENTITYREF_DEFAULT_CODEC.serialize(input); - } else { - return TypeDefinitionAwareCodec.from(type).serialize(input); + try { + if (type instanceof IdentityrefTypeDefinition) { + return IDENTITYREF_DEFAULT_CODEC.serialize(input); + } else if (type instanceof LeafrefTypeDefinition) { + return LEAFREF_DEFAULT_CODEC.serialize(input); + } else { + 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."); + 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); + return input; } } @@ -72,4 +106,18 @@ public class RestCodec { } + public static class LeafrefCodecImpl implements LeafrefCodec { + + @Override + public String serialize(Object data) { + return String.valueOf(data); + } + + @Override + 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/ToJsonBasicDataTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicDataTypesTest.java new file mode 100644 index 0000000000..73d0c82521 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicDataTypesTest.java @@ -0,0 +1,349 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.io.StringReader; +import java.math.BigDecimal; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.*; + +import javax.ws.rs.WebApplicationException; +import javax.xml.bind.DatatypeConverter; + +import org.junit.Test; +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.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 { + + @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); + + TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "simple-data-types:cont"); + + try { + jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(compositeNode, modules, dataSchemaNode); + } catch (WebApplicationException | IOException e) { + assertTrue(false); // shouldn't get here + } + + System.out.println(jsonOutput); + verifyJsonOutput(jsonOutput); + } + + private CompositeNode prepareData() { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, + ModifyAction.CREATE, null); + + List> childNodes = new ArrayList<>(); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Min"), cont, (byte) -128, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Max"), cont, (byte) 127, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Min"), cont, (short) -32768, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Max"), cont, (short) 32767, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Min"), cont, + (int) -2147483648, ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Max"), cont, (int) 2147483647, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Min"), cont, new Long( + "-9223372036854775807"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Max"), cont, new Long( + "9223372036854775807"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint8Max"), cont, (short) 255, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint16Max"), cont, (int) 65535, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint32Max"), cont, new Long( + "4294967295"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr"), cont, "lfstr", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr1"), cont, "", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool1"), cont, Boolean.TRUE, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool2"), cont, Boolean.FALSE, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal1"), cont, new BigDecimal( + "43.32"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal2"), cont, new BigDecimal( + "-0.43"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal3"), cont, new BigDecimal( + "43"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal4"), cont, new BigDecimal( + "43E3"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal6"), cont, new BigDecimal( + "33.12345"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfenum"), cont, "enum3", + ModifyAction.CREATE, null)); + + HashSet bits = new HashSet(); + bits.add("bit3"); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbits"), cont, bits, + ModifyAction.CREATE, null)); + + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbinary"), cont, DatatypeConverter + .parseBase64Binary("AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfempty"), cont, null, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion1"), cont, (int) 324, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion2"), cont, new BigDecimal( + "33.3"), ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion3"), cont, "55", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion4"), cont, Boolean.TRUE, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion5"), cont, "true", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion6"), cont, "false", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion7"), cont, null, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion8"), cont, "", + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion9"), cont, "", + ModifyAction.CREATE, null)); + + HashSet bits2 = new HashSet(); + bits2.add("bt1"); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion10"), cont, bits2, + ModifyAction.CREATE, null)); + + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion11"), cont, (short) 33, + ModifyAction.CREATE, null)); + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion12"), cont, Boolean.FALSE, + ModifyAction.CREATE, null)); + try { + childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("identityref1"), cont, new QName( + new URI("simple:data:types"), "iden"), ModifyAction.CREATE, null)); + } catch (URISyntaxException e) { + } + + cont.getChildren().addAll(childNodes); + + cont.init(); + + return cont; + } + + private void verifyJsonOutput(String jsonOutput) { + StringReader strReader = new StringReader(jsonOutput); + JsonReader jReader = new JsonReader(strReader); + + String exception = null; + try { + jsonReadCont(jReader); + } catch (IOException e) { + exception = e.getMessage(); + } + + assertNull("Error during reading Json output: " + exception, exception); + } + + private void jsonReadCont(JsonReader jReader) throws IOException { + jReader.beginObject(); + assertNotNull("cont1 is missing.", jReader.hasNext()); + + // Cont dataFromJson = new Cont(jReader.nextName()); + jReader.nextName(); + jsonReadContElements(jReader); + + assertFalse("cont shouldn't have other element.", jReader.hasNext()); + jReader.endObject(); + // return dataFromJson; + } + + private void jsonReadContElements(JsonReader jReader) throws IOException { + jReader.beginObject(); + List loadedLfs = new ArrayList<>(); + boolean exceptForDecimal5Raised = false; + boolean enumChecked = false; + boolean bitsChecked = false; + boolean lfdecimal6Checked = false; + boolean lfdecimal4Checked = false; + boolean lfdecimal3Checked = false; + boolean lfdecimal2Checked = false; + boolean lfdecimal1Checked = false; + boolean lfbool1Checked = false; + boolean lfbool2Checked = false; + boolean lfstrChecked = false; + boolean lfbinaryChecked = false; + // boolean lfref1Checked = false; + boolean lfemptyChecked = false; + boolean lfstr1Checked = false; + boolean lfidentityrefChecked = false; + + while (jReader.hasNext()) { + String keyName = jReader.nextName(); + JsonToken peek = null; + try { + peek = jReader.peek(); + } catch (IOException e) { + if (keyName.equals("lfdecimal5")) { + exceptForDecimal5Raised = true; + } else { + assertTrue("Key " + keyName + " has incorrect value for specifed type", false); + } + } + + if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + try { + jReader.nextLong(); + } catch (NumberFormatException e) { + assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false); + } + loadedLfs.add(keyName.substring(3)); + } else if (keyName.equals("identityref1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("simple-data-types:iden", jReader.nextString()); + lfidentityrefChecked = true; + } else if (keyName.equals("lfstr")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("lfstr", jReader.nextString()); + lfstrChecked = true; + } else if (keyName.equals("lfstr1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("", jReader.nextString()); + lfstr1Checked = true; + } else if (keyName.equals("lfbool1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(true, jReader.nextBoolean()); + lfbool1Checked = true; + } else if (keyName.equals("lfbool2")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(false, jReader.nextBoolean()); + lfbool2Checked = true; + } else if (keyName.equals("lfbool3")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); + assertEquals(false, jReader.nextBoolean()); + } else if (keyName.equals("lfdecimal1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43.32), (Double) jReader.nextDouble()); + lfdecimal1Checked = true; + } else if (keyName.equals("lfdecimal2")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(-0.43), (Double) jReader.nextDouble()); + lfdecimal2Checked = true; + } else if (keyName.equals("lfdecimal3")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43), (Double) jReader.nextDouble()); + lfdecimal3Checked = true; + } else if (keyName.equals("lfdecimal4")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(43E3), (Double) jReader.nextDouble()); + lfdecimal4Checked = true; + } else if (keyName.equals("lfdecimal6")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); + assertEquals(new Double(33.12345), (Double) jReader.nextDouble()); + lfdecimal6Checked = true; + } else if (keyName.equals("lfenum")) { + assertEquals("enum3", jReader.nextString()); + enumChecked = true; + } else if (keyName.equals("lfbits")) { + assertEquals("bit3 bit2", jReader.nextString()); + bitsChecked = true; + } else if (keyName.equals("lfbinary")) { + assertEquals("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", jReader.nextString()); + lfbinaryChecked = true; + } else if (keyName.equals("lfempty")) { + jReader.beginArray(); + jReader.nextNull(); + jReader.endArray(); + lfemptyChecked = true; + } else if (keyName.startsWith("lfunion")) { + checkLfUnion(jReader, keyName, peek); + } else { + assertTrue("Key " + keyName + " doesn't exists in yang file.", false); + } + + } + Collections.sort(loadedLfs); + String expectedLfsStr = "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]"; + String actualLfsStr = loadedLfs.toString(); + assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr); + assertTrue("Enum wasn't checked", enumChecked); + assertTrue("Bits wasn't checked", bitsChecked); + assertTrue("Decimal1 wasn't checked", lfdecimal1Checked); + assertTrue("Decimal2 wasn't checked", lfdecimal2Checked); + assertTrue("Decimal3 wasn't checked", lfdecimal3Checked); + assertTrue("Decimal4 wasn't checked", lfdecimal4Checked); + assertTrue("Decimal5 wasn't checked", lfdecimal6Checked); + assertTrue("lfbool1 wasn't checked", lfbool1Checked); + assertTrue("lfbool2 wasn't checked", lfbool2Checked); + assertTrue("lfstr wasn't checked", lfstrChecked); + assertTrue("lfstr1 wasn't checked", lfstr1Checked); + assertTrue("lfbinary wasn't checked", lfbinaryChecked); + assertTrue("lfempty wasn't checked", lfemptyChecked); + assertTrue("lfidentityref wasn't checked", lfidentityrefChecked); + jReader.endObject(); + } + + private void checkLfUnion(JsonReader jReader, String keyName, JsonToken peek) throws IOException { + if (keyName.equals("lfunion1")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("324", jReader.nextString()); + } else if (keyName.equals("lfunion2")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("33.3", jReader.nextString()); + } else if (keyName.equals("lfunion3")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("55", jReader.nextString()); + } else if (keyName.equals("lfunion4")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("true", jReader.nextString()); + } else if (keyName.equals("lfunion5")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("true", jReader.nextString()); + } else if (keyName.equals("lfunion6")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("false", jReader.nextString()); + } else if (keyName.equals("lfunion7")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("", jReader.nextString()); + } else if (keyName.equals("lfunion8")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("", jReader.nextString()); + } else if (keyName.equals("lfunion9")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("", jReader.nextString()); + } else if (keyName.equals("lfunion10")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("bt1", jReader.nextString()); + } else if (keyName.equals("lfunion11")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("33", jReader.nextString()); + } else if (keyName.equals("lfunion12")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("false", jReader.nextString()); + } else if (keyName.equals("lfunion13")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("44", jReader.nextString()); + } else if (keyName.equals("lfunion14")) { + assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); + assertEquals("21", jReader.nextString()); + } + } +} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java similarity index 87% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java index e7a185a01d..b01d4104b2 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicYangTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonBasicYangTypesTest.java @@ -1,20 +1,20 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; -import java.io.*; -import java.util.*; +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; +import java.util.Set; import org.junit.Test; +import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; import org.opendaylight.controller.sal.restconf.impl.test.structures.*; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; -import com.google.gson.stream.*; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonToken; public class ToJsonBasicYangTypesTest { @@ -25,7 +25,7 @@ public class ToJsonBasicYangTypesTest { @Test public void compositeNodeAndYangWithJsonReaderEmptyDataTest() { String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson(prepareCompositeNodeWithEmpties(), - "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml"); + "/cnsn-to-json/simple-yang-types", "/cnsn-to-json/simple-yang-types/xml", "simple-yang-types", "cont1"); verifyJsonOutputForEmpty(jsonOutput); } @@ -36,8 +36,8 @@ public class ToJsonBasicYangTypesTest { @Test public void xmlAndYangTypesWithJsonReaderTest() { String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/simple-yang-types/xml/data.xml"), - "/yang-to-json-conversion/simple-yang-types", "/yang-to-json-conversion/simple-yang-types/xml"); + 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"); verifyJsonOutput(jsonOutput); } @@ -204,10 +204,12 @@ public class ToJsonBasicYangTypesTest { return lstItem; } - private String nextValue(JsonReader jReader) throws IOException { + private Object nextValue(JsonReader jReader) throws IOException { if (jReader.peek().equals(JsonToken.NULL)) { jReader.nextNull(); return null; + } else if (jReader.peek().equals(JsonToken.NUMBER)) { + return jReader.nextInt(); } else { return jReader.nextString(); } @@ -289,11 +291,11 @@ public class ToJsonBasicYangTypesTest { LstItem lst11_2 = null; LstItem lst11_3 = null; for (LstItem lstItem : lstItems) { - if (lstItem.getLfs().get("lf111").getValue().equals("1")) { + if (lstItem.getLfs().get("lf111").getValue().equals(1)) { lst11_1 = lstItem; - } else if (lstItem.getLfs().get("lf111").getValue().equals("2")) { + } else if (lstItem.getLfs().get("lf111").getValue().equals(2)) { lst11_2 = lstItem; - } else if (lstItem.getLfs().get("lf111").getValue().equals("3")) { + } else if (lstItem.getLfs().get("lf111").getValue().equals(3)) { lst11_3 = lstItem; } } @@ -307,8 +309,11 @@ public class ToJsonBasicYangTypesTest { assertEquals(1, lst11_1.getLfs().size()); assertEquals(1, lst11_1.getConts().size()); assertEquals(1, lst11_1.getLsts().size()); - assertEquals(lst11_1.getLsts().get("lst111"), new Lst("lst111").addLstItem(new LstItem().addLf("lf1111", "35")) - .addLstItem(new LstItem().addLf("lf1111", "34")).addLstItem(new LstItem()).addLstItem(new LstItem())); + assertEquals( + lst11_1.getLsts().get("lst111"), + new Lst("lst111").addLstItem(new LstItem().addLf("lf1111", (int) 35)) + .addLstItem(new LstItem().addLf("lf1111", (int) 34)).addLstItem(new LstItem()) + .addLstItem(new LstItem())); assertEquals(lst11_1.getConts().get("cont111"), new Cont("cont111")); // : lst11_1 @@ -327,9 +332,10 @@ public class ToJsonBasicYangTypesTest { assertEquals(1, lst11_2_cont111.getLsts().size()); assertTrue(lst11_2_cont111.getConts().isEmpty()); - assertEquals(new LfLst("lflst1111").addLf("1024").addLf("4096"), lst11_2_cont111.getLfLsts().get("lflst1111")); + assertEquals(new LfLst("lflst1111").addLf((int) 1024).addLf((int) 4096), + lst11_2_cont111.getLfLsts().get("lflst1111")); assertEquals( - new Lst("lst1111").addLstItem(new LstItem().addLf("lf1111B", "4")).addLstItem( + new Lst("lst1111").addLstItem(new LstItem().addLf("lf1111B", (int) 4)).addLstItem( new LstItem().addLf("lf1111A", "lf1111A str12")), lst11_2_cont111.getLsts().get("lst1111")); // :-cont111 assertEquals(lst11_2.getLsts().get("lst112"), new Lst("lst112").addLstItem(new LstItem())); @@ -369,9 +375,9 @@ public class ToJsonBasicYangTypesTest { assertNotNull(lflst12); assertEquals(3, lflst11.getLfs().size()); - assertTrue(lflst11.getLfs().contains(new Lf("55"))); - assertTrue(lflst11.getLfs().contains(new Lf("56"))); - assertTrue(lflst11.getLfs().contains(new Lf("57"))); + assertTrue(lflst11.getLfs().contains(new Lf(55))); + assertTrue(lflst11.getLfs().contains(new Lf(56))); + assertTrue(lflst11.getLfs().contains(new Lf(57))); assertEquals(3, lflst12.getLfs().size()); assertTrue(lflst12.getLfs().contains(new Lf("lflst12 str1"))); @@ -387,9 +393,9 @@ public class ToJsonBasicYangTypesTest { LstItem lst11_2 = null; for (LstItem lstItem : lst11.getLstItems()) { Lf lf = lstItem.getLfs().get("lf111"); - if (lf != null && lf.getValue().equals("140")) { + if (lf != null && lf.getValue().equals(140)) { lst11_1 = lstItem; - } else if (lf != null && lf.getValue().equals("141")) { + } else if (lf != null && lf.getValue().equals(141)) { lst11_2 = lstItem; } } @@ -414,15 +420,15 @@ public class ToJsonBasicYangTypesTest { // cont111 check assertEquals(new Lf("lf1111", "lf1111 str2"), lst11_2_cont.getLfs().get("lf1111")); - assertEquals(new LfLst("lflst1111").addLf(new Lf("2049")).addLf(new Lf("1025")).addLf(new Lf("4097")), - lst11_2_cont.getLfLsts().get("lflst1111")); + assertEquals(new LfLst("lflst1111").addLf(new Lf(2049)).addLf(new Lf(1025)).addLf(new Lf(4097)), lst11_2_cont + .getLfLsts().get("lflst1111")); assertNotNull(lst11_2_cont.getLsts().get("lst1111")); checkLst1111(lst11_2_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str21"), new Lf( - "lf1111B", "5"), new Lf("lf1111A", "lf1111A str22"), new Lf("lf1111B", "8")); + "lf1111B", 5), new Lf("lf1111A", "lf1111A str22"), new Lf("lf1111B", 8)); - checkLst11x(lst11_2.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "55")), - new LstItem().addLf(new Lf("lf1111", "56"))); + checkLst11x(lst11_2.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", 55)), + new LstItem().addLf(new Lf("lf1111", 56))); checkLst11x(lst11_2.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str22")), new LstItem().addLf(new Lf("lf1121", "lf1121 str21"))); } @@ -444,14 +450,14 @@ public class ToJsonBasicYangTypesTest { // cont111 check assertEquals(new Lf("lf1111", "lf1111 str"), lst11_1_cont.getLfs().get("lf1111")); - assertEquals(new LfLst("lflst1111").addLf(new Lf("2048")).addLf(new Lf("1024")).addLf(new Lf("4096")), - lst11_1_cont.getLfLsts().get("lflst1111")); + assertEquals(new LfLst("lflst1111").addLf(new Lf(2048)).addLf(new Lf(1024)).addLf(new Lf(4096)), lst11_1_cont + .getLfLsts().get("lflst1111")); assertNotNull(lst11_1_cont.getLsts().get("lst1111")); checkLst1111(lst11_1_cont.getLsts().get("lst1111").getLstItems(), new Lf("lf1111A", "lf1111A str11"), new Lf( - "lf1111B", "4"), new Lf("lf1111A", "lf1111A str12"), new Lf("lf1111B", "7")); + "lf1111B", 4), new Lf("lf1111A", "lf1111A str12"), new Lf("lf1111B", 7)); - checkLst11x(lst11_1.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", "65"))); + checkLst11x(lst11_1.getLsts().get("lst111"), new LstItem().addLf(new Lf("lf1111", 65))); checkLst11x(lst11_1.getLsts().get("lst112"), new LstItem().addLf(new Lf("lf1121", "lf1121 str11"))); } @@ -491,8 +497,8 @@ public class ToJsonBasicYangTypesTest { null, ModifyAction.CREATE, null); cont1.getChildren().add(lst11_1); - MutableSimpleNode lf111_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_1, "1", - ModifyAction.CREATE, null); + MutableSimpleNode lf111_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_1, + (short) 1, ModifyAction.CREATE, null); lst11_1.getChildren().add(lf111_1); // lst111_1_1 @@ -500,7 +506,7 @@ public class ToJsonBasicYangTypesTest { lst11_1, null, ModifyAction.CREATE, null); lst11_1.getChildren().add(lst111_1_1); MutableSimpleNode lf1111_1_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111"), - lst111_1_1, "34", ModifyAction.CREATE, null); + lst111_1_1, (int) 34, ModifyAction.CREATE, null); lst111_1_1.getChildren().add(lf1111_1_1); lst111_1_1.init(); // :lst111_1_1 @@ -510,7 +516,7 @@ public class ToJsonBasicYangTypesTest { lst11_1, null, ModifyAction.CREATE, null); lst11_1.getChildren().add(lst111_1_2); MutableSimpleNode lf1111_1_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111"), - lst111_1_2, "35", ModifyAction.CREATE, null); + lst111_1_2, (int) 35, ModifyAction.CREATE, null); lst111_1_2.getChildren().add(lf1111_1_2); lst111_1_2.init(); // :lst111_1_2 @@ -541,8 +547,8 @@ public class ToJsonBasicYangTypesTest { null, ModifyAction.CREATE, null); cont1.getChildren().add(lst11_2); - MutableSimpleNode lf111_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_2, "2", - ModifyAction.CREATE, null); + MutableSimpleNode lf111_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_2, + (short) 2, ModifyAction.CREATE, null); lst11_2.getChildren().add(lf111_2); // cont111_2 @@ -551,10 +557,10 @@ public class ToJsonBasicYangTypesTest { lst11_2.getChildren().add(cont111_2); MutableSimpleNode lflst1111_2_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lflst1111"), - cont111_2, "1024", ModifyAction.CREATE, null); + cont111_2, (int) 1024, ModifyAction.CREATE, null); cont111_2.getChildren().add(lflst1111_2_2); MutableSimpleNode lflst1111_2_3 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lflst1111"), - cont111_2, "4096", ModifyAction.CREATE, null); + cont111_2, (int) 4096, ModifyAction.CREATE, null); cont111_2.getChildren().add(lflst1111_2_3); // lst1111_2 @@ -562,16 +568,16 @@ public class ToJsonBasicYangTypesTest { cont111_2, null, ModifyAction.CREATE, null); cont111_2.getChildren().add(lst1111_2_1); MutableSimpleNode lf1111B_2_1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111B"), - lst1111_2_1, "4", ModifyAction.CREATE, null); + lst1111_2_1, (short) 4, ModifyAction.CREATE, null); lst1111_2_1.getChildren().add(lf1111B_2_1); lst1111_2_1.init(); MutableCompositeNode lst1111_2_2 = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("lst1111"), cont111_2, null, ModifyAction.CREATE, null); cont111_2.getChildren().add(lst1111_2_2); - MutableSimpleNode lf1111B_2_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111A"), + MutableSimpleNode lf1111A_2_2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1111A"), lst1111_2_2, "lf1111A str12", ModifyAction.CREATE, null); - lst1111_2_2.getChildren().add(lf1111B_2_2); + lst1111_2_2.getChildren().add(lf1111A_2_2); lst1111_2_2.init(); // :lst1111_2 @@ -591,8 +597,8 @@ public class ToJsonBasicYangTypesTest { null, ModifyAction.CREATE, null); cont1.getChildren().add(lst11_3); - MutableSimpleNode lf111_3 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_3, "3", - ModifyAction.CREATE, null); + MutableSimpleNode lf111_3 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf111"), lst11_3, + (short) 3, ModifyAction.CREATE, null); lst11_3.getChildren().add(lf111_3); // cont111_3 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/ToJsonChoiceCaseTest.java new file mode 100644 index 0000000000..b745411bf4 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonChoiceCaseTest.java @@ -0,0 +1,123 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.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.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 { + + @BeforeClass + public static void initialization() { + dataLoad("/cnsn-to-json/choice"); + } + + /** + * Test when some data are in one case node and other in another. This isn't + * correct. Next Json validator should return error because nodes has to be + * from one case below concrete choice. + * + */ + @Test + public void nodeSchemasOnVariousChoiceCasePathTest() { + testWrapper("/cnsn-to-json/choice/xml/data_various_path_err.xml", "choice-case-test:cont"); + } + + /** + * Test when some data are in one case node and other in another. + * Additionally data are loadef from various choices. This isn't correct. + * Next Json validator should return error because nodes has to be from one + * case below concrete choice. + * + */ + @Test + public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() { + testWrapper("/cnsn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml", + "choice-case-test:cont"); + } + + /** + * Test when second level data are red first, then first and at the end + * third level. Level represents pass through couple choice-case + */ + + @Test + public void nodeSchemasWithRandomOrderAccordingLevel() { + testWrapper("/cnsn-to-json/choice/xml/data_random_level.xml", "choice-case-test:cont"); + } + + /** + * Test when element from no first case is used + */ + @Test + public void nodeSchemasNotInFirstCase() { + testWrapper("/cnsn-to-json/choice/xml/data_no_first_case.xml", "choice-case-test:cont"); + } + + /** + * Test when element in case is list + */ + @Test + public void nodeSchemaAsList() { + testWrapper("/cnsn-to-json/choice/xml/data_list.xml", "choice-case-test:cont"); + } + + /** + * Test when element in case is container + */ + @Test + public void nodeSchemaAsContainer() { + testWrapper("/cnsn-to-json/choice/xml/data_container.xml", "choice-case-test:cont"); + } + + /** + * Test when element in case is leaflist + */ + @Test + public void nodeSchemaAsLeafList() { + testWrapper("/cnsn-to-json/choice/xml/data_leaflist.xml", "choice-case-test:cont"); + } + + /** + * + */ + @Test + public void nodeSchemasInMultipleChoicesTest() { + testWrapper("/cnsn-to-json/choice/xml/data_more_choices_same_level.xml", "choice-case-test:cont"); + } + + /** + * Test whether is possible to find data schema for node which is specified + * as dirrect subnode of choice (case without CASE key word) + */ + @Test + public void nodeSchemasInCaseNotDefinedWithCaseKeyword() { + testWrapper("/cnsn-to-json/choice/xml/data_case_defined_without_case.xml", "choice-case-test:cont"); + } + + /** + * Test of multiple use of choices + */ + @Test + public void nodeSchemasInThreeChoicesAtSameLevel() { + testWrapper("/cnsn-to-json/choice/xml/data_three_choices_same_level.xml", "choice-case-test:cont"); + } + + private void testWrapper(String xmlPath, String pathToSchemaNode) { + CompositeNode compNode = TestUtils.loadCompositeNode(xmlPath); + TestUtils.normalizeCompositeNode(compNode, modules, dataSchemaNode, pathToSchemaNode); + try { + TestUtils.writeCompNodeWithSchemaContextToJson(compNode, modules, dataSchemaNode); + } 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/test/ToJsonIdentityrefTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java similarity index 85% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java index 0ec2dd4f5e..6d30559ccd 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonIdentityrefTest.java @@ -1,4 +1,4 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -9,7 +9,8 @@ import java.util.regex.Pattern; import javax.ws.rs.WebApplicationException; -import org.junit.*; +import org.junit.BeforeClass; +import org.junit.Test; 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.*; @@ -19,16 +20,14 @@ public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - dataLoad("/yang-to-json-conversion/identityref", 2, "identityref-module", "cont"); + dataLoad("/cnsn-to-json/identityref", 2, "identityref-module", "cont"); } - @Ignore @Test public void identityrefToJsonTest() { String json = null; try { - json = TestUtils - .writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), null, modules, dataSchemaNode); + json = TestUtils.writeCompNodeWithSchemaContextToJson(prepareCompositeNode(), modules, dataSchemaNode); } 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/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 similarity index 75% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonLeafrefType.java index b4b8b4b3e3..1ac81a332f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/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 @@ -1,4 +1,4 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -9,21 +9,30 @@ 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("/yang-to-json-conversion/leafref", 2, "main-module", "cont"); + dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont"); } + @Ignore @Test public void leafrefAbsolutePathToExistingLeafTest() { String json = null; try { - json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + 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); @@ -35,13 +44,14 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { assertTrue(mtch.matches()); } + @Ignore @Test public void leafrefRelativePathToExistingLeafTest() { String json = null; try { - json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + 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); @@ -57,13 +67,14 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { * 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("/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + 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); @@ -79,13 +90,14 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { * 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("/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + 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); @@ -101,6 +113,7 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { /** * Tests case when leaflist element is refers to leaf. */ + @Ignore @Test public void leafrefFromLeafListToLeafTest() { String json = null; @@ -108,8 +121,8 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { json = TestUtils .writeCompNodeWithSchemaContextToJson( TestUtils - .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + .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); @@ -126,13 +139,14 @@ public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { /** * Tests case when leaflist element is refers to leaf. */ + @Ignore @Test public void leafrefFromLeafrefToLeafrefTest() { String json = null; try { - json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml"), - "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode); + 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); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonWithAugmentTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java similarity index 66% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonWithAugmentTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java index 994916dea5..73bd178ff3 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonWithAugmentTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/ToJsonWithAugmentTest.java @@ -1,8 +1,10 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.opendaylight.controller.sal.restconf.impl.test.TestUtils; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; public class ToJsonWithAugmentTest { @@ -12,9 +14,10 @@ public class ToJsonWithAugmentTest { */ @Test public void augmentedElementsToJson() { - String jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/augmentation/xml/data.xml"), - "/yang-to-json-conversion/augmentation", "/yang-to-json-conversion/augmentation/xml", "yang", "cont"); + + 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"); 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/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 new file mode 100644 index 0000000000..d04337865a --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java @@ -0,0 +1,247 @@ +package org.opendaylight.controller.sal.restconf.impl.cnsn.to.xml.test; + +import static org.junit.Assert.*; + +import java.io.StringWriter; +import java.util.Set; + +import javax.activation.UnsupportedDataTypeException; +import javax.xml.transform.*; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.opendaylight.controller.sal.rest.impl.XmlMapper; +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.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.model.api.*; +import org.w3c.dom.Document; + +/** + * + * 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 CnSnToXmlTest { + + private static Set modules; + private static DataSchemaNode dataSchemaNode; + + @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); + + } + + @Test + public void snAsYangIdentityrefToXMLTest() { + serializeToXml(prepareIdentityrefData(), "x:iden"); + } + + @Test + public void snAsYangStringToXmlTest() { + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.STRING_DEFAULT_CODEC.deserialize("lfStr value"), + "lfStr"), "lfStr value"); + } + + @Test + public void snAsYangInt8ToXmlTest() { + String elName = "lfInt8"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.INT8_DEFAULT_CODEC.deserialize("14"), elName), "<" + + elName + ">14"); + } + + @Test + public void snAsYangInt16ToXmlTest() { + String elName = "lfInt16"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.INT16_DEFAULT_CODEC.deserialize("3000"), elName), + "<" + elName + ">3000"); + } + + @Test + public void snAsYangInt32ToXmlTest() { + String elName = "lfInt32"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.INT32_DEFAULT_CODEC.deserialize("201234"), elName), + "<" + elName + ">201234"); + } + + @Test + public void snAsYangInt64ToXmlTest() { + String elName = "lfInt64"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.INT64_DEFAULT_CODEC.deserialize("5123456789"), + elName), "<" + elName + ">5123456789"); + } + + @Test + public void snAsYangUint8ToXmlTest() { + String elName = "lfUint8"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT8_DEFAULT_CODEC.deserialize("200"), elName), + "<" + elName + ">200"); + } + + @Test + public void snAsYangUint16ToXmlTest() { + String elName = "lfUint16"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT16_DEFAULT_CODEC.deserialize("4000"), elName), + "<" + elName + ">4000"); + } + + @Test + public void snAsYangUint32ToXmlTest() { + String elName = "lfUint32"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT32_DEFAULT_CODEC.deserialize("4123456789"), + elName), "<" + elName + ">4123456789"); + } + + @Test + public void snAsYangUint64ToXmlTest() { + String elName = "lfUint64"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UINT64_DEFAULT_CODEC.deserialize("5123456789"), + elName), "<" + elName + ">5123456789"); + } + + @Test + public void snAsYangBinaryToXmlTest() { + String elName = "lfBinary"; + serializeToXml( + prepareCnStructForYangData( + TypeDefinitionAwareCodec.BINARY_DEFAULT_CODEC + .deserialize("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"), + elName), "<" + elName + ">ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567"); + } + + @Test + public void snAsYangBitsToXmlTest() { + String elName = "lfBits"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.BITS_DEFAULT_CODEC.deserialize("one two"), elName), + "<" + elName + ">one two", "<" + elName + ">two one"); + } + + @Test + public void snAsYangEnumerationToXmlTest() { + String elName = "lfEnumeration"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.ENUMERATION_DEFAULT_CODEC.deserialize("enum2"), + elName), "<" + elName + ">enum2"); + } + + @Test + public void snAsYangEmptyToXmlTest() { + String elName = "lfEmpty"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.EMPTY_DEFAULT_CODEC.deserialize(null), elName), "<" + + elName + "/>"); + } + + @Test + public void snAsYangBooleanToXmlTest() { + String elName = "lfBoolean"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("str"), elName), + "<" + elName + ">false"); + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.BOOLEAN_DEFAULT_CODEC.deserialize("true"), elName), + "<" + elName + ">true"); + } + + @Test + public void snAsYangUnionToXmlTest() { + String elName = "lfUnion"; + String int8 = "15"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(int8), elName), "<" + + elName + ">15"); + + String bits = "first second"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bits), elName), "<" + + elName + ">first second"); + + String bool = "str"; + serializeToXml( + prepareCnStructForYangData(TypeDefinitionAwareCodec.UNION_DEFAULT_CODEC.deserialize(bool), elName), "<" + + elName + ">str"); + } + + 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) { + } + } + assertNotNull(xmlMapper); + boolean containSearchedStr = false; + String strRepresentation = ""; + for (String searchedStr : xmlRepresentation) { + if (xmlString.contains(searchedStr)) { + containSearchedStr = true; + break; + } + strRepresentation = strRepresentation + "[" + searchedStr + "]"; + } + assertTrue("At least one of specified strings " + strRepresentation + " wasn't found.", containSearchedStr); + + } + + private CompositeNode prepareIdentityrefData() { + 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); + + MutableSimpleNode lf11 = NodeFactory.createMutableSimpleNode( + TestUtils.buildQName("lf11", "basic:module", "2013-12-2"), cont1, + TestUtils.buildQName("iden", "referenced:module", "2013-12-2"), ModifyAction.CREATE, null); + cont1.getChildren().add(lf11); + cont1.init(); + cont.init(); + + return cont; + } + + private CompositeNode prepareCnStructForYangData(Object data, String leafName) { + MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, + ModifyAction.CREATE, null); + + MutableSimpleNode lf1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName(leafName), 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/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 similarity index 97% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JsonToCnSnTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java index 546becba1f..b02ea9a3a2 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/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 @@ -1,4 +1,4 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; import static org.junit.Assert.*; @@ -11,7 +11,7 @@ import javax.ws.rs.WebApplicationException; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.*; +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.*; @@ -220,12 +220,7 @@ public class JsonToCnSnTest { DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); assertNotNull(dataSchemaNode); - RestconfImpl restconf = RestconfImpl.getInstance(); - ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); - - TestUtils.prepareMockForRestconfBeforeNormalization(modules, dataSchemaNode, restconf); - - restconf.createConfigurationData("identityref-module:cont", compositeNode); + TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, "identityref-module:cont"); assertEquals("cont", compositeNode.getNodeType().getLocalName()); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlTest.java deleted file mode 100644 index d8261a6253..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlTest.java +++ /dev/null @@ -1,89 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.StringWriter; -import java.util.Set; - -import javax.activation.UnsupportedDataTypeException; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.stream.StreamResult; - -import org.junit.BeforeClass; -import org.junit.Test; -import org.opendaylight.controller.sal.rest.impl.XmlMapper; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.ModifyAction; -import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; -import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode; -import org.opendaylight.yangtools.yang.data.impl.NodeFactory; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.opendaylight.yangtools.yang.model.api.Module; -import org.w3c.dom.Document; - -public class CnSnToXmlTest { - - private static Set modules; - private static DataSchemaNode dataSchemaNode; - - @BeforeClass - public static void initialization() { - modules = TestUtils.resolveModules("/cnsn-to-xml/identityref"); - assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("identityref-module", modules); - assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont"); - assertNotNull(dataSchemaNode); - - } - - @Test - public void compositeNodeToXMLTest() { - XmlMapper xmlMapper = new XmlMapper(); - String xmlString = null; - if (dataSchemaNode instanceof DataNodeContainer) { - try { - Document doc = xmlMapper.write(prepareData(), (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) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - assertNotNull(xmlString); - assertTrue(xmlString.contains("x:iden")); - - } - - private CompositeNode prepareData() { - MutableCompositeNode cont = NodeFactory.createMutableCompositeNode( - TestUtils.buildQName("cont", "identityref:module", "2013-12-2"), null, null, ModifyAction.CREATE, null); - MutableCompositeNode cont1 = NodeFactory - .createMutableCompositeNode(TestUtils.buildQName("cont1", "identityref:module", "2013-12-2"), cont, - null, ModifyAction.CREATE, null); - cont.getChildren().add(cont1); - - MutableSimpleNode lf11 = NodeFactory.createMutableSimpleNode( - TestUtils.buildQName("lf1", "identityref:module", "2013-12-2"), cont1, - TestUtils.buildQName("iden", "identity:module", "2013-12-2"), ModifyAction.CREATE, null); - cont1.getChildren().add(lf11); - cont1.init(); - cont.init(); - - return cont; - } - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java deleted file mode 100644 index f55e92f9c9..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromJsonToCompositeNodeTest.java +++ /dev/null @@ -1,350 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.net.URISyntaxException; -import java.util.List; - -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.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.DataSchemaNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.gson.JsonSyntaxException; - -public class FromJsonToCompositeNodeTest { - - private static final Logger LOG = LoggerFactory.getLogger(FromJsonToCompositeNodeTest.class); - - @Test - public void simpleListTest() { - simpleTest("/json-to-composite-node/simple-list.json", "/json-to-composite-node/simple-list-yang", "lst", - "simple:list:yang1", "simple-list-yang1"); - } - - @Test - public void simpleContainerTest() { - simpleTest("/json-to-composite-node/simple-container.json", "/json-to-composite-node/simple-container-yang", - "cont", "simple:container:yang", "simple-container-yang"); - } - - /** - * test if for every leaf list item is simple node instance created - */ - @Test - public void multipleItemsInLeafList() { - CompositeNode compositeNode = compositeContainerFromJson( - "/json-to-composite-node/multiple-leaflist-items.json", true); - assertNotNull(compositeNode); - assertEquals(3, compositeNode.getChildren().size()); - - boolean lflst1_1 = false; - boolean lflst1_2 = false; - boolean lflst1_3 = false; - - for (Node node : compositeNode.getChildren()) { - assertEquals("lflst1", node.getNodeType().getLocalName()); - assertTrue(node instanceof SimpleNode); - SimpleNode simpleNode = (SimpleNode) node; - if (simpleNode.getValue().equals("45")) { - lflst1_1 = true; - } else if (simpleNode.getValue().equals("55")) { - lflst1_2 = true; - } else if (simpleNode.getValue().equals("66")) { - lflst1_3 = true; - } - } - - assertTrue(lflst1_1); - assertTrue(lflst1_2); - assertTrue(lflst1_3); - - } - - /** - * List contains 4 items and in every item are other elements. It is - * supposed that there should be: lf11, lflst11, cont11, lst11 - */ - @Test - public void multipleItemsInListTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/multiple-items-in-list.json", - true); - assertNotNull(compositeNode); - - assertEquals("lst", compositeNode.getNodeType().getLocalName()); - - verityMultipleItemsInList(compositeNode); - } - - @Test - public void nullArrayToSimpleNodeWithNullValueTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/array-with-null.json", true); - assertNotNull(compositeNode); - assertEquals("cont", compositeNode.getNodeType().getLocalName()); - - assertNotNull(compositeNode.getChildren()); - assertEquals(1, compositeNode.getChildren().size()); - Node lfNode = compositeNode.getChildren().iterator().next(); - - assertTrue(lfNode instanceof SimpleNode); - assertEquals(null, ((SimpleNode) lfNode).getValue()); - - } - - @Test - public void incorrectTopLevelElementsTest() { - Throwable cause1 = null; - try { - compositeContainerFromJson("/json-to-composite-node/wrong-top-level1.json", true); - } catch (WebApplicationException e) { - cause1 = e; - } - - assertNotNull(cause1); - assertTrue(cause1 - .getCause() - .getMessage() - .contains( - "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); - - Throwable cause2 = null; - try { - compositeContainerFromJson("/json-to-composite-node/wrong-top-level2.json", true); - } catch (WebApplicationException e) { - cause2 = e; - } - assertNotNull(cause2); - assertTrue(cause2.getCause().getMessage().contains("Json Object should contain one element")); - - Throwable cause3 = null; - try { - compositeContainerFromJson("/json-to-composite-node/wrong-top-level3.json", true); - } catch (WebApplicationException e) { - cause3 = e; - } - assertNotNull(cause3); - assertTrue(cause3 - .getCause() - .getMessage() - .contains( - "First element in Json Object has to be \"Object\" or \"Array with one Object element\". Other scenarios are not supported yet.")); - - } - - /** - * if leaf list with no data is in json then no corresponding data is - * created in composite node. if leaf with no data then exception is raised - */ - @Test - public void emptyDataReadTest() { - CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/empty-data.json", true); - - assertNotNull(compositeNode); - - assertEquals("cont", compositeNode.getNodeType().getLocalName()); - assertTrue(compositeNode instanceof CompositeNode); - List> children = ((CompositeNode) compositeNode).getChildren(); - assertEquals(1, children.size()); - assertEquals("lflst2", children.get(0).getNodeType().getLocalName()); - assertEquals("45", children.get(0).getValue()); - - String reason = null; - try { - compositeContainerFromJson("/json-to-composite-node/empty-data1.json", true); - } catch (JsonSyntaxException e) { - reason = e.getMessage(); - } - - assertTrue(reason.contains("Expected value at line")); - - } - - /** - * Tests whether namespace stay unchanged if concrete values are - * present in composite or simple node and if the method for update is - * called. - * - */ - @Test - public void notSupplyNamespaceIfAlreadySupplied() { - - CompositeNode compositeNode = compositeContainerFromJson("/json-to-composite-node/simple-list.json"); - assertNotNull(compositeNode); - - DataSchemaNode dataSchemaNode1 = null; - DataSchemaNode dataSchemaNode2 = null; - try { - dataSchemaNode1 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang", - "simple-list-yang1"); - dataSchemaNode2 = TestUtils.obtainSchemaFromYang("/json-to-composite-node/simple-list-yang", - "simple-list-yang2"); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } - assertNotNull(dataSchemaNode1); - assertNotNull(dataSchemaNode2); - - // supplement namespaces according to first data schema - - // "simple:data:types1" - TestUtils.supplementNamespace(dataSchemaNode1, compositeNode); - - assertTrue(compositeNode instanceof CompositeNodeWrapper); - CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(); - - 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"); - - } - - private void simpleTest(String jsonPath, String yangPath, String topLevelElementName, String namespace, - String moduleName) { - CompositeNode compositeNode = compositeContainerFromJson(jsonPath); - assertNotNull(compositeNode); - - DataSchemaNode dataSchemaNode = null; - try { - dataSchemaNode = TestUtils.obtainSchemaFromYang(yangPath, moduleName); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } - assertNotNull(dataSchemaNode); - - TestUtils.supplementNamespace(dataSchemaNode, compositeNode); - - assertTrue(compositeNode instanceof CompositeNodeWrapper); - CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(); - - assertEquals(topLevelElementName, compNode.getNodeType().getLocalName()); - verifyCompositeNode(compNode, namespace); - } - - private void verityMultipleItemsInList(CompositeNode compositeNode) { - List> childrenNodes = compositeNode.getChildren(); - assertEquals(4, childrenNodes.size()); - boolean lf11Found = false; - boolean cont11Found = false; - boolean lst11Found = false; - for (Node lst1Item : childrenNodes) { - assertEquals("lst1", lst1Item.getNodeType().getLocalName()); - assertTrue(lst1Item instanceof CompositeNode); - - List> childrenLst1 = ((CompositeNode) lst1Item).getChildren(); - assertEquals(1, childrenLst1.size()); - String localName = childrenLst1.get(0).getNodeType().getLocalName(); - if (localName.equals("lf11")) { - assertTrue(childrenLst1.get(0) instanceof SimpleNode); - lf11Found = true; - } else if (localName.equals("lflst11")) { - assertTrue(childrenLst1.get(0) instanceof SimpleNode); - assertEquals("45", ((SimpleNode) childrenLst1.get(0)).getValue()); - lf11Found = true; - } else if (localName.equals("cont11")) { - assertTrue(childrenLst1.get(0) instanceof CompositeNode); - cont11Found = true; - } else if (localName.equals("lst11")) { - lst11Found = true; - assertTrue(childrenLst1.get(0) instanceof CompositeNode); - assertEquals(0, ((CompositeNode) childrenLst1.get(0)).getChildren().size()); - } - - } - assertTrue(lf11Found); - assertTrue(cont11Found); - assertTrue(lst11Found); - } - - private void verifyCompositeNode(CompositeNode compositeNode, String namespace) { - boolean cont1Found = false; - boolean lst1Found = false; - boolean lflst1_1Found = false; - boolean lflst1_2Found = false; - boolean lf1Found = false; - - assertEquals(namespace, compositeNode.getNodeType().getNamespace().toString()); - - for (Node node : compositeNode.getChildren()) { - if (node.getNodeType().getLocalName().equals("cont1")) { - if (node instanceof CompositeNode) { - cont1Found = true; - assertEquals(0, ((CompositeNode) node).getChildren().size()); - } - } else if (node.getNodeType().getLocalName().equals("lst1")) { - if (node instanceof CompositeNode) { - lst1Found = true; - assertEquals(0, ((CompositeNode) node).getChildren().size()); - } - } else if (node.getNodeType().getLocalName().equals("lflst1")) { - if (node instanceof SimpleNode) { - if (((SimpleNode) node).getValue().equals("lflst1_1")) { - lflst1_1Found = true; - } else if (((SimpleNode) node).getValue().equals("lflst1_2")) { - lflst1_2Found = true; - } - } - - } else if (node.getNodeType().getLocalName().equals("lf1")) { - if (node instanceof SimpleNode) { - if (((SimpleNode) node).getValue().equals("lf1")) { - lf1Found = true; - } - } - } - assertEquals(namespace, node.getNodeType().getNamespace().toString()); - } - assertTrue(cont1Found); - assertTrue(lst1Found); - assertTrue(lflst1_1Found); - assertTrue(lflst1_2Found); - 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 = FromJsonToCompositeNodeTest.class.getResourceAsStream(jsonPath); - 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); - } - return null; - } - -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java deleted file mode 100644 index a72bd60d30..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/FromXmlToCompositeNodeTest.java +++ /dev/null @@ -1,280 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import java.io.*; -import java.net.URISyntaxException; - -import javax.ws.rs.WebApplicationException; - -import org.junit.*; -import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.*; -import org.opendaylight.yangtools.yang.data.api.*; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; -import org.slf4j.*; - -public class FromXmlToCompositeNodeTest { - private static final Logger LOG = LoggerFactory.getLogger(FromXmlToCompositeNodeTest.class); - - /** - * top level element represents container. second level element is list with - * two elements. - */ - @Test - public void testXmlDataContainer() { - CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-container.xml", false); - assertNotNull(compNode); - DataSchemaNode dataSchemaNode = null; - try { - dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/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()); - } - - assertEquals("", lf2.getValue()); - assertEquals(null, lf3.getValue()); - } - - @Test - public void testXmlDataList() { - CompositeNode compNode = compositeContainerFromXml("/xml-to-composite-node/data-list.xml", false); - assertNotNull(compNode); - - DataSchemaNode dataSchemaNode = null; - try { - dataSchemaNode = TestUtils.obtainSchemaFromYang("/xml-to-composite-node/data-list-yang", - "data-container-yang"); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - } - 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 = compositeContainerFromXml("/xml-to-composite-node/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()); - - } - - 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 compositeContainerFromXml(String xmlPath, boolean dummyNamespaces) { - XmlToCompositeNodeProvider xmlToCompositeNodeProvider = XmlToCompositeNodeProvider.INSTANCE; - try { - InputStream xmlStream = FromXmlToCompositeNodeTest.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; - } - -} 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 bc77430aea..cac77eb368 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 @@ -6,17 +6,14 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.FileNotFoundException; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLEncoder; import java.util.List; import java.util.Set; -import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.LogRecord; -import javax.ws.rs.client.Entity; import javax.ws.rs.core.Application; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -27,13 +24,11 @@ import org.glassfish.jersey.test.TestProperties; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.md.sal.common.api.TransactionStatus; 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.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.Module; @@ -74,8 +69,7 @@ public class ReadConfAndOperDataTest extends JerseyTest { String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml"); when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); @@ -92,8 +86,7 @@ public class ReadConfAndOperDataTest extends JerseyTest { public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException { String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0"); - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml"); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); 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 c36de6e4ea..41cc0ddb51 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 @@ -1,7 +1,9 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.FileNotFoundException; import java.io.InputStream; @@ -9,9 +11,7 @@ import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; -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; @@ -23,7 +23,8 @@ public class RestconfImplTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath()); + Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs") + .getPath()); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); ControllerContext controllerContext = ControllerContext.getInstance(); controllerContext.setSchemas(schemaContext); @@ -33,7 +34,7 @@ public class RestconfImplTest { @Test public void testExample() throws FileNotFoundException { InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream); + CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder(xmlStream); 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 309fb6441a..366d99dbcb 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,13 +1,13 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.*; -import java.net.*; +import java.net.URI; +import java.net.URISyntaxException; import java.sql.Date; import java.util.*; import java.util.concurrent.Future; @@ -20,21 +20,23 @@ 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.StructuredDataToJsonProvider; +import org.opendaylight.controller.sal.rest.impl.*; import org.opendaylight.controller.sal.restconf.impl.*; -import org.opendaylight.yangtools.yang.common.*; +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.model.api.*; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; -import org.slf4j.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xml.sax.SAXException; import com.google.common.base.Preconditions; -final class TestUtils { +public final class TestUtils { private static final Logger logger = LoggerFactory.getLogger(TestUtils.class); @@ -75,27 +77,7 @@ final class TestUtils { return result; } - public static CompositeNode loadCompositeNode(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; - } + public static Document loadDocumentFrom(InputStream inputStream) { try { @@ -131,18 +113,17 @@ final class TestUtils { } - static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, String outputPath) { - return convertCompositeNodeDataAndYangToJson(compositeNode, yangPath, outputPath, null, null); - } - - static String convertCompositeNodeDataAndYangToJson(CompositeNode compositeNode, String yangPath, + 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, outputPath, modules, dataSchemaNode); + return writeCompNodeWithSchemaContextToJson(compositeNode, modules, dataSchemaNode); } catch (WebApplicationException | IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -151,7 +132,16 @@ final class TestUtils { } - static Module resolveModule(String searchedModuleName, Set modules) { + public static void normalizeCompositeNode(CompositeNode compositeNode, Set modules, + DataSchemaNode dataSchemaNode, String schemaNodePath) { + RestconfImpl restconf = RestconfImpl.getInstance(); + ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); + + TestUtils.prepareMockForRestconfBeforeNormalization(modules, dataSchemaNode, 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) { @@ -167,11 +157,11 @@ final class TestUtils { return module; } - static Set resolveModules(String yangPath) { + public static Set resolveModules(String yangPath) { Set modules = null; try { - modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangPath).getPath()); + modules = TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); } catch (FileNotFoundException e) { e.printStackTrace(); } @@ -179,7 +169,7 @@ final class TestUtils { return modules; } - static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) { + public static DataSchemaNode resolveDataSchemaNode(Module module, String searchedDataSchemaName) { assertNotNull("Module is missing", module); DataSchemaNode dataSchemaNode = null; @@ -195,39 +185,33 @@ final class TestUtils { return dataSchemaNode; } - static String writeCompNodeWithSchemaContextToJson(CompositeNode compositeNode, String outputPath, - Set modules, DataSchemaNode dataSchemaNode) throws IOException, WebApplicationException { + 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 contContext = ControllerContext.getInstance(); - contContext.setSchemas(loadSchemaContext(modules)); + ControllerContext.getInstance().setSchemas(loadSchemaContext(modules)); StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, null, byteArrayOS); jsonResult = byteArrayOS.toString(); - if (outputPath != null) { - try { - outputToFile(byteArrayOS, outputPath); - } catch (IOException e) { - System.out.println("Output file wasn't cloased sucessfuly."); - } - } return jsonResult; } - static CompositeNode loadCompositeNode(String xmlDataPath) { - InputStream xmlStream = ToJsonBasicDataTypesTest.class.getResourceAsStream(xmlDataPath); + public static CompositeNode loadCompositeNode(String xmlDataPath) { + InputStream xmlStream = TestUtils.class.getResourceAsStream(xmlDataPath); CompositeNode compositeNode = null; try { - compositeNode = TestUtils.loadCompositeNode(xmlStream); - } catch (FileNotFoundException e) { + XmlReader xmlReader = new XmlReader(); + compositeNode = xmlReader.read(xmlStream); + + } catch (UnsupportedFormatException | XMLStreamException e) { e.printStackTrace(); } return compositeNode; @@ -236,7 +220,7 @@ final class TestUtils { static void outputToFile(ByteArrayOutputStream outputStream, String outputDir) throws IOException { FileOutputStream fileOS = null; try { - String path = ToJsonBasicDataTypesTest.class.getResource(outputDir).getPath(); + String path = TestUtils.class.getResource(outputDir).getPath(); File outFile = new File(path + "/data.json"); fileOS = new FileOutputStream(outFile); try { @@ -285,7 +269,7 @@ final class TestUtils { } private static FileReader getFileReader(String path) { - String fullPath = ToJsonBasicDataTypesTest.class.getResource(path).getPath(); + 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); @@ -317,7 +301,7 @@ final class TestUtils { return strBuilder.toString(); } - static QName buildQName(String name, String uri, String date) { + public static QName buildQName(String name, String uri, String date) { try { URI u = new URI(uri); Date dt = null; @@ -330,11 +314,11 @@ final class TestUtils { } } - static QName buildQName(String name) { + public static QName buildQName(String name) { return buildQName(name, "", null); } - static void supplementNamespace(DataSchemaNode dataSchemaNode, CompositeNode compositeNode) { + public static void supplementNamespace(DataSchemaNode dataSchemaNode, CompositeNode compositeNode) { RestconfImpl restconf = RestconfImpl.getInstance(); InstanceIdWithSchemaNode instIdAndSchema = new InstanceIdWithSchemaNode(mock(InstanceIdentifier.class), @@ -358,13 +342,14 @@ final class TestUtils { restconf.createConfigurationData("something", compositeNode); } - static DataSchemaNode obtainSchemaFromYang(String yangFolder) throws FileNotFoundException { + public static DataSchemaNode obtainSchemaFromYang(String yangFolder) throws FileNotFoundException { return obtainSchemaFromYang(yangFolder, null); } - static DataSchemaNode obtainSchemaFromYang(String yangFolder, String moduleName) throws FileNotFoundException { + public static DataSchemaNode obtainSchemaFromYang(String yangFolder, String moduleName) + throws FileNotFoundException { Set modules = null; - modules = TestUtils.loadModules(ToJsonBasicDataTypesTest.class.getResource(yangFolder).getPath()); + modules = TestUtils.loadModules(TestUtils.class.getResource(yangFolder).getPath()); if (modules == null) { return null; @@ -403,7 +388,7 @@ final class TestUtils { } - static void addDummyNamespaceToAllNodes(NodeWrapper wrappedNode) throws URISyntaxException { + public static void addDummyNamespaceToAllNodes(NodeWrapper wrappedNode) throws URISyntaxException { wrappedNode.setNamespace(new URI("")); if (wrappedNode instanceof CompositeNodeWrapper) { for (NodeWrapper childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) { @@ -411,7 +396,7 @@ final class TestUtils { } } } - + public static void prepareMockForRestconfBeforeNormalization(Set modules, DataSchemaNode dataSchemaNode, RestconfImpl restconf) { ControllerContext instance = ControllerContext.getInstance(); @@ -426,5 +411,42 @@ final class TestUtils { .build()).build()); restconf.setBroker(mockedBrokerFacade); } + + static CompositeNode loadCompositeNodeWithXmlTreeBuilder(String xmlDataPath) { + InputStream xmlStream = TestUtils.class.getResourceAsStream(xmlDataPath); + CompositeNode compositeNode = null; + try { + compositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder(xmlStream); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + return compositeNode; + + + + } + + + 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/ToJsonBasicDataTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicDataTypesTest.java deleted file mode 100644 index d0f1805502..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonBasicDataTypesTest.java +++ /dev/null @@ -1,217 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.io.*; -import java.util.*; - -import org.junit.Test; - -import com.google.gson.stream.*; - -public class ToJsonBasicDataTypesTest { - - @Test - public void simpleYangDataTest() { - String jsonOutput; - jsonOutput = TestUtils.convertCompositeNodeDataAndYangToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/simple-data-types/xml/data.xml"), - "/yang-to-json-conversion/simple-data-types", "/yang-to-json-conversion/simple-data-types/xml"); - verifyJsonOutput(jsonOutput); - } - - private void verifyJsonOutput(String jsonOutput) { - StringReader strReader = new StringReader(jsonOutput); - JsonReader jReader = new JsonReader(strReader); - - String exception = null; - try { - jsonReadCont(jReader); - } catch (IOException e) { - exception = e.getMessage(); - } - - assertNull("Error during reading Json output: " + exception, exception); - } - - private void jsonReadCont(JsonReader jReader) throws IOException { - jReader.beginObject(); - assertNotNull("cont1 is missing.", jReader.hasNext()); - - // Cont dataFromJson = new Cont(jReader.nextName()); - jReader.nextName(); - jsonReadContElements(jReader); - - assertFalse("cont shouldn't have other element.", jReader.hasNext()); - jReader.endObject(); - // return dataFromJson; - } - - private void jsonReadContElements(JsonReader jReader) throws IOException { - jReader.beginObject(); - List loadedLfs = new ArrayList<>(); - boolean exceptForDecimal5Raised = false; - boolean enumChecked = false; - boolean bitsChecked = false; - boolean lfdecimal6Checked = false; - boolean lfdecimal4Checked = false; - boolean lfdecimal3Checked = false; - boolean lfdecimal2Checked = false; - boolean lfdecimal1Checked = false; - boolean lfbool1Checked = false; - boolean lfbool2Checked = false; - boolean lfstrChecked = false; - boolean lfbinaryChecked = false; - // boolean lfref1Checked = false; - boolean lfemptyChecked = false; - boolean lfstr1Checked = false; - - while (jReader.hasNext()) { - String keyName = jReader.nextName(); - JsonToken peek = null; - try { - peek = jReader.peek(); - } catch (IOException e) { - if (keyName.equals("lfdecimal5")) { - exceptForDecimal5Raised = true; - } else { - assertTrue("Key " + keyName + " has incorrect value for specifed type", false); - } - } - - if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - try { - jReader.nextLong(); - } catch (NumberFormatException e) { - assertTrue("Key " + keyName + " has incorrect value - " + e.getMessage(), false); - } - loadedLfs.add(keyName.substring(3)); - } else if (keyName.equals("lfstr")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - assertEquals("lfstr", jReader.nextString()); - lfstrChecked = true; - } else if (keyName.equals("lfstr1")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - assertEquals("", jReader.nextString()); - lfstr1Checked = true; - } else if (keyName.equals("lfbool1")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); - assertEquals(true, jReader.nextBoolean()); - lfbool1Checked = true; - } else if (keyName.equals("lfbool2")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); - assertEquals(false, jReader.nextBoolean()); - lfbool2Checked = true; - } else if (keyName.equals("lfbool3")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); - assertEquals(false, jReader.nextBoolean()); - } else if (keyName.equals("lfdecimal1")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - assertEquals(new Double(43.32), (Double) jReader.nextDouble()); - lfdecimal1Checked = true; - } else if (keyName.equals("lfdecimal2")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - assertEquals(new Double(-0.43), (Double) jReader.nextDouble()); - lfdecimal2Checked = true; - } else if (keyName.equals("lfdecimal3")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - assertEquals(new Double(43), (Double) jReader.nextDouble()); - lfdecimal3Checked = true; - } else if (keyName.equals("lfdecimal4")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - assertEquals(new Double(43E3), (Double) jReader.nextDouble()); - lfdecimal4Checked = true; - } else if (keyName.equals("lfdecimal6")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - assertEquals(new Double(33.12345), (Double) jReader.nextDouble()); - lfdecimal6Checked = true; - } else if (keyName.equals("lfenum")) { - assertEquals("enum3", jReader.nextString()); - enumChecked = true; - } else if (keyName.equals("lfbits")) { - assertEquals("bit3", jReader.nextString()); - bitsChecked = true; - } else if (keyName.equals("lfbinary")) { - assertEquals("AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", jReader.nextString()); - lfbinaryChecked = true; - } else if (keyName.equals("lfempty")) { - jReader.beginArray(); - jReader.nextNull(); - jReader.endArray(); - lfemptyChecked = true; - } else if (keyName.startsWith("lfunion")) { - checkLfUnion(jReader, keyName, peek); - } else { - assertTrue("Key " + keyName + " doesn't exists in yang file.", false); - } - - } - Collections.sort(loadedLfs); - String expectedLfsStr = "[int16Max, int16Min, int32Max, int32Min, int64Max, int64Min, int8Max, int8Min, uint16Max, uint32Max, uint8Max]"; - String actualLfsStr = loadedLfs.toString(); - assertEquals("Some leaves are missing", expectedLfsStr, actualLfsStr); - // assertTrue("For lfdecimal5 wasn't catch error",exceptForDecimal5Raised); - assertTrue("Enum wasn't checked", enumChecked); - assertTrue("Bits wasn't checked", bitsChecked); - assertTrue("Decimal1 wasn't checked", lfdecimal1Checked); - assertTrue("Decimal2 wasn't checked", lfdecimal2Checked); - assertTrue("Decimal3 wasn't checked", lfdecimal3Checked); - assertTrue("Decimal4 wasn't checked", lfdecimal4Checked); - assertTrue("Decimal5 wasn't checked", lfdecimal6Checked); - assertTrue("lfbool1 wasn't checked", lfbool1Checked); - assertTrue("lfbool2 wasn't checked", lfbool2Checked); - assertTrue("lfstr wasn't checked", lfstrChecked); - assertTrue("lfstr1 wasn't checked", lfstr1Checked); - assertTrue("lfbinary wasn't checked", lfbinaryChecked); - assertTrue("lfempty wasn't checked", lfemptyChecked); - // assertTrue("lfref1 wasn't checked", lfref1Checked); - - jReader.endObject(); - - } - - private void checkLfUnion(JsonReader jReader, String keyName, JsonToken peek) throws IOException { - if (keyName.equals("lfunion1")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion2")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion3")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextInt(); - } else if (keyName.equals("lfunion4")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); - jReader.nextBoolean(); - } else if (keyName.equals("lfunion5")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion6")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion7")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion8")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion9")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion10")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.STRING, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion11")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.NUMBER, peek); - jReader.nextString(); - } else if (keyName.equals("lfunion12")) { - assertEquals("Key " + keyName + " has incorrect type", JsonToken.BOOLEAN, peek); - jReader.nextBoolean(); - } - } -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java deleted file mode 100644 index c5682cb5cc..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java +++ /dev/null @@ -1,181 +0,0 @@ -package org.opendaylight.controller.sal.restconf.impl.test; - -import static org.junit.Assert.assertTrue; - -import java.io.IOException; - -import javax.ws.rs.WebApplicationException; - -import org.junit.*; - -public class ToJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { - - @BeforeClass - public static void initialization() { - dataLoad("/yang-to-json-conversion/choice"); - } - - /** - * Test when some data are in one case node and other in another. This isn't - * correct. Next Json validator should return error because nodes has to be - * from one case below concrete choice. - * - */ - @Test - public void nodeSchemasOnVariousChoiceCasePathTest() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_various_path_err.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when some data are in one case node and other in another. - * Additionally data are loadef from various choices. This isn't correct. - * Next Json validator should return error because nodes has to be from one - * case below concrete choice. - * - */ - @Test - public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() { - try { - TestUtils - .writeCompNodeWithSchemaContextToJson( - TestUtils - .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level_various_paths_err.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when second level data are red first, then first and at the end - * third level. Level represents pass through couple choice-case - */ - - @Test - public void nodeSchemasWithRandomOrderAccordingLevel() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_random_level.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when element from no first case is used - */ - @Test - public void nodeSchemasNotInFirstCase() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_no_first_case.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when element in case is list - */ - @Test - public void nodeSchemaAsList() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_list.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when element in case is container - */ - @Test - public void nodeSchemaAsContainer() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_container.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test when element in case is leaflist - */ - @Test - public void nodeSchemaAsLeafList() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson( - TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_leaflist.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * - */ - @Test - public void nodeSchemasInMultipleChoicesTest() { - try { - TestUtils - .writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test whether is possible to find data schema for node which is specified - * as dirrect subnode of choice (case without CASE key word) - */ - @Test - public void nodeSchemasInCaseNotDefinedWithCaseKeyword() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_case_defined_without_case.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } catch (WebApplicationException | IOException e) { - // shouldn't end here - assertTrue(false); - } - } - - /** - * Test of multiple use of choices - */ - @Test - public void nodeSchemasInThreeChoicesAtSameLevel() { - try { - TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils - .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_three_choices_same_level.xml"), - "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode); - } 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/test/XmlProvidersTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java index ec7dba67b9..4cea120d4d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java @@ -11,8 +11,7 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.LogRecord; @@ -33,12 +32,10 @@ import org.opendaylight.controller.sal.rest.api.Draft01; import org.opendaylight.controller.sal.rest.api.RestconfService; 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.common.RpcResult; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.*; +import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -77,14 +74,31 @@ public class XmlProvidersTest extends JerseyTest { public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException { String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0"); - InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream); + CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData(); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE).get(); assertEquals(200, response.getStatus()); } + private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() { + CompositeNode intface; + try { + intface = new CompositeNodeWrapper(new URI("interface"), "interface"); + List> childs = new ArrayList<>(); + + childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0")); + childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd")); + childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE)); + childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface")); + intface.setValue(childs); + return intface; + } catch (URISyntaxException e) { + } + + return null; + } + @Test public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException { String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0"); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java index a1e06c3cab..afd40d1c2b 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/Lf.java @@ -1,21 +1,20 @@ package org.opendaylight.controller.sal.restconf.impl.test.structures; public class Lf extends YangElement { - private String value; + private Object value; private int numOfEqualItems = 0; - - public Lf(String name, String value) { + public Lf(String name, Object value) { super(name); this.value = value; } - public Lf(String value) { + public Lf(Object value) { super(""); this.value = value; } - public String getValue() { + public Object getValue() { return value; } @@ -43,11 +42,10 @@ public class Lf extends YangElement { } return true; } - + public void incNumOfEqualItems() { this.numOfEqualItems++; } - @Override public int hashCode() { diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java index 87fed95f6f..dda590944a 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LfLst.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test.structures; -import java.util.*; +import java.util.HashSet; +import java.util.Set; public class LfLst extends YangElement { Set lfs; @@ -10,11 +11,10 @@ public class LfLst extends YangElement { lfs = new HashSet<>(); } - public LfLst addLf(String value) { + public LfLst addLf(Object value) { return addLf(new Lf(value)); } - public LfLst addLf(Lf lf) { while (this.lfs.contains(lf)) { lf.incNumOfEqualItems(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java index 9eb58b5344..10582de083 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/structures/LstItem.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test.structures; -import java.util.*; +import java.util.HashMap; +import java.util.Map; public class LstItem { String lstName; @@ -42,7 +43,7 @@ public class LstItem { return this; } - public LstItem addLf(String name, String value) { + public LstItem addLf(String name, Object value) { lfs.put(name, new Lf(name, value)); return this; } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/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 similarity index 97% rename from opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlToCnSnTest.java rename to opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlToCnSnTest.java index 594433491e..50ab0857b7 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/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 @@ -1,4 +1,4 @@ -package org.opendaylight.controller.sal.restconf.impl.test; +package org.opendaylight.controller.sal.restconf.impl.xml.to.cnsn.test; import static org.junit.Assert.*; @@ -11,7 +11,7 @@ import javax.ws.rs.WebApplicationException; import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.*; +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.*; @@ -358,12 +358,7 @@ public class XmlToCnSnTest { DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); assertNotNull(dataSchemaNode); - RestconfImpl restconf = RestconfImpl.getInstance(); - ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); - - TestUtils.prepareMockForRestconfBeforeNormalization(modules, dataSchemaNode, restconf); - - restconf.createConfigurationData(moduleName + ":" + schemaName, compositeNode); + TestUtils.normalizeCompositeNode(compositeNode, modules, dataSchemaNode, moduleName + ":" + schemaName); SimpleNode lf11 = getLf11(compositeNode); assertTrue(lf11.getValue() instanceof QName); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-container.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-container.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-container.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-leaf.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-leaf.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-leaf.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-leaf.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-leaflist.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-leaflist.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-leaflist.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-leaflist.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-list.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-list.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/augment-list.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/augment-list.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/xml/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/xml/data.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/xml/data.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/xml/data.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/yang.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/yang.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/augmentation/yang.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/augmentation/yang.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/choice.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/choice.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/choice.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/choice.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_case_defined_without_case.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_case_defined_without_case.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_case_defined_without_case.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_case_defined_without_case.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_container.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_container.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_container.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_container.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_leaflist.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_leaflist.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_leaflist.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_leaflist.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_list.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_list.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_list.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_list.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_more_choices_same_level.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_more_choices_same_level.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_more_choices_same_level.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_more_choices_same_level.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_more_choices_same_level_various_paths_err.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_more_choices_same_level_various_paths_err.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_no_first_case.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_no_first_case.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_no_first_case.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_no_first_case.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_random_level.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_random_level.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_random_level.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_random_level.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_three_choices_same_level.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_three_choices_same_level.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_three_choices_same_level.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_three_choices_same_level.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_various_path_err.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_various_path_err.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/choice/xml/data_various_path_err.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/choice/xml/data_various_path_err.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/identityref/identity-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/identityref/identity-module.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/identityref/identity-module.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/identityref/identity-module.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/identityref/identityref-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/identityref/identityref-module.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/identityref/identityref-module.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/identityref/identityref-module.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/cont-augment-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/cont-augment-module.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/cont-augment-module.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/cont-augment-module.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/main-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/main-module.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/main-module.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/main-module.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/simple-data-types.yang similarity index 90% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/simple-data-types.yang index 759b3ecf71..9bdea81579 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/simple-data-types.yang +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/simple-data-types.yang @@ -5,6 +5,9 @@ module simple-data-types { revision 2013-11-12 { } + identity iden { + } + typedef tpdfempty { type empty; } @@ -245,6 +248,26 @@ module simple-data-types { 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; + } + } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/data.xml similarity index 80% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/data.xml index 93986647c3..56872a337d 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-data-types/xml/data.xml +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-data-types/xml/data.xml @@ -21,9 +21,9 @@ 43E3 33.12345 enum3 - bit3 - AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 - + bit3 bit2 + ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz + 324 33.3 55 @@ -36,4 +36,8 @@ bt1 33 false + 44 + 21 + + x:iden \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/simple-yang-types.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/simple-yang-types.yang similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/simple-yang-types.yang rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/simple-yang-types.yang diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/awaited_output_data.json similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_data.json rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/awaited_output_data.json diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_empty_data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/awaited_output_empty_data.json similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/awaited_output_empty_data.json rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/awaited_output_empty_data.json diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/data.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/data.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/data.xml diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/empty_data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/empty_data.xml similarity index 100% rename from opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/simple-yang-types/xml/empty_data.xml rename to opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-json/simple-yang-types/xml/empty_data.xml 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 new file mode 100644 index 0000000000..7023c94f2f --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/basic-module.yang @@ -0,0 +1,96 @@ +module basic-module { + namespace "basic:module"; + + prefix "basmod"; + + import referenced-module {prefix refmo; revision-date 2013-12-2;} + + revision 2013-12-2 { + } + + container cont { + container cont1 { + leaf lf11 { + type identityref { + base "refmo:iden"; + } + } + } + leaf lfStr { + type string; + } + leaf lfInt8 { + type int8; + } + + leaf lfInt16 { + type int16; + } + + leaf lfInt32 { + type int32; + } + + leaf lfInt64 { + type int64; + } + + leaf lfUint8 { + type uint8; + } + + leaf lfUint16 { + type uint16; + } + + leaf lfUint32 { + type uint32; + } + + leaf lfUint64 { + type uint64; + } + + leaf lfBinary { + type binary; + } + + leaf lfBits { + type bits { + bit one; + bit two; + bit three; + } + } + + leaf lfEnumeration { + type enumeration { + enum enum1; + enum enum2; + enum enum3; + } + } + + leaf lfEmpty { + type empty; + } + + leaf lfBoolean { + type boolean; + } + + leaf lfUnion { + type union { + type int8; + type string; + type bits { + bit first; + bit second; + } + type boolean; + } + } + + } + +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/referenced-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/referenced-module.yang new file mode 100644 index 0000000000..9821b1e169 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/resources/cnsn-to-xml/yang/referenced-module.yang @@ -0,0 +1,10 @@ +module referenced-module { + namespace "referenced:module"; + + prefix "refmod"; + revision 2013-12-2 { + } + + identity iden { + } +} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json deleted file mode 100644 index a19d9485f6..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/array-with-null.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "cont": { - "lf":[null] - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json deleted file mode 100644 index a6ad7f6272..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "cont": { - "lflst1":[], - "lflst2":[45] - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json deleted file mode 100644 index 10d964d644..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/empty-data1.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "cont": { - "lf": - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json deleted file mode 100644 index 8e882664cd..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-items-in-list.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "lst":[ - { - "lst1": [ - { - "lf11":"lf11_1" - }, - { - "lflst11":[ - 45 - ] - }, - { - "cont11":{ - } - }, - { - "lst11":[ - { - } - ] - } - ] - } - ] -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json deleted file mode 100644 index b61a8a8f2e..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/multiple-leaflist-items.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "cont": { - "lflst1":[45,55,66] - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang deleted file mode 100644 index 493101ced1..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container-yang/simple-container.yang +++ /dev/null @@ -1,20 +0,0 @@ -module simple-container-yang { - namespace "simple:container:yang"; - - prefix "smpdtp"; - revision 2013-11-12 { - } - - container cont { - container cont1 { - } - list lst1 { - } - leaf-list lflst1 { - type string; - } - leaf lf1 { - type string; - } - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json deleted file mode 100644 index 1be414991e..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-container.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "cont":{ - "cont1":{ - }, - "lst1": [ - { - } - ], - "lflst1":[ - "lflst1_1", - "lflst1_2" - ], - "lf1":"lf1" - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang deleted file mode 100644 index 0ce8ea428c..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list1.yang +++ /dev/null @@ -1,20 +0,0 @@ -module simple-list-yang1 { - namespace "simple:list:yang1"; - - prefix "smplstyg"; - revision 2013-11-12 { - } - - list lst { - container cont1 { - } - list lst1 { - } - leaf-list lflst1 { - type string; - } - leaf lf1 { - type string; - } - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang deleted file mode 100644 index 0872a4754d..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list-yang/simple-list2.yang +++ /dev/null @@ -1,20 +0,0 @@ -module simple-list-yang2 { - namespace "simple:list:yang2"; - - prefix "smplstyg"; - revision 2013-11-12 { - } - - list lst { - container cont1 { - } - list lst1 { - } - leaf-list lflst1 { - type string; - } - leaf lf1 { - type string; - } - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json deleted file mode 100644 index fee6baab1a..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/simple-list.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "lst":[ - { - "cont1":{ - }, - "lst1": [ - { - } - ], - "lflst1":[ - "lflst1_1", - "lflst1_2" - ], - "lf1":"lf1" - } - ] -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json deleted file mode 100644 index 3ae3a72f4d..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level1.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - - "lst":[ - { - }, - { - } - ] -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json deleted file mode 100644 index f0f5540cbf..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level2.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - - "cont": { - }, - "lst":[ - { - } - ] -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json b/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json deleted file mode 100644 index 7288969971..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/json-to-composite-node/wrong-top-level3.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "lf":"hello" -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang deleted file mode 100644 index b038eb193c..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container-yang/data-container.yang +++ /dev/null @@ -1,35 +0,0 @@ -module data-container-yang { - namespace "data:container:yang"; - - prefix "dtconyg"; - revision 2013-11-19 { - } - - container cont { - leaf lf1 { - type string; - } - - leaf lf2 { - type string; - } - - leaf lf3 { - type empty; - } - - leaf-list lflst1 { - type string; - } - list lst1 { - leaf lf11 { - type string; - } - } - container cont1 { - leaf lf11 { - type uint8; - } - } - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml deleted file mode 100644 index ce97dd1715..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-container.xml +++ /dev/null @@ -1,14 +0,0 @@ - - str0 - - - 121 - 131 - str1 - - str2 - - - 100 - - diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-container.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-container.yang deleted file mode 100644 index 3df34134f4..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-container.yang +++ /dev/null @@ -1,28 +0,0 @@ -module data-container-yang { - namespace "data:container:yang"; - - prefix "dtconyg"; - revision 2013-11-19 { - } - - container cont { - list lst1 { - leaf lf11 { - type string; - } - leaf-list lflst11 { - type string; - } - list lst11 { - leaf lf111 { - type string; - } - } - container cont11 { - leaf lf111 { - type uint8; - } - } - } - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-list.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-list.yang deleted file mode 100644 index 47e2a450e6..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list-yang/data-list.yang +++ /dev/null @@ -1,22 +0,0 @@ -module data-list-yang { - namespace "data:list:yang"; - - prefix "dtlstyg"; - - import data-container-yang { - prefix "dtconyg"; - revision-date 2013-11-19; - } - - - revision 2013-11-19 { - } - - - - augment "/dtconyg:cont" { - leaf lf1 { - type string; - } - } -} diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list.xml deleted file mode 100644 index cab23c656e..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/data-list.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - str0 - 121 - 131 - str1 - - str2 - - - 100 - - - - 221 - - 100 - - - lf1 - diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/empty-data.xml b/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/empty-data.xml deleted file mode 100644 index 162a556887..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/xml-to-composite-node/empty-data.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang deleted file mode 100644 index 30890bf9c3..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identity-module.yang +++ /dev/null @@ -1,10 +0,0 @@ -module identity-module { - namespace "identity:module"; - - prefix "idemod"; - revision 2013-12-2 { - } - - identity iden { - } -} \ No newline at end of file diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang b/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang deleted file mode 100644 index 273f5d77ee..0000000000 --- a/opendaylight/md-sal/sal-rest-connector/src/test/resources/yang-to-json-conversion/identityref/identityref-module.yang +++ /dev/null @@ -1,19 +0,0 @@ -module identityref-module { - namespace "identityref:module"; - - prefix "iderefmod"; - - import identity-module {prefix idemo; revision-date 2013-12-2;} - - revision 2013-12-2 { - } - - container cont { - leaf lf1 { - type identityref { - base "idemo:iden"; - } - } - } - -} \ No newline at end of file