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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThrows;
15 import com.google.common.collect.ImmutableList;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
20 import org.opendaylight.yangtools.yang.model.spi.DefaultSchemaTreeInference;
21 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
23 public class YT1414Test {
24 private static final QName MY_CONTAINER = QName.create("uri:my-module", "2014-10-07", "my-container");
25 private static final QName MY_LIST = QName.create(MY_CONTAINER, "my-list");
26 private static final Absolute MY_LIST_ID = Absolute.of(MY_CONTAINER, MY_LIST);
28 private static final QName FOO = QName.create("foo", "foo");
29 private static final QName BAR = QName.create(FOO, "bar");
30 private static final Absolute BAR_FOO_ID = Absolute.of(BAR, FOO);
33 public void testToFromSchemaTreeInference() {
34 final var stack = SchemaInferenceStack.of(
35 YangParserTestUtils.parseYangResourceDirectory("/schema-context-util"));
36 stack.enterSchemaTree(MY_LIST_ID);
37 final var inference = stack.toSchemaTreeInference();
38 assertThat(inference, instanceOf(DefaultSchemaTreeInference.class));
39 assertEquals(MY_LIST_ID, inference.toSchemaNodeIdentifier());
40 assertEquals(MY_LIST_ID, stack.toSchemaNodeIdentifier());
41 assertEquals(MY_LIST_ID, SchemaInferenceStack.ofInference(inference).toSchemaNodeIdentifier());
45 public void testOfUntrustedSchemaTreeInference() {
46 final var context = YangParserTestUtils.parseYangResource("/yt1414.yang");
47 final var foo = context.findSchemaTreeNode(Absolute.of(FOO)).orElseThrow();
48 final var bar = context.findSchemaTreeNode(Absolute.of(BAR)).orElseThrow();
49 final var barFoo = context.findSchemaTreeNode(BAR_FOO_ID).orElseThrow();
51 // Let's check that correct thing works out
52 final var correct = DefaultSchemaTreeInference.of(context, BAR_FOO_ID);
53 assertEquals(List.of(bar, barFoo), correct.statementPath());
54 assertEquals(correct.statementPath(),
55 SchemaInferenceStack.ofUntrusted(correct).toSchemaTreeInference().statementPath());
57 // Now let's try some abuse: we use 'foo' instead of 'barFoo', created unsafely ...
58 final var incorrect = DefaultSchemaTreeInference.unsafeOf(context, ImmutableList.of(bar, foo));
59 // ... the default non-verify method is happy to oblige ...
60 assertEquals(incorrect.statementPath(),
61 SchemaInferenceStack.ofInference(incorrect).toSchemaTreeInference().statementPath());
62 // ... but ofUntrusted() will reject it
63 assertEquals("Provided " + incorrect + " is not consistent with resolved path " + correct,
64 assertThrows(IllegalArgumentException.class, () -> SchemaInferenceStack.ofUntrusted(incorrect))