2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.utils.validation;
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;
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;
26 * Unit test for {@link RestconfValidation}
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");
33 * Test of successful validation of module revision.
36 public void validateAndGetRevisionTest() {
37 Date revision = RestconfValidation.validateAndGetRevision(revisions.iterator());
38 assertNotNull("Correct module revision should be validated", revision);
40 Calendar c = Calendar.getInstance();
43 assertEquals(2014, c.get(Calendar.YEAR));
44 assertEquals(0, c.get(Calendar.MONTH));
45 assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
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.
53 public void validateAndGetRevisionNotSuppliedTest() {
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());
65 * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
66 * catching <code>RestconfDocumentedException</code>.
69 public void validateAndGetRevisionNotParsableTest() {
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"));
79 * Test of successful validation of module name.
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);
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.
93 public void validateAndGetModulNameNotSuppliedTest() {
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());
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.
110 public void validateAndGetModuleNameNotParsableFirstTest() {
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());
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.
128 public void validateAndGetModuleNameNotParsableNextTest() {
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());
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.
145 public void validateAndGetModuleNameNotParsableXmlTest() {
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());