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.Iterator;
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;
32 public class MustAndWhenStmtTest {
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");
38 public void mustStmtTest() throws ReactorException {
39 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40 reactor.addSources(MUST_MODULE);
42 final SchemaContext result = reactor.buildEffective();
43 assertNotNull(result);
45 final Module testModule = result.findModules("must-test").iterator().next();
46 assertNotNull(testModule);
48 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
49 QName.create(testModule.getQNameModule(), "interface"));
50 assertNotNull(container);
51 assertTrue(container.isPresenceContainer());
53 final Set<MustDefinition> musts = container.getConstraints().getMustConstraints();
54 assertEquals(2, musts.size());
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 "
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 "
68 assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
72 public void whenStmtTest() throws ReactorException {
73 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
74 reactor.addSources(WHEN_MODULE);
76 final SchemaContext result = reactor.buildEffective();
77 assertNotNull(result);
79 final Module testModule = result.findModules("when-test").iterator().next();
80 assertNotNull(testModule);
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()