2f0ef537461e4ba97b038c8f1a587b0708a1cd14
[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(
34                 YangParserTestUtils.parseYangResourceDirectory("/schema-context-util"));
35         stack.enterSchemaTree(MY_LIST_ID);
36         final var inference = assertInstanceOf(DefaultSchemaTreeInference.class, stack.toSchemaTreeInference());
37         assertEquals(MY_LIST_ID, inference.toSchemaNodeIdentifier());
38         assertEquals(MY_LIST_ID, stack.toSchemaNodeIdentifier());
39         assertEquals(MY_LIST_ID, SchemaInferenceStack.ofInference(inference).toSchemaNodeIdentifier());
40     }
41
42     @Test
43     void testOfUntrustedSchemaTreeInference() {
44         final var context = YangParserTestUtils.parseYang("""
45             module foo {
46               namespace foo;
47               prefix foo;
48
49               container foo;
50
51               container bar {
52                 list foo;
53               }
54             }""");
55         final var foo = context.findSchemaTreeNode(Absolute.of(FOO)).orElseThrow();
56         final var bar = context.findSchemaTreeNode(Absolute.of(BAR)).orElseThrow();
57         final var barFoo = context.findSchemaTreeNode(BAR_FOO_ID).orElseThrow();
58
59         // Let's check that correct thing works out
60         final var correct = DefaultSchemaTreeInference.of(context, BAR_FOO_ID);
61         assertEquals(List.of(bar, barFoo), correct.statementPath());
62         assertEquals(correct.statementPath(),
63                 SchemaInferenceStack.ofUntrusted(correct).toSchemaTreeInference().statementPath());
64
65         // Now let's try some abuse: we use 'foo' instead of 'barFoo', created unsafely ...
66         final var incorrect = DefaultSchemaTreeInference.unsafeOf(context, ImmutableList.of(bar, foo));
67         // ... the default non-verify method is happy to oblige ...
68         assertEquals(incorrect.statementPath(),
69                 SchemaInferenceStack.ofInference(incorrect).toSchemaTreeInference().statementPath());
70         // ... but ofUntrusted() will reject it
71         assertEquals("Provided " + incorrect + " is not consistent with resolved path " + correct,
72                 assertThrows(IllegalArgumentException.class, () -> SchemaInferenceStack.ofUntrusted(incorrect))
73                         .getMessage());
74     }
75 }