A race condition occurs between ARPHandler and HostTracker if the ARP
[controller.git] / opendaylight / sal / yang-prototype / yang / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / validator / YangModelValidationModuleTest.java
1 /*
2  * Copyright (c) 2013 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.controller.yang.validator;
9
10 import static org.junit.Assert.assertThat;
11 import static org.junit.matchers.JUnitMatchers.containsString;
12 import static org.mockito.Mockito.*;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
17 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_stmtContext;
18 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext;
19 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext;
20 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext;
21 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
22 import org.opendaylight.controller.yang.parser.util.YangValidationException;
23
24 public class YangModelValidationModuleTest {
25
26     private YangModelBasicValidationListener valid;
27
28     @Before
29     public void setUp() {
30         valid = new YangModelBasicValidationListener();
31     }
32
33     @Test(expected = YangValidationException.class)
34     public void testRevisionInvalidDateFormat() {
35         Revision_stmtContext mockedRev = mockModuleWithRevision("badFormat",
36                 "module1");
37
38         try {
39             valid.enterRevision_stmt(mockedRev);
40         } catch (YangValidationException e) {
41             assertThat(
42                     e.getMessage(),
43                     containsString("Revision:badFormat, invalid date format expected date format is:"));
44             throw e;
45         }
46     }
47
48     @Test
49     public void testRevisionValidDateFormat() {
50         Revision_stmtContext mockedRev = mockModuleWithRevision(
51                 YangModelValidationTest.getFormattedDate(), "module1");
52
53         valid.enterRevision_stmt(mockedRev);
54     }
55
56     @Test(expected = YangValidationException.class)
57     public void testNoHeaderStmts() {
58         Revision_stmtContext rev = mockModuleWithRevision("1999-4-5", "module1");
59
60         try {
61             valid.enterModule_stmt((Module_stmtContext) rev.getParent()
62                     .getParent());
63         } catch (YangValidationException e) {
64             assertThat(
65                     e.getMessage(),
66                     containsString("Missing Module-header statement in Module:module1"));
67             throw e;
68         }
69     }
70
71     @Test(expected = YangValidationException.class)
72     public void testMultipleModulesPerSession() {
73         Module_stmtContext module1 = (Module_stmtContext) mockModuleWithRevision(
74                 "1999-09-10", "m1").getParent().getParent();
75         YangModelValidationTest.addChild(module1, YangModelValidationTest
76                 .mockStatement(Namespace_stmtContext.class, ""));
77
78         Module_stmtContext module2 = (Module_stmtContext) mockModuleWithRevision(
79                 "1999-09-10", "m2").getParent().getParent();
80         YangModelValidationTest.addChild(module1, YangModelValidationTest
81                 .mockStatement(Namespace_stmtContext.class, ""));
82         valid.enterModule_stmt(module1);
83
84         try {
85             valid.enterModule_stmt(module2);
86         } catch (YangValidationException e) {
87             assertThat(e.getMessage(),
88                     containsString("Multiple (sub)modules per file"));
89             throw e;
90         }
91     }
92
93     @Test(expected = YangValidationException.class)
94     public void testNoNamespace() {
95         Module_header_stmtsContext header = YangModelValidationTest
96                 .mockStatement(Module_header_stmtsContext.class, null);
97         Module_stmtContext mod = YangModelValidationTest.mockStatement(
98                 Module_stmtContext.class, "module1");
99         YangModelValidationTest.addChild(mod, header);
100
101         try {
102             valid.enterModule_header_stmts(header);
103         } catch (YangValidationException e) {
104             assertThat(
105                     e.getMessage(),
106                     containsString("Missing Namespace statement in Module-header:"));
107             throw e;
108         }
109     }
110
111     @Test(expected = YangValidationException.class)
112     public void testNoPrefix() {
113         Module_header_stmtsContext header = YangModelValidationTest
114                 .mockStatement(Module_header_stmtsContext.class, null);
115         Namespace_stmtContext nmspc = YangModelValidationTest.mockStatement(
116                 Namespace_stmtContext.class, "http://test");
117         Module_stmtContext mod = YangModelValidationTest.mockStatement(
118                 Module_stmtContext.class, "module1");
119         YangModelValidationTest.addChild(mod, header);
120         YangModelValidationTest.addChild(header, nmspc);
121
122         try {
123             valid.enterModule_header_stmts(header);
124         } catch (YangValidationException e) {
125             assertThat(
126                     e.getMessage(),
127                     containsString("Missing Prefix statement in Module-header:"));
128             throw e;
129         }
130     }
131
132     @Test(expected = YangValidationException.class)
133     public void testInvalidYangVersion() {
134
135         Yang_version_stmtContext yangVersion = YangModelValidationTest
136                 .mockStatement(Yang_version_stmtContext.class, "55Unsup");
137
138         Module_stmtContext mod = YangModelValidationTest.mockStatement(
139                 Module_stmtContext.class, "module1");
140         YangModelValidationTest.addChild(mod, yangVersion);
141
142         try {
143             valid.enterYang_version_stmt(yangVersion);
144         } catch (YangValidationException e) {
145             assertThat(
146                     e.getMessage(),
147                     containsString("Unsupported yang version:55Unsup, supported version:"
148                             + BasicValidations.SUPPORTED_YANG_VERSION));
149             throw e;
150         }
151     }
152
153     @Test
154     public void testValidYangVersion() {
155
156         Yang_version_stmtContext ctx = mock(Yang_version_stmtContext.class);
157         doReturn(1).when(ctx).getChildCount();
158         YangModelValidationTest.mockName(ctx, "1");
159
160         valid.enterYang_version_stmt(ctx);
161     }
162
163     private static Revision_stmtContext mockModuleWithRevision(String date,
164             String moduleName) {
165         Revision_stmtContext mockedRev = YangModelValidationTest.mockStatement(
166                 Revision_stmtContext.class, date);
167         Revision_stmtsContext revs = YangModelValidationTest.mockStatement(
168                 Revision_stmtsContext.class, null);
169         Module_stmtContext mod = YangModelValidationTest.mockStatement(
170                 Module_stmtContext.class, moduleName);
171
172         YangModelValidationTest.addChild(revs, mockedRev);
173         YangModelValidationTest.addChild(mod, revs);
174         return mockedRev;
175     }
176 }