2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
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;
28 * Abstract base class containing useful utilities and assertions.
30 public abstract class AbstractYangTest {
31 @SuppressWarnings("checkstyle:illegalCatch")
32 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
33 final EffectiveModelContext ret;
35 ret = TestUtils.parseYangSource(yangResourceName);
36 } catch (Exception e) {
37 Throwables.throwIfUnchecked(e);
38 throw new AssertionError("Failed to assemble effective model", e);
44 @SuppressWarnings("checkstyle:illegalCatch")
45 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
46 final EffectiveModelContext ret;
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);
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);
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);
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);
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);
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);
96 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
97 final String... yangResourceName) {
98 return assertException(InferenceException.class, matcher, yangResourceName);
101 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
102 final Matcher<String> matcher) {
103 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
106 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
107 final Matcher<String> matcher, final String... yangResourceName) {
108 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
111 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
112 final String yangResourceName, final Matcher<String> matcher) {
113 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
116 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
117 final Matcher<String> matcher, final String... yangResourceName) {
118 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
121 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
122 final Matcher<String> matcher, final String... yangResourceName) {
123 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
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());
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());