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