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