902da6a3568493dbe70e48c154bd19d0b36f796a
[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.FileNotFoundException;
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.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
27
28 public class YinOpenconfigVersionTest {
29
30     @Test
31     public void basicTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
32         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic",
33                 StatementParserMode.SEMVER_MODE);
34         assertNotNull(context);
35
36         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
37         Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
38         Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
39                 .iterator().next();
40
41         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
42         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
43         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
44     }
45
46     @Test
47     public void basicImportTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
48         SchemaContext context = StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import",
49                 StatementParserMode.SEMVER_MODE);
50         assertNotNull(context);
51
52         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
53         Module semVer = context.findModuleByNamespace(new URI("http://openconfig.net/yang/openconfig-ext"))
54                 .iterator().next();
55
56         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
57         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
58         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
59         assertEquals(SemVer.valueOf("0.1.2"), bar.getSemanticVersion());
60     }
61
62     @Test
63     public void basicImportErrTest1() throws SourceException, FileNotFoundException, ReactorException,
64             URISyntaxException {
65         try {
66             StmtTestUtils.parseYinSources("/openconfig-version/yin-input/basic-import-invalid", StatementParserMode.SEMVER_MODE);
67             fail("Test should fail due to invalid openconfig version");
68         } catch (ReactorException e) {
69             assertTrue(e.getCause().getMessage()
70                     .startsWith("Unable to find module compatible with requested import [bar(0.1.2)]."));
71         }
72     }
73 }