2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.model.util;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import org.junit.jupiter.api.BeforeAll;
15 import org.junit.jupiter.api.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;
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"));
30 private static EffectiveModelContext context;
32 private final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
35 static void beforeClass() {
36 context = YangParserTestUtils.parseYangResource("/ietf-restconf.yang");
40 void testEnterYangData() {
41 assertNotNull(stack.enterYangData(new YangDataName(RESTCONF, "yang-api")));
42 assertNotNull(stack.enterDataTree(QName.create(RESTCONF, "restconf")));
46 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());
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());