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;
15 import java.io.FileNotFoundException;
17 import java.net.URISyntaxException;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.concepts.SemVer;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
29 public class SemanticVersionMultipleImportTest {
32 public void multipleInvalidDeprecatedTest() throws SourceException, FileNotFoundException, ReactorException,
35 StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-invalid-deprecated",
36 StatementParserMode.SEMVER_MODE);
37 fail("Test should fail due to invalid semantic version");
38 } catch (ReactorException e) {
39 assertTrue(e.getCause().getMessage()
40 .startsWith("Unable to find module compatible with requested import [bar(1.0.0)]."));
45 public void multipleInvalidNosufficientTest() throws SourceException, FileNotFoundException, ReactorException,
48 StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-invalid-nosufficient",
49 StatementParserMode.SEMVER_MODE);
50 fail("Test should fail due to invalid semantic version");
51 } catch (ReactorException e) {
52 assertTrue(e.getCause().getMessage()
53 .startsWith("Unable to find module compatible with requested import [bar(2.5.5)]."));
58 public void multipleValidDefaultsTest() throws SourceException, FileNotFoundException, ReactorException,
60 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-defaults",
61 StatementParserMode.SEMVER_MODE);
62 assertNotNull(context);
64 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
65 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
68 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
69 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
70 Module bar = findImportedModule(context, foo, "bar");
71 assertEquals(SemVer.valueOf("0.9.5"), bar.getSemanticVersion());
75 public void multipleValidSpecifiedTest() throws SourceException, FileNotFoundException, ReactorException,
77 SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-specified",
78 StatementParserMode.SEMVER_MODE);
79 assertNotNull(context);
81 Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
82 Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
85 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
86 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
87 Module bar = findImportedModule(context, foo, "bar");
88 assertEquals(SemVer.valueOf("5.5.6"), bar.getSemanticVersion());
91 private static Module findImportedModule(SchemaContext context, Module rootModule, String importedModuleName) {
92 ModuleImport requestedModuleImport = null;
93 Set<ModuleImport> rootImports = rootModule.getImports();
94 for (ModuleImport moduleImport : rootImports) {
95 if (moduleImport.getModuleName().equals(importedModuleName)) {
96 requestedModuleImport = moduleImport;
101 Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
102 requestedModuleImport.getRevision());
103 return importedModule;