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