2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.sal.binding.generator.impl;
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;
17 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.List;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
26 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
27 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
28 import org.opendaylight.yangtools.sal.binding.model.api.Type;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
32 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
34 public class ChoiceCaseGenTypesTest {
36 private final static List<File> yangModels = new ArrayList<>();
37 private final static URL yangModelsFolder = AugmentedTypeTest.class.getResource("/choice-case-type-test-models")
41 public static void loadTestResources() throws URISyntaxException {
42 final File augFolder = new File(yangModelsFolder.toURI());
43 for (final File fileEntry : augFolder.listFiles()) {
44 if (fileEntry.isFile()) {
45 yangModels.add(fileEntry);
50 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName,
52 GeneratedType searchedGenType = null;
53 int searchedGenTypeCounter = 0;
54 for (Type type : genTypes) {
55 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
56 GeneratedType genType = (GeneratedType) type;
57 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
58 searchedGenType = genType;
59 searchedGenTypeCounter++;
63 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
64 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
65 searchedGenTypeCounter);
66 return searchedGenType;
70 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName) {
71 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
75 public void choiceCaseResolvingTypeTest() {
76 final YangModelParser parser = new YangParserImpl();
77 final Set<Module> modules = parser.parseYangModels(yangModels);
79 final SchemaContext context = parser.resolveSchemaContext(modules);
81 assertNotNull("context is null", context);
82 final BindingGenerator bindingGen = new BindingGeneratorImpl();
83 final List<Type> genTypes = bindingGen.generateTypes(context);
85 assertNotNull("genTypes is null", genTypes);
86 assertFalse("genTypes is empty", genTypes.isEmpty());
88 // test for file choice-monitoring
89 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
90 GeneratedType genType = null;
92 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
94 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
95 containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
96 containsInterface("LockType", genType);
98 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
99 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
100 containsInterface("LockType", genType);
102 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
103 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
104 containsInterface("LockType", genType);
106 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
108 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
109 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
110 containsInterface("AlgorithmAndHash", genType);
112 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
113 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
114 containsInterface("AlgorithmAndHash", genType);
116 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
117 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
118 containsInterface("AlgorithmAndHash", genType);
120 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
121 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
122 containsInterface("AlgorithmAndHash", genType);
124 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
125 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
126 containsInterface("AlgorithmAndHash", genType);
128 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
129 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
130 containsInterface("AlgorithmAndHash", genType);
132 // test for file augment-monitoring
134 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
135 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
138 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
139 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
140 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
141 containsInterface("LockType", genType);
143 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
144 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
145 containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
146 containsInterface("LockType", genType);
148 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
149 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
151 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
152 containsInterface("LockType", genType);
155 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
157 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
158 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
159 containsInterface("Augmentation<PartialLock>", genType);
161 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
162 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
164 genType = checkGeneratedType(genTypes, "Foo", pcgPref
165 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
166 containsMethods(genType, new NameTypePattern("getFoo", "String"));
167 containsInterface("AugCaseByChoice", genType);
169 genType = checkGeneratedType(genTypes, "Bar", pcgPref
170 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
171 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
172 containsInterface("AugCaseByChoice", genType);
174 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
175 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
176 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
177 containsInterface("Augmentation<Datastore>", genType);
179 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
181 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
182 + ".netconf.state.datastores.datastore.storage.format"); // case
183 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
184 containsInterface("StorageFormat", genType);
186 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
187 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
188 containsInterface("StorageFormat", genType);
190 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
191 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
192 containsInterface("StorageFormat", genType);