Add XMLNamespace
[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 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 java.util.Set;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
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.stmt.ModuleEffectiveStatement;
35 import org.opendaylight.yangtools.yang.model.api.stmt.UnrecognizedStatement;
36 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
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.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
41
42 public class EffectiveSchemaContextTest {
43     @Test
44     public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException,
45             YangSyntaxErrorException {
46         final EffectiveSchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
47             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/foo.yang"))
48             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/bar.yang"))
49             .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/baz.yang"))
50             .buildEffective();
51         assertNotNull(schemaContext);
52
53         final Collection<? extends DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
54         assertEquals(3, dataDefinitions.size());
55
56         final Collection<? extends DataSchemaNode> childNodes = schemaContext.getChildNodes();
57         assertEquals(3, childNodes.size());
58
59         final Collection<? extends NotificationDefinition> notifications = schemaContext.getNotifications();
60         assertEquals(3, notifications.size());
61
62         final Collection<? extends RpcDefinition> rpcs = schemaContext.getOperations();
63         assertEquals(3, rpcs.size());
64
65         final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
66         assertEquals(3, extensions.size());
67
68         for (ModuleEffectiveStatement module : schemaContext.getModuleStatements().values()) {
69             assertEquals(1, module.getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
70         }
71
72         assertNull(schemaContext.dataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
73
74         assertFalse(schemaContext.findModule("foo", Revision.of("2016-08-21")).isPresent());
75         assertFalse(schemaContext.findModule(XMLNamespace.of("foo-namespace"), Revision.of("2016-08-21")).isPresent());
76
77         assertFalse(schemaContext.isAugmenting());
78         assertFalse(schemaContext.isAddedByUses());
79         assertEquals(Optional.empty(), schemaContext.effectiveConfig());
80         assertFalse(schemaContext.getWhenCondition().isPresent());
81         assertEquals(0, schemaContext.getMustConstraints().size());
82         assertFalse(schemaContext.getDescription().isPresent());
83         assertFalse(schemaContext.getReference().isPresent());
84         assertEquals(SchemaContext.NAME, schemaContext.getQName());
85         assertEquals(SchemaPath.ROOT, schemaContext.getPath());
86         assertEquals(Status.CURRENT, schemaContext.getStatus());
87         assertNotNull(schemaContext.getUses());
88         assertTrue(schemaContext.getUses().isEmpty());
89         assertNotNull(schemaContext.getAvailableAugmentations());
90         assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
91
92         Module fooModule = schemaContext.findModule("foo", Revision.of("2016-09-21")).get();
93         assertEquals(3, schemaContext.getModules().size());
94         assertEquals(3, schemaContext.getRootDeclaredStatements().size());
95         assertEquals(3, schemaContext.getModuleStatements().size());
96
97         final Set<Module> modules = schemaContext.getModules();
98         final SchemaContext copiedSchemaContext =  SimpleSchemaContext.forModules(modules);
99         assertNotNull(copiedSchemaContext);
100         assertEquals(modules, copiedSchemaContext.getModules());
101     }
102 }