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);
"Bad char " + currentChar(offset, data) + " on position " + offset + ".");
}
}
+
return ImmutableList.copyOf(path);
}
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);
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;
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;
/**
* @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;
}
}
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;
*/
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.
public void validateAndGetModulNameTest() {
String moduleName = RestconfValidation.validateAndGetModulName(names.iterator());
assertNotNull("Correct module name should be validated", moduleName);
- assertEquals("module1", moduleName);
+ assertEquals("_module-1", moduleName);
}
/**
}
/**
- * 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());
+ }
+ }
}