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
9 package org.opendaylight.yangtools.yang.stmt;
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;
17 import com.google.common.collect.Lists;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.InputStream;
23 import java.net.URISyntaxException;
24 import java.text.ParseException;
25 import java.util.Collection;
26 import java.util.List;
28 import org.junit.Test;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
35 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
38 import org.opendaylight.yangtools.yang.model.api.Status;
39 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
42 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
43 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
45 public class EffectiveSchemaContextTest {
48 public void testEffectiveSchemaContext() throws ReactorException, ParseException, FileNotFoundException,
50 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
52 final File yangFile1 = new File(getClass().getResource("/effective-schema-context-test/foo.yang").toURI());
53 assertNotNull(yangFile1);
54 final File yangFile2 = new File(getClass().getResource("/effective-schema-context-test/bar.yang").toURI());
55 assertNotNull(yangFile2);
56 final File yangFile3 = new File(getClass().getResource("/effective-schema-context-test/baz.yang").toURI());
57 assertNotNull(yangFile3);
59 final InputStream yangInputStream1 = new FileInputStream(yangFile1);
60 assertNotNull(yangInputStream1);
61 final InputStream yangInputStream2 = new FileInputStream(yangFile2);
62 assertNotNull(yangInputStream2);
63 final InputStream yangInputStream3 = new FileInputStream(yangFile3);
64 assertNotNull(yangInputStream3);
66 final SchemaContext schemaContext = reactor.buildEffective(Lists.newArrayList(
67 yangInputStream1, yangInputStream2, yangInputStream3));
68 assertNotNull(schemaContext);
70 final Set<DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
71 assertEquals(3, dataDefinitions.size());
73 final Collection<DataSchemaNode> childNodes = schemaContext.getChildNodes();
74 assertEquals(3, childNodes.size());
76 final Set<NotificationDefinition> notifications = schemaContext.getNotifications();
77 assertEquals(3, notifications.size());
79 final Set<RpcDefinition> rpcs = schemaContext.getOperations();
80 assertEquals(3, rpcs.size());
82 final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
83 assertEquals(3, extensions.size());
85 final List<UnknownSchemaNode> unknownSchemaNodes = schemaContext.getUnknownSchemaNodes();
86 assertEquals(3, unknownSchemaNodes.size());
88 assertNull(schemaContext.getDataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
90 assertNull(schemaContext.findModuleByName("foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-08-21")));
91 assertNull(schemaContext.findModuleByNamespaceAndRevision(
92 null, SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21")));
93 assertNull(schemaContext.findModuleByNamespaceAndRevision(
94 URI.create("foo-namespace"), SimpleDateFormatUtil.getRevisionFormat().parse("2016-08-21")));
96 assertFalse(schemaContext.isAugmenting());
97 assertFalse(schemaContext.isAddedByUses());
98 assertFalse(schemaContext.isConfiguration());
99 assertFalse(schemaContext.isPresenceContainer());
100 assertNull(schemaContext.getConstraints());
101 assertNull(schemaContext.getDescription());
102 assertNull(schemaContext.getReference());
103 assertEquals(SchemaContext.NAME, schemaContext.getQName());
104 assertEquals(SchemaPath.ROOT, schemaContext.getPath());
105 assertEquals(Status.CURRENT, schemaContext.getStatus());
106 assertTrue(schemaContext.getUses() instanceof Set);
107 assertTrue(schemaContext.getUses().isEmpty());
108 assertTrue(schemaContext.getAvailableAugmentations() instanceof Set);
109 assertTrue(schemaContext.getAvailableAugmentations().isEmpty());
111 Module fooModule = schemaContext.findModuleByName(
112 "foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21"));
113 assertFalse(schemaContext.getModuleSource(fooModule).isPresent());
115 assertEquals(3, schemaContext.getAllModuleIdentifiers().size());
116 assertEquals(3, ((EffectiveSchemaContext) schemaContext).getRootDeclaredStatements().size());
117 assertEquals(3,((EffectiveSchemaContext) schemaContext).getRootEffectiveStatements().size());
119 final Set<Module> modules = schemaContext.getModules();
120 final SchemaContext copiedSchemaContext = EffectiveSchemaContext.resolveSchemaContext(modules);
121 assertNotNull(copiedSchemaContext);
122 assertEquals(modules, copiedSchemaContext.getModules());