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