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.io.IOException;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
20 import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
29 public class ChoiceCaseGenTypesTest extends AbstractTypesTest {
31 public ChoiceCaseGenTypesTest() {
32 super(ChoiceCaseGenTypesTest.class.getResource("/choice-case-type-test-models"));
35 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
36 final String packageName, final int occurences) {
37 GeneratedType searchedGenType = null;
38 int searchedGenTypeCounter = 0;
39 for (Type type : genTypes) {
40 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
41 GeneratedType genType = (GeneratedType) type;
42 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
43 searchedGenType = genType;
44 searchedGenTypeCounter++;
48 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
49 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
50 searchedGenTypeCounter);
51 return searchedGenType;
55 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
56 final String packageName) {
57 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
61 public void choiceCaseResolvingTypeTest() throws IOException, SourceException, ReactorException {
62 final SchemaContext context = YangParserTestUtils.parseYangSources(testModels);
64 assertNotNull("context is null", context);
65 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
66 final List<Type> genTypes = bindingGen.generateTypes(context);
68 assertNotNull("genTypes is null", genTypes);
69 assertFalse("genTypes is empty", genTypes.isEmpty());
71 // test for file choice-monitoring
72 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
73 GeneratedType genType = null;
75 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
77 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
78 SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
79 containsInterface("LockType", genType);
81 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
82 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
83 containsInterface("LockType", genType);
85 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
86 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
87 containsInterface("LockType", genType);
89 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
91 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
92 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
93 containsInterface("AlgorithmAndHash", genType);
95 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
96 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
97 containsInterface("AlgorithmAndHash", genType);
99 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
100 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
101 containsInterface("AlgorithmAndHash", genType);
103 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
104 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
105 containsInterface("AlgorithmAndHash", genType);
107 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
108 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
109 containsInterface("AlgorithmAndHash", genType);
111 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
112 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
113 containsInterface("AlgorithmAndHash", genType);
115 // test for file augment-monitoring
117 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
118 pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.augment.monitoring.rev130701";
120 genType = checkGeneratedType(genTypes, "AutonomousLock", pcgPref
121 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
122 containsMethods(genType, new NameTypePattern("getAutonomousDef", "AutonomousDef"));
123 containsInterface("LockType", genType);
125 genType = checkGeneratedType(genTypes, "AnonymousLock", pcgPref
126 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
127 containsMethods(genType, new NameTypePattern("getLockTime", "Long"));
128 containsInterface("LockType", genType);
130 genType = checkGeneratedType(genTypes, "LeafAugCase", pcgPref
131 + ".netconf.state.datastores.datastore.locks.lock.type"); // choice
132 containsMethods(genType, new NameTypePattern("getLeafAugCase", "String"));
133 containsInterface("LockType", genType);
136 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type/nm:partial-lock"
138 genType = checkGeneratedType(genTypes, "PartialLock1", pcgPref); // case
139 containsMethods(genType, new NameTypePattern("getAugCaseByChoice", "AugCaseByChoice"));
140 containsInterface("Augmentation<PartialLock>", genType);
142 genType = checkGeneratedType(genTypes, "AugCaseByChoice", pcgPref
143 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock"); // choice
145 genType = checkGeneratedType(genTypes, "Foo", pcgPref
146 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
147 containsMethods(genType, new NameTypePattern("getFoo", "String"));
148 containsInterface("AugCaseByChoice", genType);
150 genType = checkGeneratedType(genTypes, "Bar", pcgPref
151 + ".netconf.state.datastores.datastore.locks.lock.type.partial.lock.aug._case.by.choice"); // case
152 containsMethods(genType, new NameTypePattern("isBar", "Boolean"));
153 containsInterface("AugCaseByChoice", genType);
155 // augment "/nm:netconf-state/nm:datastores/nm:datastore" {
156 genType = checkGeneratedType(genTypes, "Datastore1", pcgPref);
157 containsMethods(genType, new NameTypePattern("getStorageFormat", "StorageFormat"));
158 containsInterface("Augmentation<Datastore>", genType);
160 genType = checkGeneratedType(genTypes, "StorageFormat", pcgPref + ".netconf.state.datastores.datastore"); // choice
162 genType = checkGeneratedType(genTypes, "UnknownFiles", pcgPref
163 + ".netconf.state.datastores.datastore.storage.format"); // case
164 containsMethods(genType, new NameTypePattern("getFiles", "List<Files>"));
165 containsInterface("StorageFormat", genType);
167 genType = checkGeneratedType(genTypes, "Xml", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
168 containsMethods(genType, new NameTypePattern("getXmlDef", "XmlDef"));
169 containsInterface("StorageFormat", genType);
171 genType = checkGeneratedType(genTypes, "Yang", pcgPref + ".netconf.state.datastores.datastore.storage.format"); // case
172 containsMethods(genType, new NameTypePattern("getYangFileName", "String"));
173 containsInterface("StorageFormat", genType);