Add XMLNamespace
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / yin / YinOpenconfigVersionTest.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
9 package org.opendaylight.yangtools.yang.stmt.openconfigver.yin;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.io.IOException;
17 import java.net.URISyntaxException;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.concepts.SemVer;
20 import org.opendaylight.yangtools.yang.common.XMLNamespace;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
26 import org.xml.sax.SAXException;
27
28 public class YinOpenconfigVersionTest {
29     @Test
30     public void basicTest() throws URISyntaxException, SAXException, IOException, ReactorException {
31         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic",
32                 StatementParserMode.SEMVER_MODE);
33         assertNotNull(context);
34
35         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
36         Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
37         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
38             .iterator().next();
39
40         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
41         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
42         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
43     }
44
45     @Test
46     public void basicImportTest1() throws URISyntaxException, SAXException, IOException, ReactorException {
47         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import",
48                 StatementParserMode.SEMVER_MODE);
49         assertNotNull(context);
50
51         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
52         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
53             .iterator().next();
54
55         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
56         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
57         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
58         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
59     }
60
61     @Test
62     public void basicImportErrTest1() throws URISyntaxException, SAXException, IOException {
63         try {
64             StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import-invalid",
65                 StatementParserMode.SEMVER_MODE);
66             fail("Test should fail due to invalid openconfig version");
67         } catch (ReactorException e) {
68             assertTrue(e.getCause().getCause().getMessage()
69                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
70         }
71     }
72 }