c8e4673ebb767a0e0a69ded72ba412d9556b7a68
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / semver / SemanticVersionMultipleImportTest.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 package org.opendaylight.yangtools.yang.stmt.semver;
9
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;
14
15 import java.net.URI;
16 import java.util.Set;
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;
25
26 public class SemanticVersionMultipleImportTest {
27
28     @Test
29     public void multipleInvalidDeprecatedTest() throws Exception {
30         try {
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)]."));
37         }
38     }
39
40     @Test
41     public void multipleInvalidNosufficientTest() throws Exception {
42         try {
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)]."));
49         }
50     }
51
52     @Test
53     public void multipleValidDefaultsTest() throws Exception {
54         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-defaults",
55                 StatementParserMode.SEMVER_MODE);
56         assertNotNull(context);
57
58         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
59         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
60                 .iterator().next();
61
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());
66     }
67
68     @Test
69     public void multipleValidSpecifiedTest() throws Exception {
70         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-specified",
71                 StatementParserMode.SEMVER_MODE);
72         assertNotNull(context);
73
74         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
75         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
76                 .iterator().next();
77
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());
82     }
83
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;
91                 break;
92             }
93         }
94
95         Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
96                 requestedModuleImport.getRevision());
97         return importedModule;
98     }
99 }