Bug 6880: [Yang 1.1] Allow leaf-lists to have default values
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / stmt / rfc7950 / ActionStatementTest.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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc7950;
10
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;
15
16 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.HashSet;
20 import java.util.Set;
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;
32
33 public class ActionStatementTest {
34
35     private static final String FOO_NS = "foo-namespace";
36     private static final String FOO_REV = "2016-12-13";
37
38     @Test
39     public void testActionStatementInDataContainers() throws ReactorException, FileNotFoundException,
40             URISyntaxException, ParseException {
41         final SchemaContext schemaContext = StmtTestUtils.parseYangSource("/rfc7950/action-stmt/foo.yang");
42         assertNotNull(schemaContext);
43
44         assertContainsActions(schemaContext, "root", "grp-action", "aug-action");
45         assertContainsActions(schemaContext, "top-list", "top-list-action");
46         assertContainsActions(schemaContext, "top", "top-action");
47
48         final Set<GroupingDefinition> groupings = schemaContext.getGroupings();
49         assertEquals(1, groupings.size());
50         assertContainsActions(groupings.iterator().next(), "grp-action");
51
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");
58     }
59
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,
63                 dataContainerName));
64         assertTrue(dataChildByName instanceof ActionNodeContainer);
65         assertContainsActions((ActionNodeContainer) dataChildByName, actionNames);
66     }
67
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());
72
73         final Set<QName> actionQNames = new HashSet<>();
74         actions.forEach(n -> actionQNames.add(n.getQName()));
75
76         for (final String actionName : actionNames) {
77             assertTrue(actionQNames.contains(QName.create(FOO_NS, FOO_REV, actionName)));
78         }
79     }
80
81     @Test
82     public void testActionUnsupportedInYang10() throws ReactorException, FileNotFoundException, URISyntaxException {
83         try {
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"));
88         }
89     }
90
91     @Test
92     public void testActionWithinIllegalAncestor() throws FileNotFoundException, URISyntaxException {
93         try {
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"));
99         }
100     }
101
102     @Test
103     public void testActionWithinListWithoutKey() throws FileNotFoundException, URISyntaxException {
104         try {
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"));
111         }
112     }
113
114     @Test
115     public void testActionInUsedGroupingWithinCase() throws FileNotFoundException, URISyntaxException {
116         try {
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 " +
122                             "case statement"));
123         }
124     }
125
126     @Test
127     public void testActionInUsedGroupingAtTopLevelOfModule() throws FileNotFoundException, URISyntaxException {
128         try {
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"));
134         }
135     }
136 }