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.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;
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.model.api.GeneratedTransferObject;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.Type;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 public class ChoiceCaseGenTypesTest extends AbstractTypesTest {
30 public ChoiceCaseGenTypesTest() {
31 super(ChoiceCaseGenTypesTest.class.getResource("/choice-case-type-test-models"));
34 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
35 final String packageName, final int occurences) {
36 GeneratedType searchedGenType = null;
37 int searchedGenTypeCounter = 0;
38 for (Type type : genTypes) {
39 if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) {
40 GeneratedType genType = (GeneratedType) type;
41 if (genType.getName().equals(genTypeName) && genType.getPackageName().equals(packageName)) {
42 searchedGenType = genType;
43 searchedGenTypeCounter++;
47 assertNotNull("Generated type " + genTypeName + " wasn't found", searchedGenType);
48 assertEquals(genTypeName + " generated type has incorrect number of occurences.", occurences,
49 searchedGenTypeCounter);
50 return searchedGenType;
54 private static GeneratedType checkGeneratedType(final List<Type> genTypes, final String genTypeName,
55 final String packageName) {
56 return checkGeneratedType(genTypes, genTypeName, packageName, 1);
60 public void choiceCaseResolvingTypeTest() throws IOException, SourceException, ReactorException {
61 final SchemaContext context = YangParserTestUtils.parseYangSources(testModels);
63 assertNotNull("context is null", context);
64 final BindingGenerator bindingGen = new BindingGeneratorImpl(true);
65 final List<Type> genTypes = bindingGen.generateTypes(context);
67 assertNotNull("genTypes is null", genTypes);
68 assertFalse("genTypes is empty", genTypes.isEmpty());
70 // test for file choice-monitoring
71 String pcgPref = "org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.choice.monitoring.rev130701.netconf.state.datastores.datastore.locks";
72 GeneratedType genType = null;
74 checkGeneratedType(genTypes, "LockType", pcgPref); // choice
76 genType = checkGeneratedType(genTypes, "GlobalLock", pcgPref + ".lock.type"); // case
77 SupportTestUtil.containsMethods(genType, new NameTypePattern("getGlobalLock", "GlobalLock"));
78 containsInterface("LockType", genType);
80 genType = checkGeneratedType(genTypes, "PartialLock", pcgPref + ".lock.type"); // case
81 containsMethods(genType, new NameTypePattern("getPartialLock", "List<PartialLock>"));
82 containsInterface("LockType", genType);
84 genType = checkGeneratedType(genTypes, "Fingerprint", pcgPref + ".lock.type"); // case
85 containsMethods(genType, new NameTypePattern("getAlgorithmAndHash", "AlgorithmAndHash"));
86 containsInterface("LockType", genType);
88 genType = checkGeneratedType(genTypes, "AlgorithmAndHash", pcgPref + ".lock.type.fingerprint"); // choice
90 genType = checkGeneratedType(genTypes, "Md5", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
91 containsMethods(genType, new NameTypePattern("getMd5", "TlsFingerprintType"));
92 containsInterface("AlgorithmAndHash", genType);
94 genType = checkGeneratedType(genTypes, "Sha1", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
95 containsMethods(genType, new NameTypePattern("getSha1", "TlsFingerprintType"));
96 containsInterface("AlgorithmAndHash", genType);
98 genType = checkGeneratedType(genTypes, "Sha224", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
99 containsMethods(genType, new NameTypePattern("getSha224", "TlsFingerprintType"));
100 containsInterface("AlgorithmAndHash", genType);
102 genType = checkGeneratedType(genTypes, "Sha256", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
103 containsMethods(genType, new NameTypePattern("getSha256", "TlsFingerprintType"));
104 containsInterface("AlgorithmAndHash", genType);
106 genType = checkGeneratedType(genTypes, "Sha384", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
107 containsMethods(genType, new NameTypePattern("getSha384", "TlsFingerprintType"));
108 containsInterface("AlgorithmAndHash", genType);
110 genType = checkGeneratedType(genTypes, "Sha512", pcgPref + ".lock.type.fingerprint.algorithm.and.hash"); // case
111 containsMethods(genType, new NameTypePattern("getSha512", "TlsFingerprintType"));
112 containsInterface("AlgorithmAndHash", genType);
114 // test for file augment-monitoring
116 // "/nm:netconf-state/nm:datastores/nm:datastore/nm:locks/nm:lock-type"
117 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);