Fixed resolution problems in Code Generator
[yangtools.git] / code-generator / binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / binding / generator / impl / ChoiceCaseGenTypesTest.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.yangtools.sal.binding.generator.impl;
9
10 import static org.junit.Assert.*;
11 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.*;
12
13 import java.io.File;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
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.parser.api.YangModelParser;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28
29 public class ChoiceCaseGenTypesTest {
30
31     private final static List<File> yangModels = new ArrayList<>();
32     private final static String yangModelsFolder = AugmentedTypeTest.class.getResource("/choice-case-type-test-models")
33             .getPath();
34
35     @BeforeClass
36     public static void loadTestResources() {
37         final File augFolder = new File(yangModelsFolder);
38         for (final File fileEntry : augFolder.listFiles()) {
39             if (fileEntry.isFile()) {
40                 yangModels.add(fileEntry);
41             }
42         }
43     }
44
45     private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName,
46             int occurences) {
47         GeneratedType searchedGenType = null;
48         int searchedGenTypeCounter = 0;
49         for (Type type : genTypes) {
50             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
51                 GeneratedType genType = (GeneratedType) type;
52                 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
53                     searchedGenType = genType;
54                     searchedGenTypeCounter++;
55                 }
56             }
57         }
58         assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
59         assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
60                 searchedGenTypeCounter);
61         return searchedGenType;
62
63     }
64
65     private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName) {
66         return checkGeneratedType(genTypes, genTypeName, packageName, 1);
67     }
68
69     @Test
70     public void choiceCaseResolvingTypeTest() {
71         final YangModelParser parser = new YangParserImpl();
72         final Set<Module> modules = parser.parseYangModels(yangModels);
73         final SchemaContext context = parser.resolveSchemaContext(modules);
74
75         assertNotNull("context is null", context);
76         final BindingGenerator bindingGen = new BindingGeneratorImpl();
77         final List<Type> genTypes = bindingGen.generateTypes(context);
78
79         assertNotNull("genTypes is null", genTypes);
80         assertFalse("genTypes is empty", genTypes.isEmpty());
81
82         // test for file choice-monitoring
83         String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
84         GeneratedType genType = null;
85
86         checkGeneratedType(genTypes, "LockType", pcgPref); // choice
87
88         genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
89         containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
90         containsInterface("LockType", genType);
91
92         genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
93         containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
94         containsInterface("LockType", genType);
95
96         genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
97         containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
98         containsInterface("LockType", genType);
99
100         genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
101
102         genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
103         containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
104         containsInterface("AlgorithmAndHash", genType);
105
106         genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
107         containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
108         containsInterface("AlgorithmAndHash", genType);
109
110         genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
111         containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
112         containsInterface("AlgorithmAndHash", genType);
113
114         genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
115         containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
116         containsInterface("AlgorithmAndHash", genType);
117
118         genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
119         containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
120         containsInterface("AlgorithmAndHash", genType);
121
122         genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
123         containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
124         containsInterface("AlgorithmAndHash", genType);
125
126         // test for file augment-monitoring
127         // augment
128         // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
129         pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
130         genType = null;
131
132         genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
133                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
134         containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
135         containsInterface("LockType", genType);
136
137         genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
138                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
139         containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
140         containsInterface("LockType", genType);
141
142         genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
143                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
144         containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
145         containsInterface("LockType", genType);
146
147         // augment
148         // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
149         // {
150         genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
151         containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
152         containsInterface("Augmentation<PartialLock>", genType);
153
154         genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
155                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
156
157         genType = checkGeneratedType(genTypes, "Foo", pcgPref
158                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
159         containsMethods(genType, new NameTypePattern("getFoo", "String"));
160         containsInterface("AugCaseByChoice", genType);
161
162         genType = checkGeneratedType(genTypes, "Bar", pcgPref
163                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
164         containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
165         containsInterface("AugCaseByChoice", genType);
166
167         // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
168         genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
169         containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
170         containsInterface("Augmentation<Datastore>", genType);
171
172         genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
173
174         genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
175                 + ".netconf.state.datastores.datastore.storage.format"); // case
176         containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
177         containsInterface("StorageFormat", genType);
178
179         genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
180         containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
181         containsInterface("StorageFormat", genType);
182
183         genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
184         containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
185         containsInterface("StorageFormat", genType);
186
187     }
188 }