02457ddf77914e4aad518b8586ac9c11cba26c3d
[yangtools.git] / yang / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / OpenconfigVersionImportTest.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.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 org.junit.Test;
17 import org.opendaylight.yangtools.concepts.SemVer;
18 import org.opendaylight.yangtools.yang.common.XMLNamespace;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.parser.api.ImportResolutionMode;
22 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
25
26 public class OpenconfigVersionImportTest {
27     private static final YangParserConfiguration SEMVER = YangParserConfiguration.builder()
28         .importResolutionMode(ImportResolutionMode.OPENCONFIG_SEMVER)
29         .build();
30
31     @Test
32     public void importValidTest() throws Exception {
33         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/import/import-valid", SEMVER);
34         assertNotNull(context);
35
36         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
37             .iterator().next();
38
39         assertEquals(SemVer.valueOf("1.0.0"), semVer.getSemanticVersion().get());
40     }
41
42     @Test
43     public void importInvalidDeprecatedTest1() {
44         ReactorException ex = assertThrows(ReactorException.class,
45             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-deprecated-1", SEMVER));
46         assertThat(ex.getCause().getCause().getMessage(), startsWith(
47             "Unable to find module compatible with requested import [openconfig-extensions(1.0.0)]."));
48     }
49
50     @Test
51     public void importInvalidDeprecatedTest2() {
52         ReactorException ex = assertThrows(ReactorException.class,
53             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-deprecated-2", SEMVER));
54         assertThat(ex.getCause().getCause().getMessage(), startsWith(
55             "Unable to find module compatible with requested import [openconfig-extensions(0.9.9)]."));
56     }
57
58     @Test
59     public void importInvalidNotsufficientTest1() {
60         ReactorException ex = assertThrows(ReactorException.class,
61             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-notsufficient-1", SEMVER));
62         assertThat(ex.getCause().getCause().getMessage(), startsWith(
63             "Unable to find module compatible with requested import [openconfig-extensions(2.0.0)]."));
64     }
65
66     @Test
67     public void importInvalidNotsufficientTest2() {
68         ReactorException ex = assertThrows(ReactorException.class,
69             () -> StmtTestUtils.parseYangSources("/openconfig-version/import/import-invalid-notsufficient-2", SEMVER));
70         assertThat(ex.getCause().getCause().getMessage(), startsWith(
71             "Unable to find module compatible with requested import [openconfig-extensions(2.0.5)]."));
72     }
73 }