Assert reported error 17/107517/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 26 Aug 2023 19:20:29 +0000 (21:20 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 26 Aug 2023 19:36:20 +0000 (21:36 +0200)
We are asserting that an error is throw, but we are not making it clear
which error should be reported. Check all errors for their
getErrorMessage().

JIRA: NETCONF-1128
Change-Id: Idded2736f070216b3c2dc976c8d9ddc601393410
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
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
restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/RestconfValidationTest.java

index b3233895da0900eb28b81417c0ef1b16bbdf532f..cb1db474442fe633f78a6772a612390367016775 100644 (file)
@@ -12,13 +12,11 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
 
-import java.io.InputStream;
 import javax.ws.rs.core.MediaType;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -189,13 +187,15 @@ public class XmlBodyReaderMountPointTest extends AbstractBodyReaderTest {
     @Test
     public void wrongRootElementTest() throws Exception {
         mockPutBodyReader("instance-identifier-module:cont/yang-ext:mount", xmlBodyReader);
-        final InputStream inputStream = XmlBodyReaderTest.class.getResourceAsStream(
+        final var inputStream = XmlBodyReaderTest.class.getResourceAsStream(
             "/instanceidentifier/xml/bug7933.xml");
 
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var ex = assertThrows(RestconfDocumentedException.class,
             () -> xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null, inputStream));
-        final RestconfError restconfError = ex.getErrors().get(0);
-        assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
-        assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
+        final var error = ex.getErrors().get(0);
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
+        assertEquals("Error parsing input: Not correct message root element \"cont1\", should be "
+            + "\"(urn:ietf:params:xml:ns:netconf:base:1.0)data\"", error.getErrorMessage());
     }
 }
index f0571c57fe839291690534ca4693ee8aba785afd..eaf81661f776cf19d58007a2578b59b16c857da0 100644 (file)
@@ -14,13 +14,11 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.fail;
 
-import java.io.InputStream;
 import javax.ws.rs.core.MediaType;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
@@ -251,15 +249,16 @@ public class XmlBodyReaderTest extends AbstractBodyReaderTest {
     @Test
     public void wrongRootElementTest() throws Exception {
         mockPutBodyReader("instance-identifier-module:cont", xmlBodyReader);
-        final InputStream inputStream =
+        final var inputStream =
                 XmlBodyReaderTest.class.getResourceAsStream("/instanceidentifier/xml/bug7933.xml");
 
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var ex = assertThrows(RestconfDocumentedException.class,
             () -> xmlBodyReader.readFrom(null, null, null, MEDIA_TYPE, null, inputStream));
-
-        final RestconfError restconfError = ex.getErrors().get(0);
-        assertEquals(ErrorType.PROTOCOL, restconfError.getErrorType());
-        assertEquals(ErrorTag.MALFORMED_MESSAGE, restconfError.getErrorTag());
+        final var error = ex.getErrors().get(0);
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.MALFORMED_MESSAGE, error.getErrorTag());
+        assertEquals("Error parsing input: Not correct message root element \"cont1\", should be "
+            + "\"(instance:identifier:module?revision=2014-01-17)cont\"", error.getErrorMessage());
     }
 
     @Test
index df3986521de1d28a016e20f2166bd5fd11c6276c..e9d13bbdb80effdc02bed6df87808671f9434c89 100644 (file)
@@ -18,7 +18,9 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import org.junit.Test;
+import org.junit.function.ThrowingRunnable;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.yangtools.yang.common.ErrorTag;
 import org.opendaylight.yangtools.yang.common.ErrorType;
 import org.opendaylight.yangtools.yang.common.Revision;
@@ -46,11 +48,9 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetRevisionNotSuppliedTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var error = assertInvalidValue(
             () -> ParserIdentifier.validateAndGetRevision(Collections.emptyIterator()));
-
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Revision date must be supplied.", error.getErrorMessage());
     }
 
     /**
@@ -80,10 +80,10 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModulNameNotSuppliedTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var error = assertInvalidValue(
             () -> ParserIdentifier.validateAndGetModulName(Collections.emptyIterator()));
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Module name must be supplied.", error.getErrorMessage());
+
     }
 
     /**
@@ -93,11 +93,10 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableFirstTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var error = assertInvalidValue(
             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
                 "01-not-parsable-as-name-on-firts-char")));
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
     }
 
     /**
@@ -107,11 +106,9 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableNextTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
-            () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
-                "not-parsable-as-name-after-first-char*")));
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        final var error = assertInvalidValue(() -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
+            "not-parsable-as-name-after-first-char*")));
+        assertEquals("Supplied name has not expected identifier format.", error.getErrorMessage());
     }
 
     /**
@@ -120,10 +117,9 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableXmlTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var error = assertInvalidValue(
             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("xMl-module-name")));
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Identifier must NOT start with XML ignore case.", error.getErrorMessage());
     }
 
     /**
@@ -132,9 +128,18 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameEmptyTest() {
-        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+        final var error = assertInvalidValue(
             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("")));
-        assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
-        assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals("Identifier must start with character from set 'a-zA-Z_", error.getErrorMessage());
+    }
+
+    private static RestconfError assertInvalidValue(final ThrowingRunnable runnable) {
+        final var ex = assertThrows(RestconfDocumentedException.class, runnable);
+        final var errors = ex.getErrors();
+        assertEquals(1, errors.size());
+        final var error = errors.get(0);
+        assertEquals(ErrorType.PROTOCOL, error.getErrorType());
+        assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
+        return error;
     }
 }