76715e864d17c5dd231a57bafe2029ded3dded2e
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / YangModelValidationListenerTest_Module.java
1 package org.opendaylight.controller.yang.model.parser.impl;
2
3 import static org.hamcrest.core.Is.*;
4 import static org.junit.Assert.*;
5 import static org.junit.matchers.JUnitMatchers.*;
6 import static org.mockito.Mockito.*;
7
8 import java.util.Date;
9
10 import org.antlr.v4.runtime.tree.ParseTree;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Import_stmtContext;
14 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Include_stmtContext;
15 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_header_stmtsContext;
16 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Module_stmtContext;
17 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Namespace_stmtContext;
18 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Prefix_stmtContext;
19 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
20 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtContext;
21 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Revision_stmtsContext;
22 import org.opendaylight.controller.antlrv4.code.gen.YangParser.StringContext;
23 import org.opendaylight.controller.antlrv4.code.gen.YangParser.Yang_version_stmtContext;
24
25 public class YangModelValidationListenerTest_Module {
26
27     private YangModelValidationListener valid;
28     private Module_stmtContext ctx;
29
30     @Before
31     public void setUp() {
32         valid = new YangModelValidationListener();
33     }
34
35     @Test(expected = YangValidationException.class)
36     public void testRevisionInvalidDateFormat() {
37         Revision_stmtContext mockedRev = mockModuleWithRevision(2, "badFormat");
38
39         try {
40             valid.enterRevision_stmt(mockedRev);
41         } catch (YangValidationException e) {
42             assertThat(
43                     e.getMessage(),
44                     containsString("Invalid date format for revision:badFormat in (sub)module:module1, expected date format is:"));
45             throw e;
46         }
47     }
48
49     private Revision_stmtContext mockModuleWithRevision(int moduleChildren,
50             String date) {
51         Revision_stmtContext mockedRev = mock(Revision_stmtContext.class);
52         doReturn(1).when(mockedRev).getChildCount();
53         mockName(mockedRev, date);
54
55         Revision_stmtsContext revs = mockRevisionsParent(2, mockedRev);
56
57         mockModuleParent(moduleChildren, revs, "module1");
58         return mockedRev;
59     }
60
61     @Test
62     public void testRevisionValidDateFormat() {
63         Revision_stmtContext mockedRev = mockModuleWithRevision(2,
64                 getFormattedDate());
65
66         valid.enterRevision_stmt(mockedRev);
67     }
68
69     private String getFormattedDate() {
70         return YangModelParserListenerImpl.simpleDateFormat.format(new Date());
71     }
72
73     @Test(expected = YangValidationException.class)
74     public void testNoRevision() {
75
76         Module_stmtContext ctx = mock(Module_stmtContext.class);
77         doReturn(1).when(ctx).getChildCount();
78         mockName(ctx, "module1");
79
80         try {
81             valid.enterModule_stmt(ctx);
82         } catch (YangValidationException e) {
83             assertThat(
84                     e.getMessage(),
85                     containsString("Missing revision statements in module:module1"));
86             throw e;
87         }
88     }
89
90     @Test(expected = YangValidationException.class)
91     public void testNoHeaderStmts() {
92         mockModuleWithRevision(2, "1999-4-5");
93
94         try {
95             valid.enterModule_stmt(ctx);
96         } catch (YangValidationException e) {
97             assertThat(
98                     e.getMessage(),
99                     containsString("Missing header statements in module:module1"));
100             throw e;
101         }
102     }
103
104     @Test(expected = YangValidationException.class)
105     public void testNoNamespace() {
106         Module_header_stmtsContext header = mock(Module_header_stmtsContext.class);
107         mockModuleParent(2, header, "module1");
108
109         try {
110             valid.enterModule_header_stmts(header);
111         } catch (YangValidationException e) {
112             assertThat(
113                     e.getMessage(),
114                     containsString("Missing namespace statement in module:module1"));
115             throw e;
116         }
117     }
118
119     @Test
120     public void testPrefixes() {
121         Prefix_stmtContext pref = mock(Prefix_stmtContext.class);
122         doReturn(1).when(pref).getChildCount();
123         mockName(pref, "unique1");
124         mockModuleParent(2, pref, "module1");
125         valid.enterPrefix_stmt(pref);
126
127         pref = mock(Prefix_stmtContext.class);
128         doReturn(1).when(pref).getChildCount();
129         mockName(pref, "unique1");
130         mockModuleParent(2, pref, "module1");
131
132         try {
133             valid.enterPrefix_stmt(pref);
134         } catch (Exception e) {
135             return;
136         }
137
138         fail("Validation Exception should have occured");
139     }
140
141     @Test
142     public void testNamespace() {
143         Namespace_stmtContext namespace = mock(Namespace_stmtContext.class);
144         doReturn(1).when(namespace).getChildCount();
145         mockName(namespace, "http://test.parsing.uri.com");
146         mockModuleParent(2, namespace, "module1");
147         valid.enterNamespace_stmt(namespace);
148
149         namespace = mock(Namespace_stmtContext.class);
150         doReturn(1).when(namespace).getChildCount();
151         mockName(namespace, "invalid uri");
152         mockModuleParent(2, namespace, "module1");
153         try {
154             valid.enterNamespace_stmt(namespace);
155         } catch (YangValidationException e) {
156             assertThat(
157                     e.getMessage(),
158                     containsString("Namespace:invalid uri in module:module1 cannot be parsed as URI"));
159             return;
160         }
161
162         fail("Validation Exception should have occured");
163     }
164
165     @Test
166     public void testImports() {
167         Import_stmtContext impor = mockImport("unique1", "p1");
168         mockModuleParent(2, impor, "module1");
169         valid.enterImport_stmt(impor);
170
171         impor = mockImport("unique1", "p2");
172         mockModuleParent(2, impor, "module1");
173         mockName(impor, "unique1");
174
175         try {
176             valid.enterImport_stmt(impor);
177         } catch (YangValidationException e) {
178             assertThat(
179                     e.getMessage(),
180                     containsString("Module:unique1 imported twice in (sub)module:module1"));
181             return;
182         }
183
184         fail("Validation Exception should have occured");
185     }
186
187     @Test
188     public void testIncludes() {
189         Include_stmtContext impor = mockInclude("unique1");
190         mockModuleParent(2, impor, "module1");
191         valid.enterInclude_stmt(impor);
192
193         impor = mockInclude("unique1");
194         mockModuleParent(2, impor, "module1");
195         mockName(impor, "unique1");
196
197         try {
198             valid.enterInclude_stmt(impor);
199         } catch (YangValidationException e) {
200             assertThat(
201                     e.getMessage(),
202                     containsString("Submodule:unique1 included twice in (sub)module:module1"));
203             return;
204         }
205
206         fail("Validation Exception should have occured");
207     }
208
209     private Import_stmtContext mockImport(String name, String prefixName) {
210         Import_stmtContext impor = mock(Import_stmtContext.class);
211         doReturn(3).when(impor).getChildCount();
212         Prefix_stmtContext prefix = mock(Prefix_stmtContext.class);
213         mockName(prefix, prefixName);
214         doReturn(prefix).when(impor).getChild(1);
215         Revision_date_stmtContext revDate = mock(Revision_date_stmtContext.class);
216         mockName(revDate, getFormattedDate());
217         doReturn(revDate).when(impor).getChild(2);
218         mockName(impor, name);
219         return impor;
220     }
221
222     private Include_stmtContext mockInclude(String name) {
223         Include_stmtContext impor = mock(Include_stmtContext.class);
224         doReturn(2).when(impor).getChildCount();
225         Revision_date_stmtContext revDate = mock(Revision_date_stmtContext.class);
226         mockName(revDate, getFormattedDate());
227         doReturn(revDate).when(impor).getChild(1);
228         mockName(impor, name);
229         return impor;
230     }
231
232     @Test(expected = YangValidationException.class)
233     public void testInvalidYangVersion() {
234
235         Yang_version_stmtContext yangVersion = mock(Yang_version_stmtContext.class);
236         doReturn(1).when(yangVersion).getChildCount();
237         mockName(yangVersion, "55Unsup");
238
239         mockModuleParent(2, yangVersion, "module1");
240
241         try {
242             valid.enterYang_version_stmt(yangVersion);
243         } catch (YangValidationException e) {
244             assertThat(
245                     e.getMessage(),
246                     containsString("Unsupported yang version:55Unsup, in (sub)module:module1, supported version:"
247                             + YangModelValidationListener.SUPPORTED_YANG_VERSION));
248             throw e;
249         }
250     }
251
252     private void mockModuleParent(int moduleChildren, ParseTree child,
253             String moduleName) {
254         ctx = mock(Module_stmtContext.class);
255         doReturn(moduleChildren).when(ctx).getChildCount();
256         mockName(ctx, moduleName);
257         doReturn(child).when(ctx).getChild(1);
258         doReturn(ctx).when(child).getParent();
259     }
260
261     static Revision_stmtsContext mockRevisionsParent(int moduleChildren,
262             Revision_stmtContext mockedRev) {
263         Revision_stmtsContext revs = mock(Revision_stmtsContext.class);
264         doReturn(moduleChildren).when(revs).getChildCount();
265         doReturn(mockedRev).when(revs).getChild(1);
266         doReturn(revs).when(mockedRev).getParent();
267         return revs;
268     }
269
270     @Test
271     public void testValidYangVersion() {
272
273         Yang_version_stmtContext ctx = mock(Yang_version_stmtContext.class);
274         doReturn(1).when(ctx).getChildCount();
275         mockName(ctx, "1");
276
277         valid.enterYang_version_stmt(ctx);
278     }
279
280     @Test
281     public void testIdentifierMatching() {
282         YangModelValidationListener.checkIdentifier("_ok98-.87.-.8...88-asdAD",
283                 null);
284         YangModelValidationListener.checkIdentifier("AA.bcd", null);
285         YangModelValidationListener.checkIdentifier("a", null);
286
287         int thrown = 0;
288
289         try {
290             YangModelValidationListener.checkIdentifier("9aa", null);
291         } catch (YangValidationException e) {
292             thrown++;
293         }
294         try {
295             YangModelValidationListener.checkIdentifier("-", null);
296         } catch (YangValidationException e) {
297             thrown++;
298         }
299         try {
300             YangModelValidationListener.checkIdentifier(".", null);
301         } catch (YangValidationException e) {
302             thrown++;
303         }
304
305         assertThat(thrown, is(3));
306     }
307
308     static void mockName(ParseTree mockedRev, String name) {
309         StringContext nameCtx = mock(StringContext.class);
310         ParseTree internalName = mock(ParseTree.class);
311         doReturn(name).when(internalName).getText();
312         doReturn(internalName).when(nameCtx).getChild(0);
313         doReturn(nameCtx).when(mockedRev).getChild(0);
314     }
315 }