Remove usage of ExpectedException
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / SubstatementValidatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertThrows;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.ByteArrayOutputStream;
17 import java.io.PrintStream;
18 import java.io.UnsupportedEncodingException;
19 import java.util.Collection;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
26
27 public class SubstatementValidatorTest {
28     @SuppressWarnings("checkstyle:regexpSinglelineJava")
29     private final PrintStream stdout = System.out;
30     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
31
32     @Before
33     public void setUp() throws UnsupportedEncodingException {
34         System.setOut(new PrintStream(output, true, "UTF-8"));
35     }
36
37     @After
38     public void cleanUp() {
39         System.setOut(stdout);
40     }
41
42     @Test
43     public void noException() throws Exception {
44         assertNotNull(TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI()));
45     }
46
47     @Test
48     public void undesirableElementException() throws Exception {
49         try {
50             TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
51             fail("Unexpected success");
52         } catch (ReactorException ex) {
53             assertNotNull(ex.getCause());
54             assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
55         }
56     }
57
58     @Test
59     public void maximalElementCountException() throws Exception {
60         try {
61             TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
62             fail("Unexpected success");
63         } catch (ReactorException ex) {
64             assertNotNull(ex.getCause());
65             assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
66         }
67     }
68
69     @Test
70     public void missingElementException() {
71         assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
72             SubstatementValidatorTest.class.getResource("/substatement-validator/missing-element").toURI()));
73     }
74
75     @Test
76     public void bug6173Test() throws Exception {
77         final Collection<? extends Module> loadModules = TestUtils.loadModules(getClass()
78             .getResource("/substatement-validator/empty-element").toURI()).getModules();
79         assertEquals(1, loadModules.size());
80     }
81
82     @Test
83     public void bug4310test() throws Exception {
84         assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
85             SubstatementValidatorTest.class.getResource("/substatement-validator/bug-4310").toURI()));
86     }
87 }