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