Modernize SchemaInferenceStack
[yangtools.git] / model / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / YT1414Test.java
1 /*
2  * Copyright (c) 2022 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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13
14 import com.google.common.collect.ImmutableList;
15 import java.util.List;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
19 import org.opendaylight.yangtools.yang.model.spi.DefaultSchemaTreeInference;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21
22 class YT1414Test {
23     private static final QName MY_CONTAINER = QName.create("uri:my-module", "2014-10-07", "my-container");
24     private static final QName MY_LIST = QName.create(MY_CONTAINER, "my-list");
25     private static final Absolute MY_LIST_ID = Absolute.of(MY_CONTAINER, MY_LIST);
26
27     private static final QName FOO = QName.create("foo", "foo");
28     private static final QName BAR = QName.create(FOO, "bar");
29     private static final Absolute BAR_FOO_ID = Absolute.of(BAR, FOO);
30
31     @Test
32     void testToFromSchemaTreeInference() {
33         final var stack = SchemaInferenceStack.of(SchemaInferenceStackTest.CONTEXT);
34         final var ex = assertThrows(IllegalStateException.class, stack::toSchemaTreeInference);
35         assertEquals("Cannot convert uninstantiated context SchemaInferenceStack{path=[]}", ex.getMessage());
36
37         stack.enterSchemaTree(MY_LIST_ID);
38         final var inference = assertInstanceOf(DefaultSchemaTreeInference.class, stack.toSchemaTreeInference());
39         assertEquals(MY_LIST_ID, inference.toSchemaNodeIdentifier());
40         assertEquals(MY_LIST_ID, stack.toSchemaNodeIdentifier());
41         assertEquals(MY_LIST_ID, SchemaInferenceStack.ofInference(inference).toSchemaNodeIdentifier());
42     }
43
44     @Test
45     void testOfUntrustedSchemaTreeInference() {
46         final var context = YangParserTestUtils.parseYang("""
47             module foo {
48               namespace foo;
49               prefix foo;
50
51               container foo;
52
53               container bar {
54                 list foo;
55               }
56             }""");
57         final var foo = context.findSchemaTreeNode(Absolute.of(FOO)).orElseThrow();
58         final var bar = context.findSchemaTreeNode(Absolute.of(BAR)).orElseThrow();
59         final var barFoo = context.findSchemaTreeNode(BAR_FOO_ID).orElseThrow();
60
61         // Let's check that correct thing works out
62         final var correct = DefaultSchemaTreeInference.of(context, BAR_FOO_ID);
63         assertEquals(List.of(bar, barFoo), correct.statementPath());
64         assertEquals(correct.statementPath(),
65                 SchemaInferenceStack.ofUntrusted(correct).toSchemaTreeInference().statementPath());
66
67         // Now let's try some abuse: we use 'foo' instead of 'barFoo', created unsafely ...
68         final var incorrect = DefaultSchemaTreeInference.unsafeOf(context, ImmutableList.of(bar, foo));
69         // ... the default non-verify method is happy to oblige ...
70         assertEquals(incorrect.statementPath(),
71                 SchemaInferenceStack.ofInference(incorrect).toSchemaTreeInference().statementPath());
72         // ... but ofUntrusted() will reject it
73         assertEquals("Provided " + incorrect + " is not consistent with resolved path " + correct,
74                 assertThrows(IllegalArgumentException.class, () -> SchemaInferenceStack.ofUntrusted(incorrect))
75                         .getMessage());
76     }
77 }