Bug 6244: Add context to exceptions thrown by yang statement parser
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / semver / SemanticVersionDefaultsTest.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.semver;
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.io.FileNotFoundException;
16 import java.net.URI;
17 import java.net.URISyntaxException;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.concepts.SemVer;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.stmt.StmtTestUtils;
26
27 public class SemanticVersionDefaultsTest {
28
29     @Test
30     public void defaultsTest() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
31         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/defaults/defaults",
32                 StatementParserMode.SEMVER_MODE);
33         assertNotNull(context);
34
35         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
36         Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
37
38         assertEquals(SemVer.valueOf("0.0.0"), foo.getSemanticVersion());
39         assertEquals(SemVer.valueOf("0.0.0"), bar.getSemanticVersion());
40     }
41
42     @Test
43     public void defaultMajorValidTest() throws SourceException, FileNotFoundException, ReactorException,
44             URISyntaxException {
45         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/defaults/default-major-valid",
46                 StatementParserMode.SEMVER_MODE);
47         assertNotNull(context);
48
49         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
50         Module bar = context.findModuleByNamespace(new URI("bar")).iterator().next();
51
52         assertEquals(SemVer.valueOf("0.0.0"), foo.getSemanticVersion());
53         assertEquals(SemVer.valueOf("0.99.99"), bar.getSemanticVersion());
54     }
55
56     @Test
57     public void defaultMajorInvalidTest() throws SourceException, FileNotFoundException, ReactorException,
58             URISyntaxException {
59         try {
60             StmtTestUtils.parseYangSources("/semantic-version/defaults/default-major-invalid", StatementParserMode.SEMVER_MODE);
61             fail("Test should fail due to invalid semantic version");
62         } catch (ReactorException e) {
63             assertTrue(e.getCause().getMessage()
64                     .startsWith("Unable to find module compatible with requested import [bar(0.0.0)]."));
65         }
66     }
67 }