Fix ReactorStmtCtx.calculateParentRefcount()
[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.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.ri.type.InvalidBitDefinitionException;
25 import org.opendaylight.yangtools.yang.model.ri.type.InvalidEnumDefinitionException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30
31 /**
32  * Abstract base class containing useful utilities and assertions.
33  */
34 public abstract class AbstractYangTest {
35     public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
36         return assertEffectiveModel(List.of(yangResourceName), null);
37     }
38
39     @SuppressWarnings("checkstyle:illegalCatch")
40     public static @NonNull EffectiveModelContext assertEffectiveModel(final List<String> yangResourceName,
41         final @Nullable Set<QName> supportedFeatures) {
42         final EffectiveModelContext ret;
43         try {
44             ret = TestUtils.parseYangSource(yangResourceName, supportedFeatures);
45         } catch (Exception e) {
46             Throwables.throwIfUnchecked(e);
47             throw new AssertionError("Failed to assemble effective model", e);
48         }
49         assertNotNull(ret);
50         return ret;
51     }
52
53     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
54         return assertEffectiveModelDir(resourceDirName, null);
55     }
56
57     @SuppressWarnings("checkstyle:illegalCatch")
58     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName,
59             final @Nullable Set<QName> supportedFeatures) {
60         final EffectiveModelContext ret;
61         try {
62             ret = TestUtils.loadModules(resourceDirName, supportedFeatures);
63         } catch (Exception e) {
64             Throwables.throwIfUnchecked(e);
65             throw new AssertionError("Failed to assemble effective model of " + resourceDirName, e);
66         }
67         assertNotNull(ret);
68         return ret;
69     }
70
71     public static <E extends RuntimeException> @NonNull E assertException(final Class<E> cause,
72             final String... yangResourceName) {
73         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
74             () -> TestUtils.parseYangSource(yangResourceName));
75         final var actual = ex.getCause();
76         assertThat(actual, instanceOf(cause));
77         return cause.cast(actual);
78     }
79
80     public static <E extends SourceException> @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 SourceException> @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         assertThat(actual, instanceOf(cause));
100         return cause.cast(actual);
101     }
102
103     public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
104             final Class<E> cause, final Matcher<String> matcher) {
105         final var ret = assertExceptionDir(yangResourceName, cause);
106         assertThat(ret.getMessage(), matcher);
107         return ret;
108     }
109
110     public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
111             final String... yangResourceName) {
112         return assertException(InferenceException.class, matcher, yangResourceName);
113     }
114
115     public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
116             final Matcher<String> matcher) {
117         return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
118     }
119
120     public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
121             final Matcher<String> matcher, final String... yangResourceName) {
122         return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
123     }
124
125     public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
126             final String yangResourceName, final Matcher<String> matcher) {
127         return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
128     }
129
130     public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
131             final Matcher<String> matcher, final String... yangResourceName) {
132         return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
133     }
134
135     public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
136             final Matcher<String> matcher, final String... yangResourceName) {
137         return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
138     }
139
140     public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
141             final String... yangResourceName) {
142         final var ret = assertException(SourceException.class, matcher, yangResourceName);
143         // SourceException is the base of the hierarchy, we should normally assert subclasses
144         assertEquals(SourceException.class, ret.getClass());
145         return ret;
146     }
147
148     public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
149             final Matcher<String> matcher) {
150         final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
151         // SourceException is the base of the hierarchy, we should normally assert subclasses
152         assertEquals(SourceException.class, ret.getClass());
153         return ret;
154     }
155 }