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