Cleanup Bug6897Test
[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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.startsWith;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.hamcrest.collection.IsIn.in;
14 import static org.junit.Assert.assertEquals;
15
16 import java.util.Collection;
17 import java.util.stream.Collectors;
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.stmt.AbstractYangTest;
28
29 public class Bug6897Test extends AbstractYangTest {
30     private static final String FOO_NS = "foo";
31
32     @Test
33     public void notificationsInDataContainersTest() throws Exception {
34         final var context = assertEffectiveModel("/rfc7950/notifications-in-data-nodes/foo.yang");
35
36         assertContainsNotifications(context, "root", "grp-notification", "aug-notification");
37         assertContainsNotifications(context, "top-list", "top-list-notification");
38         assertContainsNotifications(context, "top", "top-notification");
39
40         final Collection<? extends GroupingDefinition> groupings = context.getGroupings();
41         assertEquals(1, groupings.size());
42         assertContainsNotifications(groupings.iterator().next(), "grp-notification");
43
44         final Collection<? extends Module> modules = context.getModules();
45         assertEquals(1, modules.size());
46         final Module foo = modules.iterator().next();
47         final Collection<? extends AugmentationSchemaNode> augmentations = foo.getAugmentations();
48         assertEquals(1, augmentations.size());
49         assertContainsNotifications(augmentations.iterator().next(), "aug-notification", "grp-notification");
50     }
51
52     private static void assertContainsNotifications(final SchemaContext schemaContext, final String dataContainerName,
53             final String... notificationNames) {
54         final DataSchemaNode dataChildByName = schemaContext.getDataChildByName(
55             QName.create(FOO_NS, dataContainerName));
56         assertThat(dataChildByName, instanceOf(NotificationNodeContainer.class));
57         assertContainsNotifications((NotificationNodeContainer) dataChildByName, notificationNames);
58     }
59
60     private static void assertContainsNotifications(final NotificationNodeContainer notificationContainer,
61             final String... notificationNames) {
62         final var notifications = notificationContainer.getNotifications();
63         assertEquals(notificationNames.length, notifications.size());
64
65         final var notificationQNames = notifications.stream()
66             .map(NotificationDefinition::getQName)
67             .collect(Collectors.toUnmodifiableSet());
68
69         for (final String notificationName : notificationNames) {
70             assertThat(QName.create(FOO_NS, notificationName), in(notificationQNames));
71         }
72     }
73
74     @Test
75     public void invalid10Test() {
76         assertInvalidSubstatementException(startsWith("NOTIFICATION is not valid for"),
77             "/rfc7950/notifications-in-data-nodes/foo10.yang");
78     }
79
80     @Test
81     public void invalid11Test() {
82         assertSourceException(
83             startsWith("Notification (foo)grp-notification is defined within an rpc, action, or another notification"),
84             "/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
85     }
86
87     @Test
88     public void testNotificationWithinListWithoutKey() {
89         assertSourceException(
90             startsWith("Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list that "
91                 + "has no key statement"),
92             "/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
93     }
94
95     @Test
96     public void testNotificationInUsedGroupingWithinCase() {
97         assertSourceException(
98             startsWith("Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a "
99                 + "case statement"),
100             "/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
101     }
102 }