Move SimpleSchemaContext
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1231Test.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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.api.stmt.CaseEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ChoiceEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
22
23 public class YT1231Test {
24     private static final QName FOO = QName.create("foo", "foo");
25     private static final QName BAR = QName.create("foo", "bar");
26     private static final QName BAZ = QName.create("foo", "baz");
27     private static final QName XYZZY = QName.create("foo", "xyzzy");
28
29     @Test
30     public void testEnterDataTree() {
31         final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt1231.yang");
32         final SchemaInferenceStack stack = new SchemaInferenceStack(context);
33
34         // Trivial
35         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
36         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
37         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
38         assertEquals(Absolute.of(FOO, FOO), stack.toSchemaNodeIdentifier());
39         stack.exit();
40
41         // Has to cross four layers of choice/case
42         assertThat(stack.enterDataTree(XYZZY), instanceOf(ContainerEffectiveStatement.class));
43         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ, BAZ, XYZZY), stack.toSchemaNodeIdentifier());
44
45         stack.exit();
46         assertThat(stack.enterSchemaTree(BAR), instanceOf(ChoiceEffectiveStatement.class));
47         assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
48         assertEquals(Absolute.of(FOO, BAR, BAR), stack.toSchemaNodeIdentifier());
49     }
50
51     @Test
52     public void testEnterChoice() {
53         final EffectiveModelContext context = YangParserTestUtils.parseYangResource("/yt1231.yang");
54         final SchemaInferenceStack stack = new SchemaInferenceStack(context);
55
56         // Simple
57         assertThat(stack.enterDataTree(FOO), instanceOf(ContainerEffectiveStatement.class));
58         assertEquals(Absolute.of(FOO), stack.toSchemaNodeIdentifier());
59         assertThat(stack.enterChoice(BAR), instanceOf(ChoiceEffectiveStatement.class));
60         assertEquals(Absolute.of(FOO, BAR), stack.toSchemaNodeIdentifier());
61
62         // Has to cross choice -> case -> choice
63         assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
64         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());
65
66         // Now the same with just case -> choice
67         stack.exit();
68         assertThat(stack.enterSchemaTree(BAR), instanceOf(CaseEffectiveStatement.class));
69         assertThat(stack.enterChoice(BAZ), instanceOf(ChoiceEffectiveStatement.class));
70         assertEquals(Absolute.of(FOO, BAR, BAR, BAZ), stack.toSchemaNodeIdentifier());
71     }
72 }