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
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.hamcrest.collection.IsIn.in;
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
16 import java.util.Collection;
17 import java.util.stream.Collectors;
18 import org.junit.jupiter.api.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.GroupingDefinition;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
24 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
28 class Bug6897Test extends AbstractYangTest {
29 private static final String FOO_NS = "foo";
32 void notificationsInDataContainersTest() {
33 final var context = assertEffectiveModel("/rfc7950/notifications-in-data-nodes/foo.yang");
35 assertContainsNotifications(context, "root", "grp-notification", "aug-notification");
36 assertContainsNotifications(context, "top-list", "top-list-notification");
37 assertContainsNotifications(context, "top", "top-notification");
39 final Collection<? extends GroupingDefinition> groupings = context.getGroupings();
40 assertEquals(1, groupings.size());
41 assertContainsNotifications(groupings.iterator().next(), "grp-notification");
43 final Collection<? extends Module> modules = context.getModules();
44 assertEquals(1, modules.size());
45 final Module foo = modules.iterator().next();
46 final Collection<? extends AugmentationSchemaNode> augmentations = foo.getAugmentations();
47 assertEquals(1, augmentations.size());
48 assertContainsNotifications(augmentations.iterator().next(), "aug-notification", "grp-notification");
51 private static void assertContainsNotifications(final SchemaContext schemaContext, final String dataContainerName,
52 final String... notificationNames) {
53 final var dataChildByName = schemaContext.getDataChildByName(QName.create(FOO_NS, dataContainerName));
54 assertContainsNotifications(assertInstanceOf(NotificationNodeContainer.class, dataChildByName),
58 private static void assertContainsNotifications(final NotificationNodeContainer notificationContainer,
59 final String... notificationNames) {
60 final var notifications = notificationContainer.getNotifications();
61 assertEquals(notificationNames.length, notifications.size());
63 final var notificationQNames = notifications.stream()
64 .map(NotificationDefinition::getQName)
65 .collect(Collectors.toUnmodifiableSet());
67 for (final String notificationName : notificationNames) {
68 assertThat(QName.create(FOO_NS, notificationName), in(notificationQNames));
73 void invalid10Test() {
74 assertInvalidSubstatementException(startsWith("NOTIFICATION is not valid for"),
75 "/rfc7950/notifications-in-data-nodes/foo10.yang");
79 void invalid11Test() {
80 assertSourceException(
81 startsWith("Notification (foo)grp-notification is defined within an rpc, action, or another notification"),
82 "/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
86 void testNotificationWithinListWithoutKey() {
87 assertSourceException(
88 startsWith("Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list that "
89 + "has no key statement"),
90 "/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
94 void testNotificationInUsedGroupingWithinCase() {
95 assertSourceException(
96 startsWith("Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a "
98 "/rfc7950/notifications-in-data-nodes/baz-invalid.yang");