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.openconfigver;
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 org.junit.Test;
16 import org.opendaylight.yangtools.concepts.SemVer;
17 import org.opendaylight.yangtools.yang.common.XMLNamespace;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
25 public class OpenconfigVersionMultipleImportTest {
28 public void multipleInvalidDeprecatedTest() throws Exception {
30 StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-invalid-deprecated",
31 StatementParserMode.SEMVER_MODE);
32 fail("Test should fail due to invalid openconfig version");
33 } catch (ReactorException e) {
34 assertTrue(e.getCause().getCause().getMessage()
35 .startsWith("Unable to find module compatible with requested import [bar(1.0.0)]."));
40 public void multipleInvalidNosufficientTest() throws Exception {
42 StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-invalid-nosufficient",
43 StatementParserMode.SEMVER_MODE);
44 fail("Test should fail due to invalid openconfig version");
45 } catch (ReactorException e) {
46 assertTrue(e.getCause().getCause().getMessage()
47 .startsWith("Unable to find module compatible with requested import [bar(2.5.5)]."));
52 public void multipleValidDefaultsTest() throws Exception {
53 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-valid-defaults",
54 StatementParserMode.SEMVER_MODE);
55 assertNotNull(context);
57 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
58 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
61 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
62 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
63 Module bar = findImportedModule(context, foo, "bar");
64 assertEquals(SemVer.valueOf("0.9.5"), bar.getSemanticVersion().get());
68 public void multipleValidSpecifiedTest() throws Exception {
69 SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-valid-specified",
70 StatementParserMode.SEMVER_MODE);
71 assertNotNull(context);
73 Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
74 Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
77 assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
78 assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
79 Module bar = findImportedModule(context, foo, "bar");
80 assertEquals(SemVer.valueOf("5.5.6"), bar.getSemanticVersion().get());
83 private static Module findImportedModule(final SchemaContext context, final Module rootModule,
84 final String importedModuleName) {
85 ModuleImport requestedModuleImport = null;
86 for (ModuleImport moduleImport : rootModule.getImports()) {
87 if (moduleImport.getModuleName().equals(importedModuleName)) {
88 requestedModuleImport = moduleImport;
93 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision()).get();