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