dc0d1f5d5adc8f03f9edac271501bba444aa5972
[yangtools.git] / yang / yang-parser-impl / 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.URI;
18 import java.net.URISyntaxException;
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.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
30     @Test
31     public void basicTest() throws URISyntaxException, SAXException, IOException, ReactorException {
32         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic",
33                 StatementParserMode.SEMVER_MODE);
34         assertNotNull(context);
35
36         Module foo = context.findModules(URI.create("foo")).iterator().next();
37         Module bar = context.findModules(URI.create("bar")).iterator().next();
38         Module semVer = context.findModules(URI.create("http://openconfig.net/yang/openconfig-ext")).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(URI.create("foo")).iterator().next();
52         Module semVer = context.findModules(URI.create("http://openconfig.net/yang/openconfig-ext")).iterator().next();
53
54         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
55         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
56         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
57         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion().get());
58     }
59
60     @Test
61     public void basicImportErrTest1() throws URISyntaxException, SAXException, IOException {
62         try {
63             StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import-invalid",
64                 StatementParserMode.SEMVER_MODE);
65             fail("Test should fail due to invalid openconfig version");
66         } catch (ReactorException e) {
67             assertTrue(e.getCause().getCause().getMessage()
68                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
69         }
70     }
71 }