Add DefaultSchemaTreeInference.unsafeOf()
[yangtools.git] / model / yang-model-spi / src / test / java / org / opendaylight / yangtools / yang / model / spi / 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.spi;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertSame;
13 import static org.junit.Assert.assertThrows;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import com.google.common.collect.ImmutableList;
18 import java.util.Optional;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ContainerEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
28
29 @RunWith(MockitoJUnitRunner.StrictStubs.class)
30 public class YT1414Test {
31     @Mock
32     public EffectiveModelContext modelContext;
33     @Mock
34     public ContainerEffectiveStatement container;
35
36     @Test
37     public void testUnsafeOf() {
38         final var path = ImmutableList.of(container);
39         final var inference = DefaultSchemaTreeInference.unsafeOf(modelContext, path);
40         assertSame(modelContext, inference.getEffectiveModelContext());
41         assertSame(path, inference.statementPath());
42     }
43
44     @Test
45     public void testVerifiedOf() {
46         final var qname = QName.create("foo", "foo");
47         doReturn(qname).when(container).argument();
48
49         final var module = mock(ModuleEffectiveStatement.class);
50         doReturn(Optional.of(module)).when(modelContext).findModuleStatement(qname.getModule());
51         doReturn(Optional.of(container)).when(module).findSchemaTreeNode(qname);
52
53         final var path = ImmutableList.of(container);
54         final var inference = DefaultSchemaTreeInference.verifiedOf(modelContext, path);
55
56         assertSame(modelContext, inference.getEffectiveModelContext());
57         assertSame(path, inference.statementPath());
58     }
59
60     @Test
61     public void testVerifiedOfNegative() {
62         final var qname = QName.create("foo", "foo");
63         doReturn(qname).when(container).argument();
64
65         final var module = mock(ModuleEffectiveStatement.class);
66         doReturn(Optional.of(module)).when(modelContext).findModuleStatement(qname.getModule());
67         doReturn(Optional.of(mock(ListEffectiveStatement.class))).when(module).findSchemaTreeNode(qname);
68
69         assertThat(assertThrows(IllegalArgumentException.class,
70             () -> DefaultSchemaTreeInference.verifiedOf(modelContext, ImmutableList.of(container)))
71             .getMessage(), startsWith(
72                 "Provided path [container] is not consistent with resolved path [Mock for ListEffectiveStatement, "));
73     }
74 }