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