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