Bug 6871: [YANG 1.1] Allow "must" in "input", "output" and "notification" statements
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6871Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
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;
15
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.Date;
20 import java.util.Set;
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;
31
32 public class Bug6871Test {
33
34     @Test
35     public void testValidYang11Model() throws ReactorException, FileNotFoundException, URISyntaxException,
36             ParseException {
37         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/bug6871/foo.yang");
38         assertNotNull(schemaContext);
39
40         final Date revision = SimpleDateFormatUtil.getRevisionFormat().parse("2016-12-14");
41
42         final Module foo = schemaContext.findModuleByName("foo", revision);
43         assertNotNull(foo);
44
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());
50
51         final Set<RpcDefinition> rpcs = foo.getRpcs();
52         assertEquals(1, rpcs.size());
53         final RpcDefinition myRpc = rpcs.iterator().next();
54
55         final ContainerSchemaNode input = myRpc.getInput();
56         assertNotNull(input);
57         mustConstraints = input.getConstraints().getMustConstraints();
58         assertEquals(2, mustConstraints.size());
59
60         final ContainerSchemaNode output = myRpc.getOutput();
61         assertNotNull(output);
62         mustConstraints = output.getConstraints().getMustConstraints();
63         assertEquals(2, mustConstraints.size());
64     }
65
66     @Test
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");
71     }
72
73     private static void assertException(final String sourcePath, final String exceptionMessage)
74             throws FileNotFoundException, URISyntaxException {
75         try {
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));
80         }
81     }
82 }