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.Date;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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 Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-12-14");
38 final Module foo = schemaContext.findModuleByName("foo", revision);
41 final Set<NotificationDefinition> notifications = foo.getNotifications();
42 assertEquals(1, notifications.size());
43 final NotificationDefinition myNotification = notifications.iterator().next();
44 Set<MustDefinition> mustConstraints = myNotification.getConstraints().getMustConstraints();
45 assertEquals(2, mustConstraints.size());
47 final Set<RpcDefinition> rpcs = foo.getRpcs();
48 assertEquals(1, rpcs.size());
49 final RpcDefinition myRpc = rpcs.iterator().next();
51 final ContainerSchemaNode input = myRpc.getInput();
53 mustConstraints = input.getConstraints().getMustConstraints();
54 assertEquals(2, mustConstraints.size());
56 final ContainerSchemaNode output = myRpc.getOutput();
57 assertNotNull(output);
58 mustConstraints = output.getConstraints().getMustConstraints();
59 assertEquals(2, mustConstraints.size());
63 public void testInvalidYang10Model() throws Exception {
64 assertException("/rfc7950/bug6871/foo10.yang", "MUST is not valid for NOTIFICATION");
65 assertException("/rfc7950/bug6871/bar10.yang", "MUST is not valid for INPUT");
66 assertException("/rfc7950/bug6871/baz10.yang", "MUST is not valid for OUTPUT");
69 private static void assertException(final String sourcePath, final String exceptionMessage) throws Exception {
71 StmtTestUtils.parseYangSource(sourcePath);
72 fail("Test should fail due to invalid Yang 1.0");
73 } catch (final ReactorException ex) {
74 assertTrue(ex.getCause().getMessage().startsWith(exceptionMessage));