Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / MustAndWhenStmtTest.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.hamcrest.CoreMatchers.anyOf;
12 import static org.hamcrest.CoreMatchers.is;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertThat;
16 import static org.junit.Assert.assertTrue;
17
18 import java.util.Iterator;
19 import java.util.Set;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
29
30 public class MustAndWhenStmtTest {
31
32     private static final YangStatementSourceImpl MUST_MODULE = new YangStatementSourceImpl
33             ("/must-when-stmt-test/must-test.yang", false);
34     private static final YangStatementSourceImpl WHEN_MODULE = new YangStatementSourceImpl("/must-when-stmt-test/" +
35             "when-test.yang", false);
36
37     @Test
38     public void mustStmtTest() throws ReactorException {
39         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40         StmtTestUtils.addSources(reactor, MUST_MODULE);
41
42         EffectiveSchemaContext result = reactor.buildEffective();
43         assertNotNull(result);
44
45         Module testModule = result.findModuleByName("must-test", null);
46         assertNotNull(testModule);
47
48         ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName("interface");
49         assertNotNull(container);
50         assertTrue(container.isPresenceContainer());
51
52         Set<MustDefinition> musts = container.getConstraints().getMustConstraints();
53         assertEquals(2, musts.size());
54
55         Iterator<MustDefinition> mustsIterator = musts.iterator();
56         MustDefinition mustStmt = mustsIterator.next();
57         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and " +
58                 "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
59         assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 " +
60                 ".. 17966")));
61         assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
62         mustStmt = mustsIterator.next();
63         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and " +
64                 "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
65         assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 " +
66                 ".. 17966")));
67         assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
68     }
69
70     @Test
71     public void whenStmtTest() throws ReactorException {
72         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
73         StmtTestUtils.addSources(reactor, WHEN_MODULE);
74
75         EffectiveSchemaContext result = reactor.buildEffective();
76         assertNotNull(result);
77
78         Module testModule = result.findModuleByName("when-test", null);
79         assertNotNull(testModule);
80
81         ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName("test-container");
82         assertNotNull(container);
83         assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition().toString());
84     }
85 }