Added full YIID to ietf-restconf-monitoring DS
[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.Revision;
24
25 /**
26  * Unit test for {@link ParserIdentifier}'s validate methods.
27  */
28 public class RestconfValidationTest {
29     private static final List<String> REVISIONS = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
30     private static final List<String> NAMES = Arrays.asList("_module-1", "_module-2", "_module-3");
31
32     /**
33      * Test of successful validation of module revision.
34      */
35     @Test
36     public void validateAndGetRevisionTest() {
37         final Revision revision = ParserIdentifier.validateAndGetRevision(REVISIONS.iterator());
38         assertNotNull("Correct module revision should be validated", revision);
39         assertEquals(Revision.of("2014-01-01"), revision);
40     }
41
42     /**
43      * Negative test of module revision validation when there is no revision. Test fails catching
44      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
45      */
46     @Test
47     public void validateAndGetRevisionNotSuppliedTest() {
48         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
49             () -> ParserIdentifier.validateAndGetRevision(Collections.emptyIterator()));
50
51         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
52         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
53         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
54     }
55
56     /**
57      * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
58      * catching <code>RestconfDocumentedException</code>.
59      */
60     @Test
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"));
65     }
66
67     /**
68      * Test of successful validation of module name.
69      */
70     @Test
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);
75     }
76
77     /**
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.
80      */
81     @Test
82     public void validateAndGetModulNameNotSuppliedTest() {
83         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
84             () -> ParserIdentifier.validateAndGetModulName(Collections.emptyIterator()));
85         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
86         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
87         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
88     }
89
90     /**
91      * Negative test of module name validation when supplied name is not parsable as module name on the first
92      * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
93      * error tag and error status code.
94      */
95     @Test
96     public void validateAndGetModuleNameNotParsableFirstTest() {
97         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
98             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
99                 "01-not-parsable-as-name-on-firts-char")));
100         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
101         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
102         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
103     }
104
105     /**
106      * Negative test of module name validation when supplied name is not parsable as module name on any of the
107      * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
108      * for correct error type, error tag and error status code.
109      */
110     @Test
111     public void validateAndGetModuleNameNotParsableNextTest() {
112         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
113             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator(
114                 "not-parsable-as-name-after-first-char*")));
115         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
116         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
117         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
118     }
119
120     /**
121      * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
122      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
123      */
124     @Test
125     public void validateAndGetModuleNameNotParsableXmlTest() {
126         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
127             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("xMl-module-name")));
128         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
129         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
130         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
131     }
132
133     /**
134      * Negative test of module name validation when supplied name is empty. Test fails catching
135      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
136      */
137     @Test
138     public void validateAndGetModuleNameEmptyTest() {
139         final RestconfDocumentedException ex = assertThrows(RestconfDocumentedException.class,
140             () -> ParserIdentifier.validateAndGetModulName(Iterators.singletonIterator("")));
141         assertEquals(RestconfError.ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
142         assertEquals(RestconfError.ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
143         assertEquals(400, ex.getErrors().get(0).getErrorTag().getStatusCode());
144     }
145 }