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 4378a882a5af177198b73121d5b43d99658a99a2..b525ecb698bd1b08583451d27cedc483194317d9 100644 (file)
@@ -5,65 +5,53 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 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 static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
-import com.google.common.base.Optional;
 import java.io.File;
 import java.io.InputStream;
-import java.net.URI;
-import java.text.ParseException;
 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.controller.md.sal.dom.api.DOMMountPoint;
-import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 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.SimpleDateFormatUtil;
+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;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+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 SchemaContext schemaContext;
+    private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME =  QNameModule.create(
+        XMLNamespace.of("instance:identifier:module"), Revision.of("2014-01-17"));
 
-    private static final QNameModule INSTANCE_IDENTIFIER_MODULE_QNAME = initializeInstanceIdentifierModule();
+    private static EffectiveModelContext schemaContext;
 
-    private static QNameModule initializeInstanceIdentifierModule() {
-        try {
-            return QNameModule.create(URI.create("instance:identifier:module"),
-                SimpleDateFormatUtil.getRevisionFormat().parse("2014-01-17"));
-        } catch (final ParseException e) {
-            throw new Error(e);
-        }
-    }
+    private final XmlNormalizedNodeBodyReader xmlBodyReader;
 
     public XmlBodyReaderMountPointTest() throws Exception {
-        this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
+        super(schemaContext);
+        this.xmlBodyReader = new XmlNormalizedNodeBodyReader(schemaContextHandler, mountPointService);
     }
 
     @Override
@@ -75,15 +63,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
     public static void initialization() throws Exception {
         final Collection<File> testFiles = TestRestconfUtils.loadFiles("/instanceidentifier/yang");
         testFiles.addAll(TestRestconfUtils.loadFiles("/invoke-rpc"));
-        schemaContext = YangParserTestUtils.parseYangSources(testFiles);
-
-        final DOMMountPointService mountPointService = mock(DOMMountPointService.class);
-        final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
-
-        when(MOUNT_POINT_SERVICE_HANDLER.get()).thenReturn(mountPointService);
-        when(mountPointService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountPoint));
-        when(mountPoint.getSchemaContext()).thenReturn(schemaContext);
-        SchemaContextHandler.setActualSchemaContext(schemaContext);
+        schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
     }
 
     @Test
@@ -129,6 +109,20 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         checkExpectValueNormalizeNodeContext(dataSchemaNode, returnValue);
     }
 
+    @Test
+    public void moduleSubContainerDataPostActionTest() throws Exception {
+        final Optional<DataSchemaNode> dataSchemaNode = schemaContext
+            .findDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
+        final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1/reset";
+        mockBodyReader(uri, this.xmlBodyReader, true);
+        final InputStream inputStream = XmlBodyReaderMountPointTest.class
+            .getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml");
+        final NormalizedNodeContext returnValue = this.xmlBodyReader.readFrom(null,
+            null, null, this.mediaType, null, inputStream);
+        checkMountPointNormalizedNodeContext(returnValue);
+        checkExpectValueNormalizeNodeContext(dataSchemaNode.get(), returnValue);
+    }
+
     @Test
     public void rpcModuleInputTest() throws Exception {
         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
@@ -139,47 +133,40 @@ 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 void checkExpectValueNormalizeNodeContext(
-            final DataSchemaNode dataSchemaNode,
+    private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
             final NormalizedNodeContext nnContext) {
         checkExpectValueNormalizeNodeContext(dataSchemaNode, nnContext, null);
     }
 
-    private void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
+    private static void checkExpectValueNormalizeNodeContext(final DataSchemaNode dataSchemaNode,
             final NormalizedNodeContext nnContext, final QName qualifiedName) {
-        YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier
-                .of(dataSchemaNode.getQName());
-        final DOMMountPoint mountPoint = nnContext
-                .getInstanceIdentifierContext().getMountPoint();
-        final DataSchemaNode mountDataSchemaNode = mountPoint
-                .getSchemaContext().getDataChildByName(
-                        dataSchemaNode.getQName());
+        YangInstanceIdentifier dataNodeIdent = YangInstanceIdentifier.of(dataSchemaNode.getQName());
+        final DOMMountPoint mountPoint = nnContext.getInstanceIdentifierContext().getMountPoint();
+        final DataSchemaNode mountDataSchemaNode = modelContext(mountPoint)
+                .getDataChildByName(dataSchemaNode.getQName());
         assertNotNull(mountDataSchemaNode);
-        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));
+        if (qualifiedName != null && dataSchemaNode instanceof DataNodeContainer) {
+            final DataSchemaNode child = ((DataNodeContainer) dataSchemaNode).getDataChildByName(qualifiedName);
+            dataNodeIdent = YangInstanceIdentifier.builder(dataNodeIdent).node(child.getQName()).build();
+            assertEquals(nnContext.getInstanceIdentifierContext().getSchemaNode(), child);
         } else {
-            assertTrue(mountDataSchemaNode.equals(dataSchemaNode));
+            assertEquals(mountDataSchemaNode, dataSchemaNode);
         }
-        assertNotNull(NormalizedNodes.findNode(nnContext.getData(),
-                dataNodeIdent));
+        assertNotNull(NormalizedNodes.findNode(nnContext.getData(), dataNodeIdent));
     }
 
     /**
@@ -199,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());
     }
 
     /**
@@ -211,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());
     }
 
     /**
@@ -234,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());
     }
 }