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
9 package org.opendaylight.yangtools.yang.stmt;
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;
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.impl.DefaultReactors;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 public class MustAndWhenStmtTest {
33 public void mustStmtTest() throws ReactorException {
34 final SchemaContext result = DefaultReactors.defaultReactor().newBuild()
35 .addSource(sourceForResource("/must-when-stmt-test/must-test.yang"))
37 assertNotNull(result);
39 final Module testModule = result.findModules("must-test").iterator().next();
40 assertNotNull(testModule);
42 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
43 QName.create(testModule.getQNameModule(), "interface"));
44 assertNotNull(container);
45 assertTrue(container.isPresenceContainer());
47 final Collection<MustDefinition> musts = container.getConstraints().getMustConstraints();
48 assertEquals(2, musts.size());
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"))));
69 public void whenStmtTest() throws ReactorException {
70 final SchemaContext result = DefaultReactors.defaultReactor().newBuild()
71 .addSource(sourceForResource("/must-when-stmt-test/when-test.yang"))
73 assertNotNull(result);
75 final Module testModule = result.findModules("when-test").iterator().next();
76 assertNotNull(testModule);
78 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
79 QName.create(testModule.getQNameModule(), "test-container"));
80 assertNotNull(container);
81 assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition()