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.assertNotNull;
12 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsInterface;
13 import static org.opendaylight.mdsal.binding.generator.impl.SupportTestUtil.containsMethods;
15 import java.util.List;
16 import org.junit.Test;
17 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21 public class ChoiceCaseGenTypesTest {
23 public void choiceCaseResolvingTypeTest() {
24 final List<GeneratedType> genTypes = DefaultBindingGenerator.generateFor(
25 YangParserTestUtils.parseYangResourceDirectory("/choice-case-type-test-models"));
27 assertNotNull("genTypes is null", genTypes);
28 assertEquals(41, genTypes.size());
30 // test for file choice-monitoring
31 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701."
32 + "netconf.state.datastores.datastore.locks";
33 GeneratedType genType = null;
35 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
37 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
38 SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
39 containsInterface("LockType", genType);
41 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
42 containsMethods(genType, new NameTypePattern("getPartialLock", "Map<PartialLockKey,PartialLock>"));
43 containsInterface("LockType", genType);
45 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
46 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
47 containsInterface("LockType", genType);
49 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
51 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
52 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
53 containsInterface("AlgorithmAndHash", genType);
55 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
56 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
57 containsInterface("AlgorithmAndHash", genType);
59 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
60 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
61 containsInterface("AlgorithmAndHash", genType);
63 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
64 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
65 containsInterface("AlgorithmAndHash", genType);
67 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
68 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
69 containsInterface("AlgorithmAndHash", genType);
71 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
72 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
73 containsInterface("AlgorithmAndHash", genType);
75 // test for file augment-monitoring
77 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
78 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
80 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
81 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
82 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
83 containsInterface("LockType", genType);
85 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
86 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
87 containsMethods(genType, new NameTypePattern("getLockTime", "Uint32"));
88 containsInterface("LockType", genType);
90 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
91 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
92 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
93 containsInterface("LockType", genType);
96 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
98 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
99 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
100 containsInterface("Augmentation<PartialLock>", genType);
102 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
103 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
105 genType = checkGeneratedType(genTypes, "Foo", pcgPref
106 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
107 containsMethods(genType, new NameTypePattern("getFoo", "String"));
108 containsInterface("AugCaseByChoice", genType);
110 genType = checkGeneratedType(genTypes, "Bar", pcgPref
111 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
112 containsMethods(genType, new NameTypePattern("getBar", "Boolean"));
113 containsInterface("AugCaseByChoice", genType);
115 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
116 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
117 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
118 containsInterface("Augmentation<Datastore>", genType);
121 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore");
123 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
124 + ".netconf.state.datastores.datastore.storage.format"); // case
125 containsMethods(genType, new NameTypePattern("getFiles", "Map<FilesKey,Files>"));
126 containsInterface("StorageFormat", genType);
129 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format");
130 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
131 containsInterface("StorageFormat", genType);
134 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format");
135 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
136 containsInterface("StorageFormat", genType);
139 private static GeneratedType checkGeneratedType(final List<GeneratedType> genTypes, final String genTypeName,
140 final String packageName, final int occurences) {
141 GeneratedType searchedGenType = null;
142 int searchedGenTypeCounter = 0;
143 for (GeneratedType genType : genTypes) {
144 if (!(genType instanceof GeneratedTransferObject)) {
145 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
146 searchedGenType = genType;
147 searchedGenTypeCounter++;
151 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
152 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
153 searchedGenTypeCounter);
154 return searchedGenType;
158 private static GeneratedType checkGeneratedType(final List<GeneratedType> genTypes, final String genTypeName,
159 final String packageName) {
160 return checkGeneratedType(genTypes, genTypeName, packageName, 1);