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.*;
11 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.*;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
27 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
29 public class ChoiceCaseGenTypesTest {
31 private final static List<File> yangModels = new ArrayList<>();
32 private final static String yangModelsFolder = AugmentedTypeTest.class.getResource("/choice-case-type-test-models")
36 public static void loadTestResources() {
37 final File augFolder = new File(yangModelsFolder);
38 for (final File fileEntry : augFolder.listFiles()) {
39 if (fileEntry.isFile()) {
40 yangModels.add(fileEntry);
45 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName,
47 GeneratedType searchedGenType = null;
48 int searchedGenTypeCounter = 0;
49 for (Type type : genTypes) {
50 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
51 GeneratedType genType = (GeneratedType) type;
52 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
53 searchedGenType = genType;
54 searchedGenTypeCounter++;
58 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
59 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
60 searchedGenTypeCounter);
61 return searchedGenType;
65 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName) {
66 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
70 public void choiceCaseResolvingTypeTest() {
71 final YangModelParser parser = new YangParserImpl();
72 final Set<Module> modules = parser.parseYangModels(yangModels);
73 final SchemaContext context = parser.resolveSchemaContext(modules);
75 assertNotNull("context is null", context);
76 final BindingGenerator bindingGen = new BindingGeneratorImpl();
77 final List<Type> genTypes = bindingGen.generateTypes(context);
79 assertNotNull("genTypes is null", genTypes);
80 assertFalse("genTypes is empty", genTypes.isEmpty());
82 // test for file choice-monitoring
83 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
84 GeneratedType genType = null;
86 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
88 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
89 containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
90 containsInterface("LockType", genType);
92 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
93 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
94 containsInterface("LockType", genType);
96 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
97 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
98 containsInterface("LockType", genType);
100 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
102 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
103 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
104 containsInterface("AlgorithmAndHash", genType);
106 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
107 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
108 containsInterface("AlgorithmAndHash", genType);
110 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
111 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
112 containsInterface("AlgorithmAndHash", genType);
114 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
115 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
116 containsInterface("AlgorithmAndHash", genType);
118 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
119 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
120 containsInterface("AlgorithmAndHash", genType);
122 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
123 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
124 containsInterface("AlgorithmAndHash", genType);
126 // test for file augment-monitoring
128 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
129 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
132 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
133 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
134 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
135 containsInterface("LockType", genType);
137 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
138 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
139 containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
140 containsInterface("LockType", genType);
142 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
143 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
144 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
145 containsInterface("LockType", genType);
148 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
150 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
151 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
152 containsInterface("Augmentation<PartialLock>", genType);
154 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
155 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
157 genType = checkGeneratedType(genTypes, "Foo", pcgPref
158 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
159 containsMethods(genType, new NameTypePattern("getFoo", "String"));
160 containsInterface("AugCaseByChoice", genType);
162 genType = checkGeneratedType(genTypes, "Bar", pcgPref
163 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
164 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
165 containsInterface("AugCaseByChoice", genType);
167 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
168 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
169 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
170 containsInterface("Augmentation<Datastore>", genType);
172 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
174 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
175 + ".netconf.state.datastores.datastore.storage.format"); // case
176 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
177 containsInterface("StorageFormat", genType);
179 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
180 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
181 containsInterface("StorageFormat", genType);
183 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
184 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
185 containsInterface("StorageFormat", genType);