Populate xpath/ hierarchy
[yangtools.git] / parser / 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 package org.opendaylight.yangtools.yang.stmt.openconfigver.yin;
9
10 import static org.hamcrest.CoreMatchers.startsWith;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
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.parser.api.ImportResolutionMode;
24 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27 import org.xml.sax.SAXException;
28
29 public class YinOpenconfigVersionTest {
30     private static final YangParserConfiguration SEMVER = YangParserConfiguration.builder()
31         .importResolutionMode(ImportResolutionMode.OPENCONFIG_SEMVER)
32         .build();
33
34     @Test
35     public void basicTest() throws URISyntaxException, SAXException, IOException, ReactorException {
36         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic", SEMVER);
37         assertNotNull(context);
38
39         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
40         Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
41         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
42             .iterator().next();
43
44         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
45         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
46         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
47     }
48
49     @Test
50     public void basicImportTest1() throws URISyntaxException, SAXException, IOException, ReactorException {
51         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import", SEMVER);
52         assertNotNull(context);
53
54         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
55         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
56             .iterator().next();
57
58         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
59         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
60         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
61         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
62     }
63
64     @Test
65     public void basicImportErrTest1() throws URISyntaxException, SAXException, IOException {
66         ReactorException ex = assertThrows(ReactorException.class,
67             () -> StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import-invalid", SEMVER));
68         assertThat(ex.getCause().getCause().getMessage(),
69             startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
70     }
71 }