Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / ModuleSourceTest.java
1 package org.opendaylight.yangtools.yang.stmt.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import java.io.IOException;
7
8 import java.io.FileReader;
9 import java.io.BufferedReader;
10 import java.io.File;
11 import java.util.Set;
12 import org.opendaylight.yangtools.yang.model.api.Module;
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
14 import java.net.URISyntaxException;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
16 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
17 import org.junit.Test;
18
19 public class ModuleSourceTest {
20
21     @Test
22     public void test() throws SourceException, ReactorException, URISyntaxException, IOException {
23         SchemaContext schema = StmtTestUtils.parseYangSources("/module-source");
24
25         assertNotNull(schema);
26
27         Set<Module> modules = schema.getModules();
28         assertNotNull(modules);
29         assertEquals(1,modules.size());
30
31         Module simpleModule = modules.iterator().next();
32         String source = simpleModule.getSource();
33         String moduleSourcePath = simpleModule.getModuleSourcePath();
34
35         File simpleYang = new File(getClass().getResource("/module-source/simple-module.yang").toURI());
36
37         assertEquals(simpleYang.getPath(), moduleSourcePath);
38         assertEquals(readFile(moduleSourcePath), source);
39     }
40
41     private String readFile(String fileName) throws IOException {
42         BufferedReader br = new BufferedReader(new FileReader(fileName));
43         try {
44             StringBuilder sb = new StringBuilder();
45             String line = br.readLine();
46
47             while (line != null) {
48                 sb.append(line);
49                 sb.append(System.lineSeparator());
50                 line = br.readLine();
51             }
52             return sb.toString();
53         } finally {
54             br.close();
55         }
56     }
57 }