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.api.meta.StatementSourceException;
25 import org.opendaylight.yangtools.yang.model.ri.type.InvalidBitDefinitionException;
26 import org.opendaylight.yangtools.yang.model.ri.type.InvalidEnumDefinitionException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 * Abstract base class containing useful utilities and assertions.
35 public abstract class AbstractYangTest {
36 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
37 return assertEffectiveModel(List.of(yangResourceName), null);
40 @SuppressWarnings("checkstyle:illegalCatch")
41 public static @NonNull EffectiveModelContext assertEffectiveModel(final List<String> yangResourceName,
42 final @Nullable Set<QName> supportedFeatures) {
43 final EffectiveModelContext ret;
45 ret = TestUtils.parseYangSource(yangResourceName, supportedFeatures);
46 } catch (Exception e) {
47 Throwables.throwIfUnchecked(e);
48 throw new AssertionError("Failed to assemble effective model", e);
54 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
55 return assertEffectiveModelDir(resourceDirName, null);
58 @SuppressWarnings("checkstyle:illegalCatch")
59 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName,
60 final @Nullable Set<QName> supportedFeatures) {
61 final EffectiveModelContext ret;
63 ret = TestUtils.loadModules(resourceDirName, supportedFeatures);
64 } catch (Exception e) {
65 Throwables.throwIfUnchecked(e);
66 throw new AssertionError("Failed to assemble effective model of " + resourceDirName, e);
72 public static <E extends RuntimeException> @NonNull E assertException(final Class<E> cause,
73 final String... yangResourceName) {
74 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
75 () -> TestUtils.parseYangSource(yangResourceName));
76 final var actual = ex.getCause();
77 return assertInstanceOf(cause, actual);
80 public static <E extends StatementSourceException> @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 StatementSourceException> @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 return assertInstanceOf(cause, actual);
102 public static <E extends StatementSourceException> @NonNull E assertExceptionDir(final String yangResourceName,
103 final Class<E> cause, final Matcher<String> matcher) {
104 final var ret = assertExceptionDir(yangResourceName, cause);
105 assertThat(ret.getMessage(), matcher);
109 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
110 final String... yangResourceName) {
111 return assertException(InferenceException.class, matcher, yangResourceName);
114 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
115 final Matcher<String> matcher) {
116 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
119 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
120 final Matcher<String> matcher, final String... yangResourceName) {
121 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
124 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
125 final String yangResourceName, final Matcher<String> matcher) {
126 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
129 public static @NonNull InvalidEnumDefinitionException assertInvalidEnumDefinitionException(
130 final Matcher<String> matcher, final String... yangResourceName) {
131 return assertArgumentException(InvalidEnumDefinitionException.class, matcher, yangResourceName);
134 public static @NonNull InvalidBitDefinitionException assertInvalidBitDefinitionException(
135 final Matcher<String> matcher, final String... yangResourceName) {
136 return assertArgumentException(InvalidBitDefinitionException.class, matcher, yangResourceName);
139 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
140 final String... yangResourceName) {
141 final var ret = assertException(SourceException.class, matcher, yangResourceName);
142 // SourceException is the base of the hierarchy, we should normally assert subclasses
143 assertEquals(SourceException.class, ret.getClass());
147 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
148 final Matcher<String> matcher) {
149 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
150 // SourceException is the base of the hierarchy, we should normally assert subclasses
151 assertEquals(SourceException.class, ret.getClass());