Generate bindingToString() and use it in generated implementations
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / BuilderGeneratorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator;
9
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;
14
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;
23
24 public class BuilderGeneratorTest {
25     private static final String TEST = "test";
26     private static final JavaTypeName TYPE_NAME = JavaTypeName.create(TEST, TEST);
27
28     @Test
29     public void basicTest() {
30         assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
31     }
32
33     @Test
34     public void builderTemplateGenerateToStringWithPropertyTest() {
35         final GeneratedType genType = mockGenType("get" + TEST);
36
37         assertEquals("/**\n"
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 implementation.\n"
41                 + " *\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"
45                 + " */\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());
51     }
52
53     @Test
54     public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
55         assertEquals("/**\n"
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 implementation.\n"
59                 + " *\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"
63                 + " */\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());
68     }
69
70     @Test
71     public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
72         assertEquals("/**\n"
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 implementation.\n"
76                 + " *\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"
80                 + " */\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());
87     }
88
89     @Test
90     public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
91         assertEquals("/**\n"
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 implementation.\n"
95                 + " *\n"
96                 + " * <p>\n"
97                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
98                 + " *              contract.\n"
99                 + " * @param obj Object for which to generate toString() result.\n"
100                 + " * @return {@link String} value of data modeled by this interface.\n"
101                 + " * @throws NullPointerException if {@code obj} is null\n"
102                 + " */\n"
103                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
104                 + " T$$ obj) {\n"
105                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
106                 + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
107                 + "    return helper.toString();\n"
108                 + "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
109     }
110
111     @Test
112     public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
113         assertEquals("/**\n"
114                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
115                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
116                 + "\n * representations across all implementation.\n"
117                 + " *\n"
118                 + " * <p>\n"
119                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
120                 + " *              contract.\n"
121                 + " * @param obj Object for which to generate toString() result.\n"
122                 + " * @return {@link String} value of data modeled by this interface.\n"
123                 + " * @throws NullPointerException if {@code obj} is null\n"
124                 + " */\n"
125                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
126                 + " T$$ obj) {\n"
127                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
128                 + "    CodeHelpers.appendValue(helper, \"test\", obj.gettest());\n"
129                 + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
130                 + "    return helper.toString();\n"
131                 + "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
132     }
133
134     @Test
135     public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
136         assertEquals("/**\n"
137                 + " * Default implementation of {@link Object#toString()} contract for this interface.\n"
138                 + " * Implementations of this interface are encouraged to defer to this method to get consistent string"
139                 + "\n * representations across all implementation.\n"
140                 + " *\n"
141                 + " * <p>\n"
142                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
143                 + " *              contract.\n"
144                 + " * @param obj Object for which to generate toString() result.\n"
145                 + " * @return {@link String} value of data modeled by this interface.\n"
146                 + " * @throws NullPointerException if {@code obj} is null\n"
147                 + " */\n"
148                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
149                 + " T$$ obj) {\n"
150                 + "    final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n"
151                 + "    CodeHelpers.appendValue(helper, \"test1\", obj.gettest1());\n"
152                 + "    CodeHelpers.appendValue(helper, \"test2\", obj.gettest2());\n"
153                 + "    CodeHelpers.appendValue(helper, \"augmentation\", obj.augmentations().values());\n"
154                 + "    return helper.toString();\n"
155                 + "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
156     }
157
158     private static GeneratedType mockAugment(final GeneratedType genType) {
159         final List<Type> impls = new ArrayList<>();
160         final Type impl = mock(Type.class);
161         doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
162         impls.add(impl);
163         doReturn(impls).when(genType).getImplements();
164         return genType;
165     }
166
167     private static GeneratedType mockGenTypeMoreMeth(final String methodeName) {
168         final GeneratedType genType = spy(GeneratedType.class);
169         doReturn(TYPE_NAME).when(genType).getIdentifier();
170         doReturn(TEST).when(genType).getName();
171         doReturn(TEST).when(genType).getPackageName();
172
173         final List<MethodSignature> listMethodSign = new ArrayList<>();
174         for (int i = 0; i < 2; i++) {
175             final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
176             listMethodSign.add(methSign);
177         }
178         doReturn(listMethodSign).when(genType).getMethodDefinitions();
179
180         final List<Type> impls = new ArrayList<>();
181         doReturn(impls).when(genType).getImplements();
182         return genType;
183     }
184
185     private static CharSequence genToString(final GeneratedType genType) {
186         return new InterfaceTemplate(genType).generateBindingToString();
187     }
188
189     private static GeneratedType mockGenType(final String methodeName) {
190         final GeneratedType genType = spy(GeneratedType.class);
191         doReturn(TYPE_NAME).when(genType).getIdentifier();
192         doReturn(TEST).when(genType).getName();
193         doReturn(TEST).when(genType).getPackageName();
194
195         final List<MethodSignature> listMethodSign = new ArrayList<>();
196         final MethodSignature methSign = mockMethSign(methodeName);
197         listMethodSign.add(methSign);
198         doReturn(listMethodSign).when(genType).getMethodDefinitions();
199
200         final List<Type> impls = new ArrayList<>();
201         doReturn(impls).when(genType).getImplements();
202         return genType;
203     }
204
205     private static MethodSignature mockMethSign(final String methodeName) {
206         final MethodSignature methSign = mock(MethodSignature.class);
207         doReturn(methodeName).when(methSign).getName();
208         final Type methType = mock(Type.class);
209         doReturn(TYPE_NAME).when(methType).getIdentifier();
210         doReturn(methType).when(methSign).getReturnType();
211         doReturn(ValueMechanics.NORMAL).when(methSign).getMechanics();
212         return methSign;
213     }
214 }