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;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.hamcrest.Matcher;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.ri.type.InvalidBitDefinitionException;
25 import org.opendaylight.yangtools.yang.model.ri.type.InvalidEnumDefinitionException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32 * Abstract base class containing useful utilities and assertions.
34 public abstract class AbstractYangTest {
35 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
36 return assertEffectiveModel(List.of(yangResourceName), null);
39 @SuppressWarnings("checkstyle:illegalCatch")
40 public static @NonNull EffectiveModelContext assertEffectiveModel(final List<String> yangResourceName,
41 final @Nullable Set<QName> supportedFeatures) {
42 final EffectiveModelContext ret;
44 ret = TestUtils.parseYangSource(yangResourceName, supportedFeatures);
45 } catch (Exception e) {
46 Throwables.throwIfUnchecked(e);
47 throw new AssertionError("Failed to assemble effective model", e);
53 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
54 return assertEffectiveModelDir(resourceDirName, null);
57 @SuppressWarnings("checkstyle:illegalCatch")
58 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName,
59 final @Nullable Set<QName> supportedFeatures) {
60 final EffectiveModelContext ret;
62 ret = TestUtils.loadModules(resourceDirName, supportedFeatures);
63 } catch (Exception e) {
64 Throwables.throwIfUnchecked(e);
65 throw new AssertionError("Failed to assemble effective model of " + resourceDirName, e);
71 public static <E extends RuntimeException> @NonNull E assertException(final Class<E> cause,
72 final String... yangResourceName) {
73 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
74 () -> TestUtils.parseYangSource(yangResourceName));
75 final var actual = ex.getCause();
76 return assertInstanceOf(cause, actual);
79 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
80 final Matcher<String> matcher, final String... yangResourceName) {
81 final var ret = assertException(cause, yangResourceName);
82 assertThat(ret.getMessage(), matcher);
86 public static <E extends IllegalArgumentException> @NonNull E assertArgumentException(final Class<E> cause,
87 final Matcher<String> matcher, final String... yangResourceName) {
88 final var ret = assertException(cause, yangResourceName);
89 assertThat(ret.getMessage(), matcher);
93 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
94 final Class<E> cause) {
95 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
96 () -> TestUtils.loadModules(yangResourceName));
97 final var actual = ex.getCause();
98 return assertInstanceOf(cause, actual);
101 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
102 final Class<E> cause, final Matcher<String> matcher) {
103 final var ret = assertExceptionDir(yangResourceName, cause);
104 assertThat(ret.getMessage(), matcher);
108 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
109 final String... yangResourceName) {
110 return assertException(InferenceException.class, matcher, yangResourceName);
113 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
114 final Matcher<String> matcher) {
115 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
118 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
119 final Matcher<String> matcher, final String... yangResourceName) {
120 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
123 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
124 final String yangResourceName, final Matcher<String> matcher) {
125 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
128 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
129 final Matcher<String> matcher, final String... yangResourceName) {
130 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
133 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
134 final Matcher<String> matcher, final String... yangResourceName) {
135 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
138 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
139 final String... yangResourceName) {
140 final var ret = assertException(SourceException.class, matcher, yangResourceName);
141 // SourceException is the base of the hierarchy, we should normally assert subclasses
142 assertEquals(SourceException.class, ret.getClass());
146 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
147 final Matcher<String> matcher) {
148 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
149 // SourceException is the base of the hierarchy, we should normally assert subclasses
150 assertEquals(SourceException.class, ret.getClass());