efa7f15ad7bd9ceb57b4565f5b6dc58cc471c311
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / parser / RestconfValidationTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
9
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;
15
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;
25
26 /**
27  * Unit test for {@link ParserIdentifier}'s validate methods.
28  */
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");
32
33     /**
34      * Test of successful validation of module revision.
35      */
36     @Test
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);
41     }
42
43     /**
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.
46      */
47     @Test
48     public void validateAndGetRevisionNotSuppliedTest() {
49         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
50             () -> ParserIdentifier.validateAndGetRevision(Collections.emptyIterator()));
51
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());
55     }
56
57     /**
58      * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
59      * catching <code>RestconfDocumentedException</code>.
60      */
61     @Test
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"));
66     }
67
68     /**
69      * Test of successful validation of module name.
70      */
71     @Test
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);
76     }
77
78     /**
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.
81      */
82     @Test
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());
89     }
90
91     /**
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.
95      */
96     @Test
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());
104     }
105
106     /**
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.
110      */
111     @Test
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());
119     }
120
121     /**
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.
124      */
125     @Test
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());
132     }
133
134     /**
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.
137      */
138     @Test
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());
145     }
146 }