Fix checkstyle in mdsal-binding-generator-impl
[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();
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."
69                 + "netconf.state.datastores.datastore.locks";
70         GeneratedType genType = null;
71
72         checkGeneratedType(genTypes, "LockType", pcgPref); // choice
73
74         genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
75         SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
76         containsInterface("LockType", genType);
77
78         genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
79         containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
80         containsInterface("LockType", genType);
81
82         genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
83         containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
84         containsInterface("LockType", genType);
85
86         genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
87
88         genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
89         containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
90         containsInterface("AlgorithmAndHash", genType);
91
92         genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
93         containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
94         containsInterface("AlgorithmAndHash", genType);
95
96         genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
97         containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
98         containsInterface("AlgorithmAndHash", genType);
99
100         genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
101         containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
102         containsInterface("AlgorithmAndHash", genType);
103
104         genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
105         containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
106         containsInterface("AlgorithmAndHash", genType);
107
108         genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
109         containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
110         containsInterface("AlgorithmAndHash", genType);
111
112         // test for file augment-monitoring
113         // augment
114         // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
115         pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
116
117         genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
118                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
119         containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
120         containsInterface("LockType", genType);
121
122         genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
123                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
124         containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
125         containsInterface("LockType", genType);
126
127         genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
128                 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
129         containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
130         containsInterface("LockType", genType);
131
132         // augment
133         // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
134         // {
135         genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
136         containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
137         containsInterface("Augmentation<PartialLock>", genType);
138
139         genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
140                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
141
142         genType = checkGeneratedType(genTypes, "Foo", pcgPref
143                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
144         containsMethods(genType, new NameTypePattern("getFoo", "String"));
145         containsInterface("AugCaseByChoice", genType);
146
147         genType = checkGeneratedType(genTypes, "Bar", pcgPref
148                 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
149         containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
150         containsInterface("AugCaseByChoice", genType);
151
152         // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
153         genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
154         containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
155         containsInterface("Augmentation<Datastore>", genType);
156
157         // choice
158         genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore");
159
160         genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
161                 + ".netconf.state.datastores.datastore.storage.format"); // case
162         containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
163         containsInterface("StorageFormat", genType);
164
165         // case
166         genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format");
167         containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
168         containsInterface("StorageFormat", genType);
169
170         // case
171         genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format");
172         containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
173         containsInterface("StorageFormat", genType);
174     }
175 }