Remove usage of deprecated yang parser classes
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / YangModelValidationTest.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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.hamcrest.CoreMatchers.containsString;
11 import static org.junit.Assert.assertThat;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16 import java.text.SimpleDateFormat;
17 import java.util.Collections;
18 import java.util.Date;
19 import org.antlr.v4.runtime.Token;
20 import org.antlr.v4.runtime.tree.ParseTree;
21 import org.antlr.v4.runtime.tree.TerminalNode;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Augment_stmtContext;
25 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Import_stmtContext;
26 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Include_stmtContext;
27 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
28 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Namespace_stmtContext;
29 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Prefix_stmtContext;
30 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Revision_date_stmtContext;
31 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Status_argContext;
32 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.StringContext;
33 import org.opendaylight.yangtools.yang.parser.impl.YangModelBasicValidationListener;
34 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
35
36 public class YangModelValidationTest {
37
38     private YangModelBasicValidationListener valid;
39
40     @Before
41     public void setUp() {
42
43         valid = new YangModelBasicValidationListener();
44     }
45
46     @Test
47     public void testPrefixes() {
48         Prefix_stmtContext pref = mockStatement(Prefix_stmtContext.class, "unique1");
49         Module_stmtContext module = mockStatement(Module_stmtContext.class, "module1");
50         addChild(module, pref);
51
52         valid.enterPrefix_stmt(pref);
53
54         pref = mockStatement(Prefix_stmtContext.class, "unique1");
55         module = mockStatement(Module_stmtContext.class, "module1");
56         addChild(module, pref);
57
58         try {
59             valid.enterPrefix_stmt(pref);
60         } catch (Exception e) {
61             return;
62         }
63
64         fail("Validation Exception should have occured");
65     }
66
67     @Test
68     public void testNamespace() {
69
70         Namespace_stmtContext namespace = mockStatement(Namespace_stmtContext.class, "http://test.parsing.uri.com");
71         Module_stmtContext module = mockStatement(Module_stmtContext.class, "module1");
72         addChild(module, namespace);
73
74         valid.enterNamespace_stmt(namespace);
75
76         namespace = mockStatement(Namespace_stmtContext.class, "invalid uri");
77         module = mockStatement(Module_stmtContext.class, "module1");
78         addChild(module, namespace);
79
80         try {
81             valid.enterNamespace_stmt(namespace);
82         } catch (YangValidationException e) {
83             assertThat(e.getMessage(), containsString("Namespace:invalid uri cannot be parsed as URI"));
84             return;
85         }
86
87         fail("Validation Exception should have occured");
88     }
89
90     @Test
91     public void testImports() {
92         Import_stmtContext impor = mockImport("unique1", "p1");
93         Module_stmtContext mod = mockStatement(Module_stmtContext.class, "module1");
94         addChild(mod, impor);
95
96         valid.enterImport_stmt(impor);
97
98         impor = mockImport("unique1", "p2");
99         mod = mockStatement(Module_stmtContext.class, "module1");
100         addChild(mod, impor);
101
102         try {
103             valid.enterImport_stmt(impor);
104         } catch (YangValidationException e) {
105             assertThat(e.getMessage(), containsString("Import:unique1 not unique"));
106             return;
107         }
108
109         fail("Validation Exception should have occured");
110     }
111
112     @Test
113     public void testIncludes() {
114         Include_stmtContext incl = mockInclude("unique1");
115         Module_stmtContext mod = mockStatement(Module_stmtContext.class, "module1");
116         addChild(mod, incl);
117         valid.enterInclude_stmt(incl);
118
119         incl = mockInclude("unique1");
120         mod = mockStatement(Module_stmtContext.class, "module1");
121         addChild(mod, incl);
122
123         try {
124             valid.enterInclude_stmt(incl);
125         } catch (YangValidationException e) {
126             assertThat(e.getMessage(), containsString("Include:unique1 not unique in (sub)module"));
127             return;
128         }
129
130         fail("Validation Exception should have occured");
131     }
132
133 //    @Test
134 //    public void testIdentifierMatching() {
135 //        List<String> ids = new ArrayList<String>();
136 //        // valid
137 //        ids.add("_ok98-.87.-.8...88-asdAD");
138 //        ids.add("AA.bcd");
139 //        ids.add("a");
140 //        // invalid
141 //        ids.add("9aa");
142 //        ids.add("-");
143 //        ids.add(".");
144 //
145 //        int thrown = 0;
146 //        for (String id : ids) {
147 //            try {
148 //                Module_stmtContext module = mock(Module_stmtContext.class);
149 //                Token token = mock(Token.class);
150 //                when(module.getStart()).thenReturn(token);
151 //                BasicValidat ions.checkIdentifierInternal(module, id);
152 //            } catch (YangValidationException e) {
153 //                thrown++;
154 //            }
155 //        }
156 //
157 //        assertEquals(3, thrown);
158 //    }
159
160     @Test(expected = YangValidationException.class)
161     public void testAugument() {
162         Augment_stmtContext augument = mockStatement(Augment_stmtContext.class, "/a:*abc/a:augument1");
163         Module_stmtContext mod1 = mockStatement(Module_stmtContext.class, "mod1");
164         addChild(mod1, augument);
165
166         Token token = mock(Token.class);
167         when(augument.getStart()).thenReturn(token);
168
169         try {
170             valid.enterAugment_stmt(augument);
171         } catch (YangValidationException e) {
172             assertThat(
173                     e.getMessage(),
174                     containsString("Schema node id:/a:*abc/a:augument1 not in required format, details:Prefixed id:a:*abc not in required format"));
175             throw e;
176         }
177     }
178
179 //    @Test
180 //    public void testDeviate() {
181 //        Deviation_stmtContext ctx = mockStatement(Deviation_stmtContext.class, "deviations");
182 //        Deviate_add_stmtContext add = mockStatement(Deviate_add_stmtContext.class, "add");
183 //        Deviate_delete_stmtContext del = mockStatement(Deviate_delete_stmtContext.class, "delete");
184 //
185 //        addChild(ctx, add);
186 //        addChild(ctx, del);
187 //
188 //        valid.enterDeviation_stmt(ctx);
189 //
190 //        HashSet<Class<? extends ParseTree>> types = Sets.newHashSet();
191 //        types.add(Deviate_add_stmtContext.class);
192 //        types.add(Deviate_delete_stmtContext.class);
193 //
194 //        int count = ValidationUtil.countPresentChildrenOfType(ctx, types);
195 //        assertEquals(2, count);
196 //    }
197
198     @Test(expected = YangValidationException.class)
199     public void testStatus() throws Exception {
200         Status_argContext status = mockStatement(Status_argContext.class, "unknown");
201         try {
202             valid.enterStatus_arg(status);
203         } catch (YangValidationException e) {
204             assertThat(e.getMessage(), containsString("illegal value for Status statement, only permitted:"));
205             throw e;
206         }
207     }
208
209     private static Import_stmtContext mockImport(final String name, final String prefixName) {
210         Import_stmtContext impor = mockStatement(Import_stmtContext.class, name);
211
212         Prefix_stmtContext prefix = mockStatement(Prefix_stmtContext.class, prefixName);
213         Revision_date_stmtContext revDate = mockStatement(Revision_date_stmtContext.class, getFormattedDate());
214
215         addChild(impor, prefix);
216         addChild(impor, revDate);
217         return impor;
218     }
219
220     static String getFormattedDate() {
221         return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
222     }
223
224     private static Include_stmtContext mockInclude(final String name) {
225         Include_stmtContext incl = mockStatement(Include_stmtContext.class, name);
226
227         Revision_date_stmtContext revDate = mockStatement(Revision_date_stmtContext.class, getFormattedDate());
228
229         addChild(incl, revDate);
230         return incl;
231     }
232
233     static void mockName(final ParseTree stmt, final String name) {
234         doReturn(1).when(stmt).getChildCount();
235
236         TerminalNode terminalNode = mock(TerminalNode.class);
237         doReturn(name).when(terminalNode).getText();
238
239         StringContext nameCtx = mock(StringContext.class);
240         doReturn(nameCtx).when(stmt).getChild(0);
241         doReturn(terminalNode).when(nameCtx).getChild(0);
242         doReturn(name).when(terminalNode).getText();
243
244         doReturn(Collections.singletonList(terminalNode)).when(nameCtx).STRING();
245     }
246
247     static <T extends ParseTree> T mockStatement(final Class<T> stmtType, final String name) {
248         T stmt = stmtType.cast(mock(stmtType));
249
250         doReturn(0).when(stmt).getChildCount();
251
252         if (name != null) {
253             mockName(stmt, name);
254         }
255         return stmt;
256     }
257
258     static void addChild(final ParseTree parent, final ParseTree child) {
259         int childCount = parent.getChildCount() + 1;
260         doReturn(childCount).when(parent).getChildCount();
261         doReturn(child).when(parent).getChild(childCount - 1);
262         doReturn(parent).when(child).getParent();
263     }
264
265 }