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 org.eclipse.jdt.annotation.NonNull;
18 import org.hamcrest.Matcher;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.InvalidSubstatementException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 * Abstract base class containing useful utilities and assertions.
28 public abstract class AbstractYangTest {
29 @SuppressWarnings("checkstyle:illegalCatch")
30 public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
31 final EffectiveModelContext ret;
33 ret = TestUtils.parseYangSource(yangResourceName);
34 } catch (Exception e) {
35 Throwables.throwIfUnchecked(e);
36 throw new AssertionError("Failed to assemble effective model", e);
42 @SuppressWarnings("checkstyle:illegalCatch")
43 public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
44 final EffectiveModelContext ret;
46 ret = TestUtils.loadModules(resourceDirName);
47 } catch (Exception e) {
48 Throwables.throwIfUnchecked(e);
49 throw new AssertionError("Failed to assemble effective model of " + resourceDirName, e);
55 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
56 final String... yangResourceName) {
57 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
58 () -> TestUtils.parseYangSource(yangResourceName));
59 final var actual = ex.getCause();
60 assertThat(actual, instanceOf(cause));
61 return cause.cast(actual);
64 public static <E extends SourceException> @NonNull E assertException(final Class<E> cause,
65 final Matcher<String> matcher, final String... yangResourceName) {
66 final var ret = assertException(cause, yangResourceName);
67 assertThat(ret.getMessage(), matcher);
71 public static <E extends SourceException> @NonNull E assertExceptionDir(final String yangResourceName,
72 final Class<E> cause) {
73 final var ex = assertThrows(SomeModifiersUnresolvedException.class,
74 () -> TestUtils.loadModules(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 assertExceptionDir(final String yangResourceName,
81 final Class<E> cause, final Matcher<String> matcher) {
82 final var ret = assertExceptionDir(yangResourceName, cause);
83 assertThat(ret.getMessage(), matcher);
87 public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
88 final String... yangResourceName) {
89 return assertException(InferenceException.class, matcher, yangResourceName);
92 public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
93 final Matcher<String> matcher) {
94 return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
97 public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
98 final Matcher<String> matcher, final String... yangResourceName) {
99 return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
102 public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
103 final String yangResourceName, final Matcher<String> matcher) {
104 return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
107 public static @NonNull SourceException assertSourceException(final Matcher<String> matcher,
108 final String... yangResourceName) {
109 final var ret = assertException(SourceException.class, matcher, yangResourceName);
110 // SourceException is the base of the hierarchy, we should normally assert subclasses
111 assertEquals(SourceException.class, ret.getClass());
115 public static @NonNull SourceException assertSourceExceptionDir(final String yangResourceName,
116 final Matcher<String> matcher) {
117 final var ret = assertExceptionDir(yangResourceName, SourceException.class, matcher);
118 // SourceException is the base of the hierarchy, we should normally assert subclasses
119 assertEquals(SourceException.class, ret.getClass());