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