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