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 org.junit.Test;
18 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
21 import org.opendaylight.mdsal.binding.model.api.MethodSignature.ValueMechanics;
22 import org.opendaylight.mdsal.binding.model.api.Type;
24 public class BuilderGeneratorTest {
25 private static final String TEST = "test";
26 private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
29 public void basicTest() {
30 assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
34 public void builderTemplateGenerateToStringWithPropertyTest() {
35 final GeneratedType genType = mockGenType("get" + TEST);
38 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
39 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
40 + "\n * representations across all implementations.\n"
42 + " * @param obj Object for which to generate toString() result.\n"
43 + " * @return {@link String} value of data modeled by this interface.\n"
44 + " * @throws NullPointerException if {@code obj} is null\n"
46 + "static String bindingToString(final test.test obj) {\n"
47 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
48 + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
49 + " return helper.toString();\n"
50 + "}\n", genToString(genType).toString());
54 public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
56 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
57 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
58 + "\n * representations across all implementations.\n"
60 + " * @param obj Object for which to generate toString() result.\n"
61 + " * @return {@link String} value of data modeled by this interface.\n"
62 + " * @throws NullPointerException if {@code obj} is null\n"
64 + "static String bindingToString(final test.test obj) {\n"
65 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
66 + " return helper.toString();\n"
67 + "}\n", genToString(mockGenType(TEST)).toString());
71 public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
73 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
74 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
75 + "\n * representations across all implementations.\n"
77 + " * @param obj Object for which to generate toString() result.\n"
78 + " * @return {@link String} value of data modeled by this interface.\n"
79 + " * @throws NullPointerException if {@code obj} is null\n"
81 + "static String bindingToString(final test.test obj) {\n"
82 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
83 + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
84 + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
85 + " return helper.toString();\n"
86 + "}\n", genToString(mockGenTypeMoreMeth("get" + TEST)).toString());
90 public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
92 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
93 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
94 + "\n * representations across all implementations.\n"
96 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
98 + " * @param obj Object for which to generate toString() result.\n"
99 + " * @return {@link String} value of data modeled by this interface.\n"
100 + " * @throws NullPointerException if {@code obj} is null\n"
102 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
104 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
105 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
106 + " return helper.toString();\n"
107 + "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
111 public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
113 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
114 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
115 + "\n * representations across all implementations.\n"
117 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
119 + " * @param obj Object for which to generate toString() result.\n"
120 + " * @return {@link String} value of data modeled by this interface.\n"
121 + " * @throws NullPointerException if {@code obj} is null\n"
123 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
125 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
126 + " CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
127 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
128 + " return helper.toString();\n"
129 + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
133 public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
135 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
136 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
137 + "\n * representations across all implementations.\n"
139 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
141 + " * @param obj Object for which to generate toString() result.\n"
142 + " * @return {@link String} value of data modeled by this interface.\n"
143 + " * @throws NullPointerException if {@code obj} is null\n"
145 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
147 + " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
148 + " CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
149 + " CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
150 + " CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
151 + " return helper.toString();\n"
152 + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
155 private static GeneratedType mockAugment(final GeneratedType genType) {
156 final List<Type> impls = new ArrayList<>();
157 final Type impl = mock(Type.class);
158 doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
160 doReturn(impls).when(genType).getImplements();
164 private static GeneratedType mockGenTypeMoreMeth(final String methodeName) {
165 final GeneratedType genType = spy(GeneratedType.class);
166 doReturn(TYPE_NAME).when(genType).getIdentifier();
167 doReturn(TEST).when(genType).getName();
168 doReturn(TEST).when(genType).getPackageName();
170 final List<MethodSignature> listMethodSign = new ArrayList<>();
171 for (int i = 0; i < 2; i++) {
172 final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
173 listMethodSign.add(methSign);
175 doReturn(listMethodSign).when(genType).getMethodDefinitions();
177 final List<Type> impls = new ArrayList<>();
178 doReturn(impls).when(genType).getImplements();
182 private static CharSequence genToString(final GeneratedType genType) {
183 return new InterfaceTemplate(genType).generateBindingToString();
186 private static GeneratedType mockGenType(final String methodeName) {
187 final GeneratedType genType = spy(GeneratedType.class);
188 doReturn(TYPE_NAME).when(genType).getIdentifier();
189 doReturn(TEST).when(genType).getName();
190 doReturn(TEST).when(genType).getPackageName();
192 final List<MethodSignature> listMethodSign = new ArrayList<>();
193 final MethodSignature methSign = mockMethSign(methodeName);
194 listMethodSign.add(methSign);
195 doReturn(listMethodSign).when(genType).getMethodDefinitions();
197 final List<Type> impls = new ArrayList<>();
198 doReturn(impls).when(genType).getImplements();
202 private static MethodSignature mockMethSign(final String methodeName) {
203 final MethodSignature methSign = mock(MethodSignature.class);
204 doReturn(methodeName).when(methSign).getName();
205 final Type methType = mock(Type.class);
206 doReturn(TYPE_NAME).when(methType).getIdentifier();
207 doReturn(methType).when(methSign).getReturnType();
208 doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();