Bug 6173: Allow refine statement to have no substatements
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / SubstatementValidatorTest.java
1 /*
2  * Copyright (c) 2015 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.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import java.io.ByteArrayOutputStream;
15 import java.io.PrintStream;
16 import java.io.UnsupportedEncodingException;
17 import java.net.URISyntaxException;
18 import java.util.Set;
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.MissingSubstatementException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.stmt.retest.TestUtils;
28
29 public class SubstatementValidatorTest {
30
31     private final PrintStream stdout = System.out;
32     private final ByteArrayOutputStream output = new ByteArrayOutputStream();
33     private String testLog;
34
35     @Rule
36     public ExpectedException expectedEx = ExpectedException.none();
37
38     @Before
39     public void setUp() throws UnsupportedEncodingException {
40         System.setOut(new PrintStream(output, true, "UTF-8"));
41     }
42
43     @After
44     public void cleanUp() {
45         System.setOut(stdout);
46     }
47
48     @Test
49     public void noException() throws URISyntaxException, ReactorException {
50         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment")
51                 .toURI());
52         assertNotNull(modules);
53     }
54
55     @Test
56     public void undesirableElementException() throws URISyntaxException, ReactorException {
57         final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
58                 "/substatement-validator/undesirable-element").toURI());
59         testLog = output.toString();
60         assertTrue(testLog.contains("TYPE is not valid for REVISION"));
61     }
62
63     @Test
64     public void maximalElementCountException() throws URISyntaxException, ReactorException {
65         final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
66                 "/substatement-validator/maximal-element").toURI());
67         testLog = output.toString();
68         assertTrue(testLog.contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
69     }
70
71     @Test
72     public void missingElementException() throws URISyntaxException, ReactorException {
73         expectedEx.expect(MissingSubstatementException.class);
74
75         final Set<Module> modules = TestUtils.loadModules(getClass().getResource(
76                 "/substatement-validator/missing-element").toURI());
77     }
78
79     @Test
80     public void bug6173Test() throws ReactorException, URISyntaxException {
81         final Set<Module> loadModules = TestUtils.loadModules(getClass().getResource(
82                 "/substatement-validator/empty-element").toURI());
83         assertEquals(1, loadModules.size());
84     }
85
86     @Test
87     public void bug4310test() throws URISyntaxException, ReactorException {
88         expectedEx.expect(IllegalArgumentException.class);
89
90         final Set<Module> modules = TestUtils.loadModules(getClass().getResource("/substatement-validator/bug-4310")
91                 .toURI());
92     }
93 }