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 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 assertThat(actual, instanceOf(cause));
77 return cause.cast(actual);
80 public static <E extends SourceException> @NonNull E assertException(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 IllegalArgumentException> @NonNull E assertArgumentException(final Class<E> cause,
88 final Matcher<String> matcher, final String... yangResourceName) {
89 final var ret = assertException(cause, yangResourceName);
90 assertThat(ret.getMessage(), matcher);
94 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
95 final Class<E> cause) {
96 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
97 () -> TestUtils.loadModules(yangResourceName));
98 final var actual = ex.getCause();
99 assertThat(actual, instanceOf(cause));
100 return cause.cast(actual);
103 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
104 final Class<E> cause, final Matcher<String> matcher) {
105 final var ret = assertExceptionDir(yangResourceName, cause);
106 assertThat(ret.getMessage(), matcher);
110 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
111 final String... yangResourceName) {
112 return assertException(InferenceException.class, matcher, yangResourceName);
115 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
116 final Matcher<String> matcher) {
117 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
120 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
121 final Matcher<String> matcher, final String... yangResourceName) {
122 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
125 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
126 final String yangResourceName, final Matcher<String> matcher) {
127 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
130 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
131 final Matcher<String> matcher, final String... yangResourceName) {
132 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
135 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
136 final Matcher<String> matcher, final String... yangResourceName) {
137 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
140 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
141 final String... yangResourceName) {
142 final var ret = assertException(SourceException.class, matcher, yangResourceName);
143 // SourceException is the base of the hierarchy, we should normally assert subclasses
144 assertEquals(SourceException.class, ret.getClass());
148 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
149 final Matcher<String> matcher) {
150 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
151 // SourceException is the base of the hierarchy, we should normally assert subclasses
152 assertEquals(SourceException.class, ret.getClass());