Merge "Bug 5912 - Restconf draft11 - utils"
authorTomas Cere <tcere@cisco.com>
Tue, 31 May 2016 14:45:35 +0000 (14:45 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 31 May 2016 14:45:35 +0000 (14:45 +0000)
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/parser/builder/YangInstanceIdentifierDeserializer.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/parser/ParserIdentifier.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/utils/validation/RestconfValidation.java
restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/utils/validation/RestconfValidationTest.java

index aaf887058ba1305ec66e81bfd7d2d340a1c29597..30b35486158bb11d18ec8faea4d5018d47c6ba20 100644 (file)
@@ -59,13 +59,13 @@ public final class YangInstanceIdentifierDeserializer {
             validArg(variables);
             final QName qname = prepareQName(variables);
 
-            if (!allCharsConsumed(variables)
-                    && (currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH)) {
+            // this is the last identifier (input is consumed) or end of identifier (slash)
+            if (allCharsConsumed(variables)
+                    || currentChar(variables.getOffset(), variables.getData()) == RestconfConstants.SLASH) {
                 prepareIdentifier(qname, path, variables);
                 path.add(variables.getCurrent().getIdentifier());
-            } else if (!allCharsConsumed(variables)
-                    && (currentChar(variables.getOffset(),
-                            variables.getData()) == ParserBuilderConstants.Deserializer.EQUAL)) {
+            } else if (currentChar(variables.getOffset(),
+                    variables.getData()) == ParserBuilderConstants.Deserializer.EQUAL) {
                 current = nextContextNode(qname, path, variables);
                 if (!current.isKeyedEntry()) {
                     prepareNodeWithValue(qname, path, variables);
@@ -77,6 +77,7 @@ public final class YangInstanceIdentifierDeserializer {
                         "Bad char " + currentChar(offset, data) + " on position " + offset + ".");
             }
         }
+
         return ImmutableList.copyOf(path);
     }
 
index a4659ce2bc56bc2048a596a9bf91cb10145953f3..9f1dcb4e066c0e370086af918b944db2f181a800 100644 (file)
@@ -55,7 +55,7 @@ public final class ParserIdentifier {
             final SchemaContext schemaContext) {
         final YangInstanceIdentifier deserialize;
         if (identifier.contains(RestconfConstants.MOUNT)) {
-            final String mountPointId = identifier.substring(0, identifier.indexOf(RestconfConstants.MOUNT));
+            final String mountPointId = identifier.substring(0, identifier.indexOf("/" + RestconfConstants.MOUNT));
             deserialize = IdentifierCodec.deserialize(mountPointId, schemaContext);
         } else {
             deserialize = IdentifierCodec.deserialize(identifier, schemaContext);
@@ -129,9 +129,7 @@ public final class ParserIdentifier {
             final StringBuilder pathBuilder = new StringBuilder();
             while (componentIter.hasNext()) {
                 final String current = componentIter.next();
-                if (pathBuilder.length() != 0) {
-                    pathBuilder.append("/");
-                }
+                pathBuilder.append("/");
                 pathBuilder.append(current);
                 if (RestconfConstants.MOUNT.equals(current)) {
                     break;
index d688a8f9a6911f01a9647b2df789955610f85374..92615b6278277e486aa25d27ac6a0aa22c951371 100644 (file)
@@ -14,6 +14,7 @@ import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
+import org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants;
 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
 
 /**
@@ -51,9 +52,33 @@ public final class RestconfValidation {
      * @return {@link String}
      */
     public static String validateAndGetModulName(final Iterator<String> moduleName) {
-        RestconfValidationUtils.checkDocumentedError(moduleName.hasNext(), ErrorType.PROTOCOL,
-                ErrorTag.INVALID_VALUE, "Module name must be supplied.");
-        return moduleName.next();
+        RestconfValidationUtils.checkDocumentedError(
+                moduleName.hasNext(),
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                "Module name must be supplied."
+        );
+
+        final String name = moduleName.next();
+
+        RestconfValidationUtils.checkDocumentedError(
+                ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                "Identifier must start with character from set 'a-zA-Z_"
+        );
+
+        RestconfValidationUtils.checkDocumentedError(
+                !name.toUpperCase().startsWith("XML"),
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                "Identifier must NOT start with XML ignore case."
+        );
+
+        RestconfValidationUtils.checkDocumentedError(
+                ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
+                ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
+                "Supplied name has not expected identifier format."
+        );
+
+        return name;
     }
 
 }
index 54b31ef5abc57f97b2080a2bccbd4a568b7e5a9b..b7d6fe8fd79fe765cb58f2614e37c6121a2876aa 100644 (file)
@@ -18,7 +18,6 @@ import java.util.Arrays;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
-import org.junit.Ignore;
 import org.junit.Test;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
@@ -28,7 +27,7 @@ import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
  */
 public class RestconfValidationTest {
     private static final List<String> revisions = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
-    private static final List<String> names = Arrays.asList("module1", "module2", "module3");
+    private static final List<String> names = Arrays.asList("_module-1", "_module-2", "_module-3");
 
     /**
      * Test of successful validation of module revision.
@@ -83,7 +82,7 @@ public class RestconfValidationTest {
     public void validateAndGetModulNameTest() {
         String moduleName = RestconfValidation.validateAndGetModulName(names.iterator());
         assertNotNull("Correct module name should be validated", moduleName);
-        assertEquals("module1", moduleName);
+        assertEquals("_module-1", moduleName);
     }
 
     /**
@@ -103,12 +102,54 @@ public class RestconfValidationTest {
     }
 
     /**
-     * Negative test of module name validation when supplied name is not parsable as module name. Test fails
-     * catching <code>RestconfDocumentedException</code>.
-     * <p>
-     * This test is ignored because tested functionality is not implemented yet.
+     * Negative test of module name validation when supplied name is not parsable as module name on the first
+     * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
+     * error tag and error status code.
+     */
+    @Test
+    public void validateAndGetModuleNameNotParsableFirstTest() {
+       try {
+           RestconfValidation.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 (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());
+       }
+    }
+
+    /**
+     * Negative test of module name validation when supplied name is not parsable as module name on any of the
+     * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
+     * for correct error type, error tag and error status code.
+     */
+    @Test
+    public void validateAndGetModuleNameNotParsableNextTest() {
+        try {
+            RestconfValidation.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 (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());
+        }
+    }
+
+    /**
+     * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
+     * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
      */
-    @Ignore
-    @Test(expected = RestconfDocumentedException.class)
-    public void validateAndGetModuleNameNotParsableTest() {}
+    @Test
+    public void validateAndGetModuleNameNotParsableXmlTest() {
+        try {
+            RestconfValidation.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
+            fail("Test should fail due to module name beginning with 'xMl'");
+        } catch (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());
+        }
+    }
 }