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.java.api.generator;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.spy;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingGenerator;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
21 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
22 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
23 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
24 import org.opendaylight.mdsal.binding.model.api.Type;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 public class BuilderGeneratorTest {
29 private static final String TEST = "test";
30 private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
33 public void basicTest() {
34 assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
38 public void builderTemplateGenerateToStringWithPropertyTest() {
39 final GeneratedType genType = mockGenType("get" + TEST);
42 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
43 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
44 + "\n * representations across all implementations.\n"
46 + " * @param obj Object for which to generate toString() result.\n"
47 + " * @return {@link String} value of data modeled by this interface.\n"
48 + " * @throws NullPointerException if {@code obj} is null\n"
50 + "static String bindingToString(final test.test obj) {\n"
51 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
52 + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
53 + " return helper.toString();\n"
54 + "}\n", genToString(genType).toString());
58 public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
60 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
61 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
62 + "\n * representations across all implementations.\n"
64 + " * @param obj Object for which to generate toString() result.\n"
65 + " * @return {@link String} value of data modeled by this interface.\n"
66 + " * @throws NullPointerException if {@code obj} is null\n"
68 + "static String bindingToString(final test.test obj) {\n"
69 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
70 + " return helper.toString();\n"
71 + "}\n", genToString(mockGenType(TEST)).toString());
75 public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
77 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
78 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
79 + "\n * representations across all implementations.\n"
81 + " * @param obj Object for which to generate toString() result.\n"
82 + " * @return {@link String} value of data modeled by this interface.\n"
83 + " * @throws NullPointerException if {@code obj} is null\n"
85 + "static String bindingToString(final test.test obj) {\n"
86 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
87 + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
88 + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
89 + " return helper.toString();\n"
90 + "}\n", genToString(mockGenTypeMoreMeth("get" + TEST)).toString());
94 public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
96 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
97 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
98 + "\n * representations across all implementations.\n"
100 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
102 + " * @param obj Object for which to generate toString() result.\n"
103 + " * @return {@link String} value of data modeled by this interface.\n"
104 + " * @throws NullPointerException if {@code obj} is null\n"
106 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
108 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
109 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
110 + " return helper.toString();\n"
111 + "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
115 public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
117 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
118 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
119 + "\n * representations across all implementations.\n"
121 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
123 + " * @param obj Object for which to generate toString() result.\n"
124 + " * @return {@link String} value of data modeled by this interface.\n"
125 + " * @throws NullPointerException if {@code obj} is null\n"
127 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
129 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
130 + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
131 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
132 + " return helper.toString();\n"
133 + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
137 public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
139 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
140 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
141 + "\n * representations across all implementations.\n"
143 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
145 + " * @param obj Object for which to generate toString() result.\n"
146 + " * @return {@link String} value of data modeled by this interface.\n"
147 + " * @throws NullPointerException if {@code obj} is null\n"
149 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
151 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
152 + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
153 + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
154 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
155 + " return helper.toString();\n"
156 + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
160 public void builderTemplateGenerateToEqualsComparingOrderTest() {
161 final EffectiveModelContext context = YangParserTestUtils.parseYangResource(
163 final List<Type> types = new DefaultBindingGenerator().generateTypes(context);
164 final BuilderTemplate bt = BuilderGenerator.templateForType((GeneratedType) types.get(19));
166 final List<String> sortedProperties = bt.properties.stream()
167 .sorted(ByTypeMemberComparator.getInstance())
168 .map(BuilderGeneratedProperty::getName)
169 .collect(Collectors.toList());
171 assertEquals(List.of(
172 // numeric types (boolean, byte, short, int, long, biginteger, bigdecimal), identityrefs, empty
173 "id16", "id16Def", "id32", "id32Def", "id64", "id64Def", "id8", "id8Def", "idBoolean", "idBooleanDef",
174 "idDecimal64", "idDecimal64Def","idEmpty", "idEmptyDef", "idIdentityref", "idIdentityrefDef",
175 "idLeafref", "idLeafrefDef", "idU16", "idU16Def", "idU32", "idU32Def", "idU64", "idU64Def", "idU8",
177 // string, binary, bits
178 "idBinary", "idBinaryDef", "idBits", "idBitsDef", "idGroupLeafString", "idLeafrefContainer1",
179 "idLeafrefContainer1Def", "idString", "idStringDef",
180 // instance identifier
181 "idInstanceIdentifier", "idInstanceIdentifierDef",
183 "idContainer1", "idContainer2", "idEnumeration", "idEnumerationDef",
184 "idGroupContainer", "idList", "idUnion", "idUnionDef"), sortedProperties);
187 private static GeneratedType mockAugment(final GeneratedType genType) {
188 final List<Type> impls = new ArrayList<>();
189 final Type impl = mock(Type.class);
190 doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
192 doReturn(impls).when(genType).getImplements();
196 private static GeneratedType mockGenTypeMoreMeth(final String methodeName) {
197 final GeneratedType genType = spy(GeneratedType.class);
198 doReturn(TYPE_NAME).when(genType).getIdentifier();
199 doReturn(TEST).when(genType).getName();
200 doReturn(TEST).when(genType).getPackageName();
202 final List<MethodSignature> listMethodSign = new ArrayList<>();
203 for (int i = 0; i < 2; i++) {
204 final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
205 listMethodSign.add(methSign);
207 doReturn(listMethodSign).when(genType).getMethodDefinitions();
209 final List<Type> impls = new ArrayList<>();
210 doReturn(impls).when(genType).getImplements();
214 private static CharSequence genToString(final GeneratedType genType) {
215 return new InterfaceTemplate(genType).generateBindingToString();
218 private static GeneratedType mockGenType(final String methodeName) {
219 final GeneratedType genType = spy(GeneratedType.class);
220 doReturn(TYPE_NAME).when(genType).getIdentifier();
221 doReturn(TEST).when(genType).getName();
222 doReturn(TEST).when(genType).getPackageName();
224 final List<MethodSignature> listMethodSign = new ArrayList<>();
225 final MethodSignature methSign = mockMethSign(methodeName);
226 listMethodSign.add(methSign);
227 doReturn(listMethodSign).when(genType).getMethodDefinitions();
229 final List<Type> impls = new ArrayList<>();
230 doReturn(impls).when(genType).getImplements();
234 private static MethodSignature mockMethSign(final String methodeName) {
235 final MethodSignature methSign = mock(MethodSignature.class);
236 doReturn(methodeName).when(methSign).getName();
237 final Type methType = mock(Type.class);
238 doReturn(TYPE_NAME).when(methType).getIdentifier();
239 doReturn(methType).when(methSign).getReturnType();
240 doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();