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;
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;
31 * Abstract base class containing useful utilities and assertions.
33 public abstract class AbstractYangTest {
34 @SuppressWarnings("checkstyle:illegalCatch")
35 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
36 final EffectiveModelContext ret;
38 ret = TestUtils.parseYangSource(yangResourceName);
39 } catch (Exception e) {
40 Throwables.throwIfUnchecked(e);
41 throw new AssertionError("Failed to assemble effective model", e);
47 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
48 return assertEffectiveModelDir(resourceDirName, null);
51 @SuppressWarnings("checkstyle:illegalCatch")
52 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName,
53 final @Nullable Set<QName> supportedFeatures) {
54 final EffectiveModelContext ret;
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);
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 assertThat(actual, instanceOf(cause));
71 return cause.cast(actual);
74 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
75 final Matcher<String> matcher, final String... yangResourceName) {
76 final var ret = assertException(cause, yangResourceName);
77 assertThat(ret.getMessage(), matcher);
81 public static <E extends IllegalArgumentException> @NonNull E assertArgumentException(final Class<E> cause,
82 final Matcher<String> matcher, final String... yangResourceName) {
83 final var ret = assertException(cause, yangResourceName);
84 assertThat(ret.getMessage(), matcher);
88 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
89 final Class<E> cause) {
90 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
91 () -> TestUtils.loadModules(yangResourceName));
92 final var actual = ex.getCause();
93 assertThat(actual, instanceOf(cause));
94 return cause.cast(actual);
97 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
98 final Class<E> cause, final Matcher<String> matcher) {
99 final var ret = assertExceptionDir(yangResourceName, cause);
100 assertThat(ret.getMessage(), matcher);
104 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
105 final String... yangResourceName) {
106 return assertException(InferenceException.class, matcher, yangResourceName);
109 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
110 final Matcher<String> matcher) {
111 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
114 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
115 final Matcher<String> matcher, final String... yangResourceName) {
116 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
119 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
120 final String yangResourceName, final Matcher<String> matcher) {
121 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
124 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
125 final Matcher<String> matcher, final String... yangResourceName) {
126 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
129 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
130 final Matcher<String> matcher, final String... yangResourceName) {
131 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
134 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
135 final String... yangResourceName) {
136 final var ret = assertException(SourceException.class, matcher, yangResourceName);
137 // SourceException is the base of the hierarchy, we should normally assert subclasses
138 assertEquals(SourceException.class, ret.getClass());
142 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
143 final Matcher<String> matcher) {
144 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
145 // SourceException is the base of the hierarchy, we should normally assert subclasses
146 assertEquals(SourceException.class, ret.getClass());