Eliminate org.opendaylight.restconf.nb.rfc8040.utils.validations 48/90948/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Jul 2020 12:43:05 +0000 (14:43 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Jul 2020 12:44:08 +0000 (14:44 +0200)
This package is self-serving detail of identifiers parsing. Fold
it into ParserIdentifier, which is its only production user.

Change-Id: Ie07f7097e30374882f694f1d88cb49e15b0199ad
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/ParserIdentifier.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/validations/RestconfValidation.java [deleted file]
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/parser/RestconfValidationTest.java [moved from restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/validations/RestconfValidationTest.java with 88% similarity]

index 62f2ce7cd483f51852279338ff76890d98cbc94a..4f8e1e0cb0b1e6c086f85ed03c795dc3b6634fdf 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.restconf.nb.rfc8040.utils.parser;
 
 import static com.google.common.base.Verify.verifyNotNull;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
 import java.time.format.DateTimeParseException;
@@ -16,6 +17,7 @@ import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map.Entry;
 import java.util.Optional;
 import org.eclipse.jdt.annotation.Nullable;
@@ -28,7 +30,7 @@ import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.common.schema.SchemaExportContext;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
-import org.opendaylight.restconf.nb.rfc8040.utils.validations.RestconfValidation;
+import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -236,8 +238,8 @@ public final class ParserIdentifier {
         final Iterable<String> pathComponents = RestconfConstants.SLASH_SPLITTER.split(identifier);
         final Iterator<String> componentIter = pathComponents.iterator();
         if (!Iterables.contains(pathComponents, RestconfConstants.MOUNT)) {
-            final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
-            final Revision revision = RestconfValidation.validateAndGetRevision(componentIter);
+            final String moduleName = validateAndGetModulName(componentIter);
+            final Revision revision = validateAndGetRevision(componentIter);
             final Module module = schemaContext.findModule(moduleName, revision).orElse(null);
             return new SchemaExportContext(schemaContext, module, sourceProvider);
         } else {
@@ -257,13 +259,54 @@ public final class ParserIdentifier {
 
                 pathBuilder.append(current);
             }
-            final InstanceIdentifierContext<?> point = ParserIdentifier
-                    .toInstanceIdentifier(pathBuilder.toString(), schemaContext, Optional.of(domMountPointService));
-            final String moduleName = RestconfValidation.validateAndGetModulName(componentIter);
-            final Revision revision = RestconfValidation.validateAndGetRevision(componentIter);
+            final InstanceIdentifierContext<?> point = toInstanceIdentifier(pathBuilder.toString(), schemaContext,
+                Optional.of(domMountPointService));
+            final String moduleName = validateAndGetModulName(componentIter);
+            final Revision revision = validateAndGetRevision(componentIter);
             final Module module = point.getMountPoint().getSchemaContext().findModule(moduleName, revision)
                     .orElse(null);
             return new SchemaExportContext(point.getMountPoint().getSchemaContext(), module, sourceProvider);
         }
     }
+
+    /**
+     * Validation and parsing of revision.
+     *
+     * @param revisionDate iterator
+     * @return A Revision
+     */
+    @VisibleForTesting
+    static Revision validateAndGetRevision(final Iterator<String> revisionDate) {
+        RestconfDocumentedException.throwIf(!revisionDate.hasNext(), "Revision date must be supplied.",
+            ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+        try {
+            return Revision.of(revisionDate.next());
+        } catch (final DateTimeParseException e) {
+            throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
+        }
+    }
+
+    /**
+     * Validation of name.
+     *
+     * @param moduleName iterator
+     * @return {@link String}
+     */
+    @VisibleForTesting
+    static String validateAndGetModulName(final Iterator<String> moduleName) {
+        RestconfDocumentedException.throwIf(!moduleName.hasNext(), "Module name must be supplied.", ErrorType.PROTOCOL,
+            ErrorTag.INVALID_VALUE);
+        final String name = moduleName.next();
+
+        RestconfDocumentedException.throwIf(
+            name.isEmpty() || !ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
+            "Identifier must start with character from set 'a-zA-Z_", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+        RestconfDocumentedException.throwIf(name.toUpperCase(Locale.ROOT).startsWith("XML"),
+            "Identifier must NOT start with XML ignore case.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+        RestconfDocumentedException.throwIf(
+            !ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
+            "Supplied name has not expected identifier format.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
+
+        return name;
+    }
 }
diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/validations/RestconfValidation.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/validations/RestconfValidation.java
deleted file mode 100644 (file)
index a48d27d..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * 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.utils.validations;
-
-import java.time.format.DateTimeParseException;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.Locale;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
-import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
-import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants;
-import org.opendaylight.yangtools.yang.common.Revision;
-
-/**
- * Util class for validations.
- *
- */
-public final class RestconfValidation {
-
-    private RestconfValidation() {
-        throw new UnsupportedOperationException("Util class.");
-    }
-
-    /**
-     * Validation and parsing of revision.
-     *
-     * @param revisionDate
-     *             iterator
-     * @return {@link Date}
-     */
-    public static Revision validateAndGetRevision(final Iterator<String> revisionDate) {
-        RestconfDocumentedException.throwIf(!revisionDate.hasNext(), "Revision date must be supplied.",
-            ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-        try {
-            return Revision.of(revisionDate.next());
-        } catch (final DateTimeParseException e) {
-            throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
-        }
-    }
-
-    /**
-     * Validation of name.
-     *
-     * @param moduleName
-     *             iterator
-     * @return {@link String}
-     */
-    public static String validateAndGetModulName(final Iterator<String> moduleName) {
-        RestconfDocumentedException.throwIf(!moduleName.hasNext(), "Module name must be supplied.", ErrorType.PROTOCOL,
-            ErrorTag.INVALID_VALUE);
-        final String name = moduleName.next();
-
-        RestconfDocumentedException.throwIf(
-            name.isEmpty() || !ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
-            "Identifier must start with character from set 'a-zA-Z_", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-        RestconfDocumentedException.throwIf(name.toUpperCase(Locale.ROOT).startsWith("XML"),
-            "Identifier must NOT start with XML ignore case.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-        RestconfDocumentedException.throwIf(
-            !ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
-            "Supplied name has not expected identifier format.", ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-
-        return name;
-    }
-
-}
@@ -5,8 +5,7 @@
  * 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.utils.validations;
+package org.opendaylight.restconf.nb.rfc8040.utils.parser;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -22,7 +21,7 @@ import org.opendaylight.restconf.common.errors.RestconfError;
 import org.opendaylight.yangtools.yang.common.Revision;
 
 /**
- * Unit test for {@link RestconfValidation}.
+ * Unit test for {@link ParserIdentifier}'s validate methods.
  */
 public class RestconfValidationTest {
     private static final List<String> REVISIONS = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
@@ -33,7 +32,7 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetRevisionTest() {
-        final Revision revision = RestconfValidation.validateAndGetRevision(REVISIONS.iterator());
+        final Revision revision = ParserIdentifier.validateAndGetRevision(REVISIONS.iterator());
         assertNotNull("Correct module revision should be validated", revision);
         assertEquals(Revision.of("2014-01-01"), revision);
     }
@@ -45,7 +44,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetRevisionNotSuppliedTest() {
         try {
-            RestconfValidation.validateAndGetRevision(new ArrayList<String>().iterator());
+            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());
@@ -61,7 +60,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetRevisionNotParsableTest() {
         try {
-            RestconfValidation.validateAndGetRevision(Arrays.asList("not-parsable-as-date").iterator());
+            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"));
@@ -73,7 +72,7 @@ public class RestconfValidationTest {
      */
     @Test
     public void validateAndGetModulNameTest() {
-        final String moduleName = RestconfValidation.validateAndGetModulName(NAMES.iterator());
+        final String moduleName = ParserIdentifier.validateAndGetModulName(NAMES.iterator());
         assertNotNull("Correct module name should be validated", moduleName);
         assertEquals("_module-1", moduleName);
     }
@@ -85,7 +84,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetModulNameNotSuppliedTest() {
         try {
-            RestconfValidation.validateAndGetModulName(new ArrayList<String>().iterator());
+            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());
@@ -102,7 +101,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetModuleNameNotParsableFirstTest() {
         try {
-            RestconfValidation.validateAndGetModulName(
+            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) {
@@ -120,7 +119,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetModuleNameNotParsableNextTest() {
         try {
-            RestconfValidation.validateAndGetModulName(
+            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) {
@@ -137,7 +136,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetModuleNameNotParsableXmlTest() {
         try {
-            RestconfValidation.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
+            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());
@@ -153,7 +152,7 @@ public class RestconfValidationTest {
     @Test
     public void validateAndGetModuleNameEmptyTest() {
         try {
-            RestconfValidation.validateAndGetModulName(Arrays.asList("").iterator());
+            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());