2 * Copyright (c) 2022 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.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
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;
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);
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);
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());
43 void testOfUntrustedSchemaTreeInference() {
44 final var context = YangParserTestUtils.parseYang("""
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();
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());
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))