Add AbstractYangTest
[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 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;
23
24 /**
25  * Abstract base class containing useful utilities and assertions.
26  */
27 public abstract class AbstractYangTest {
28     public static @NonNull EffectiveModelContext assertEffectiveModel(final String... yangResourceName)
29             throws Exception {
30         final var ret = TestUtils.parseYangSource(yangResourceName);
31         assertNotNull(ret);
32         return ret;
33     }
34
35     public static @NonNull EffectiveModelContext assertEffectiveModelDir(final String resourceDirName)
36             throws Exception {
37         final var ret =  TestUtils.loadModules(resourceDirName);
38         assertNotNull(ret);
39         return ret;
40     }
41
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);
49     }
50
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);
55         return ret;
56     }
57
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);
65     }
66
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);
71         return ret;
72     }
73
74     public static @NonNull InferenceException assertInferenceException(final Matcher<String> matcher,
75             final String... yangResourceName) {
76         return assertException(InferenceException.class, matcher, yangResourceName);
77     }
78
79     public static @NonNull InferenceException assertInferenceExceptionDir(final String yangResourceName,
80             final Matcher<String> matcher) {
81         return assertExceptionDir(yangResourceName, InferenceException.class, matcher);
82     }
83
84     public static @NonNull InvalidSubstatementException assertInvalidSubstatementException(
85             final Matcher<String> matcher, final String... yangResourceName) {
86         return assertException(InvalidSubstatementException.class, matcher, yangResourceName);
87     }
88
89     public static @NonNull InvalidSubstatementException assertInvalidSubstatementExceptionDir(
90             final String yangResourceName, final Matcher<String> matcher) {
91         return assertExceptionDir(yangResourceName, InvalidSubstatementException.class, matcher);
92     }
93
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());
99         return ret;
100     }
101
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());
107         return ret;
108     }
109 }