2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
16 import com.google.common.collect.Iterators;
17 import java.util.Arrays;
18 import java.util.Collections;
19 import java.util.List;
20 import org.junit.Test;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.yangtools.yang.common.ErrorTag;
23 import org.opendaylight.yangtools.yang.common.ErrorType;
24 import org.opendaylight.yangtools.yang.common.Revision;
27 * Unit test for {@link ParserIdentifier}'s validate methods.
29 public class RestconfValidationTest {
30 private static final List<String> REVISIONS = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
31 private static final List<String> NAMES = Arrays.asList("_module-1", "_module-2", "_module-3");
34 * Test of successful validation of module revision.
37 public void validateAndGetRevisionTest() {
38 final Revision revision = ParserIdentifier.validateAndGetRevision(REVISIONS.iterator());
39 assertNotNull("Correct module revision should be validated", revision);
40 assertEquals(Revision.of("2014-01-01"), revision);
44 * Negative test of module revision validation when there is no revision. Test fails catching
45 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
48 public void validateAndGetRevisionNotSuppliedTest() {
49 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
50 () -> ParserIdentifier.validateAndGetRevision(Collections.emptyIterator()));
52 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
53 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
57 * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
58 * catching <code>RestconfDocumentedException</code>.
61 public void validateAndGetRevisionNotParsableTest() {
62 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
63 () -> ParserIdentifier.validateAndGetRevision(Iterators.singletonIterator("not-parsable-as-date")));
64 assertThat(ex.getMessage(), containsString("Supplied revision is not in expected date format YYYY-mm-dd"));
68 * Test of successful validation of module name.
71 public void validateAndGetModulNameTest() {
72 final String moduleName = ParserIdentifier.validateAndGetModulName(NAMES.iterator());
73 assertNotNull("Correct module name should be validated", moduleName);
74 assertEquals("_module-1", moduleName);
78 * Negative test of module name validation when there is no module name. Test fails catching
79 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
82 public void validateAndGetModulNameNotSuppliedTest() {
83 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
84 () -> ParserIdentifier.validateAndGetModulName(Collections.emptyIterator()));
85 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
86 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
90 * Negative test of module name validation when supplied name is not parsable as module name on the first
91 * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
92 * error tag and error status code.
95 public void validateAndGetModuleNameNotParsableFirstTest() {
96 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
97 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
98 "01-not-parsable-as-name-on-firts-char")));
99 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
100 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
104 * Negative test of module name validation when supplied name is not parsable as module name on any of the
105 * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
106 * for correct error type, error tag and error status code.
109 public void validateAndGetModuleNameNotParsableNextTest() {
110 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
111 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
112 "not-parsable-as-name-after-first-char*")));
113 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
114 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
118 * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
119 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
122 public void validateAndGetModuleNameNotParsableXmlTest() {
123 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
124 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("xMl-module-name")));
125 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
126 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
130 * Negative test of module name validation when supplied name is empty. Test fails catching
131 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
134 public void validateAndGetModuleNameEmptyTest() {
135 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
136 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("")));
137 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
138 assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());