Add XMLNamespace
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / OpenconfigVersionMultipleImportTest.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.openconfigver;
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 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;
24
25 public class OpenconfigVersionMultipleImportTest {
26
27     @Test
28     public void multipleInvalidDeprecatedTest() throws Exception {
29         try {
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)]."));
36         }
37     }
38
39     @Test
40     public void multipleInvalidNosufficientTest() throws Exception {
41         try {
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)]."));
48         }
49     }
50
51     @Test
52     public void multipleValidDefaultsTest() throws Exception {
53         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-valid-defaults",
54                 StatementParserMode.SEMVER_MODE);
55         assertNotNull(context);
56
57         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
58         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
59             .iterator().next();
60
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());
65     }
66
67     @Test
68     public void multipleValidSpecifiedTest() throws Exception {
69         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/multiple/multiple-valid-specified",
70                 StatementParserMode.SEMVER_MODE);
71         assertNotNull(context);
72
73         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
74         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
75             .iterator().next();
76
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());
81     }
82
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;
89                 break;
90             }
91         }
92
93         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision()).get();
94     }
95 }