ab592aca2821d40f72f0f8e8aac115131348ec4e
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / yin / YinFileGroupingStmtTest.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.stmt.yin;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.Optional;
19 import java.util.Set;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
29 import org.opendaylight.yangtools.yang.stmt.TestUtils;
30 import org.xml.sax.SAXException;
31
32 public class YinFileGroupingStmtTest {
33
34     private SchemaContext context;
35
36     @Before
37     public void init() throws ReactorException, SAXException, IOException, URISyntaxException {
38         context = TestUtils.loadYinModules(getClass().getResource("/semantic-statement-parser/yin/modules").toURI());
39         assertEquals(9, context.getModules().size());
40     }
41
42     @Test
43     public void testGrouping() throws URISyntaxException {
44         final Module testModule = TestUtils.findModule(context, "config").get();
45         assertNotNull(testModule);
46
47         final Set<GroupingDefinition> groupings = testModule.getGroupings();
48         assertEquals(1, groupings.size());
49
50         final Iterator<GroupingDefinition> groupingsIterator = groupings.iterator();
51         final GroupingDefinition grouping = groupingsIterator.next();
52         assertEquals("service-ref", grouping.getQName().getLocalName());
53         assertEquals(Optional.of("Type of references to a particular service instance. This type\n"
54                 + "can be used when defining module configuration to refer to a\n"
55                 + "particular service instance. Containers using this grouping\n"
56                 + "should not define anything else. The run-time implementation\n"
57                 + "is expected to inject a reference to the service as the value\n"
58                 + "of the container."), grouping.getDescription());
59
60         final Collection<DataSchemaNode> children = grouping.getChildNodes();
61         assertEquals(2, children.size());
62
63         final LeafSchemaNode leaf1 = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
64                 testModule.getQNameModule(), "type"));
65         assertNotNull(leaf1);
66         assertTrue(leaf1.isMandatory());
67
68         final LeafSchemaNode leaf2 = (LeafSchemaNode) grouping.getDataChildByName(QName.create(
69                 testModule.getQNameModule(), "name"));
70         assertNotNull(leaf2);
71         assertTrue(leaf2.isMandatory());
72     }
73 }