2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
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;
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;
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;
42 public class EffectiveSchemaContextTest {
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"))
51 assertNotNull(schemaContext);
53 final Collection<? extends DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
54 assertEquals(3, dataDefinitions.size());
56 final Collection<? extends DataSchemaNode> childNodes = schemaContext.getChildNodes();
57 assertEquals(3, childNodes.size());
59 final Collection<? extends NotificationDefinition> notifications = schemaContext.getNotifications();
60 assertEquals(3, notifications.size());
62 final Collection<? extends RpcDefinition> rpcs = schemaContext.getOperations();
63 assertEquals(3, rpcs.size());
65 final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
66 assertEquals(3, extensions.size());
68 for (ModuleEffectiveStatement module : schemaContext.getModuleStatements().values()) {
69 assertEquals(1, module.getDeclared().declaredSubstatements(UnrecognizedStatement.class).size());
72 assertNull(schemaContext.dataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
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());
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());
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());
97 final Set<Module> modules = schemaContext.getModules();
98 final SchemaContext copiedSchemaContext = SimpleSchemaContext.forModules(modules);
99 assertNotNull(copiedSchemaContext);
100 assertEquals(modules, copiedSchemaContext.getModules());