Bug 6859 #2 Binding generator v1 refactoring
[mdsal.git] / binding / mdsal-binding-generator-impl / src / test / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.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.yangtools.sal.binding.generator.impl.SupportTestUtil.containsInterface;
14 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsMethods;
15
16 import java.io.IOException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.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 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
27
28 public class ChoiceCaseGenTypesTest extends AbstractTypesTest {
29
30     public ChoiceCaseGenTypesTest() {
31         super(ChoiceCaseGenTypesTest.class.getResource("/choice-case-type-test-models"));
32     }
33
34     private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
35             final String packageName, final int occurences) {
36         GeneratedType searchedGenType = null;
37         int searchedGenTypeCounter = 0;
38         for (Type type : genTypes) {
39             if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
40                 GeneratedType genType = (GeneratedType) type;
41                 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
42                     searchedGenType = genType;
43                     searchedGenTypeCounter++;
44                 }
45             }
46         }
47         assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
48         assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
49                 searchedGenTypeCounter);
50         return searchedGenType;
51
52     }
53
54     private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
55             final String packageName) {
56         return checkGeneratedType(genTypes, genTypeName, packageName, 1);
57     }
58
59     @Test
60     public void choiceCaseResolvingTypeTest() throws IOException, SourceException, ReactorException {
61         final SchemaContext context = YangParserTestUtils.parseYangSources(testModels);
62
63         assertNotNull("context is null", context);
64         final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
65         final List<Type> genTypes = bindingGen.generateTypes(context);
66
67         assertNotNull("genTypes is null", genTypes);
68         assertFalse("genTypes is empty", genTypes.isEmpty());
69
70         // test for file choice-monitoring
71         String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
72         GeneratedType genType = null;
73
74         checkGeneratedType(genTypes, "LockType", pcgPref); // choice
75
76         genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
77         SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
78         containsInterface("LockType", genType);
79
80         genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
81         containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
82         containsInterface("LockType", genType);
83
84         genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
85         containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
86         containsInterface("LockType", genType);
87
88         genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
89
90         genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
91         containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
92         containsInterface("AlgorithmAndHash", genType);
93
94         genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
95         containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
96         containsInterface("AlgorithmAndHash", genType);
97
98         genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
99         containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
100         containsInterface("AlgorithmAndHash", genType);
101
102         genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
103         containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
104         containsInterface("AlgorithmAndHash", genType);
105
106         genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
107         containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
108         containsInterface("AlgorithmAndHash", genType);
109
110         genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
111         containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
112         containsInterface("AlgorithmAndHash", genType);
113
114         // test for file augment-monitoring
115         // augment
116         // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
117         pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
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 }