Deprecate simple DataTreeFactory.create()
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AbstractYangTest.java
1 /*
2  * Copyright (c) 2021 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.stmt;
9
10 import static org.hamcrest.MatcherAssert.assertThat;
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
13 import static org.junit.jupiter.api.Assertions.assertNotNull;
14 import static org.junit.jupiter.api.Assertions.assertThrows;
15
16 import com.google.common.base.Throwables;
17 import java.util.List;
18 import java.util.Set;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.hamcrest.Matcher;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceException;
25 import org.opendaylight.yangtools.yang.model.ri.type.InvalidBitDefinitionException;
26 import org.opendaylight.yangtools.yang.model.ri.type.InvalidEnumDefinitionException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31
32 /**
33  * Abstract base class containing useful utilities and assertions.
34  */
35 public abstract class AbstractYangTest {
36     public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
37         return assertEffectiveModel(List.of(yangResourceName), null);
38     }
39
40     @SuppressWarnings("checkstyle:illegalCatch")
41     public static @NonNull EffectiveModelContext assertEffectiveModel(final List<String> yangResourceName,
42         final @Nullable Set<QName> supportedFeatures) {
43         final EffectiveModelContext ret;
44         try {
45             ret = TestUtils.parseYangSource(yangResourceName, supportedFeatures);
46         } catch (Exception e) {
47             Throwables.throwIfUnchecked(e);
48             throw new AssertionError("Failed to assemble effective model", e);
49         }
50         assertNotNull(ret);
51         return ret;
52     }
53
54     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
55         return assertEffectiveModelDir(resourceDirName, null);
56     }
57
58     @SuppressWarnings("checkstyle:illegalCatch")
59     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName,
60             final @Nullable Set<QName> supportedFeatures) {
61         final EffectiveModelContext ret;
62         try {
63             ret = TestUtils.loadModules(resourceDirName, supportedFeatures);
64         } catch (Exception e) {
65             Throwables.throwIfUnchecked(e);
66             throw new AssertionError("Failed to assemble effective model of " + resourceDirName, e);
67         }
68         assertNotNull(ret);
69         return ret;
70     }
71
72     public static <E extends RuntimeException> @NonNull E assertException(final Class<E> cause,
73             final String... yangResourceName) {
74         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
75             () -> TestUtils.parseYangSource(yangResourceName));
76         final var actual = ex.getCause();
77         return assertInstanceOf(cause, actual);
78     }
79
80     public static <E extends StatementSourceException> @NonNull E assertException(final Class<E> cause,
81             final Matcher<String> matcher, final String... yangResourceName) {
82         final var ret = assertException(cause, yangResourceName);
83         assertThat(ret.getMessage(), matcher);
84         return ret;
85     }
86
87     public static <E extends IllegalArgumentException> @NonNull E assertArgumentException(final Class<E> cause,
88             final Matcher<String> matcher, final String... yangResourceName) {
89         final var ret = assertException(cause, yangResourceName);
90         assertThat(ret.getMessage(), matcher);
91         return ret;
92     }
93
94     public static <E extends StatementSourceException> @NonNull E assertExceptionDir(final String yangResourceName,
95             final Class<E> cause) {
96         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
97             () -> TestUtils.loadModules(yangResourceName));
98         final var actual = ex.getCause();
99         return assertInstanceOf(cause, actual);
100     }
101
102     public static <E extends StatementSourceException> @NonNull E assertExceptionDir(final String yangResourceName,
103             final Class<E> cause, final Matcher<String> matcher) {
104         final var ret = assertExceptionDir(yangResourceName, cause);
105         assertThat(ret.getMessage(), matcher);
106         return ret;
107     }
108
109     public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
110             final String... yangResourceName) {
111         return assertException(InferenceException.class, matcher, yangResourceName);
112     }
113
114     public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
115             final Matcher<String> matcher) {
116         return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
117     }
118
119     public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
120             final Matcher<String> matcher, final String... yangResourceName) {
121         return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
122     }
123
124     public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
125             final String yangResourceName, final Matcher<String> matcher) {
126         return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
127     }
128
129     public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
130             final Matcher<String> matcher, final String... yangResourceName) {
131         return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
132     }
133
134     public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
135             final Matcher<String> matcher, final String... yangResourceName) {
136         return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
137     }
138
139     public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
140             final String... yangResourceName) {
141         final var ret = assertException(SourceException.class, matcher, yangResourceName);
142         // SourceException is the base of the hierarchy, we should normally assert subclasses
143         assertEquals(SourceException.class, ret.getClass());
144         return ret;
145     }
146
147     public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
148             final Matcher<String> matcher) {
149         final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
150         // SourceException is the base of the hierarchy, we should normally assert subclasses
151         assertEquals(SourceException.class, ret.getClass());
152         return ret;
153     }
154 }