Improve reader mocking 11/107511/2
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 26 Aug 2023 08:32:48 +0000 (10:32 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 26 Aug 2023 09:29:37 +0000 (11:29 +0200)
Rather than passing a boolean flag, split POST/PUT method mocking, so as
to identify callers relying on either.

JIRA: NETCONF-1128
Change-Id: I8220b91da162b3a41b298e364f03da41933956fa
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/AbstractBodyReaderTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/JsonBodyReaderTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlBodyReaderMountPointTest.java
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/jersey/providers/XmlBodyReaderTest.java

index 39680bbba3289c2d3536bd979a92921363ddeaaa..9fd6a1d5cd7cc2e504adddaa610251b27806122f 100644 (file)
@@ -61,8 +61,16 @@ public abstract class AbstractBodyReaderTest extends AbstractInstanceIdentifierT
             .getService(DOMSchemaService.class);
     }
 
-    protected static void mockBodyReader(final String identifier,
-            final AbstractNormalizedNodeBodyReader normalizedNodeProvider, final boolean isPost) {
+    protected static void mockPostBodyReader(final String identifier, final AbstractNormalizedNodeBodyReader reader) {
+        mockBodyReader(identifier, reader, HttpMethod.POST);
+    }
+
+    protected static void mockPutBodyReader(final String identifier, final AbstractNormalizedNodeBodyReader reader) {
+        mockBodyReader(identifier, reader, HttpMethod.PUT);
+    }
+
+    private static void mockBodyReader(final String identifier, final AbstractNormalizedNodeBodyReader reader,
+            final String method) {
         final var pathParm = new MultivaluedHashMap<String, String>(2);
         if (!identifier.isEmpty()) {
             pathParm.put("identifier", List.of(identifier));
@@ -72,11 +80,11 @@ public abstract class AbstractBodyReaderTest extends AbstractInstanceIdentifierT
         doReturn(pathParm).when(uriInfoMock).getPathParameters();
         doReturn(pathParm).when(uriInfoMock).getPathParameters(false);
         doReturn(pathParm).when(uriInfoMock).getPathParameters(true);
-        normalizedNodeProvider.setUriInfo(uriInfoMock);
+        reader.setUriInfo(uriInfoMock);
 
         final var request = mock(Request.class);
-        doReturn(isPost ? HttpMethod.POST : HttpMethod.PUT).when(request).getMethod();
-        normalizedNodeProvider.setRequest(request);
+        doReturn(method).when(request).getMethod();
+        reader.setRequest(request);
     }
 
     protected static void checkMountPointNormalizedNodePayload(final NormalizedNodePayload nnContext) {
index 8be260b5fec6f74ddc84fd286b4e0c531350637c..88ea705eafc0bd3e206f6f60b90f431e8f6ef8ca 100644 (file)
@@ -5,7 +5,6 @@
  * 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.hamcrest.CoreMatchers.instanceOf;
@@ -61,7 +60,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, jsonBodyReader, false);
+        mockPutBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/jsondata.json"));
         checkNormalizedNodePayload(payload);
@@ -76,7 +75,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
         final String uri = "instance-identifier-module:cont/cont1";
-        mockBodyReader(uri, jsonBodyReader, false);
+        mockPutBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_sub_container.json"));
         checkNormalizedNodePayload(payload);
@@ -90,7 +89,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, jsonBodyReader, true);
+        mockPostBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_sub_container.json"));
         checkNormalizedNodePayload(payload);
@@ -106,7 +105,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
             .node(cont1QName).node(actionQName);
         final String uri = "instance-identifier-module:cont/cont1/reset";
-        mockBodyReader(uri, jsonBodyReader, true);
+        mockPostBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             JsonBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_cont_action.json"));
         checkNormalizedNodePayload(payload);
@@ -121,7 +120,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName(), contAugmentQName);
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, jsonBodyReader, true);
+        mockPostBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_container.json"));
         checkNormalizedNodePayload(payload);
@@ -139,7 +138,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
                 .node(augmentChoice1QName).node(augmentChoice2QName).node(containerQName);
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, jsonBodyReader, true);
+        mockPostBodyReader(uri, jsonBodyReader);
         final NormalizedNodePayload payload = jsonBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/json/json_augment_choice_container.json"));
         checkNormalizedNodePayload(payload);
