2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
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;
23 class MustAndWhenStmtTest extends AbstractYangTest {
26 final var result = assertEffectiveModel("/must-when-stmt-test/must-test.yang");
28 final var testModule = result.findModules("must-test").iterator().next();
29 assertNotNull(testModule);
31 final var container = (ContainerSchemaNode) testModule.getDataChildByName(
32 QName.create(testModule.getQNameModule(), "interface"));
33 assertNotNull(container);
34 assertTrue(container.isPresenceContainer());
36 final var musts = container.getMustConstraints();
37 assertEquals(2, musts.size());
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"))));
59 final var result = assertEffectiveModel("/must-when-stmt-test/when-test.yang");
61 final var testModule = result.findModules("when-test").iterator().next();
62 assertNotNull(testModule);
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());