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