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 org.eclipse.jdt.annotation.NonNull;
17 import org.hamcrest.Matcher;
18 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 * Abstract base class containing useful utilities and assertions.
27 public abstract class AbstractYangTest {
28 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName)
30 final var ret = TestUtils.parseYangSource(yangResourceName);
35 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName)
37 final var ret = TestUtils.loadModules(resourceDirName);
42 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
43 final String... yangResourceName) {
44 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
45 () -> TestUtils.parseYangSource(yangResourceName));
46 final var actual = ex.getCause();
47 assertThat(actual, instanceOf(cause));
48 return cause.cast(actual);
51 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
52 final Matcher<String> matcher, final String... yangResourceName) {
53 final var ret = assertException(cause, yangResourceName);
54 assertThat(ret.getMessage(), matcher);
58 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
59 final Class<E> cause) {
60 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
61 () -> TestUtils.loadModules(yangResourceName));
62 final var actual = ex.getCause();
63 assertThat(actual, instanceOf(cause));
64 return cause.cast(actual);
67 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
68 final Class<E> cause, final Matcher<String> matcher) {
69 final var ret = assertExceptionDir(yangResourceName, cause);
70 assertThat(ret.getMessage(), matcher);
74 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
75 final String... yangResourceName) {
76 return assertException(InferenceException.class, matcher, yangResourceName);
79 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
80 final Matcher<String> matcher) {
81 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
84 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
85 final Matcher<String> matcher, final String... yangResourceName) {
86 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
89 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
90 final String yangResourceName, final Matcher<String> matcher) {
91 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
94 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
95 final String... yangResourceName) {
96 final var ret = assertException(SourceException.class, matcher, yangResourceName);
97 // SourceException is the base of the hierarchy, we should normally assert subclasses
98 assertEquals(SourceException.class, ret.getClass());
102 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
103 final Matcher<String> matcher) {
104 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
105 // SourceException is the base of the hierarchy, we should normally assert subclasses
106 assertEquals(SourceException.class, ret.getClass());