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