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.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.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         return assertInstanceOf(cause, actual);
77     }
78
79     public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
80             final Matcher<String> matcher, final String... yangResourceName) {
81         final var ret = assertException(cause, yangResourceName);
82         assertThat(ret.getMessage(), matcher);
83         return ret;
84     }
85
86     public static <E extends IllegalArgumentException> @NonNull E assertArgumentException(final Class<E> cause,
87             final Matcher<String> matcher, final String... yangResourceName) {
88         final var ret = assertException(cause, yangResourceName);
89         assertThat(ret.getMessage(), matcher);
90         return ret;
91     }
92
93     public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
94             final Class<E> cause) {
95         final var ex = assertThrows(SomeModifiersUnresolvedException.class,
96             () -> TestUtils.loadModules(yangResourceName));
97         final var actual = ex.getCause();
98         return assertInstanceOf(cause, actual);
99     }
100
101     public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
102             final Class<E> cause, final Matcher<String> matcher) {
103         final var ret = assertExceptionDir(yangResourceName, cause);
104         assertThat(ret.getMessage(), matcher);
105         return ret;
106     }
107
108     public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
109             final String... yangResourceName) {
110         return assertException(InferenceException.class, matcher, yangResourceName);
111     }
112
113     public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
114             final Matcher<String> matcher) {
115         return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
116     }
117
118     public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
119             final Matcher<String> matcher, final String... yangResourceName) {
120         return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
121     }
122
123     public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
124             final String yangResourceName, final Matcher<String> matcher) {
125         return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
126     }
127
128     public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
129             final Matcher<String> matcher, final String... yangResourceName) {
130         return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
131     }
132
133     public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
134             final Matcher<String> matcher, final String... yangResourceName) {
135         return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
136     }
137
138     public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
139             final String... yangResourceName) {
140         final var ret = assertException(SourceException.class, matcher, yangResourceName);
141         // SourceException is the base of the hierarchy, we should normally assert subclasses
142         assertEquals(SourceException.class, ret.getClass());
143         return ret;
144     }
145
146     public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
147             final Matcher<String> matcher) {
148         final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
149         // SourceException is the base of the hierarchy, we should normally assert subclasses
150         assertEquals(SourceException.class, ret.getClass());
151         return ret;
152     }
153 }