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.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.Date;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
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.NotificationDefinition;
27 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
32 public class Bug6871Test {
35 public void testValidYang11Model() throws ReactorException, FileNotFoundException, URISyntaxException,
37 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6871/foo.yang");
38 assertNotNull(schemaContext);
40 final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-12-14");
42 final Module foo = schemaContext.findModuleByName("foo", revision);
45 final Set<NotificationDefinition> notifications = foo.getNotifications();
46 assertEquals(1, notifications.size());
47 final NotificationDefinition myNotification = notifications.iterator().next();
48 Set<MustDefinition> mustConstraints = myNotification.getConstraints().getMustConstraints();
49 assertEquals(2, mustConstraints.size());
51 final Set<RpcDefinition> rpcs = foo.getRpcs();
52 assertEquals(1, rpcs.size());
53 final RpcDefinition myRpc = rpcs.iterator().next();
55 final ContainerSchemaNode input = myRpc.getInput();
57 mustConstraints = input.getConstraints().getMustConstraints();
58 assertEquals(2, mustConstraints.size());
60 final ContainerSchemaNode output = myRpc.getOutput();
61 assertNotNull(output);
62 mustConstraints = output.getConstraints().getMustConstraints();
63 assertEquals(2, mustConstraints.size());
67 public void testInvalidYang10Model() throws FileNotFoundException, URISyntaxException {
68 assertException("/rfc7950/bug6871/foo10.yang", "MUST is not valid for NOTIFICATION");
69 assertException("/rfc7950/bug6871/bar10.yang", "MUST is not valid for INPUT");
70 assertException("/rfc7950/bug6871/baz10.yang", "MUST is not valid for OUTPUT");
73 private static void assertException(final String sourcePath, final String exceptionMessage)
74 throws FileNotFoundException, URISyntaxException {
76 StmtTestUtils.parseYangSource(sourcePath);
77 fail("Test should fail due to invalid Yang 1.0");
78 } catch (final ReactorException ex) {
79 assertTrue(ex.getCause().getMessage().startsWith(exceptionMessage));