2f3390d1e9ca113538b578690a6615f9da1de715
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.stmt;
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 import static org.opendaylight.yangtools.yang.stmt.StmtTestUtils.sourceForResource;
18
19 import java.util.Iterator;
20 import java.util.Set;
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.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31
32 public class MustAndWhenStmtTest {
33
34     private static final StatementStreamSource MUST_MODULE = sourceForResource("/must-when-stmt-test/must-test.yang");
35     private static final StatementStreamSource WHEN_MODULE = sourceForResource("/must-when-stmt-test/when-test.yang");
36
37     @Test
38     public void mustStmtTest() throws ReactorException {
39         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40         reactor.addSources(MUST_MODULE);
41
42         final SchemaContext result = reactor.buildEffective();
43         assertNotNull(result);
44
45         final Module testModule = result.findModules("must-test").iterator().next();
46         assertNotNull(testModule);
47
48         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
49             QName.create(testModule.getQNameModule(), "interface"));
50         assertNotNull(container);
51         assertTrue(container.isPresenceContainer());
52
53         final Set<MustDefinition> musts = container.getConstraints().getMustConstraints();
54         assertEquals(2, musts.size());
55
56         final Iterator<MustDefinition> mustsIterator = musts.iterator();
57         MustDefinition mustStmt = mustsIterator.next();
58         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
59                 + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
60         assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 "
61                 + ".. 17966")));
62         assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
63         mustStmt = mustsIterator.next();
64         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
65                 + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
66         assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 "
67                 + ".. 17966")));
68         assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
69     }
70
71     @Test
72     public void whenStmtTest() throws ReactorException {
73         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
74         reactor.addSources(WHEN_MODULE);
75
76         final SchemaContext result = reactor.buildEffective();
77         assertNotNull(result);
78
79         final Module testModule = result.findModules("when-test").iterator().next();
80         assertNotNull(testModule);
81
82         final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
83             QName.create(testModule.getQNameModule(), "test-container"));
84         assertNotNull(container);
85         assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition().toString());
86     }
87 }