Populate parser/ hierarchy
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / EffectiveSchemaContextTest.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;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import java.text.ParseException;
19 import java.util.Collection;
20 import java.util.Optional;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.Revision;
24 import org.opendaylight.yangtools.yang.common.XMLNamespace;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
27 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
28 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
31 import org.opendaylight.yangtools.yang.model.api.Status;
32 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
33 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
34 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
38
39 public class EffectiveSchemaContextTest {
40     @Test
41     public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException,
42             YangSyntaxErrorException {
43         final EffectiveSchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
44             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/foo.yang"))
45             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/bar.yang"))
46             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/baz.yang"))
47             .buildEffective();
48         assertNotNull(schemaContext);
49
50         final Collection<? extends DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
51         assertEquals(3, dataDefinitions.size());
52
53         final Collection<? extends DataSchemaNode> childNodes = schemaContext.getChildNodes();
54         assertEquals(3, childNodes.size());
55
56         final Collection<? extends NotificationDefinition> notifications = schemaContext.getNotifications();
57         assertEquals(3, notifications.size());
58
59         final Collection<? extends RpcDefinition> rpcs = schemaContext.getOperations();
60         assertEquals(3, rpcs.size());
61
62         final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
63         assertEquals(3, extensions.size());
64
65         for (ModuleEffectiveStatement module : schemaContext.getModuleStatements().values()) {
66             assertEquals(1, module.getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
67         }
68
69         assertNull(schemaContext.dataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
70
71         assertFalse(schemaContext.findModule("foo", Revision.of("2016-08-21")).isPresent());
72         assertFalse(schemaContext.findModule(XMLNamespace.of("foo-namespace"), Revision.of("2016-08-21")).isPresent());
73
74         assertFalse(schemaContext.isAugmenting());
75         assertFalse(schemaContext.isAddedByUses());
76         assertEquals(Optional.empty(), schemaContext.effectiveConfig());
77         assertFalse(schemaContext.getWhenCondition().isPresent());
78         assertEquals(0, schemaContext.getMustConstraints().size());
79         assertFalse(schemaContext.getDescription().isPresent());
80         assertFalse(schemaContext.getReference().isPresent());
81         assertEquals(SchemaContext.NAME, schemaContext.getQName());
82         assertEquals(SchemaPath.ROOT, schemaContext.getPath());
83         assertEquals(Status.CURRENT, schemaContext.getStatus());
84         assertNotNull(schemaContext.getUses());
85         assertTrue(schemaContext.getUses().isEmpty());
86         assertNotNull(schemaContext.getAvailableAugmentations());
87         assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
88
89         assertTrue(schemaContext.findModule("foo", Revision.of("2016-09-21")).isPresent());
90         assertEquals(3, schemaContext.getModules().size());
91         assertEquals(3, schemaContext.getRootDeclaredStatements().size());
92         assertEquals(3, schemaContext.getModuleStatements().size());
93     }
94 }