Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / utils / validations / 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
9 package org.opendaylight.restconf.nb.rfc8040.utils.validations;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
21 import org.opendaylight.restconf.common.errors.RestconfError;
22 import org.opendaylight.yangtools.yang.common.Revision;
23
24 /**
25  * Unit test for {@link RestconfValidation}.
26  */
27 public class RestconfValidationTest {
28     private static final List<String> REVISIONS = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
29     private static final List<String> NAMES = Arrays.asList("_module-1", "_module-2", "_module-3");
30
31     /**
32      * Test of successful validation of module revision.
33      */
34     @Test
35     public void validateAndGetRevisionTest() {
36         final Revision revision = RestconfValidation.validateAndGetRevision(REVISIONS.iterator());
37         assertNotNull("Correct module revision should be validated", revision);
38         assertEquals(Revision.of("2014-01-01"), revision);
39     }
40
41     /**
42      * Negative test of module revision validation when there is no revision. Test fails catching
43      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
44      */
45     @Test
46     public void validateAndGetRevisionNotSuppliedTest() {
47         try {
48             RestconfValidation.validateAndGetRevision(new ArrayList<String>().iterator());
49             fail("Test should fail due to not supplied module revision");
50         } catch (final RestconfDocumentedException e) {
51             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
52             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
53             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
54         }
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         try {
64             RestconfValidation.validateAndGetRevision(Arrays.asList("not-parsable-as-date").iterator());
65             fail("Test should fail due to not parsable module revision");
66         } catch (final RestconfDocumentedException e) {
67             assertTrue(e.getMessage().contains("Supplied revision is not in expected date format YYYY-mm-dd"));
68         }
69     }
70
71     /**
72      * Test of successful validation of module name.
73      */
74     @Test
75     public void validateAndGetModulNameTest() {
76         final String moduleName = RestconfValidation.validateAndGetModulName(NAMES.iterator());
77         assertNotNull("Correct module name should be validated", moduleName);
78         assertEquals("_module-1", moduleName);
79     }
80
81     /**
82      * Negative test of module name validation when there is no module name. Test fails catching
83      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
84      */
85     @Test
86     public void validateAndGetModulNameNotSuppliedTest() {
87         try {
88             RestconfValidation.validateAndGetModulName(new ArrayList<String>().iterator());
89             fail("Test should fail due to not supplied module name");
90         } catch (final RestconfDocumentedException e) {
91             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
92             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
93             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
94         }
95     }
96
97     /**
98      * Negative test of module name validation when supplied name is not parsable as module name on the first
99      * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
100      * error tag and error status code.
101      */
102     @Test
103     public void validateAndGetModuleNameNotParsableFirstTest() {
104         try {
105             RestconfValidation.validateAndGetModulName(
106                     Arrays.asList("01-not-parsable-as-name-on-firts-char").iterator());
107             fail("Test should fail due to not parsable module name on the first character");
108         } catch (final RestconfDocumentedException e) {
109             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
110             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
111             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
112         }
113     }
114
115     /**
116      * Negative test of module name validation when supplied name is not parsable as module name on any of the
117      * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
118      * for correct error type, error tag and error status code.
119      */
120     @Test
121     public void validateAndGetModuleNameNotParsableNextTest() {
122         try {
123             RestconfValidation.validateAndGetModulName(
124                     Arrays.asList("not-parsable-as-name-after-first-char*").iterator());
125             fail("Test should fail due to not parsable module name on any character after the first character");
126         } catch (final RestconfDocumentedException e) {
127             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
128             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
129             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
130         }
131     }
132
133     /**
134      * Negative test of module name validation when supplied name begins with 'XML' ignore case. 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 validateAndGetModuleNameNotParsableXmlTest() {
139         try {
140             RestconfValidation.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
141             fail("Test should fail due to module name beginning with 'xMl'");
142         } catch (final RestconfDocumentedException e) {
143             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
144             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
145             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
146         }
147     }
148
149     /**
150      * Negative test of module name validation when supplied name is empty. Test fails catching
151      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
152      */
153     @Test
154     public void validateAndGetModuleNameEmptyTest() {
155         try {
156             RestconfValidation.validateAndGetModulName(Arrays.asList("").iterator());
157             fail("Test should fail due to empty module name");
158         } catch (final RestconfDocumentedException e) {
159             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
160             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
161             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
162         }
163     }
164 }