584b1487aef4c6a4092e5e8d677849555f6084c1
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / Bug6897Test.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 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.Set;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
25 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
28 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
29
30 public class Bug6897Test {
31     private static final String FOO_NS = "foo";
32
33     @Test
34     public void notificationsInDataContainersTest() throws Exception {
35         final SchemaContext schemaContext = StmtTestUtils
36                 .parseYangSource("/rfc7950/notifications-in-data-nodes/foo.yang");
37         assertNotNull(schemaContext);
38
39         assertContainsNotifications(schemaContext, "root", "grp-notification", "aug-notification");
40         assertContainsNotifications(schemaContext, "top-list", "top-list-notification");
41         assertContainsNotifications(schemaContext, "top", "top-notification");
42
43         final Collection<? extends GroupingDefinition> groupings = schemaContext.getGroupings();
44         assertEquals(1, groupings.size());
45         assertContainsNotifications(groupings.iterator().next(), "grp-notification");
46
47         final Collection<? extends Module> modules = schemaContext.getModules();
48         assertEquals(1, modules.size());
49         final Module foo = modules.iterator().next();
50         final Collection<? extends AugmentationSchemaNode> augmentations = foo.getAugmentations();
51         assertEquals(1, augmentations.size());
52         assertContainsNotifications(augmentations.iterator().next(), "aug-notification", "grp-notification");
53     }
54
55     private static void assertContainsNotifications(final SchemaContext schemaContext, final String dataContainerName,
56             final String... notificationNames) {
57         final DataSchemaNode dataChildByName = schemaContext.getDataChildByName(
58             QName.create(FOO_NS, dataContainerName));
59         assertTrue(dataChildByName instanceof NotificationNodeContainer);
60         assertContainsNotifications((NotificationNodeContainer) dataChildByName, notificationNames);
61     }
62
63     private static void assertContainsNotifications(final NotificationNodeContainer notificationContainer,
64             final String... notificationNames) {
65         final Collection<? extends NotificationDefinition> notifications = notificationContainer.getNotifications();
66         assertEquals(notificationNames.length, notifications.size());
67
68         final Set<QName> notificationQNames = new HashSet<>();
69         notifications.forEach(n -> notificationQNames.add(n.getQName()));
70
71         for (final String notificationName : notificationNames) {
72             assertTrue(notificationQNames.contains(QName.create(FOO_NS, notificationName)));
73         }
74     }
75
76     @Test
77     public void invalid10Test() throws Exception {
78         try {
79             StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo10.yang");
80             fail("Test should fail due to invalid Yang 1.0");
81         } catch (final SomeModifiersUnresolvedException e) {
82             assertTrue(e.getCause().getMessage().startsWith("NOTIFICATION is not valid for"));
83         }
84     }
85
86     @Test
87     public void invalid11Test() throws Exception {
88         try {
89             StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
90             fail("Test should fail due to invalid Yang 1.1");
91         } catch (final SomeModifiersUnresolvedException e) {
92             assertTrue(e.getCause().getMessage().startsWith(
93                 "Notification (foo)grp-notification is defined within an rpc, action, or another notification"));
94         }
95     }
96
97     @Test
98     public void testNotificationWithinListWithoutKey() throws Exception {
99         try {
100             StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
101             fail("Test should fail due to invalid Yang 1.1");
102         } catch (final SomeModifiersUnresolvedException e) {
103             assertTrue(e.getCause().getMessage().startsWith(
104                 "Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list "
105                         + "that has no key statement"));
106         }
107     }
108
109     @Test
110     public void testNotificationInUsedGroupingWithinCase() throws Exception {
111         try {
112             StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
113             fail("Test should fail due to invalid Yang 1.1");
114         } catch (final SomeModifiersUnresolvedException e) {
115             assertTrue(e.getCause().getMessage().startsWith(
116                 "Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a case "
117                         + "statement"));
118         }
119     }
120 }