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.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
33 public class MustAndWhenStmtTest {
35 private static final StatementStreamSource MUST_MODULE = sourceForResource("/must-when-stmt-test/must-test.yang");
36 private static final StatementStreamSource WHEN_MODULE = sourceForResource("/must-when-stmt-test/when-test.yang");
39 public void mustStmtTest() throws ReactorException {
40 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
41 reactor.addSources(MUST_MODULE);
43 final SchemaContext result = reactor.buildEffective();
44 assertNotNull(result);
46 final Module testModule = result.findModules("must-test").iterator().next();
47 assertNotNull(testModule);
49 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
50 QName.create(testModule.getQNameModule(), "interface"));
51 assertNotNull(container);
52 assertTrue(container.isPresenceContainer());
54 final Collection<MustDefinition> musts = container.getConstraints().getMustConstraints();
55 assertEquals(2, musts.size());
57 final Iterator<MustDefinition> mustsIterator = musts.iterator();
58 MustDefinition mustStmt = mustsIterator.next();
59 assertThat(mustStmt.getXpath().toString(), anyOf(is("ifType != 'ethernet' or (ifType = 'ethernet' and "
60 + "ifMTU = 1500)"), is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
61 assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
62 is(Optional.of("An atm MTU must be 64 .. 17966"))));
63 assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
64 is(Optional.of("An atm error"))));
65 mustStmt = mustsIterator.next();
66 assertThat(mustStmt.getXpath().toString(), anyOf(
67 is("ifType != 'ethernet' or (ifType = 'ethernet' and ifMTU = 1500)"),
68 is("ifType != 'atm' or (ifType = 'atm' and ifMTU <= 17966 and ifMTU >= 64)")));
69 assertThat(mustStmt.getErrorMessage(), anyOf(is(Optional.of("An ethernet MTU must be 1500")),
70 is(Optional.of("An atm MTU must be 64 .. 17966"))));
71 assertThat(mustStmt.getErrorAppTag(), anyOf(is(Optional.of("An ethernet error")),
72 is(Optional.of("An atm error"))));
76 public void whenStmtTest() throws ReactorException {
77 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
78 reactor.addSources(WHEN_MODULE);
80 final SchemaContext result = reactor.buildEffective();
81 assertNotNull(result);
83 final Module testModule = result.findModules("when-test").iterator().next();
84 assertNotNull(testModule);
86 final ContainerSchemaNode container = (ContainerSchemaNode) testModule.getDataChildByName(
87 QName.create(testModule.getQNameModule(), "test-container"));
88 assertNotNull(container);
89 assertEquals("conditional-leaf = 'autumn-leaf'", container.getConstraints().getWhenCondition()