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.parser.stmt.rfc7950;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import java.util.Collection;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
23 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
29 public class Bug6871Test {
32 public void testValidYang11Model() throws Exception {
33 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6871/foo.yang");
34 assertNotNull(schemaContext);
36 final Module foo = schemaContext.findModule("foo", Revision.of("2016-12-14")).get();
38 final Set<NotificationDefinition> notifications = foo.getNotifications();
39 assertEquals(1, notifications.size());
40 final NotificationDefinition myNotification = notifications.iterator().next();
41 Collection<MustDefinition> mustConstraints = myNotification.getMustConstraints();
42 assertEquals(2, mustConstraints.size());
44 final Set<RpcDefinition> rpcs = foo.getRpcs();
45 assertEquals(1, rpcs.size());
46 final RpcDefinition myRpc = rpcs.iterator().next();
48 final ContainerSchemaNode input = myRpc.getInput();
50 mustConstraints = input.getConstraints().getMustConstraints();
51 assertEquals(2, mustConstraints.size());
53 final ContainerSchemaNode output = myRpc.getOutput();
54 assertNotNull(output);
55 mustConstraints = output.getConstraints().getMustConstraints();
56 assertEquals(2, mustConstraints.size());
60 public void testInvalidYang10Model() throws Exception {
61 assertException("/rfc7950/bug6871/foo10.yang", "MUST is not valid for NOTIFICATION");
62 assertException("/rfc7950/bug6871/bar10.yang", "MUST is not valid for INPUT");
63 assertException("/rfc7950/bug6871/baz10.yang", "MUST is not valid for OUTPUT");
66 private static void assertException(final String sourcePath, final String exceptionMessage) throws Exception {
68 StmtTestUtils.parseYangSource(sourcePath);
69 fail("Test should fail due to invalid Yang 1.0");
70 } catch (final ReactorException ex) {
71 assertTrue(ex.getCause().getMessage().startsWith(exceptionMessage));