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;
18 import java.util.Iterator;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
30 public class MustAndWhenStmtTest {
32 private static final YangStatementSourceImpl MUST_MODULE = new YangStatementSourceImpl
33 ("/must-when-stmt-test/must-test.yang", false);
34 private static final YangStatementSourceImpl WHEN_MODULE = new YangStatementSourceImpl("/must-when-stmt-test/" +
35 "when-test.yang", false);
38 public void mustStmtTest() throws ReactorException {
39 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
40 StmtTestUtils.addSources(reactor, MUST_MODULE);
42 EffectiveSchemaContext result = reactor.buildEffective();
43 assertNotNull(result);
45 Module testModule = result.findModuleByName("must-test", null);
46 assertNotNull(testModule);
48 ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName("interface");
49 assertNotNull(container);
50 assertTrue(container.isPresenceContainer());
52 Set<MustDefinition> musts = container.getConstraints().getMustConstraints();
53 assertEquals(2, musts.size());
55 Iterator<MustDefinition> mustsIterator = musts.iterator();
56 MustDefinition mustStmt = mustsIterator.next();
57 assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and " +
58 "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
59 assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 " +
61 assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
62 mustStmt = mustsIterator.next();
63 assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and " +
64 "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
65 assertThat(mustStmt.getErrorMessage(), anyOf(is("An ethernet MTU must be 1500"), is("An atm MTU must be 64 " +
67 assertThat(mustStmt.getErrorAppTag(), anyOf(is("An ethernet error"), is("An atm error")));
71 public void whenStmtTest() throws ReactorException {
72 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
73 StmtTestUtils.addSources(reactor, WHEN_MODULE);
75 EffectiveSchemaContext result = reactor.buildEffective();
76 assertNotNull(result);
78 Module testModule = result.findModuleByName("when-test", null);
79 assertNotNull(testModule);
81 ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName("test-container");
82 assertNotNull(container);
83 assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition().toString());