12924ef2dbaf695d363cc8626551db50ba8fe5b3
[controller.git] / opendaylight / sal / yang-prototype / code-generator / yang-model-parser-impl / src / test / java / org / opendaylight / controller / yang / model / parser / impl / TypesResolutionTest.java
1 /*
2  * Copyright (c) 2013 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.controller.yang.model.parser.impl;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.File;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.model.api.type.EnumTypeDefinition.EnumPair;
19 import org.opendaylight.controller.model.util.EnumerationType;
20 import org.opendaylight.controller.yang.model.api.Module;
21 import org.opendaylight.controller.yang.model.api.TypeDefinition;
22 import org.opendaylight.controller.yang.model.parser.api.YangModelParser;
23
24 public class TypesResolutionTest {
25
26     private YangModelParser parser;
27     private String[] testFiles;
28     private Set<Module> modules;
29
30     @Before
31     public void init() {
32         parser = new YangModelParserImpl();
33         testFiles = new String[5];
34
35         File testDir = new File("src/test/resources/types");
36         String[] fileList = testDir.list();
37         int i = 0;
38         for(String fileName : fileList) {
39             File file = new File(testDir, fileName);
40             testFiles[i] = file.getAbsolutePath();
41             i++;
42         }
43
44         modules = parser.parseYangModels(testFiles);
45         assertEquals(5, modules.size());
46     }
47
48     @Test
49     public void testIetfInetTypes() {
50         Module tested = findModule(modules, "ietf-inet-types");
51         Set<TypeDefinition<?>> typedefs = tested.getTypeDefinitions();
52         assertEquals(14, typedefs.size());
53
54         TypeDefinition<?> t1 = findTypedef(typedefs, "ip-version");
55         EnumerationType en = (EnumerationType)t1.getBaseType();
56         List<EnumPair> values = en.getValues();
57
58         EnumPair value0 = values.get(0);
59         assertEquals("unknown", value0.getName());
60         assertEquals(0, (int)value0.getValue());
61
62         EnumPair value1 = values.get(1);
63         assertEquals("ipv4", value1.getName());
64         assertEquals(1, (int)value1.getValue());
65
66         EnumPair value2 = values.get(2);
67         assertEquals("ipv6", value2.getName());
68         assertEquals(2, (int)value2.getValue());
69     }
70
71     private Module findModule(Set<Module> modules, String name) {
72         for(Module module : modules) {
73             if(module.getName().equals(name)) {
74                 return module;
75             }
76         }
77         return null;
78     }
79
80     private TypeDefinition<?> findTypedef(Set<TypeDefinition<?>> typedefs, String name) {
81         for(TypeDefinition<?> td : typedefs) {
82             if(td.getQName().getLocalName().equals(name)) {
83                 return td;
84             }
85         }
86         return null;
87     }
88
89 }