Restore unrecognized statement defensiveness
[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.assertThrows;
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.nio.charset.StandardCharsets;
20 import java.util.Collection;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
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     @Before
34     public void setUp() throws UnsupportedEncodingException {
35         System.setOut(new PrintStream(output, true, StandardCharsets.UTF_8));
36     }
37
38     @After
39     public void cleanUp() {
40         System.setOut(stdout);
41     }
42
43     @Test
44     public void noException() throws Exception {
45         assertNotNull(TestUtils.loadModules(getClass().getResource("/augment-test/augment-in-augment").toURI()));
46     }
47
48     @Test
49     public void undesirableElementException() throws Exception {
50         try {
51             TestUtils.loadModules(getClass().getResource("/substatement-validator/undesirable-element").toURI());
52             fail("Unexpected success");
53         } catch (ReactorException ex) {
54             assertNotNull(ex.getCause());
55             assertTrue(ex.getCause().getMessage().contains("TYPE is not valid for REVISION"));
56         }
57     }
58
59     @Test
60     public void maximalElementCountException() throws Exception {
61         try {
62             TestUtils.loadModules(getClass().getResource("/substatement-validator/maximal-element").toURI());
63             fail("Unexpected success");
64         } catch (ReactorException ex) {
65             assertNotNull(ex.getCause());
66             assertTrue(ex.getCause().getMessage().contains("Maximal count of DESCRIPTION for AUGMENT is 1"));
67         }
68     }
69
70     @Test
71     public void missingElementException() {
72         assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
73             SubstatementValidatorTest.class.getResource("/substatement-validator/missing-element").toURI()));
74     }
75
76     @Test
77     public void bug6173Test() throws Exception {
78         final Collection<? extends Module> loadModules = TestUtils.loadModules(getClass()
79             .getResource("/substatement-validator/empty-element").toURI()).getModules();
80         assertEquals(1, loadModules.size());
81     }
82
83     @Test
84     public void bug4310test() throws Exception {
85         assertThrows(SomeModifiersUnresolvedException.class, () -> TestUtils.loadModules(
86             SubstatementValidatorTest.class.getResource("/substatement-validator/bug-4310").toURI()));
87     }
88 }