Cleanup RestconfValidationTest 49/90949/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Jul 2020 12:54:16 +0000 (14:54 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Jul 2020 12:54:16 +0000 (14:54 +0200)
The tests here could use proper asserts, clean them up.

Change-Id: I75aceba1942a857fe95c9604db4f8e485158adf4
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/RestconfValidationTest.java

index 9b23494a6b411307f09acc094161600d53093f72..cf1a8cc15ea2c95acc5ea33ba98f36fa6e861f9b 100644 (file)
@@ -7,13 +7,15 @@
  */
 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.junit.Assert.assertThrows;
 
-import java.util.ArrayList;
+import com.google.common.collect.Iterators;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import org.junit.Test;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
@@ -43,14 +45,12 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetRevisionNotSuppliedTest() {
-        try {
-            ParserIdentifier.validateAndGetRevision(new ArrayList<String>().iterator());
-            fail("Test should fail due to not supplied module revision");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetRevision(Collections.emptyIterator()));
+
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 
     /**
@@ -59,12 +59,9 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetRevisionNotParsableTest() {
-        try {
-            ParserIdentifier.validateAndGetRevision(Arrays.asList("not-parsable-as-date").iterator());
-            fail("Test should fail due to not parsable module revision");
-        } catch (final RestconfDocumentedException e) {
-            assertTrue(e.getMessage().contains("Supplied revision is not in expected date format YYYY-mm-dd"));
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetRevision(Iterators.singletonIterator("not-parsable-as-date")));
+        assertThat(ex.getMessage(), containsString("Supplied revision is not in expected date format YYYY-mm-dd"));
     }
 
     /**
@@ -83,14 +80,11 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModulNameNotSuppliedTest() {
-        try {
-            ParserIdentifier.validateAndGetModulName(new ArrayList<String>().iterator());
-            fail("Test should fail due to not supplied module name");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetModulName(Collections.emptyIterator()));
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 
     /**
@@ -100,15 +94,12 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableFirstTest() {
-        try {
-            ParserIdentifier.validateAndGetModulName(
-                    Arrays.asList("01-not-parsable-as-name-on-firts-char").iterator());
-            fail("Test should fail due to not parsable module name on the first character");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
+                "01-not-parsable-as-name-on-firts-char")));
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 
     /**
@@ -118,15 +109,12 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableNextTest() {
-        try {
-            ParserIdentifier.validateAndGetModulName(
-                    Arrays.asList("not-parsable-as-name-after-first-char*").iterator());
-            fail("Test should fail due to not parsable module name on any character after the first character");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
+                "not-parsable-as-name-after-first-char*")));
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 
     /**
@@ -135,14 +123,11 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameNotParsableXmlTest() {
-        try {
-            ParserIdentifier.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
-            fail("Test should fail due to module name beginning with 'xMl'");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("xMl-module-name")));
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 
     /**
@@ -151,13 +136,10 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModuleNameEmptyTest() {
-        try {
-            ParserIdentifier.validateAndGetModulName(Arrays.asList("").iterator());
-            fail("Test should fail due to empty module name");
-        } catch (final RestconfDocumentedException e) {
-            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
-            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
-            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
-        }
+        final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
+            () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("")));
+        assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
+        assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
+        assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
     }
 }