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