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;
18 import java.net.URISyntaxException;
19 import java.text.ParseException;
20 import java.util.Collection;
21 import java.util.Optional;
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.util.SimpleSchemaContext;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.EffectiveSchemaContext;
41 public class EffectiveSchemaContextTest {
43 public void testEffectiveSchemaContext() throws ReactorException, ParseException, URISyntaxException, IOException,
44 YangSyntaxErrorException {
45 final EffectiveSchemaContext schemaContext = RFC7950Reactors.defaultReactor().newBuild()
46 .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/foo.yang"))
47 .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/bar.yang"))
48 .addSource(StmtTestUtils.sourceForResource("/effective-schema-context-test/baz.yang"))
50 assertNotNull(schemaContext);
52 final Collection<? extends DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
53 assertEquals(3, dataDefinitions.size());
55 final Collection<? extends DataSchemaNode> childNodes = schemaContext.getChildNodes();
56 assertEquals(3, childNodes.size());
58 final Collection<? extends NotificationDefinition> notifications = schemaContext.getNotifications();
59 assertEquals(3, notifications.size());
61 final Collection<? extends RpcDefinition> rpcs = schemaContext.getOperations();
62 assertEquals(3, rpcs.size());
64 final Collection<? extends ExtensionDefinition> extensions = schemaContext.getExtensions();
65 assertEquals(3, extensions.size());
67 final Collection<? extends UnknownSchemaNode> unknownSchemaNodes = schemaContext.getUnknownSchemaNodes();
68 assertEquals(3, unknownSchemaNodes.size());
70 assertNull(schemaContext.dataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
72 assertFalse(schemaContext.findModule("foo", Revision.of("2016-08-21")).isPresent());
73 assertFalse(schemaContext.findModule(URI.create("foo-namespace"), Revision.of("2016-08-21")).isPresent());
75 assertFalse(schemaContext.isAugmenting());
76 assertFalse(schemaContext.isAddedByUses());
77 assertEquals(Optional.empty(), schemaContext.effectiveConfig());
78 assertFalse(schemaContext.getWhenCondition().isPresent());
79 assertEquals(0, schemaContext.getMustConstraints().size());
80 assertFalse(schemaContext.getDescription().isPresent());
81 assertFalse(schemaContext.getReference().isPresent());
82 assertEquals(SchemaContext.NAME, schemaContext.getQName());
83 assertEquals(SchemaPath.ROOT, schemaContext.getPath());
84 assertEquals(Status.CURRENT, schemaContext.getStatus());
85 assertNotNull(schemaContext.getUses());
86 assertTrue(schemaContext.getUses().isEmpty());
87 assertNotNull(schemaContext.getAvailableAugmentations());
88 assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
90 Module fooModule = schemaContext.findModule("foo", Revision.of("2016-09-21")).get();
91 assertEquals(3, schemaContext.getModules().size());
92 assertEquals(3, schemaContext.getRootDeclaredStatements().size());
93 assertEquals(3, schemaContext.getModuleStatements().size());
95 final Set<Module> modules = schemaContext.getModules();
96 final SchemaContext copiedSchemaContext = SimpleSchemaContext.forModules(modules);
97 assertNotNull(copiedSchemaContext);
98 assertEquals(modules, copiedSchemaContext.getModules());