8e9d2cf147bdb7112f8662d935be53d4a7692955
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / Bug5712Test.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
9 package org.opendaylight.yangtools.yang.stmt.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.io.File;
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Set;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31
32 public class Bug5712Test {
33
34     @Test
35     public void testTypedefWithNewStatementParser() throws ReactorException, SourceException, FileNotFoundException,
36             URISyntaxException {
37         SchemaContext schemaContext = StmtTestUtils.parseYangSources("/bugs/bug5712");
38         assertNotNull(schemaContext);
39
40         Module badModule = schemaContext.findModuleByName("bad", null);
41         assertNotNull(badModule);
42
43         checkThing2TypeDef(badModule);
44     }
45
46     private void checkThing2TypeDef(Module badModule) {
47         TypeDefinition<?> thing2 = null;
48         for (TypeDefinition<?> typeDef : badModule.getTypeDefinitions()) {
49             if (typeDef.getQName().getLocalName().equals("thing2")) {
50                 thing2 = typeDef;
51                 break;
52             }
53         }
54
55         assertNotNull(thing2);
56         TypeDefinition<?> baseType = thing2.getBaseType();
57         assertEquals(QName.create("urn:opendaylight:bad", "2016-04-11", "thing"), baseType.getQName());
58     }
59
60     @Test
61     public void testTypedefWithOldParser() throws URISyntaxException, IOException {
62         Set<Module> modules = loadModules(getClass().getResource("/bugs/bug5712").toURI());
63         assertNotNull(modules);
64         assertEquals(1, modules.size());
65
66         checkThing2TypeDef(modules.iterator().next());
67     }
68
69     public static Set<Module> loadModules(final URI resourceDirectory) throws IOException {
70         return loadSchemaContext(resourceDirectory).getModules();
71     }
72
73     public static SchemaContext loadSchemaContext(final URI resourceDirectory) throws IOException {
74         final YangContextParser parser = new YangParserImpl();
75         final File testDir = new File(resourceDirectory);
76         final String[] fileList = testDir.list();
77         final List<File> testFiles = new ArrayList<>();
78         if (fileList == null) {
79             throw new FileNotFoundException(resourceDirectory.toString());
80         }
81         for (String fileName : fileList) {
82             testFiles.add(new File(testDir, fileName));
83         }
84         return parser.parseFiles(testFiles);
85     }
86
87 }