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.semver;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
16 import java.io.FileNotFoundException;
18 import java.net.URISyntaxException;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.concepts.SemVer;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
33 public class SemanticVersionTest {
35 public void basicTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
36 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic", StatementParserMode.SEMVER_MODE);
37 assertNotNull(context);
39 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
40 Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
41 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
44 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
45 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
46 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
50 public void basicTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
51 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-2", StatementParserMode.SEMVER_MODE);
52 assertNotNull(context);
54 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
55 Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
56 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
59 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
60 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
61 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
65 public void basicTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
66 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-3", StatementParserMode.SEMVER_MODE);
67 assertNotNull(context);
69 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
70 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
73 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
74 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
78 public void basicImportTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
79 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/basic-import-1",
80 StatementParserMode.SEMVER_MODE);
81 assertNotNull(context);
83 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
84 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
87 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
88 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
89 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
90 assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
94 public void multipleModulesTest() throws SourceException, FileNotFoundException, ReactorException,
96 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple-modules",
97 StatementParserMode.SEMVER_MODE);
98 assertNotNull(context);
100 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
101 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
104 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
105 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
106 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
107 assertEquals(SemVer.valueOf("0.10.4"), bar.getSemanticVersion());
111 public void basicImportErrTest1() throws SourceException, FileNotFoundException, ReactorException,
114 StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-1", StatementParserMode.SEMVER_MODE);
115 fail("Test should fail due to invalid semantic version");
116 } catch (ReactorException e) {
117 assertTrue(e.getCause().getMessage()
118 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
123 public void basicImportErrTest2() throws SourceException, FileNotFoundException, ReactorException,
126 StmtTestUtils.parseYangSources("/semantic-version/basic-import-invalid-2", StatementParserMode.SEMVER_MODE);
127 fail("Test should fail due to invalid semantic version");
128 } catch (ReactorException e) {
129 assertTrue(e.getCause().getMessage()
130 .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
135 public void nodeTest() throws Exception {
136 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/node-test", StatementParserMode.SEMVER_MODE);
137 assertNotNull(context);
139 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
140 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
143 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
144 assertEquals(SemVer.valueOf("2016.1.1"), foo.getSemanticVersion());
145 Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
146 assertEquals(SemVer.valueOf("2016.4.6"), bar.getSemanticVersion());
148 QName root = QName.create("foo", "2016-01-01", "foo-root");
149 QName container20160404 = QName.create("foo", "2016-01-01", "con20160404");
150 SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
151 SchemaPath.create(true, root, container20160404));
152 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
154 QName container20160405 = QName.create("foo", "2016-01-01", "con20160405");
155 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
156 SchemaPath.create(true, root, container20160405));
157 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
159 QName container20160406 = QName.create("foo", "2016-01-01", "con20160406");
160 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
161 SchemaPath.create(true, root, container20160406));
162 assertTrue(findDataSchemaNode instanceof ContainerSchemaNode);
164 QName container20170406 = QName.create("foo", "2016-01-01", "con20170406");
165 findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context,
166 SchemaPath.create(true, root, container20170406));
167 assertNull(findDataSchemaNode);