Bug 5912 - Restconf draft11 - utils
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / utils / validation / 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.utils.validation;
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.Calendar;
19 import java.util.Date;
20 import java.util.List;
21 import org.junit.Test;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
23 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
24
25 /**
26  * Unit test for {@link RestconfValidation}
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         Date revision = RestconfValidation.validateAndGetRevision(revisions.iterator());
38         assertNotNull("Correct module revision should be validated", revision);
39
40         Calendar c = Calendar.getInstance();
41         c.setTime(revision);
42
43         assertEquals(2014, c.get(Calendar.YEAR));
44         assertEquals(0, c.get(Calendar.MONTH));
45         assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
46     }
47
48     /**
49      * Negative test of module revision validation when there is no revision. Test fails catching
50      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
51      */
52     @Test
53     public void validateAndGetRevisionNotSuppliedTest() {
54         try {
55             RestconfValidation.validateAndGetRevision(new ArrayList<String>().iterator());
56             fail("Test should fail due to not supplied module revision");
57         } catch (RestconfDocumentedException e) {
58             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
59             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
60             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
61         }
62     }
63
64     /**
65      * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
66      * catching <code>RestconfDocumentedException</code>.
67      */
68     @Test
69     public void validateAndGetRevisionNotParsableTest() {
70         try {
71             RestconfValidation.validateAndGetRevision(Arrays.asList("not-parsable-as-date").iterator());
72             fail("Test should fail due to not parsable module revision");
73         } catch (RestconfDocumentedException e) {
74             assertTrue(e.getMessage().contains("Supplied revision is not in expected date format YYYY-mm-dd"));
75         }
76     }
77
78     /**
79      * Test of successful validation of module name.
80      */
81     @Test
82     public void validateAndGetModulNameTest() {
83         String moduleName = RestconfValidation.validateAndGetModulName(names.iterator());
84         assertNotNull("Correct module name should be validated", moduleName);
85         assertEquals("_module-1", moduleName);
86     }
87
88     /**
89      * Negative test of module name validation when there is no module name. Test fails catching
90      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
91      */
92     @Test
93     public void validateAndGetModulNameNotSuppliedTest() {
94         try {
95             RestconfValidation.validateAndGetModulName(new ArrayList<String>().iterator());
96             fail("Test should fail due to not supplied module name");
97         } catch (RestconfDocumentedException e) {
98             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
99             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
100             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
101         }
102     }
103
104     /**
105      * Negative test of module name validation when supplied name is not parsable as module name on the first
106      * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
107      * error tag and error status code.
108      */
109     @Test
110     public void validateAndGetModuleNameNotParsableFirstTest() {
111        try {
112            RestconfValidation.validateAndGetModulName(
113                    Arrays.asList("01-not-parsable-as-name-on-firts-char").iterator());
114            fail("Test should fail due to not parsable module name on the first character");
115        } catch (RestconfDocumentedException e) {
116            assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
117            assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
118            assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
119        }
120     }
121
122     /**
123      * Negative test of module name validation when supplied name is not parsable as module name on any of the
124      * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
125      * for correct error type, error tag and error status code.
126      */
127     @Test
128     public void validateAndGetModuleNameNotParsableNextTest() {
129         try {
130             RestconfValidation.validateAndGetModulName(
131                     Arrays.asList("not-parsable-as-name-after-first-char*").iterator());
132             fail("Test should fail due to not parsable module name on any character after the first character");
133         } catch (RestconfDocumentedException e) {
134             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
135             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
136             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
137         }
138     }
139
140     /**
141      * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
142      * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
143      */
144     @Test
145     public void validateAndGetModuleNameNotParsableXmlTest() {
146         try {
147             RestconfValidation.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
148             fail("Test should fail due to module name beginning with 'xMl'");
149         } catch (RestconfDocumentedException e) {
150             assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
151             assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
152             assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
153         }
154     }
155 }