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.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;
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 return assertInstanceOf(cause, actual);
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);
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);
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);
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);
102 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
103 final String... yangResourceName) {
104 return assertException(InferenceException.class, matcher, yangResourceName);
107 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
108 final Matcher<String> matcher) {
109 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
112 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
113 final Matcher<String> matcher, final String... yangResourceName) {
114 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
117 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
118 final String yangResourceName, final Matcher<String> matcher) {
119 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
122 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
123 final Matcher<String> matcher, final String... yangResourceName) {
124 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
127 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
128 final Matcher<String> matcher, final String... yangResourceName) {
129 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
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());
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());