Use Assert.equals() instead of assertTrue()
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / XmlBodyReaderMountPointTest.java
index d8121fbe97f14a94a0735b167f9126d5ad8a9eda..b525ecb698bd1b08583451d27cedc483194317d9 100644 (file)
@@ -9,15 +9,14 @@ package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.InputStream;
-import java.net.URI;
 import java.util.Collection;
 import java.util.Optional;
 import javax.ws.rs.core.MediaType;
-import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
@@ -27,11 +26,13 @@ import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.AbstractBodyReaderTest;
 import org.opendaylight.restconf.nb.rfc8040.jersey.providers.test.XmlBodyReaderTest;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.common.XMLNamespace;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
@@ -41,15 +42,16 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
-    private final XmlNormalizedNodeBodyReader xmlBodyReader;
+    private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME =  QNameModule.create(
+        XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
+
     private static EffectiveModelContext schemaContext;
 
-    private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME =  QNameModule.create(
-        URI.create("instance:identifier:module"), Revision.of("2014-01-17"));
+    private final XmlNormalizedNodeBodyReader xmlBodyReader;
 
     public XmlBodyReaderMountPointTest() throws Exception {
         super(schemaContext);
-        this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointServiceHandler);
+        this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
     }
 
     @Override
@@ -131,17 +133,18 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
                 null, null, this.mediaType, null, inputStream);
         checkNormalizedNodeContext(returnValue);
         final ContainerNode contNode = (ContainerNode) returnValue.getData();
-        final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(QName.create(contNode.getNodeType(), "cont"));
-        final Optional<DataContainerChild<? extends PathArgument, ?>> contDataNodePotential = contNode
-                .getChild(yangCont.getLastPathArgument());
+        final YangInstanceIdentifier yangCont = YangInstanceIdentifier.of(
+            QName.create(contNode.getIdentifier().getNodeType(), "cont"));
+        final Optional<DataContainerChild> contDataNodePotential =
+            contNode.findChildByArg(yangCont.getLastPathArgument());
         assertTrue(contDataNodePotential.isPresent());
         final ContainerNode contDataNode = (ContainerNode) contDataNodePotential.get();
-        final YangInstanceIdentifier yangLeaf =
-                YangInstanceIdentifier.of(QName.create(contDataNode.getNodeType(), "lf"));
-        final Optional<DataContainerChild<? extends PathArgument, ?>> leafDataNode = contDataNode.getChild(
+        final YangInstanceIdentifier yangLeaf = YangInstanceIdentifier.of(
+            QName.create(contDataNode.getIdentifier().getNodeType(), "lf"));
+        final Optional<DataContainerChild> leafDataNode = contDataNode.findChildByArg(
                 yangLeaf.getLastPathArgument());
         assertTrue(leafDataNode.isPresent());
-        assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().getValue().toString()));
+        assertTrue("lf-test".equalsIgnoreCase(leafDataNode.get().body().toString()));
     }
 
     private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
@@ -159,9 +162,9 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
             final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
             dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
-            assertTrue(nnContext.getInstanceIdentifierContext().getSchemaNode().equals(child));
+            assertEquals(nnContext.getInstanceIdentifierContext().getSchemaNode(), child);
         } else {
-            assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
+            assertEquals(mountDataSchemaNode, dataSchemaNode);
         }
         assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
     }
@@ -183,9 +186,9 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         checkMountPointNormalizedNodeContext(returnValue);
         // check if container was found both according to its name and namespace
         assertEquals("Not correct container found, name was ignored",
-                "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+                "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
         assertEquals("Not correct container found, namespace was ignored",
-                "foo:module", returnValue.getData().getNodeType().getNamespace().toString());
+                "foo:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
     }
 
     /**
@@ -195,19 +198,19 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
      */
     @Test
     public void findBarContainerUsingNamespaceTest() throws Exception {
-        mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, true);
-        final InputStream inputStream = XmlBodyReaderTest.class
-                .getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml");
-        final NormalizedNodeContext returnValue = this.xmlBodyReader
-                .readFrom(null, null, null, this.mediaType, null, inputStream);
+        mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
+        final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/xml/xmlDataFindBarContainer.xml");
+        final NormalizedNodeContext returnValue = xmlBodyReader.readFrom(null, null, null, this.mediaType, null,
+            inputStream);
 
         // check return value
         checkMountPointNormalizedNodeContext(returnValue);
         // check if container was found both according to its name and namespace
         assertEquals("Not correct container found, name was ignored",
-                "foo-bar-container", returnValue.getData().getNodeType().getLocalName());
+                "foo-bar-container", returnValue.getData().getIdentifier().getNodeType().getLocalName());
         assertEquals("Not correct container found, namespace was ignored",
-                "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
+                "bar:module", returnValue.getData().getIdentifier().getNodeType().getNamespace().toString());
     }
 
     /**
@@ -218,16 +221,13 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
     @Test
     public void wrongRootElementTest() throws Exception {
         mockBodyReader("instance-identifier-module:cont/yang-ext:mount", this.xmlBodyReader, false);
-        final InputStream inputStream =
-                XmlBodyReaderTest.class.getResourceAsStream(
-                "/instanceidentifier/xml/bug7933.xml");
-        try {
-            this.xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
-            Assert.fail("Test should fail due to malformed PUT operation message");
-        } catch (final RestconfDocumentedException exception) {
-            final RestconfError restconfError = exception.getErrors().get(0);
-            Assert.assertEquals(RestconfError.ErrorType.PROTOCOL, restconfError.getErrorType());
-            Assert.assertEquals(RestconfError.ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
-        }
+        final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
+            "/instanceidentifier/xml/bug7933.xml");
+
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> xmlBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream));
+        final RestconfError restconfError = ex.getErrors().get(0);
+        assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
     }
 }