4e40aa701d6ebcc91d7ed93547cf7d22f71971b2
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / OpenconfigVersionBorderCaseTest.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 OpenconfigVersionBorderCaseTest {
27     private static final YangParserConfiguration SEMVER = YangParserConfiguration.builder()
28         .importResolutionMode(ImportResolutionMode.OPENCONFIG_SEMVER)
29         .build();
30
31     @Test
32     public void borderCaseValidMajorTest() throws Exception {
33         SchemaContext context = StmtTestUtils.parseYangSources(
34             "/openconfig-version/border-case/border-case-valid-major", SEMVER);
35         assertNotNull(context);
36
37         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
38         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
39             .iterator().next();
40
41         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
42         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
43         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
44         assertEquals(SemVer.valueOf("5.5.5"), bar.getSemanticVersion().get());
45     }
46
47     @Test
48     public void borderCaseValidMinorTest() throws Exception {
49         SchemaContext context = StmtTestUtils.parseYangSources(
50             "/openconfig-version/border-case/border-case-valid-minor", SEMVER);
51         assertNotNull(context);
52
53         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
54         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
55             .iterator().next();
56
57         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
58         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
59         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
60         assertEquals(SemVer.valueOf("5.6.5"), bar.getSemanticVersion().get());
61     }
62
63     @Test
64     public void borderCaseValidPatchTest() throws Exception {
65         SchemaContext context = StmtTestUtils.parseYangSources(
66             "/openconfig-version/border-case/border-case-valid-patch", SEMVER);
67         assertNotNull(context);
68
69         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
70         Module semVer = context.findModules(XMLNamespace.of("http://openconfig.net/yang/openconfig-ext"))
71             .iterator().next();
72
73         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion().get());
74         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion().get());
75         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
76         assertEquals(SemVer.valueOf("5.5.6"), bar.getSemanticVersion().get());
77     }
78
79     @Test
80     public void borderCaseInvalidMajorTest() throws Exception {
81         final ReactorException ex = assertThrows(ReactorException.class,
82             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-major", SEMVER));
83         assertThat(ex.getCause().getCause().getMessage(),
84             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
85     }
86
87     @Test
88     public void borderCaseInvalidMinorTest() {
89         final ReactorException ex = assertThrows(ReactorException.class,
90             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-minor", SEMVER));
91         assertThat(ex.getCause().getCause().getMessage(),
92             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
93     }
94
95     @Test
96     public void borderCaseInvalidPatchTest() throws Exception {
97         final ReactorException ex = assertThrows(ReactorException.class,
98             () -> StmtTestUtils.parseYangSources("/openconfig-version/border-case/border-case-invalid-patch", SEMVER));
99         assertThat(ex.getCause().getCause().getMessage(),
100             startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
101     }
102 }