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.restconf.common.errors.RestconfError;
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(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
54 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
58 * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
59 * catching <code>RestconfDocumentedException</code>.
62 public void validateAndGetRevisionNotParsableTest() {
63 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
64 () -> ParserIdentifier.validateAndGetRevision(Iterators.singletonIterator("not-parsable-as-date")));
65 assertThat(ex.getMessage(), containsString("Supplied revision is not in expected date format YYYY-mm-dd"));
69 * Test of successful validation of module name.
72 public void validateAndGetModulNameTest() {
73 final String moduleName = ParserIdentifier.validateAndGetModulName(NAMES.iterator());
74 assertNotNull("Correct module name should be validated", moduleName);
75 assertEquals("_module-1", moduleName);
79 * Negative test of module name validation when there is no module name. Test fails catching
80 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
83 public void validateAndGetModulNameNotSuppliedTest() {
84 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
85 () -> ParserIdentifier.validateAndGetModulName(Collections.emptyIterator()));
86 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
87 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
88 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
92 * Negative test of module name validation when supplied name is not parsable as module name on the first
93 * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
94 * error tag and error status code.
97 public void validateAndGetModuleNameNotParsableFirstTest() {
98 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
99 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
100 "01-not-parsable-as-name-on-firts-char")));
101 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
102 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
103 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
107 * Negative test of module name validation when supplied name is not parsable as module name on any of the
108 * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
109 * for correct error type, error tag and error status code.
112 public void validateAndGetModuleNameNotParsableNextTest() {
113 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
114 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
115 "not-parsable-as-name-after-first-char*")));
116 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
117 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
118 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
122 * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
123 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
126 public void validateAndGetModuleNameNotParsableXmlTest() {
127 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
128 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("xMl-module-name")));
129 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
130 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
131 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
135 * Negative test of module name validation when supplied name is empty. Test fails catching
136 * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
139 public void validateAndGetModuleNameEmptyTest() {
140 final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
141 () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("")));
142 assertEquals(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
143 assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
144 assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());