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