@@ -148,7 +147,7 @@ public class JsonBodyReaderTest extends AbstractBodyReaderTest {
 
     @Test
     public void testRangeViolation() throws Exception {
-        mockBodyReader("netconf786:foo", jsonBodyReader, false);
+        mockPutBodyReader("netconf786:foo", jsonBodyReader);
 
         final InputStream inputStream = new ByteArrayInputStream(("{\n"
             + "  \"netconf786:foo\": {\n"
index 6b68b89a9d7e1f090d86949a994e807da367d506..b3233895da0900eb28b81417c0ef1b16bbdf532f 100644 (file)
@@ -62,7 +62,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         final DataSchemaNode dataSchemaNode = schemaContext
                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, false);
+        mockPutBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
         checkMountPointNormalizedNodePayload(payload);
@@ -74,7 +74,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         final DataSchemaNode dataSchemaNode = schemaContext
                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont/cont1";
-        mockBodyReader(uri, xmlBodyReader, false);
+        mockPutBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
         checkMountPointNormalizedNodePayload(payload);
@@ -87,7 +87,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         final DataSchemaNode dataSchemaNode = schemaContext
                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
         final String uri = "instance-identifier-module:cont/yang-ext:mount/instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
         checkMountPointNormalizedNodePayload(payload);
@@ -99,7 +99,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
         final DataSchemaNode dataSchemaNode = schemaContext
             .getDataChildByName(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, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload pyaload = xmlBodyReader.readFrom(null,null, null, MEDIA_TYPE, null,
             XmlBodyReaderMountPointTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
         checkMountPointNormalizedNodePayload(pyaload);
@@ -109,7 +109,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
     @Test
     public void rpcModuleInputTest() throws Exception {
         final String uri = "instance-identifier-module:cont/yang-ext:mount/invoke-rpc-module:rpc-test";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderMountPointTest.class.getResourceAsStream("/invoke-rpc/xml/rpc-input.xml"));
         checkNormalizedNodePayload(payload);
@@ -150,7 +150,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
      */
     @Test
     public void findFooContainerUsingNamespaceTest() throws Exception {
-        mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
+        mockPostBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
 
@@ -169,7 +169,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
      */
     @Test
     public void findBarContainerUsingNamespaceTest() throws Exception {
-        mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, true);
+        mockPostBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
 
@@ -188,7 +188,7 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
      */
     @Test
     public void wrongRootElementTest() throws Exception {
-        mockBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader, false);
+        mockPutBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader);
         final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
             "/instanceidentifier/xml/bug7933.xml");
 
index 8fa9efd3af2b15291440a2e31661cf346f25281f..a93f14ad89448e5d843eaa4689a5724485d8c9a3 100644 (file)
@@ -68,16 +68,17 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
 
     @Test
     public void putXmlTest() throws Exception {
-        runXmlTest(false, "foo:top-level-list=key-value");
+        mockPutBodyReader("foo:top-level-list=key-value", xmlBodyReader);
+        runXmlTest();
     }
 
     @Test
     public void postXmlTest() throws Exception {
-        runXmlTest(true, "");
+        mockPostBodyReader("", xmlBodyReader);
+        runXmlTest();
     }
 
-    private void runXmlTest(final boolean isPost, final String path) throws Exception {
-        mockBodyReader(path, xmlBodyReader, isPost);
+    private void runXmlTest() throws Exception {
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/foo-xml-test/foo.xml"));
         assertNotNull(payload);
@@ -111,7 +112,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
                 .getDataChildByName(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName());
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, false);
+        mockPutBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmldata.xml"));
         checkNormalizedNodePayload(payload);
@@ -126,7 +127,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
         final DataSchemaNode dataSchemaNodeOnPath = ((DataNodeContainer) dataSchemaNode).getDataChildByName(cont1QName);
         final String uri = "instance-identifier-module:cont/cont1";
-        mockBodyReader(uri, xmlBodyReader, false);
+        mockPutBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
         checkNormalizedNodePayload(payload);
@@ -140,7 +141,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         final QName cont1QName = QName.create(dataSchemaNode.getQName(), "cont1");
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName()).node(cont1QName);
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_sub_container.xml"));
         checkNormalizedNodePayload(payload);
@@ -156,7 +157,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName())
             .node(cont1QName).node(actionQName);
         final String uri = "instance-identifier-module:cont/cont1/reset";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_cont_action.xml"));
         checkNormalizedNodePayload(payload);
@@ -171,7 +172,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
         final QName contAugmentQName = QName.create(augmentModule.getQNameModule(), "cont-augment");
         final YangInstanceIdentifier dataII = YangInstanceIdentifier.of(dataSchemaNode.getQName(), contAugmentQName);
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_container.xml"));
         checkNormalizedNodePayload(payload);
@@ -190,7 +191,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
             .node(augmentChoice2QName)
             .node(QName.create(augmentChoice1QName, "case-choice-case-container1"));
         final String uri = "instance-identifier-module:cont";
-        mockBodyReader(uri, xmlBodyReader, true);
+        mockPostBodyReader(uri, xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xml_augment_choice_container.xml"));
         checkNormalizedNodePayload(payload);
@@ -212,7 +213,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
      */
     @Test
     public void findFooContainerUsingNamespaceTest() throws Exception {
-        mockBodyReader("", xmlBodyReader, true);
+        mockPostBodyReader("", xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindFooContainer.xml"));
 
@@ -232,7 +233,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
      */
     @Test
     public void findBarContainerUsingNamespaceTest() throws Exception {
-        mockBodyReader("", xmlBodyReader, true);
+        mockPostBodyReader("", xmlBodyReader);
         final NormalizedNodePayload payload = xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null,
             XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/xmlDataFindBarContainer.xml"));
 
@@ -251,7 +252,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
      */
     @Test
     public void wrongRootElementTest() throws Exception {
-        mockBodyReader("instance-identifier-module:cont", xmlBodyReader, false);
+        mockPutBodyReader("instance-identifier-module:cont", xmlBodyReader);
         final InputStream inputStream =
                 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
 
@@ -265,7 +266,7 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
 
     @Test
     public void testRangeViolation() throws Exception {
-        mockBodyReader("netconf786:foo", xmlBodyReader, false);
+        mockPutBodyReader("netconf786:foo", xmlBodyReader);
 
         final InputStream inputStream = new ByteArrayInputStream(
             "<foo xmlns=\"netconf786\"><bar>100</bar></foo>".getBytes(StandardCharsets.UTF_8));