Generate bindingEquals() 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 implementations.\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 implementations.\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 implementations.\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 implementations.\n"
95                 + " *\n"
96                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
97                 + " *              contract.\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"
101                 + " */\n"
102                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
103                 + " T$$ obj) {\n"
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());
108     }
109
110     @Test
111     public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
112         assertEquals("/**\n"
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"
116                 + " *\n"
117                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
118                 + " *              contract.\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"
122                 + " */\n"
123                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
124                 + " T$$ obj) {\n"
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());
130     }
131
132     @Test
133     public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
134         assertEquals("/**\n"
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"
138                 + " *\n"
139                 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\n"
140                 + " *              contract.\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"
144                 + " */\n"
145                 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
146                 + " T$$ obj) {\n"
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());
153     }
154
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();
159         impls.add(impl);
160         doReturn(impls).when(genType).getImplements();
161         return genType;
162     }
163
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();
169
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);
174         }
175         doReturn(listMethodSign).when(genType).getMethodDefinitions();
176
177         final List<Type> impls = new ArrayList<>();
178         doReturn(impls).when(genType).getImplements();
179         return genType;
180     }
181
182     private static CharSequence genToString(final GeneratedType genType) {
183         return new InterfaceTemplate(genType).generateBindingToString();
184     }
185
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();
191
192         final List<MethodSignature> listMethodSign = new ArrayList<>();
193         final MethodSignature methSign = mockMethSign(methodeName);
194         listMethodSign.add(methSign);
195         doReturn(listMethodSign).when(genType).getMethodDefinitions();
196
197         final List<Type> impls = new ArrayList<>();
198         doReturn(impls).when(genType).getImplements();
199         return genType;
200     }
201
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();
209         return methSign;
210     }
211 }