Remove unneeded throws
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / MustAndWhenStmtTest.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.hamcrest.CoreMatchers.anyOf;
11 import static org.hamcrest.CoreMatchers.is;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
17
18 import java.util.Collection;
19 import java.util.Iterator;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29
30 public class MustAndWhenStmtTest {
31     @Test
32     public void mustStmtTest() throws ReactorException {
33         final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
34                 .addSource(sourceForResource("/must-when-stmt-test/must-test.yang"))
35                 .buildEffective();
36         assertNotNull(result);
37
38         final Module testModule = result.findModules("must-test").iterator().next();
39         assertNotNull(testModule);
40
41         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
42             QName.create(testModule.getQNameModule(), "interface"));
43         assertNotNull(container);
44         assertTrue(container.isPresenceContainer());
45
46         final Collection<? extends MustDefinition> musts = container.getMustConstraints();
47         assertEquals(2, musts.size());
48
49         final Iterator<? extends MustDefinition> mustsIterator = musts.iterator();
50         MustDefinition mustStmt = mustsIterator.next();
51         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
52                 + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
53         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
54             is(Optional.of("An atm MTU must be 64 .. 17966"))));
55         assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
56             is(Optional.of("An atm error"))));
57         mustStmt = mustsIterator.next();
58         assertThat(mustStmt.getXpath().toString(), anyOf(
59             is("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)"),
60             is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
61         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
62             is(Optional.of("An atm MTU must be 64 .. 17966"))));
63         assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
64             is(Optional.of("An atm error"))));
65     }
66
67     @Test
68     public void whenStmtTest() throws ReactorException {
69         final SchemaContext result = RFC7950Reactors.defaultReactor().newBuild()
70                 .addSource(sourceForResource("/must-when-stmt-test/when-test.yang"))
71                 .buildEffective();
72         assertNotNull(result);
73
74         final Module testModule = result.findModules("when-test").iterator().next();
75         assertNotNull(testModule);
76
77         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
78             QName.create(testModule.getQNameModule(), "test-container"));
79         assertNotNull(container);
80         assertEquals("conditional-leaf = 'autumn-leaf'", container.getWhenCondition().orElseThrow().toString());
81     }
82 }