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.nb.rfc8040.utils.validations;
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.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;
25 * Unit test for {@link RestconfValidation}.
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");
32 * Test of successful validation of module revision.
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);
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.
46 public void validateAndGetRevisionNotSuppliedTest() {
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());
58 * Negative test of module revision validation when supplied revision is not parsable as revision. Test fails
59 * catching <code>RestconfDocumentedException</code>.
62 public void validateAndGetRevisionNotParsableTest() {
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"));
72 * Test of successful validation of module name.
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);
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.
86 public void validateAndGetModulNameNotSuppliedTest() {
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());
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.
103 public void validateAndGetModuleNameNotParsableFirstTest() {
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());
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.
121 public void validateAndGetModuleNameNotParsableNextTest() {
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());
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.
138 public void validateAndGetModuleNameNotParsableXmlTest() {
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());
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.
154 public void validateAndGetModuleNameEmptyTest() {
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());