9beb2b9e29642884f0d767cedcc50f09a07939d3
[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
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         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
50                 .toURI());
51         assertNotNull(modules);
52     }
53
54     @Test
55     public void undesirableElementException() throws Exception {
56         try {
57             TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
58             fail("Unexpected success");
59         } catch (ReactorException ex) {
60             assertNotNull(ex.getCause());
61             assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
62         }
63     }
64
65     @Test
66     public void maximalElementCountException() throws Exception {
67         try {
68             TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
69             fail("Unexpected success");
70         } catch (ReactorException ex) {
71             assertNotNull(ex.getCause());
72             assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
73         }
74     }
75
76     @Test
77     public void missingElementException() throws Exception {
78         expectedEx.expect(SomeModifiersUnresolvedException.class);
79
80         TestUtils.loadModules(getClass().getResource("/substatement-validator/missing-element").toURI());
81     }
82
83     @Test
84     public void bug6173Test() throws Exception {
85         final Set<Module> loadModules = TestUtils.loadModules(getClass().getResource(
86                 "/substatement-validator/empty-element").toURI());
87         assertEquals(1, loadModules.size());
88     }
89
90     @Test
91     public void bug4310test() throws Exception {
92         expectedEx.expect(SomeModifiersUnresolvedException.class);
93         TestUtils.loadModules(getClass().getResource("/substatement-validator/bug-4310").toURI());
94     }
95 }