Deprecate simple DataTreeFactory.create()
[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.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertNotNull;
15 import static org.junit.jupiter.api.Assertions.assertTrue;
16
17 import java.util.Optional;
18 import org.junit.jupiter.api.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
22
23 class MustAndWhenStmtTest extends AbstractYangTest {
24     @Test
25     void mustStmtTest() {
26         final var result = assertEffectiveModel("/must-when-stmt-test/must-test.yang");
27
28         final var testModule = result.findModules("must-test").iterator().next();
29         assertNotNull(testModule);
30
31         final var container = (ContainerSchemaNode) testModule.getDataChildByName(
32             QName.create(testModule.getQNameModule(), "interface"));
33         assertNotNull(container);
34         assertTrue(container.isPresenceContainer());
35
36         final var musts = container.getMustConstraints();
37         assertEquals(2, musts.size());
38
39         final var mustsIterator = musts.iterator();
40         MustDefinition mustStmt = mustsIterator.next();
41         assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
42             + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
43         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
44             is(Optional.of("An atm MTU must be 64 .. 17966"))));
45         assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
46             is(Optional.of("An atm error"))));
47         mustStmt = mustsIterator.next();
48         assertThat(mustStmt.getXpath().toString(), anyOf(
49             is("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)"),
50             is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
51         assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
52             is(Optional.of("An atm MTU must be 64 .. 17966"))));
53         assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
54             is(Optional.of("An atm error"))));
55     }
56
57     @Test
58     void whenStmtTest() {
59         final var result = assertEffectiveModel("/must-when-stmt-test/when-test.yang");
60
61         final var testModule = result.findModules("when-test").iterator().next();
62         assertNotNull(testModule);
63
64         final var container = (ContainerSchemaNode) testModule.getDataChildByName(
65             QName.create(testModule.getQNameModule(), "test-container"));
66         assertNotNull(container);
67         assertEquals("conditional-leaf = 'autumn-leaf'", container.getWhenCondition().orElseThrow().toString());
68     }
69 }