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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsInterface;
14 import static org.opendaylight.yangtools.sal.binding.generator.impl.SupportTestUtil.containsMethods;
15 import java.io.IOException;
16 import java.util.List;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.sal.binding.generator.api.BindingGenerator;
19 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
20 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
21 import org.opendaylight.yangtools.sal.binding.model.api.Type;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.parser.api.YangContextParser;
24 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
26 public class ChoiceCaseGenTypesTest extends AbstractTypesTest {
28 public ChoiceCaseGenTypesTest() {
29 super(ChoiceCaseGenTypesTest.class.getResource("/choice-case-type-test-models"));
32 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
33 final String packageName, final int occurences) {
34 GeneratedType searchedGenType = null;
35 int searchedGenTypeCounter = 0;
36 for (Type type : genTypes) {
37 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
38 GeneratedType genType = (GeneratedType) type;
39 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
40 searchedGenType = genType;
41 searchedGenTypeCounter++;
45 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
46 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
47 searchedGenTypeCounter);
48 return searchedGenType;
52 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
53 final String packageName) {
54 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
58 public void choiceCaseResolvingTypeTest() throws IOException {
59 final YangContextParser parser = new YangParserImpl();
60 final SchemaContext context = parser.parseFiles(testModels);
62 assertNotNull("context is null", context);
63 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
64 final List<Type> genTypes = bindingGen.generateTypes(context);
66 assertNotNull("genTypes is null", genTypes);
67 assertFalse("genTypes is empty", genTypes.isEmpty());
69 // test for file choice-monitoring
70 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
71 GeneratedType genType = null;
73 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
75 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
76 containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
77 containsInterface("LockType", genType);
79 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
80 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
81 containsInterface("LockType", genType);
83 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
84 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
85 containsInterface("LockType", genType);
87 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
89 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
90 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
91 containsInterface("AlgorithmAndHash", genType);
93 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
94 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
95 containsInterface("AlgorithmAndHash", genType);
97 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
98 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
99 containsInterface("AlgorithmAndHash", genType);
101 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
102 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
103 containsInterface("AlgorithmAndHash", genType);
105 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
106 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
107 containsInterface("AlgorithmAndHash", genType);
109 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
110 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
111 containsInterface("AlgorithmAndHash", genType);
113 // test for file augment-monitoring
115 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
116 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
119 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
120 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
121 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
122 containsInterface("LockType", genType);
124 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
125 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
126 containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
127 containsInterface("LockType", genType);
129 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
130 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
131 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
132 containsInterface("LockType", genType);
135 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
137 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
138 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
139 containsInterface("Augmentation<PartialLock>", genType);
141 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
142 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
144 genType = checkGeneratedType(genTypes, "Foo", pcgPref
145 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
146 containsMethods(genType, new NameTypePattern("getFoo", "String"));
147 containsInterface("AugCaseByChoice", genType);
149 genType = checkGeneratedType(genTypes, "Bar", pcgPref
150 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
151 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
152 containsInterface("AugCaseByChoice", genType);
154 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
155 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
156 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
157 containsInterface("Augmentation<Datastore>", genType);
159 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
161 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
162 + ".netconf.state.datastores.datastore.storage.format"); // case
163 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
164 containsInterface("StorageFormat", genType);
166 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
167 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
168 containsInterface("StorageFormat", genType);
170 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
171 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
172 containsInterface("StorageFormat", genType);