Populate parser/ hierarchy
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / openconfigver / OpenconfigVersionDefaultsTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import java.util.Optional;
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 OpenconfigVersionDefaultsTest {
27     private static final YangParserConfiguration SEMVER = YangParserConfiguration.builder()
28         .importResolutionMode(ImportResolutionMode.OPENCONFIG_SEMVER)
29         .build();
30
31     @Test
32     public void defaultsTest() throws Exception {
33         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/defaults", SEMVER);
34         assertNotNull(context);
35
36         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
37         Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
38
39         assertEquals(Optional.empty(), foo.getSemanticVersion());
40         assertEquals(Optional.empty(), bar.getSemanticVersion());
41     }
42
43     @Test
44     public void defaultMajorValidTest() throws Exception {
45         SchemaContext context = StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-valid",
46             SEMVER);
47         assertNotNull(context);
48
49         Module foo = context.findModules(XMLNamespace.of("foo")).iterator().next();
50         Module bar = context.findModules(XMLNamespace.of("bar")).iterator().next();
51
52         assertEquals(Optional.empty(), foo.getSemanticVersion());
53         assertEquals(SemVer.valueOf("0.99.99"), bar.getSemanticVersion().get());
54     }
55
56     @Test
57     public void defaultMajorInvalidTest() throws Exception {
58         try {
59             StmtTestUtils.parseYangSources("/openconfig-version/defaults/default-major-invalid", SEMVER);
60             fail("Test should fail due to invalid openconfig version");
61         } catch (ReactorException e) {
62             assertTrue(e.getCause().getCause().getMessage()
63                     .startsWith("Unable to find module compatible with requested import [bar(0.0.1)]."));
64         }
65     }
66 }