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.containsInterface;
12 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsMethods;
15 import java.util.ArrayList;
16 import java.util.List;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 public class ChoiceCaseGenTypesTest {
32 private final static List<File> yangModels = new ArrayList<>();
33 private final static String yangModelsFolder = AugmentedTypeTest.class.getResource("/choice-case-type-test-models")
37 public static void loadTestResources() {
38 final File augFolder = new File(yangModelsFolder);
39 for (final File fileEntry : augFolder.listFiles()) {
40 if (fileEntry.isFile()) {
41 yangModels.add(fileEntry);
46 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName,
48 GeneratedType searchedGenType = null;
49 int searchedGenTypeCounter = 0;
50 for (Type type : genTypes) {
51 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
52 GeneratedType genType = (GeneratedType) type;
53 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
54 searchedGenType = genType;
55 searchedGenTypeCounter++;
59 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
60 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
61 searchedGenTypeCounter);
62 return searchedGenType;
66 private static GeneratedType checkGeneratedType(List<Type> genTypes, String genTypeName, String packageName) {
67 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
71 public void choiceCaseResolvingTypeTest() {
72 final YangModelParser parser = new YangParserImpl();
73 final Set<Module> modules = parser.parseYangModels(yangModels);
75 final SchemaContext context = parser.resolveSchemaContext(modules);
77 assertNotNull("context is null", context);
78 final BindingGenerator bindingGen = new BindingGeneratorImpl();
79 final List<Type> genTypes = bindingGen.generateTypes(context);
81 assertNotNull("genTypes is null", genTypes);
82 assertFalse("genTypes is empty", genTypes.isEmpty());
84 // test for file choice-monitoring
85 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
86 GeneratedType genType = null;
88 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
90 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
91 containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
92 containsInterface("LockType", genType);
94 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
95 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
96 containsInterface("LockType", genType);
98 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
99 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
100 containsInterface("LockType", genType);
102 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
104 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
105 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
106 containsInterface("AlgorithmAndHash", genType);
108 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
109 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
110 containsInterface("AlgorithmAndHash", genType);
112 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
113 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
114 containsInterface("AlgorithmAndHash", genType);
116 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
117 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
118 containsInterface("AlgorithmAndHash", genType);
120 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
121 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
122 containsInterface("AlgorithmAndHash", genType);
124 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
125 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
126 containsInterface("AlgorithmAndHash", genType);
128 // test for file augment-monitoring
130 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
131 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
134 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
135 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
136 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
137 containsInterface("LockType", genType);
139 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
140 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
141 containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
142 containsInterface("LockType", genType);
144 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
145 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
147 containsMethods(genType, new NameTypePattern("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 containsMethods(genType, new NameTypePattern("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 containsMethods(genType, new NameTypePattern("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 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
168 containsInterface("AugCaseByChoice", genType);
170 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
171 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
172 containsMethods(genType, new NameTypePattern("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 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
180 containsInterface("StorageFormat", genType);
182 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
183 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
184 containsInterface("StorageFormat", genType);
186 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
187 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
188 containsInterface("StorageFormat", genType);