Bug 7933: NPE when posting using XML
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / test / providers / TestXmlBodyReader.java
index 0a3f14a384d61ef8c309d6b89300c4757a35568a..1ac78c8e25932d05e623520959da84e084b7965a 100644 (file)
@@ -20,11 +20,14 @@ import java.net.URI;
 import java.text.ParseException;
 import java.util.Collection;
 import javax.ws.rs.core.MediaType;
+import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
@@ -64,7 +67,7 @@ public class TestXmlBodyReader extends AbstractBodyReaderTest {
         }
     }
 
-    public TestXmlBodyReader () throws NoSuchFieldException, SecurityException {
+    public TestXmlBodyReader() throws Exception {
         super();
         this.xmlBodyReader = new XmlNormalizedNodeBodyReader();
     }
@@ -252,4 +255,24 @@ public class TestXmlBodyReader extends AbstractBodyReaderTest {
         assertEquals("Not correct container found, namespace was ignored",
                 "bar:module", returnValue.getData().getNodeType().getNamespace().toString());
     }
+
+    /**
+     * Test PUT operation when message root element is not the same as the last element in request URI.
+     * PUT operation message should always start with schema node from URI otherwise exception should be
+     * thrown.
+     */
+    @Test
+    public void wrongRootElementTest() throws Exception {
+        mockBodyReader("instance-identifier-module:cont", this.xmlBodyReader, false);
+        final InputStream inputStream = TestXmlBodyReader.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());
+        }
+    }
 }