Reduce exception guard
[netconf.git] / restconf / restconf-nb / 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.yangtools.yang.common.ErrorTag;
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(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
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(ErrorType.PROTOCOL, ex.getErrors().get(0).getErrorType());
86         assertEquals(ErrorTag.INVALID_VALUE, ex.getErrors().get(0).getErrorTag());
87     }
88
89     /**
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.
93      */
94     @Test
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());
101     }
102
103     /**
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.
107      */
108     @Test
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());
115     }
116
117     /**
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.
120      */
121     @Test
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());
127     }
128
129     /**
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.
132      */
133     @Test
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());
139     }
140 }