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