Increase unit test coverage for Yangtools
[yangtools.git] / yang / yang-parser-impl / 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 com.google.common.collect.Lists;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.text.ParseException;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Set;
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;
44
45 public class EffectiveSchemaContextTest {
46
47     @Test
48     public void testEffectiveSchemaContext() throws ReactorException, ParseException, FileNotFoundException,
49             URISyntaxException {
50         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
51
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);
58
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);
65
66         final SchemaContext schemaContext = reactor.buildEffective(Lists.newArrayList(
67                 yangInputStream1, yangInputStream2, yangInputStream3));
68         assertNotNull(schemaContext);
69
70         final Set<DataSchemaNode> dataDefinitions = schemaContext.getDataDefinitions();
71         assertEquals(3, dataDefinitions.size());
72
73         final Collection<DataSchemaNode> childNodes = schemaContext.getChildNodes();
74         assertEquals(3, childNodes.size());
75
76         final Set<NotificationDefinition> notifications = schemaContext.getNotifications();
77         assertEquals(3, notifications.size());
78
79         final Set<RpcDefinition> rpcs = schemaContext.getOperations();
80         assertEquals(3, rpcs.size());
81
82         final Set<ExtensionDefinition> extensions = schemaContext.getExtensions();
83         assertEquals(3, extensions.size());
84
85         final List<UnknownSchemaNode> unknownSchemaNodes = schemaContext.getUnknownSchemaNodes();
86         assertEquals(3, unknownSchemaNodes.size());
87
88         assertNull(schemaContext.getDataChildByName(QName.create("foo-namespace", "2016-09-21", "foo-cont")));
89
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")));
95
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());
110
111         Module fooModule = schemaContext.findModuleByName(
112                 "foo", SimpleDateFormatUtil.getRevisionFormat().parse("2016-09-21"));
113         assertFalse(schemaContext.getModuleSource(fooModule).isPresent());
114
115         assertEquals(3, schemaContext.getAllModuleIdentifiers().size());
116         assertEquals(3, ((EffectiveSchemaContext) schemaContext).getRootDeclaredStatements().size());
117         assertEquals(3,((EffectiveSchemaContext) schemaContext).getRootEffectiveStatements().size());
118
119         final Set<Module> modules = schemaContext.getModules();
120         final SchemaContext copiedSchemaContext = EffectiveSchemaContext.resolveSchemaContext(modules);
121         assertNotNull(copiedSchemaContext);
122         assertEquals(modules, copiedSchemaContext.getModules());
123     }
124 }