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 / SemanticVersionBorderCaseTest.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 SemanticVersionBorderCaseTest {
28
29     @Test
30     public void borderCaseValidMajorTest() throws SourceException, FileNotFoundException, ReactorException,
31             URISyntaxException {
32         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-valid-major",
33                 StatementParserMode.SEMVER_MODE);
34         assertNotNull(context);
35
36         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
37         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
38                 .iterator().next();
39
40         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
41         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
42         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
43         assertEquals(SemVer.valueOf("5.5.5"), bar.getSemanticVersion());
44     }
45
46     @Test
47     public void borderCaseValidMinorTest() throws SourceException, FileNotFoundException, ReactorException,
48             URISyntaxException {
49         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-valid-minor",
50                 StatementParserMode.SEMVER_MODE);
51         assertNotNull(context);
52
53         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
54         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
55                 .iterator().next();
56
57         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
58         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
59         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
60         assertEquals(SemVer.valueOf("5.6.5"), bar.getSemanticVersion());
61     }
62
63     @Test
64     public void borderCaseValidPatchTest() throws SourceException, FileNotFoundException, ReactorException,
65             URISyntaxException {
66         SchemaContext context = StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-valid-patch",
67                 StatementParserMode.SEMVER_MODE);
68         assertNotNull(context);
69
70         Module foo = context.findModuleByNamespace(new URI("foo")).iterator().next();
71         Module semVer = context.findModuleByNamespace(new URI("urn:opendaylight:yang:extension:semantic-version"))
72                 .iterator().next();
73
74         assertEquals(SemVer.valueOf("0.0.1"), semVer.getSemanticVersion());
75         assertEquals(SemVer.valueOf("0.1.1"), foo.getSemanticVersion());
76         Module bar = StmtTestUtils.findImportedModule(context, foo, "bar");
77         assertEquals(SemVer.valueOf("5.5.6"), bar.getSemanticVersion());
78     }
79
80     @Test
81     public void borderCaseInvalidMajorTest() throws SourceException, FileNotFoundException, ReactorException,
82             URISyntaxException {
83         try {
84             StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-invalid-major",
85                     StatementParserMode.SEMVER_MODE);
86             fail("Test should fail due to invalid semantic version");
87         } catch (ReactorException e) {
88             assertTrue(e.getCause().getMessage()
89                     .startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
90         }
91     }
92
93     @Test
94     public void borderCaseInvalidMinorTest() throws SourceException, FileNotFoundException, ReactorException,
95             URISyntaxException {
96         try {
97             StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-invalid-minor",
98                     StatementParserMode.SEMVER_MODE);
99             fail("Test should fail due to invalid semantic version");
100         } catch (ReactorException e) {
101             assertTrue(e.getCause().getMessage()
102                     .startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
103         }
104     }
105
106     @Test
107     public void borderCaseInvalidPatchTest() throws SourceException, FileNotFoundException, ReactorException,
108             URISyntaxException {
109         try {
110             StmtTestUtils.parseYangSources("/semantic-version/border-case/border-case-invalid-patch",
111                     StatementParserMode.SEMVER_MODE);
112             fail("Test should fail due to invalid semantic version");
113         } catch (ReactorException e) {
114             assertTrue(e.getCause().getMessage()
115                     .startsWith("Unable to find module compatible with requested import [bar(5.5.5)]."));
116         }
117     }
118 }