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.controller.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.controller.sal.binding.generator.impl.SupportTestUtil.containsInterface;
14 import static org.opendaylight.controller.sal.binding.generator.impl.SupportTestUtil.containsSignatures;
17 import java.util.ArrayList;
18 import java.util.List;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.sal.binding.generator.api.BindingGenerator;
24 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
25 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
26 import org.opendaylight.controller.sal.binding.model.api.Type;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
30 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
32 public class ChoiceCaseGenTypesTest {
34 private final static List<File> yangModels = new ArrayList<>();
35 private final static String yangModelsFolder = AugmentedTypeTest.class.getResource("/choice-case-type-test-models")
39 public static void loadTestResources() {
40 final File augFolder = new File(yangModelsFolder);
41 for (final File fileEntry : augFolder.listFiles()) {
42 if (fileEntry.isFile()) {
43 yangModels.add(fileEntry);
48 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName,
50 GeneratedType searchedGenType = null;
51 int searchedGenTypeCounter = 0;
52 for (Type type : genTypes) {
53 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
54 GeneratedType genType = (GeneratedType) type;
55 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
56 searchedGenType = genType;
57 searchedGenTypeCounter++;
61 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
62 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
63 searchedGenTypeCounter);
64 return searchedGenType;
68 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName) {
69 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
73 public void choiceCaseResolvingTypeTest() {
74 final YangModelParser parser = new YangParserImpl();
75 final Set<Module> modules = parser.parseYangModels(yangModels);
76 final SchemaContext context = parser.resolveSchemaContext(modules);
78 assertNotNull("context is null", context);
79 final BindingGenerator bindingGen = new BindingGeneratorImpl();
80 final List<Type> genTypes = bindingGen.generateTypes(context);
82 assertNotNull("genTypes is null", genTypes);
83 assertFalse("genTypes is empty", genTypes.isEmpty());
85 // test for file choice-monitoring
86 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev201371.netconf.state.datastores.datastore.locks";
87 GeneratedType genType = null;
89 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
91 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
92 containsSignatures(genType, new MethodSignaturePattern("getGlobalLock", "GlobalLock"));
93 containsInterface("LockType", genType);
95 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
96 containsSignatures(genType, new MethodSignaturePattern("getPartialLock", "List<PartialLock>"));
97 containsInterface("LockType", genType);
99 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
100 containsSignatures(genType, new MethodSignaturePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
101 containsInterface("LockType", genType);
103 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
105 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
106 containsSignatures(genType, new MethodSignaturePattern("getMd5", "TlsFingerprintType"));
107 containsInterface("AlgorithmAndHash", genType);
109 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
110 containsSignatures(genType, new MethodSignaturePattern("getSha1", "TlsFingerprintType"));
111 containsInterface("AlgorithmAndHash", genType);
113 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
114 containsSignatures(genType, new MethodSignaturePattern("getSha224", "TlsFingerprintType"));
115 containsInterface("AlgorithmAndHash", genType);
117 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
118 containsSignatures(genType, new MethodSignaturePattern("getSha256", "TlsFingerprintType"));
119 containsInterface("AlgorithmAndHash", genType);
121 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
122 containsSignatures(genType, new MethodSignaturePattern("getSha384", "TlsFingerprintType"));
123 containsInterface("AlgorithmAndHash", genType);
125 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
126 containsSignatures(genType, new MethodSignaturePattern("getSha512", "TlsFingerprintType"));
127 containsInterface("AlgorithmAndHash", genType);
129 // test for file augment-monitoring
131 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
132 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev201371";
135 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
136 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
137 containsSignatures(genType, new MethodSignaturePattern("getAutonomousDef", "AutonomousDef"));
138 containsInterface("LockType", genType);
140 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
141 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
142 containsSignatures(genType, new MethodSignaturePattern("getLockTime", "Long"));
143 containsInterface("LockType", genType);
145 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
146 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
147 containsSignatures(genType, new MethodSignaturePattern("getLeafAugCase", "String"));
148 containsInterface("LockType", genType);
151 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
153 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
154 containsSignatures(genType, new MethodSignaturePattern("getAugCaseByChoice", "AugCaseByChoice"));
155 containsInterface("Augmentation<PartialLock>", genType);
157 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
158 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
160 genType = checkGeneratedType(genTypes, "Foo", pcgPref
161 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
162 containsSignatures(genType, new MethodSignaturePattern("getFoo", "String"));
163 containsInterface("AugCaseByChoice", genType);
165 genType = checkGeneratedType(genTypes, "Bar", pcgPref
166 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
167 containsSignatures(genType, new MethodSignaturePattern("getBar", "Boolean"));
168 containsInterface("AugCaseByChoice", genType);
170 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
171 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
172 containsSignatures(genType, new MethodSignaturePattern("getStorageFormat", "StorageFormat"));
173 containsInterface("Augmentation<Datastore>", genType);
175 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
177 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
178 + ".netconf.state.datastores.datastore.storage.format"); // case
179 containsSignatures(genType, new MethodSignaturePattern("getFiles", "List<Files>"));
180 containsInterface("StorageFormat", genType);
182 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
183 containsSignatures(genType, new MethodSignaturePattern("getXmlDef", "XmlDef"));
184 containsInterface("StorageFormat", genType);
186 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
187 containsSignatures(genType, new MethodSignaturePattern("getYangFileName", "String"));
188 containsInterface("StorageFormat", genType);