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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
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;
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.HashSet;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.ActionNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
33 public class ActionStatementTest {
35 private static final String FOO_NS = "foo-namespace";
36 private static final String FOO_REV = "2016-12-13";
39 public void testActionStatementInDataContainers() throws ReactorException, FileNotFoundException,
40 URISyntaxException, ParseException {
41 final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/action-stmt/foo.yang");
42 assertNotNull(schemaContext);
44 assertContainsActions(schemaContext, "root", "grp-action", "aug-action");
45 assertContainsActions(schemaContext, "top-list", "top-list-action");
46 assertContainsActions(schemaContext, "top", "top-action");
48 final Set<GroupingDefinition> groupings = schemaContext.getGroupings();
49 assertEquals(1, groupings.size());
50 assertContainsActions(groupings.iterator().next(), "grp-action");
52 final Set<Module> modules = schemaContext.getModules();
53 assertEquals(1, modules.size());
54 final Module foo = modules.iterator().next();
55 final Set<AugmentationSchema> augmentations = foo.getAugmentations();
56 assertEquals(1, augmentations.size());
57 assertContainsActions(augmentations.iterator().next(), "aug-action", "grp-action");
60 private static void assertContainsActions(final SchemaContext schemaContext, final String dataContainerName,
61 final String... actionNames) {
62 final DataSchemaNode dataChildByName = schemaContext.getDataChildByName(QName.create(FOO_NS, FOO_REV,
64 assertTrue(dataChildByName instanceof ActionNodeContainer);
65 assertContainsActions((ActionNodeContainer) dataChildByName, actionNames);
68 private static void assertContainsActions(final ActionNodeContainer actionContainer,
69 final String... actionNames) {
70 final Set<ActionDefinition> actions = actionContainer.getActions();
71 assertEquals(actionNames.length, actions.size());
73 final Set<QName> actionQNames = new HashSet<>();
74 actions.forEach(n -> actionQNames.add(n.getQName()));
76 for (final String actionName : actionNames) {
77 assertTrue(actionQNames.contains(QName.create(FOO_NS, FOO_REV, actionName)));
82 public void testActionUnsupportedInYang10() throws ReactorException, FileNotFoundException, URISyntaxException {
84 StmtTestUtils.parseYangSource("/rfc7950/action-stmt/foo10.yang");
85 fail("Test should fail due to invalid Yang 1.0");
86 } catch (final ReactorException e) {
87 assertTrue(e.getCause().getMessage().startsWith("action is not a YANG statement or use of extension"));
92 public void testActionWithinIllegalAncestor() throws FileNotFoundException, URISyntaxException {
94 StmtTestUtils.parseYangSource("/rfc7950/action-stmt/foo-invalid.yang");
95 fail("Test should fail due to invalid Yang 1.1");
96 } catch (final ReactorException e) {
97 assertTrue(e.getCause().getMessage().startsWith("Action (foo-namespace?revision=2016-12-13)" +
98 "action-in-grouping is defined within a notification, rpc or another action"));
103 public void testActionWithinListWithoutKey() throws FileNotFoundException, URISyntaxException {
105 StmtTestUtils.parseYangSource("/rfc7950/action-stmt/bar-invalid.yang");
106 fail("Test should fail due to invalid Yang 1.1");
107 } catch (final ReactorException e) {
108 assertTrue(e.getCause().getMessage().startsWith(
109 "Action (bar-namespace?revision=2016-12-13)my-action is defined within a list" +
110 " that has no key statement"));
115 public void testActionInUsedGroupingWithinCase() throws FileNotFoundException, URISyntaxException {
117 StmtTestUtils.parseYangSource("/rfc7950/action-stmt/baz-invalid.yang");
118 fail("Test should fail due to invalid Yang 1.1");
119 } catch (final ReactorException e) {
120 assertTrue(e.getCause().getMessage().startsWith(
121 "Action (baz-namespace?revision=2016-12-13)action-in-grouping is defined within a " +
127 public void testActionInUsedGroupingAtTopLevelOfModule() throws FileNotFoundException, URISyntaxException {
129 StmtTestUtils.parseYangSource("/rfc7950/action-stmt/foobar-invalid.yang");
130 fail("Test should fail due to invalid Yang 1.1");
131 } catch (final ReactorException e) {
132 assertTrue(e.getCause().getMessage().startsWith(
133 "Action (foobar-namespace?revision=2016-12-13)my-action is defined at the top level of a module"));