2 * Copyright (c) 2016 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.mdsal.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.mdsal.binding.generator.impl.SupportTestUtil.containsInterface;
14 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsMethods;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
19 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
25 public class ChoiceCaseGenTypesTest extends AbstractTypesTest {
27 public ChoiceCaseGenTypesTest() {
28 super(ChoiceCaseGenTypesTest.class.getResource("/choice-case-type-test-models"));
31 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
32 final String packageName, final int occurences) {
33 GeneratedType searchedGenType = null;
34 int searchedGenTypeCounter = 0;
35 for (Type type : genTypes) {
36 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
37 GeneratedType genType = (GeneratedType) type;
38 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
39 searchedGenType = genType;
40 searchedGenTypeCounter++;
44 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
45 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
46 searchedGenTypeCounter);
47 return searchedGenType;
51 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
52 final String packageName) {
53 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
57 public void choiceCaseResolvingTypeTest() {
58 final SchemaContext context = YangParserTestUtils.parseYangFiles(testModels);
60 assertNotNull("context is null", context);
61 final BindingGenerator bindingGen = new BindingGeneratorImpl();
62 final List<Type> genTypes = bindingGen.generateTypes(context);
64 assertNotNull("genTypes is null", genTypes);
65 assertFalse("genTypes is empty", genTypes.isEmpty());
67 // test for file choice-monitoring
68 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701."
69 + "netconf.state.datastores.datastore.locks";
70 GeneratedType genType = null;
72 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
74 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
75 SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
76 containsInterface("LockType", genType);
78 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
79 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
80 containsInterface("LockType", genType);
82 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
83 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
84 containsInterface("LockType", genType);
86 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
88 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
89 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
90 containsInterface("AlgorithmAndHash", genType);
92 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
93 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
94 containsInterface("AlgorithmAndHash", genType);
96 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
97 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
98 containsInterface("AlgorithmAndHash", genType);
100 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
101 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
102 containsInterface("AlgorithmAndHash", genType);
104 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
105 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
106 containsInterface("AlgorithmAndHash", genType);
108 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
109 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
110 containsInterface("AlgorithmAndHash", genType);
112 // test for file augment-monitoring
114 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
115 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
117 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
118 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
119 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
120 containsInterface("LockType", genType);
122 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
123 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
124 containsMethods(genType, new NameTypePattern("getLockTime", "Uint32"));
125 containsInterface("LockType", genType);
127 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
128 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
129 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
130 containsInterface("LockType", genType);
133 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
135 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
136 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
137 containsInterface("Augmentation<PartialLock>", genType);
139 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
140 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
142 genType = checkGeneratedType(genTypes, "Foo", pcgPref
143 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
144 containsMethods(genType, new NameTypePattern("getFoo", "String"));
145 containsInterface("AugCaseByChoice", genType);
147 genType = checkGeneratedType(genTypes, "Bar", pcgPref
148 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
149 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
150 containsInterface("AugCaseByChoice", genType);
152 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
153 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
154 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
155 containsInterface("Augmentation<Datastore>", genType);
158 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore");
160 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
161 + ".netconf.state.datastores.datastore.storage.format"); // case
162 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
163 containsInterface("StorageFormat", genType);
166 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format");
167 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
168 containsInterface("StorageFormat", genType);
171 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format");
172 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
173 containsInterface("StorageFormat", genType);