*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.collection.IsIn.in;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import java.util.Collection;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.stream.Collectors;
import org.junit.Test;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
-import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
+import org.opendaylight.yangtools.yang.stmt.AbstractYangTest;
-public class Bug6897Test {
+public class Bug6897Test extends AbstractYangTest {
private static final String FOO_NS = "foo";
@Test
public void notificationsInDataContainersTest() throws Exception {
- final SchemaContext schemaContext = StmtTestUtils
- .parseYangSource("/rfc7950/notifications-in-data-nodes/foo.yang");
- assertNotNull(schemaContext);
+ final var context = assertEffectiveModel("/rfc7950/notifications-in-data-nodes/foo.yang");
- assertContainsNotifications(schemaContext, "root", "grp-notification", "aug-notification");
- assertContainsNotifications(schemaContext, "top-list", "top-list-notification");
- assertContainsNotifications(schemaContext, "top", "top-notification");
+ assertContainsNotifications(context, "root", "grp-notification", "aug-notification");
+ assertContainsNotifications(context, "top-list", "top-list-notification");
+ assertContainsNotifications(context, "top", "top-notification");
- final Collection<? extends GroupingDefinition> groupings = schemaContext.getGroupings();
+ final Collection<? extends GroupingDefinition> groupings = context.getGroupings();
assertEquals(1, groupings.size());
assertContainsNotifications(groupings.iterator().next(), "grp-notification");
- final Collection<? extends Module> modules = schemaContext.getModules();
+ final Collection<? extends Module> modules = context.getModules();
assertEquals(1, modules.size());
final Module foo = modules.iterator().next();
final Collection<? extends AugmentationSchemaNode> augmentations = foo.getAugmentations();
final String... notificationNames) {
final DataSchemaNode dataChildByName = schemaContext.getDataChildByName(
QName.create(FOO_NS, dataContainerName));
- assertTrue(dataChildByName instanceof NotificationNodeContainer);
+ assertThat(dataChildByName, instanceOf(NotificationNodeContainer.class));
assertContainsNotifications((NotificationNodeContainer) dataChildByName, notificationNames);
}
private static void assertContainsNotifications(final NotificationNodeContainer notificationContainer,
final String... notificationNames) {
- final Collection<? extends NotificationDefinition> notifications = notificationContainer.getNotifications();
+ final var notifications = notificationContainer.getNotifications();
assertEquals(notificationNames.length, notifications.size());
- final Set<QName> notificationQNames = new HashSet<>();
- notifications.forEach(n -> notificationQNames.add(n.getQName()));
+ final var notificationQNames = notifications.stream()
+ .map(NotificationDefinition::getQName)
+ .collect(Collectors.toUnmodifiableSet());
for (final String notificationName : notificationNames) {
- assertTrue(notificationQNames.contains(QName.create(FOO_NS, notificationName)));
+ assertThat(QName.create(FOO_NS, notificationName), in(notificationQNames));
}
}
@Test
- public void invalid10Test() throws Exception {
- try {
- StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo10.yang");
- fail("Test should fail due to invalid Yang 1.0");
- } catch (final SomeModifiersUnresolvedException e) {
- assertTrue(e.getCause().getMessage().startsWith("NOTIFICATION is not valid for"));
- }
+ public void invalid10Test() {
+ assertInvalidSubstatementException(startsWith("NOTIFICATION is not valid for"),
+ "/rfc7950/notifications-in-data-nodes/foo10.yang");
}
@Test
- public void invalid11Test() throws Exception {
- try {
- StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
- fail("Test should fail due to invalid Yang 1.1");
- } catch (final SomeModifiersUnresolvedException e) {
- assertTrue(e.getCause().getMessage().startsWith(
- "Notification (foo)grp-notification is defined within an rpc, action, or another notification"));
- }
+ public void invalid11Test() {
+ assertSourceException(
+ startsWith("Notification (foo)grp-notification is defined within an rpc, action, or another notification"),
+ "/rfc7950/notifications-in-data-nodes/foo-invalid.yang");
}
@Test
- public void testNotificationWithinListWithoutKey() throws Exception {
- try {
- StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
- fail("Test should fail due to invalid Yang 1.1");
- } catch (final SomeModifiersUnresolvedException e) {
- assertTrue(e.getCause().getMessage().startsWith(
- "Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list "
- + "that has no key statement"));
- }
+ public void testNotificationWithinListWithoutKey() {
+ assertSourceException(
+ startsWith("Notification (bar-namespace?revision=2016-12-08)my-notification is defined within a list that "
+ + "has no key statement"),
+ "/rfc7950/notifications-in-data-nodes/bar-invalid.yang");
}
@Test
- public void testNotificationInUsedGroupingWithinCase() throws Exception {
- try {
- StmtTestUtils.parseYangSource("/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
- fail("Test should fail due to invalid Yang 1.1");
- } catch (final SomeModifiersUnresolvedException e) {
- assertTrue(e.getCause().getMessage().startsWith(
- "Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a case "
- + "statement"));
- }
+ public void testNotificationInUsedGroupingWithinCase() {
+ assertSourceException(
+ startsWith("Notification (baz-namespace?revision=2016-12-08)notification-in-grouping is defined within a "
+ + "case statement"),
+ "/rfc7950/notifications-in-data-nodes/baz-invalid.yang");
}
}