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.assertTrue;
13 import static org.junit.Assert.fail;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.concepts.SemVer;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
26 public class SemanticVersionMultipleImportTest {
29 public void multipleInvalidDeprecatedTest() throws Exception {
31 StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-invalid-deprecated",
32 StatementParserMode.SEMVER_MODE);
33 fail("Test should fail due to invalid semantic version");
34 } catch (ReactorException e) {
35 assertTrue(e.getCause().getMessage()
36 .startsWith("Unable to find module compatible with requested import [bar(1.0.0)]."));
41 public void multipleInvalidNosufficientTest() throws Exception {
43 StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-invalid-nosufficient",
44 StatementParserMode.SEMVER_MODE);
45 fail("Test should fail due to invalid semantic version");
46 } catch (ReactorException e) {
47 assertTrue(e.getCause().getMessage()
48 .startsWith("Unable to find module compatible with requested import [bar(2.5.5)]."));
53 public void multipleValidDefaultsTest() throws Exception {
54 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-defaults",
55 StatementParserMode.SEMVER_MODE);
56 assertNotNull(context);
58 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
59 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
62 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
63 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
64 Module bar = findImportedModule(context, foo, "bar");
65 assertEquals(SemVer.valueOf("0.9.5"), bar.getSemanticVersion());
69 public void multipleValidSpecifiedTest() throws Exception {
70 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-specified",
71 StatementParserMode.SEMVER_MODE);
72 assertNotNull(context);
74 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
75 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
78 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
79 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
80 Module bar = findImportedModule(context, foo, "bar");
81 assertEquals(SemVer.valueOf("5.5.6"), bar.getSemanticVersion());
84 private static Module findImportedModule(final SchemaContext context, final Module rootModule,
85 final String importedModuleName) {
86 ModuleImport requestedModuleImport = null;
87 Set<ModuleImport> rootImports = rootModule.getImports();
88 for (ModuleImport moduleImport : rootImports) {
89 if (moduleImport.getModuleName().equals(importedModuleName)) {
90 requestedModuleImport = moduleImport;
95 Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
96 requestedModuleImport.getRevision());
97 return importedModule;