22602437e1b8204dab6f215658ab4a2d2d6e8b6c
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / AbstractYangTest.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.stmt;
9
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;
15
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;
24
25 /**
26  * Abstract base class containing useful utilities and assertions.
27  */
28 public abstract class AbstractYangTest {
29     @SuppressWarnings("checkstyle:illegalCatch")
30     public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName) {
31         final EffectiveModelContext ret;
32         try {
33             ret = TestUtils.parseYangSource(yangResourceName);
34         } catch (Exception e) {
35             Throwables.throwIfUnchecked(e);
36             throw new AssertionError("Failed to assemble effective model", e);
37         }
38         assertNotNull(ret);
39         return ret;
40     }
41
42     @SuppressWarnings("checkstyle:illegalCatch")
43     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName) {
44         final EffectiveModelContext ret;
45         try {
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);
50         }
51         assertNotNull(ret);
52         return ret;
53     }
54
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);
62     }
63
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);
68         return ret;
69     }
70
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);
78     }
79
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);
84         return ret;
85     }
86
87     public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
88             final String... yangResourceName) {
89         return assertException(InferenceException.class, matcher, yangResourceName);
90     }
91
92     public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
93             final Matcher<String> matcher) {
94         return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
95     }
96
97     public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
98             final Matcher<String> matcher, final String... yangResourceName) {
99         return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
100     }
101
102     public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
103             final String yangResourceName, final Matcher<String> matcher) {
104         return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
105     }
106
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());
112         return ret;
113     }
114
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());
120         return ret;
121     }
122 }