Bug 6244: Add context to exceptions thrown by yang statement parser
[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.io.FileNotFoundException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import java.util.Set;
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;
28
29 public class SemanticVersionMultipleImportTest {
30
31     @Test
32     public void multipleInvalidDeprecatedTest() throws SourceException, FileNotFoundException, ReactorException,
33             URISyntaxException {
34         try {
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)]."));
41         }
42     }
43
44     @Test
45     public void multipleInvalidNosufficientTest() throws SourceException, FileNotFoundException, ReactorException,
46             URISyntaxException {
47         try {
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)]."));
54         }
55     }
56
57     @Test
58     public void multipleValidDefaultsTest() throws SourceException, FileNotFoundException, ReactorException,
59             URISyntaxException {
60         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-defaults",
61                 StatementParserMode.SEMVER_MODE);
62         assertNotNull(context);
63
64         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
65         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
66                 .iterator().next();
67
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());
72     }
73
74     @Test
75     public void multipleValidSpecifiedTest() throws SourceException, FileNotFoundException, ReactorException,
76             URISyntaxException {
77         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/multiple/multiple-valid-specified",
78                 StatementParserMode.SEMVER_MODE);
79         assertNotNull(context);
80
81         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
82         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
83                 .iterator().next();
84
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());
89     }
90
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;
97                 break;
98             }
99         }
100
101         Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
102                 requestedModuleImport.getRevision());
103         return importedModule;
104     }
105 }