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