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
8 package org.opendaylight.yangtools.yang.stmt;
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;
36 public class YangModelValidationTest {
38 private YangModelBasicValidationListener valid;
43 valid = new YangModelBasicValidationListener();
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);
52 valid.enterPrefix_stmt(pref);
54 pref = mockStatement(Prefix_stmtContext.class, "unique1");
55 module = mockStatement(Module_stmtContext.class, "module1");
56 addChild(module, pref);
59 valid.enterPrefix_stmt(pref);
60 } catch (Exception e) {
64 fail("Validation Exception should have occured");
68 public void testNamespace() {
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);
74 valid.enterNamespace_stmt(namespace);
76 namespace = mockStatement(Namespace_stmtContext.class, "invalid uri");
77 module = mockStatement(Module_stmtContext.class, "module1");
78 addChild(module, namespace);
81 valid.enterNamespace_stmt(namespace);
82 } catch (YangValidationException e) {
83 assertThat(e.getMessage(), containsString("Namespace:invalid uri cannot be parsed as URI"));
87 fail("Validation Exception should have occured");
91 public void testImports() {
92 Import_stmtContext impor = mockImport("unique1", "p1");
93 Module_stmtContext mod = mockStatement(Module_stmtContext.class, "module1");
96 valid.enterImport_stmt(impor);
98 impor = mockImport("unique1", "p2");
99 mod = mockStatement(Module_stmtContext.class, "module1");
100 addChild(mod, impor);
103 valid.enterImport_stmt(impor);
104 } catch (YangValidationException e) {
105 assertThat(e.getMessage(), containsString("Import:unique1 not unique"));
109 fail("Validation Exception should have occured");
113 public void testIncludes() {
114 Include_stmtContext incl = mockInclude("unique1");
115 Module_stmtContext mod = mockStatement(Module_stmtContext.class, "module1");
117 valid.enterInclude_stmt(incl);
119 incl = mockInclude("unique1");
120 mod = mockStatement(Module_stmtContext.class, "module1");
124 valid.enterInclude_stmt(incl);
125 } catch (YangValidationException e) {
126 assertThat(e.getMessage(), containsString("Include:unique1 not unique in (sub)module"));
130 fail("Validation Exception should have occured");
134 // public void testIdentifierMatching() {
135 // List<String> ids = new ArrayList<String>();
137 // ids.add("_ok98-.87.-.8...88-asdAD");
138 // ids.add("AA.bcd");
146 // for (String id : ids) {
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) {
157 // assertEquals(3, thrown);
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);
166 Token token = mock(Token.class);
167 when(augument.getStart()).thenReturn(token);
170 valid.enterAugment_stmt(augument);
171 } catch (YangValidationException e) {
174 containsString("Schema node id:/a:*abc/a:augument1 not in required format, details:Prefixed id:a:*abc not in required format"));
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");
185 // addChild(ctx, add);
186 // addChild(ctx, del);
188 // valid.enterDeviation_stmt(ctx);
190 // HashSet<Class<? extends ParseTree>> types = Sets.newHashSet();
191 // types.add(Deviate_add_stmtContext.class);
192 // types.add(Deviate_delete_stmtContext.class);
194 // int count = ValidationUtil.countPresentChildrenOfType(ctx, types);
195 // assertEquals(2, count);
198 @Test(expected = YangValidationException.class)
199 public void testStatus() throws Exception {
200 Status_argContext status = mockStatement(Status_argContext.class, "unknown");
202 valid.enterStatus_arg(status);
203 } catch (YangValidationException e) {
204 assertThat(e.getMessage(), containsString("illegal value for Status statement, only permitted:"));
209 private static Import_stmtContext mockImport(final String name, final String prefixName) {
210 Import_stmtContext impor = mockStatement(Import_stmtContext.class, name);
212 Prefix_stmtContext prefix = mockStatement(Prefix_stmtContext.class, prefixName);
213 Revision_date_stmtContext revDate = mockStatement(Revision_date_stmtContext.class, getFormattedDate());
215 addChild(impor, prefix);
216 addChild(impor, revDate);
220 static String getFormattedDate() {
221 return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
224 private static Include_stmtContext mockInclude(final String name) {
225 Include_stmtContext incl = mockStatement(Include_stmtContext.class, name);
227 Revision_date_stmtContext revDate = mockStatement(Revision_date_stmtContext.class, getFormattedDate());
229 addChild(incl, revDate);
233 static void mockName(final ParseTree stmt, final String name) {
234 doReturn(1).when(stmt).getChildCount();
236 TerminalNode terminalNode = mock(TerminalNode.class);
237 doReturn(name).when(terminalNode).getText();
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();
244 doReturn(Collections.singletonList(terminalNode)).when(nameCtx).STRING();
247 static <T extends ParseTree> T mockStatement(final Class<T> stmtType, final String name) {
248 T stmt = stmtType.cast(mock(stmtType));
250 doReturn(0).when(stmt).getChildCount();
253 mockName(stmt, name);
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();