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;
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;
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;
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 {
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;
+ }
}
+++ /dev/null
-/*
- * 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;
- }
-
-}
* 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;
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");
*/
@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);
}
@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());
@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"));
*/
@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);
}
@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());
@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) {
@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) {
@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());
@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());