Update YangData(Effective)Statement definition
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1297Test.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.model.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.common.QNameModule;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.common.XMLNamespace;
20 import org.opendaylight.yangtools.yang.common.YangDataName;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23
24 public class YT1297Test {
25     private static final QNameModule RESTCONF =
26         QNameModule.create(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-restconf"), Revision.of("2017-01-26"));
27     private static final QNameModule BAD_MODULE =
28         QNameModule.create(XMLNamespace.of("urn:ietf:params:xml:ns:yang:ietf-restconf"), Revision.of("2018-01-26"));
29
30     private static EffectiveModelContext context;
31
32     private final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
33
34     @BeforeClass
35     public static void beforeClass() {
36         context = YangParserTestUtils.parseYangResource("/ietf-restconf.yang");
37     }
38
39     @Test
40     public void testEnterYangData() {
41         assertNotNull(stack.enterYangData(new YangDataName(RESTCONF, "yang-api")));
42         assertNotNull(stack.enterDataTree(QName.create(RESTCONF, "restconf")));
43     }
44
45     @Test
46     public void testEnterYangDataNegative() {
47         Exception ex = assertThrows(IllegalArgumentException.class,
48             () -> stack.enterYangData(new YangDataName(RESTCONF, "bad-name")));
49         assertEquals("yang-data bad-name not present in " + RESTCONF, ex.getMessage());
50         ex = assertThrows(IllegalArgumentException.class,
51             () -> stack.enterYangData(new YangDataName(BAD_MODULE, "whatever")));
52         assertEquals("Module for " + BAD_MODULE + " not found", ex.getMessage());
53
54         assertNotNull(stack.enterGrouping(QName.create(RESTCONF, "errors")));
55         ex = assertThrows(IllegalStateException.class,
56             () -> stack.enterYangData(new YangDataName(RESTCONF, "yang-api")));
57         assertEquals("Cannot lookup yang-data in a non-empty stack", ex.getMessage());
58     }
59 }