From 86b407a7355a4931a59a9fa462c9c06248456727 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Fri, 20 Dec 2013 10:34:23 +0100 Subject: [PATCH] sal-rest-connector test refactoring Mainly deleting unnecessary methods. Change-Id: I994c19ee0a9ccc95491e85667fe7ae352f62864f Signed-off-by: Jozef Gloncak --- .../controller/sal/rest/impl/JsonMapper.java | 34 +++- .../controller/sal/rest/impl/XmlMapper.java | 48 +++++- .../json/test/CnSnJsonBasicYangTypesTest.java | 21 ++- .../to/json/test/CnSnJsonChoiceCaseTest.java | 3 +- .../test/CnSnToJsonBasicDataTypesTest.java | 129 ++------------ .../test/CnSnToJsonIncorrectTopLevelTest.java | 14 +- .../to/json/test/CnSnToJsonLeafrefType.java | 3 +- .../json/test/CnSnToJsonWithAugmentTest.java | 4 +- .../CnSnToXmlNotExistingLeafTypeTest.java | 9 +- .../impl/cnsn/to/xml/test/CnSnToXmlTest.java | 5 +- .../json/to/cnsn/test/JsonToCnSnTest.java | 35 ++-- .../impl/test/ControllerContextTest.java | 17 +- .../impl/test/InvokeRpcMethodTest.java | 24 ++- .../impl/test/ReadConfAndOperDataTest.java | 25 ++- .../restconf/impl/test/RestconfImplTest.java | 12 +- .../sal/restconf/impl/test/TestUtils.java | 162 ++++++------------ .../restconf/impl/test/XmlProvidersTest.java | 6 +- .../test/YangAndXmlAndDataSchemaLoader.java | 2 +- .../to/cnsn/test/XmlLeafrefToCnSnTest.java | 61 ++----- 19 files changed, 267 insertions(+), 347 deletions(-) diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java index 121f697827..8b1bdef8ba 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/JsonMapper.java @@ -4,16 +4,35 @@ import static com.google.common.base.Preconditions.checkNotNull; import java.io.IOException; import java.net.URI; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.activation.UnsupportedDataTypeException; -import org.opendaylight.controller.sal.restconf.impl.*; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO; import org.opendaylight.controller.sal.restconf.impl.IdentityValuesDTO.IdentityValue; +import org.opendaylight.controller.sal.restconf.impl.RestCodec; import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.data.api.*; -import org.opendaylight.yangtools.yang.model.api.*; -import org.opendaylight.yangtools.yang.model.api.type.*; +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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -165,6 +184,11 @@ class JsonMapper { TypeDefinition baseType = RestUtil.resolveBaseTypeFrom(type); + if (node.getValue() == null && !(baseType instanceof EmptyTypeDefinition)) { + logger.debug("While generationg JSON output null value was found for type " + + baseType.getClass().getSimpleName() + "."); + } + // TODO check InstanceIdentifierTypeDefinition if (baseType instanceof IdentityrefTypeDefinition) { if (node.getValue() instanceof QName) { diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java index 0e9bad046b..0dd4668a74 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/XmlMapper.java @@ -1,14 +1,60 @@ package org.opendaylight.controller.sal.rest.impl; import javax.activation.UnsupportedDataTypeException; + import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.data.api.codec.LeafrefCodec; +import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec; +import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlCodecProvider; import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.util.Leafref; import org.w3c.dom.Document; +import com.google.common.base.Optional; public class XmlMapper { + private static final LeafrefCodecImpl LEAFREF_DEFAULT_CODEC = new LeafrefCodecImpl( + Optional. absent()); + + private static class LeafrefCodecImpl extends TypeDefinitionAwareCodec implements + LeafrefCodec { + + protected LeafrefCodecImpl(Optional typeDef) { + super(typeDef, Object.class); + } + + @Override + public String serialize(Object data) { + return String.valueOf(data); + } + + @Override + public Object deserialize(String data) { + return data; + } + } + + private static class XmlCodecProviderImpl implements XmlCodecProvider { + @Override + public TypeDefinitionAwareCodec> codecFor(TypeDefinition baseType) { + TypeDefinitionAwareCodec> codec = TypeDefinitionAwareCodec + .from(baseType); + + if (codec == null) { + if (baseType instanceof Leafref) { + return LEAFREF_DEFAULT_CODEC; + } + } + return codec; + } + } + + private static final XmlCodecProvider XML_CODEC_PROVIDER_IMPL = new XmlCodecProviderImpl(); + public Document write(CompositeNode data, DataNodeContainer schema) throws UnsupportedDataTypeException { - return XmlDocumentUtils.toDocument(data, schema, XmlDocumentUtils.defaultValueCodecProvider()); + return XmlDocumentUtils.toDocument(data, schema, XML_CODEC_PROVIDER_IMPL); } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java index 3e3adb3efa..e754fdec5f 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonBasicYangTypesTest.java @@ -1,6 +1,10 @@ package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.StringReader; @@ -12,10 +16,18 @@ 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.rest.impl.XmlToCompositeNodeProvider; 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.controller.sal.restconf.impl.test.structures.Cont; +import org.opendaylight.controller.sal.restconf.impl.test.structures.Lf; +import org.opendaylight.controller.sal.restconf.impl.test.structures.LfLst; +import org.opendaylight.controller.sal.restconf.impl.test.structures.Lst; +import org.opendaylight.controller.sal.restconf.impl.test.structures.LstItem; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.data.api.ModifyAction; +import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; +import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import com.google.gson.stream.JsonReader; @@ -52,7 +64,8 @@ public class CnSnJsonBasicYangTypesTest extends YangAndXmlAndDataSchemaLoader { */ @Test public void xmlAndYangTypesWithJsonReaderTest() { - CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-yang-types/xml/data.xml"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-yang-types/xml/data.xml", + XmlToCompositeNodeProvider.INSTANCE); TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); String jsonOutput = null; try { diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java index 15122cfe3a..1eb3779730 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnJsonChoiceCaseTest.java @@ -9,6 +9,7 @@ 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.rest.impl.XmlToCompositeNodeProvider; 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; @@ -112,7 +113,7 @@ public class CnSnJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { } private void testWrapper(String xmlPath, String pathToSchemaNode) { - CompositeNode compNode = TestUtils.loadCompositeNode(xmlPath); + CompositeNode compNode = TestUtils.readInputToCnSn(xmlPath, XmlToCompositeNodeProvider.INSTANCE); TestUtils.normalizeCompositeNode(compNode, modules, pathToSchemaNode); try { TestUtils.writeCompNodeWithSchemaContextToOutput(compNode, modules, dataSchemaNode, diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java index f71e2eeaa6..c45c0b3288 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonBasicDataTypesTest.java @@ -1,25 +1,26 @@ package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import java.io.IOException; import java.io.StringReader; -import java.math.BigDecimal; -import java.net.URI; -import java.net.URISyntaxException; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; 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.rest.impl.XmlToCompositeNodeProvider; 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.data.api.CompositeNode; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; @@ -34,7 +35,8 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader @Test public void simpleYangDataTest() { - CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-data-types/xml/data.xml"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml", + XmlToCompositeNodeProvider.INSTANCE); String jsonOutput = null; @@ -50,105 +52,6 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader verifyJsonOutput(jsonOutput); } - private CompositeNode prepareData() { - MutableCompositeNode cont = NodeFactory.createMutableCompositeNode(TestUtils.buildQName("cont"), null, null, - ModifyAction.CREATE, null); - - List> childNodes = new ArrayList<>(); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Min"), cont, (byte) -128, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint8Max"), cont, (byte) 127, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Min"), cont, (short) -32768, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint16Max"), cont, (short) 32767, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Min"), cont, - (int) -2147483648, ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint32Max"), cont, (int) 2147483647, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Min"), cont, new Long( - "-9223372036854775807"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnint64Max"), cont, new Long( - "9223372036854775807"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint8Max"), cont, (short) 255, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint16Max"), cont, (int) 65535, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfnuint32Max"), cont, new Long( - "4294967295"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr"), cont, "lfstr", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfstr1"), cont, "", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool1"), cont, Boolean.TRUE, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbool2"), cont, Boolean.FALSE, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal1"), cont, new BigDecimal( - "43.32"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal2"), cont, new BigDecimal( - "-0.43"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal3"), cont, new BigDecimal( - "43"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal4"), cont, new BigDecimal( - "43E3"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfdecimal6"), cont, new BigDecimal( - "33.12345"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfenum"), cont, "enum3", - ModifyAction.CREATE, null)); - - HashSet bits = new HashSet(); - bits.add("bit3"); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbits"), cont, bits, - ModifyAction.CREATE, null)); - - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfbinary"), cont, DatatypeConverter - .parseBase64Binary("AAaacdabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfempty"), cont, null, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion1"), cont, (int) 324, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion2"), cont, new BigDecimal( - "33.3"), ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion3"), cont, "55", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion4"), cont, Boolean.TRUE, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion5"), cont, "true", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion6"), cont, "false", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion7"), cont, null, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion8"), cont, "", - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion9"), cont, "", - ModifyAction.CREATE, null)); - - HashSet bits2 = new HashSet(); - bits2.add("bt1"); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion10"), cont, bits2, - ModifyAction.CREATE, null)); - - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion11"), cont, (short) 33, - ModifyAction.CREATE, null)); - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lfunion12"), cont, Boolean.FALSE, - ModifyAction.CREATE, null)); - try { - childNodes.add(NodeFactory.createMutableSimpleNode(TestUtils.buildQName("identityref1"), cont, new QName( - new URI("simple:data:types"), "iden"), ModifyAction.CREATE, null)); - } catch (URISyntaxException e) { - } - - cont.getChildren().addAll(childNodes); - - cont.init(); - - return cont; - } - private void verifyJsonOutput(String jsonOutput) { StringReader strReader = new StringReader(jsonOutput); JsonReader jReader = new JsonReader(strReader); @@ -179,7 +82,6 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader private void jsonReadContElements(JsonReader jReader) throws IOException { jReader.beginObject(); List loadedLfs = new ArrayList<>(); - boolean exceptForDecimal5Raised = false; boolean enumChecked = false; boolean bitsChecked = false; boolean lfdecimal6Checked = false; @@ -191,7 +93,6 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader boolean lfbool2Checked = false; boolean lfstrChecked = false; boolean lfbinaryChecked = false; - // boolean lfref1Checked = false; boolean lfemptyChecked = false; boolean lfstr1Checked = false; boolean lfidentityrefChecked = false; @@ -202,11 +103,7 @@ public class CnSnToJsonBasicDataTypesTest extends YangAndXmlAndDataSchemaLoader try { peek = jReader.peek(); } catch (IOException e) { - if (keyName.equals("lfdecimal5")) { - exceptForDecimal5Raised = true; - } else { - assertTrue("Key " + keyName + " has incorrect value for specifed type", false); - } + assertTrue("Key " + keyName + " has incorrect value for specifed type", false); } if (keyName.startsWith("lfnint") || keyName.startsWith("lfnuint")) { diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java index 47653ae296..6b5776263c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonIncorrectTopLevelTest.java @@ -12,11 +12,21 @@ 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.rest.impl.XmlToCompositeNodeProvider; 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.opendaylight.yangtools.yang.model.api.ConstraintDefinition; +import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.GroupingDefinition; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.opendaylight.yangtools.yang.model.api.Status; +import org.opendaylight.yangtools.yang.model.api.TypeDefinition; +import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode; +import org.opendaylight.yangtools.yang.model.api.UsesNode; +import org.opendaylight.yangtools.yang.model.api.YangNode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -138,7 +148,7 @@ public class CnSnToJsonIncorrectTopLevelTest extends YangAndXmlAndDataSchemaLoad @Test public void incorrectTopLevelElementTest() { - CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/simple-data-types/xml/data.xml"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/simple-data-types/xml/data.xml", XmlToCompositeNodeProvider.INSTANCE); DataSchemaNode incorrectDataSchema = null; incorrectDataSchema = new IncorrectDataSchema(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java index 6a92ebdf1b..3215e81719 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonLeafrefType.java @@ -12,6 +12,7 @@ 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.rest.impl.XmlToCompositeNodeProvider; 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; @@ -90,7 +91,7 @@ public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { private String toJson(String xmlDataPath) { try { - CompositeNode compositeNode = TestUtils.loadCompositeNode(xmlDataPath); + CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlDataPath, XmlToCompositeNodeProvider.INSTANCE); TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode, StructuredDataToJsonProvider.INSTANCE); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java index 878ed39ecf..3c2325c0f3 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/json/test/CnSnToJsonWithAugmentTest.java @@ -10,6 +10,7 @@ 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.rest.impl.XmlToCompositeNodeProvider; 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; @@ -27,7 +28,8 @@ public class CnSnToJsonWithAugmentTest extends YangAndXmlAndDataSchemaLoader { */ @Test public void augmentedElementsToJson() { - CompositeNode compositeNode = TestUtils.loadCompositeNode("/cnsn-to-json/augmentation/xml/data.xml"); + CompositeNode compositeNode = TestUtils.readInputToCnSn("/cnsn-to-json/augmentation/xml/data.xml", + XmlToCompositeNodeProvider.INSTANCE); TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName); String jsonOutput = null; diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java index a2fc138b51..d779b5ce7b 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlNotExistingLeafTypeTest.java @@ -31,15 +31,16 @@ public class CnSnToXmlNotExistingLeafTypeTest { @Test public void incorrectTopLevelElementTest() { - String xmlOutput = null; + boolean nullPointerExceptionRaised = false; try { - xmlOutput = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(), + TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCompositeNode(), (Set) Collections.EMPTY_SET, prepareDataSchemaNode(), StructuredDataToXmlProvider.INSTANCE); } catch (WebApplicationException | IOException e) { LOG.error("WebApplicationException or IOException was raised"); + } catch (NullPointerException e) { + nullPointerExceptionRaised = true; } - assertNotNull(xmlOutput); - assertTrue(xmlOutput.contains("any value")); + assertTrue(nullPointerExceptionRaised); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java index 1079d843f0..96e03a5a3c 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/cnsn/to/xml/test/CnSnToXmlTest.java @@ -8,7 +8,8 @@ import java.io.IOException; import javax.ws.rs.WebApplicationException; import javax.xml.transform.TransformerFactoryConfigurationError; -import org.junit.*; +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; @@ -40,13 +41,11 @@ public class CnSnToXmlTest extends YangAndXmlAndDataSchemaLoader { "prefix:iden"); } - @Ignore @Test public void snAsYangIdentityrefWithPrefixToXMLTest() { serializeToXml(prepareIdentityrefData("prefix", false), "no qname value"); } - @Ignore @Test public void snAsYangLeafrefWithPrefixToXMLTest() { serializeToXml(prepareLeafrefData(), "true", "true"); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java index e7421a1776..f2c0c29bc4 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/cnsn/test/JsonToCnSnTest.java @@ -1,9 +1,12 @@ package org.opendaylight.controller.sal.restconf.impl.json.to.cnsn.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; -import java.io.FileNotFoundException; -import java.util.*; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import javax.ws.rs.WebApplicationException; @@ -13,8 +16,9 @@ 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.*; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +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.Module; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -190,11 +194,8 @@ public class JsonToCnSnTest { // "simple:data:types1" Set modules1 = new HashSet<>(); Set modules2 = new HashSet<>(); - try { - 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) { - } + modules1 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/1"); + modules2 = TestUtils.loadModulesFrom("/json-to-cnsn/simple-list-yang/2"); assertNotNull(modules1); assertNotNull(modules2); @@ -223,12 +224,8 @@ public class JsonToCnSnTest { JsonToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - Set modules = TestUtils.resolveModulesFrom("/json-to-cnsn/identityref"); + Set modules = TestUtils.loadModulesFrom("/json-to-cnsn/identityref"); assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("identityref-module", modules); - assertNotNull(module); - DataSchemaNode dataSchemaNode = TestUtils.resolveDataSchemaNode(null, module); - assertNotNull(dataSchemaNode); TestUtils.normalizeCompositeNode(compositeNode, modules, "identityref-module:cont"); @@ -282,16 +279,10 @@ public class JsonToCnSnTest { assertNotNull(compositeNode); Set modules = null; - try { - modules = TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } + modules = TestUtils.loadModulesFrom(yangPath); assertNotNull(modules); TestUtils.normalizeCompositeNode(compositeNode, modules, moduleName + ":" + topLevelElementName); - // TestUtils.supplementNamespace(dataSchemaNode, compositeNode); assertTrue(compositeNode instanceof CompositeNodeWrapper); CompositeNode compNode = ((CompositeNodeWrapper) compositeNode).unwrap(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ControllerContextTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ControllerContextTest.java index 08ec3f2b0c..c68fcb9071 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ControllerContextTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ControllerContextTest.java @@ -1,21 +1,22 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.FileNotFoundException; import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mock; - -import static org.mockito.Mockito.*; - import org.opendaylight.controller.sal.core.api.mount.MountInstance; import org.opendaylight.controller.sal.core.api.mount.MountService; import org.opendaylight.controller.sal.restconf.impl.ControllerContext; import org.opendaylight.controller.sal.restconf.impl.InstanceIdWithSchemaNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -26,8 +27,8 @@ public class ControllerContextTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(ControllerContextTest.class.getResource("/full-versions/yangs") - .getPath()); + Set allModules = TestUtils.loadModulesFrom("/full-versions/yangs"); + assertNotNull(allModules); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); controllerContext.setSchemas(schemaContext); } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java index f181c02bbb..2370035861 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java @@ -7,15 +7,25 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.net.*; -import java.util.*; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Set; -import org.junit.*; +import org.junit.BeforeClass; +import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; -import org.opendaylight.controller.sal.restconf.impl.*; -import org.opendaylight.yangtools.yang.common.*; -import org.opendaylight.yangtools.yang.data.api.*; +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.StructuredData; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.data.api.CompositeNode; +import org.opendaylight.yangtools.yang.data.api.ModifyAction; +import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode; +import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode; +import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.model.api.Module; @@ -33,7 +43,7 @@ public class InvokeRpcMethodTest { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModulesFrom("/invoke-rpc"); + modules = TestUtils.loadModulesFrom("/invoke-rpc"); assertEquals(1, modules.size()); Module module = TestUtils.resolveModule("invoke-rpc-module", modules); assertNotNull(module); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java index e912b733f3..f2eea9dbd2 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ReadConfAndOperDataTest.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -14,15 +15,21 @@ import java.util.Set; import java.util.logging.Level; import java.util.logging.LogRecord; -import javax.ws.rs.core.*; +import javax.ws.rs.core.Application; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.test.JerseyTest; import org.glassfish.jersey.test.TestProperties; -import org.junit.*; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider; import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; -import org.opendaylight.controller.sal.restconf.impl.*; +import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.Module; @@ -39,8 +46,8 @@ public class ReadConfAndOperDataTest extends JerseyTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs") - .getPath()); + Set allModules = TestUtils.loadModulesFrom("/full-versions/yangs"); + assertNotNull(allModules); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); controllerContext = ControllerContext.getInstance(); controllerContext.setSchemas(schemaContext); @@ -63,7 +70,8 @@ public class ReadConfAndOperDataTest extends JerseyTest { String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml", true); + CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true, + XmlToCompositeNodeProvider.INSTANCE); when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); @@ -76,12 +84,13 @@ public class ReadConfAndOperDataTest extends JerseyTest { assertEquals(404, response.getStatus()); } - @Ignore @Test public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException { String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0"); - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml"); + CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true, + XmlToCompositeNodeProvider.INSTANCE); + when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java index 64c4bee3a6..c15cd53082 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfImplTest.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -10,7 +11,10 @@ import java.util.Set; import org.junit.BeforeClass; import org.junit.Test; -import org.opendaylight.controller.sal.restconf.impl.*; +import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; +import org.opendaylight.controller.sal.restconf.impl.BrokerFacade; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.model.api.Module; @@ -22,8 +26,8 @@ public class RestconfImplTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs") - .getPath()); + Set allModules = TestUtils.loadModulesFrom("/full-versions/yangs"); + assertNotNull(allModules); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); ControllerContext controllerContext = ControllerContext.getInstance(); controllerContext.setSchemas(schemaContext); @@ -32,7 +36,7 @@ public class RestconfImplTest { @Test public void testExample() throws FileNotFoundException { - CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml"); + CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", XmlToCompositeNodeProvider.INSTANCE); BrokerFacade brokerFacade = mock(BrokerFacade.class); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); assertEquals(loadedCompositeNode, brokerFacade.readOperationalData(null)); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index 25488457b2..4295c29a22 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -6,30 +6,45 @@ import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.io.*; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; import java.net.URI; import java.net.URISyntaxException; import java.sql.Date; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; 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.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.opendaylight.controller.md.sal.common.api.TransactionStatus; -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.controller.sal.restconf.impl.BrokerFacade; +import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper; +import org.opendaylight.controller.sal.restconf.impl.ControllerContext; +import org.opendaylight.controller.sal.restconf.impl.NodeWrapper; +import org.opendaylight.controller.sal.restconf.impl.RestconfImpl; +import org.opendaylight.controller.sal.restconf.impl.StructuredData; import org.opendaylight.yangtools.yang.common.QName; 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.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser; import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; import org.slf4j.Logger; @@ -45,7 +60,7 @@ public final class TestUtils { private final static YangModelParser parser = new YangParserImpl(); - public static Set loadModules(String resourceDirectory) throws FileNotFoundException { + private static Set loadModules(String resourceDirectory) throws FileNotFoundException { final File testDir = new File(resourceDirectory); final String[] fileList = testDir.list(); final List testFiles = new ArrayList(); @@ -61,23 +76,31 @@ public final class TestUtils { return parser.parseYangModels(testFiles); } + public static Set loadModulesFrom(String yangPath) { + try { + return TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); + } catch (FileNotFoundException e) { + LOG.error("Yang files at path: " + yangPath + " weren't loaded."); + } + + return null; + } + public static SchemaContext loadSchemaContext(Set modules) { return parser.resolveSchemaContext(modules); } public static SchemaContext loadSchemaContext(String resourceDirectory) throws FileNotFoundException { - return parser.resolveSchemaContext(loadModules(resourceDirectory)); + return parser.resolveSchemaContext(loadModulesFrom(resourceDirectory)); } public static Module findModule(Set modules, String moduleName) { - Module result = null; for (Module module : modules) { if (module.getName().equals(moduleName)) { - result = module; - break; + return module; } } - return result; + return null; } public static Document loadDocumentFrom(InputStream inputStream) { @@ -125,12 +148,18 @@ public final class TestUtils { RestconfImpl restconf = RestconfImpl.getInstance(); ControllerContext.getInstance().setSchemas(TestUtils.loadSchemaContext(modules)); - prepareMockForRestconfBeforeNormalization(modules, restconf); + prepareMocksForRestconf(modules, restconf); restconf.createConfigurationData(schemaNodePath, compositeNode); } + /** + * Searches module with name {@code searchedModuleName} in {@code modules}. + * If module name isn't specified and module set has only one element then + * this element is returned. + * + */ public static Module resolveModule(String searchedModuleName, Set modules) { - assertNotNull("modules can't be null.", modules); + assertNotNull("Modules can't be null.", modules); if (searchedModuleName != null) { for (Module m : modules) { if (m.getName().equals(searchedModuleName)) { @@ -143,16 +172,6 @@ public final class TestUtils { return null; } - public static Set resolveModulesFrom(String yangPath) { - try { - return TestUtils.loadModules(TestUtils.class.getResource(yangPath).getPath()); - } catch (FileNotFoundException e) { - LOG.error("Yang files at path: " + yangPath + " weren't loaded."); - } - - return null; - } - public static DataSchemaNode resolveDataSchemaNode(String searchedDataSchemaName, Module module) { assertNotNull("Module can't be null", module); @@ -168,29 +187,6 @@ public final class TestUtils { return null; } - public static CompositeNode loadCompositeNode(String xmlDataPath) { - return loadCompositeNode(xmlDataPath, false); - } - - 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) { - LOG.error(e.getMessage()); - } - if (addDumyNamespace) { - try { - addDummyNamespaceToAllNodes((CompositeNodeWrapper) compositeNode); - } catch (URISyntaxException e) { - LOG.error(e.getMessage()); - } - } - return compositeNode; - } - public static QName buildQName(String name, String uri, String date, String prefix) { try { URI u = new URI(uri); @@ -212,52 +208,7 @@ public final class TestUtils { return buildQName(name, "", null); } - public static DataSchemaNode obtainSchemaFromYang(String yangFolder) throws FileNotFoundException { - return obtainSchemaFromYang(yangFolder, null); - } - - public static DataSchemaNode obtainSchemaFromYang(String yangFolder, String moduleName) - throws FileNotFoundException { - Set modules = null; - modules = TestUtils.loadModules(TestUtils.class.getResource(yangFolder).getPath()); - - if (modules == null) { - return null; - } - if (modules.size() < 1) { - return null; - } - - Module moduleRes = null; - if (modules.size() > 1) { - if (moduleName == null) { - return null; - } else { - for (Module module : modules) { - if (module.getName().equals(moduleName)) { - moduleRes = module; - } - } - if (moduleRes == null) { - return null; - } - } - } else { - moduleRes = modules.iterator().next(); - } - - if (moduleRes.getChildNodes() == null) { - return null; - } - - if (moduleRes.getChildNodes().size() != 1) { - return null; - } - DataSchemaNode dataSchemaNode = moduleRes.getChildNodes().iterator().next(); - return dataSchemaNode; - } - - public static void addDummyNamespaceToAllNodes(NodeWrapper wrappedNode) throws URISyntaxException { + private static void addDummyNamespaceToAllNodes(NodeWrapper wrappedNode) throws URISyntaxException { wrappedNode.setNamespace(new URI("")); if (wrappedNode instanceof CompositeNodeWrapper) { for (NodeWrapper childNodeWrapper : ((CompositeNodeWrapper) wrappedNode).getValues()) { @@ -266,8 +217,7 @@ public final class TestUtils { } } - private static void prepareMockForRestconfBeforeNormalization(Set modules, RestconfImpl restconf) { - + private static void prepareMocksForRestconf(Set modules, RestconfImpl restconf) { ControllerContext controllerContext = ControllerContext.getInstance(); BrokerFacade mockedBrokerFacade = mock(BrokerFacade.class); @@ -279,16 +229,16 @@ public final class TestUtils { new DummyRpcResult.Builder().result(TransactionStatus.COMMITED) .build()).build()); - restconf.setControllerContext(ControllerContext.getInstance()); + restconf.setControllerContext(controllerContext); restconf.setBroker(mockedBrokerFacade); } - public static CompositeNode readInputToCnSn(String jsonPath, boolean dummyNamespaces, + public static CompositeNode readInputToCnSn(String path, boolean dummyNamespaces, MessageBodyReader reader) throws WebApplicationException { - InputStream jsonStream = JsonToCnSnTest.class.getResourceAsStream(jsonPath); + InputStream inputStream = TestUtils.class.getResourceAsStream(path); try { - CompositeNode compositeNode = reader.readFrom(null, null, null, null, null, jsonStream); + CompositeNode compositeNode = reader.readFrom(null, null, null, null, null, inputStream); assertTrue(compositeNode instanceof CompositeNodeWrapper); if (dummyNamespaces) { try { @@ -307,6 +257,10 @@ public final class TestUtils { return null; } + public static CompositeNode readInputToCnSn(String path, MessageBodyReader reader) { + return readInputToCnSn(path, false, reader); + } + public static String writeCompNodeWithSchemaContextToOutput(CompositeNode compositeNode, Set modules, DataSchemaNode dataSchemaNode, MessageBodyWriter messageBodyWriter) throws IOException, WebApplicationException { @@ -321,7 +275,5 @@ public final class TestUtils { byteArrayOS); return byteArrayOS.toString(); - } - } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java index 4cea120d4d..b18f526a23 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java @@ -1,6 +1,7 @@ package org.opendaylight.controller.sal.restconf.impl.test; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -35,7 +36,6 @@ import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider; import org.opendaylight.controller.sal.restconf.impl.*; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.data.api.*; -import org.opendaylight.yangtools.yang.data.impl.NodeFactory; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; @@ -51,8 +51,8 @@ public class XmlProvidersTest extends JerseyTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs") - .getPath()); + Set allModules = TestUtils.loadModulesFrom("/full-versions/yangs"); + assertNotNull(allModules); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); controllerContext = ControllerContext.getInstance(); controllerContext.setSchemas(schemaContext); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java index dd3f367f49..3d24c6ba67 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java @@ -21,7 +21,7 @@ public abstract class YangAndXmlAndDataSchemaLoader { } protected static void dataLoad(String yangPath, int modulesNumber, String moduleName, String dataSchemaName) { - modules = TestUtils.resolveModulesFrom(yangPath); + modules = TestUtils.loadModulesFrom(yangPath); assertEquals(modulesNumber, modules.size()); Module module = TestUtils.resolveModule(moduleName, modules); searchedModuleName = module == null ? "" : module.getName(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java index 894b4c2bab..beff5724eb 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/xml/to/cnsn/test/XmlLeafrefToCnSnTest.java @@ -2,20 +2,14 @@ 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; @@ -29,15 +23,10 @@ public class XmlLeafrefToCnSnTest { */ @Test public void testXmlDataContainer() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-container.xml", false); + CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-container.xml", false, + XmlToCompositeNodeProvider.INSTANCE); assertNotNull(compNode); - Set modules = null; - try { - modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-container-yang").getPath()); - } catch (FileNotFoundException e) { - LOG.error(e.getMessage()); - assertTrue(false); - } + Set modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-container-yang"); assertNotNull(modules); TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont"); @@ -77,14 +66,11 @@ public class XmlLeafrefToCnSnTest { @Test public void testXmlDataList() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/data-list.xml", false); + CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/data-list.xml", false, + XmlToCompositeNodeProvider.INSTANCE); assertNotNull(compNode); - Set modules = null; - try { - modules = TestUtils.loadModules(TestUtils.class.getResource("/xml-to-cnsn/data-list-yang").getPath()); - } catch (FileNotFoundException e) { - } + Set modules = TestUtils.loadModulesFrom("/xml-to-cnsn/data-list-yang"); assertNotNull(modules); TestUtils.normalizeCompositeNode(compNode, modules, "data-container-yang:cont"); @@ -148,7 +134,8 @@ public class XmlLeafrefToCnSnTest { @Test public void testXmlEmptyData() { - CompositeNode compNode = compositeNodeFromXml("/xml-to-cnsn/empty-data.xml", true); + CompositeNode compNode = TestUtils.readInputToCnSn("/xml-to-cnsn/empty-data.xml", true, + XmlToCompositeNodeProvider.INSTANCE); assertEquals("cont", compNode.getNodeType().getLocalName()); SimpleNode lf1 = null; SimpleNode lflst1_1 = null; @@ -323,40 +310,13 @@ public class XmlLeafrefToCnSnTest { 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); + CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlPath, false, XmlToCompositeNodeProvider.INSTANCE); assertNotNull(compositeNode); - Set modules = TestUtils.resolveModulesFrom(yangPath); + Set modules = TestUtils.loadModulesFrom(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); @@ -365,7 +325,6 @@ public class XmlLeafrefToCnSnTest { QName qName = (QName) lf11.getValue(); assertEquals(resultLocalName, qName.getLocalName()); assertEquals(resultNamespace, qName.getNamespace().toString()); - } private SimpleNode getLf11(CompositeNode compositeNode) { -- 2.36.6