Split out input/output schema nodes
[yangtools.git] / yang / 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
16
17 import java.io.IOException;
18 import java.net.URI;
19 import java.net.URISyntaxException;
20 import java.text.ParseException;
21 import java.util.Collection;
22 import java.util.Set;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.Revision;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
30 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 import org.opendaylight.yangtools.yang.model.api.Status;
34 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.util.SimpleSchemaContext;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
42
43 public class EffectiveSchemaContextTest {
44
45     @Test
46     public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException,
47             YangSyntaxErrorException {
48         final EffectiveSchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
49                 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(
50                         "/effective-schema-context-test/foo.yang")))
51                 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(
52                         "/effective-schema-context-test/bar.yang")))
53                 .addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(
54                         "/effective-schema-context-test/baz.yang")))
55                 .buildEffective();
56         assertNotNull(schemaContext);
57
58         final Collection<? extends DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
59         assertEquals(3, dataDefinitions.size());
60
61         final Collection<? extends DataSchemaNode> childNodes = schemaContext.getChildNodes();
62         assertEquals(3, childNodes.size());
63
64         final Collection<? extends NotificationDefinition> notifications = schemaContext.getNotifications();
65         assertEquals(3, notifications.size());
66
67         final Collection<? extends RpcDefinition> rpcs = schemaContext.getOperations();
68         assertEquals(3, rpcs.size());
69
70         final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
71         assertEquals(3, extensions.size());
72
73         final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = schemaContext.getUnknownSchemaNodes();
74         assertEquals(3, unknownSchemaNodes.size());
75
76         assertNull(schemaContext.getDataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
77
78         assertFalse(schemaContext.findModule("foo", Revision.of("2016-08-21")).isPresent());
79         assertFalse(schemaContext.findModule(URI.create("foo-namespace"), Revision.of("2016-08-21")).isPresent());
80
81         assertFalse(schemaContext.isAugmenting());
82         assertFalse(schemaContext.isAddedByUses());
83         assertFalse(schemaContext.isConfiguration());
84         assertFalse(schemaContext.getWhenCondition().isPresent());
85         assertEquals(0, schemaContext.getMustConstraints().size());
86         assertFalse(schemaContext.getDescription().isPresent());
87         assertFalse(schemaContext.getReference().isPresent());
88         assertEquals(SchemaContext.NAME, schemaContext.getQName());
89         assertEquals(SchemaPath.ROOT, schemaContext.getPath());
90         assertEquals(Status.CURRENT, schemaContext.getStatus());
91         assertNotNull(schemaContext.getUses());
92         assertTrue(schemaContext.getUses().isEmpty());
93         assertNotNull(schemaContext.getAvailableAugmentations());
94         assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
95
96         Module fooModule = schemaContext.findModule("foo", Revision.of("2016-09-21")).get();
97         assertEquals(3, schemaContext.getModules().size());
98         assertEquals(3, schemaContext.getRootDeclaredStatements().size());
99         assertEquals(3, schemaContext.getModuleStatements().size());
100
101         final Set<Module> modules = schemaContext.getModules();
102         final SchemaContext copiedSchemaContext =  SimpleSchemaContext.forModules(modules);
103         assertNotNull(copiedSchemaContext);
104         assertEquals(modules, copiedSchemaContext.getModules());
105     }
106 }