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 implementation.\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 implementation.\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 implementation.\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 implementation.\n"
97 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\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"
103 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
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());
112 public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
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"
119 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\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"
125 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
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());
135 public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
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"
142 + " * @param <T$$> implementation type, which has to also implement AugmentationHolder interface\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"
148 + "static <T$$ extends test.test & AugmentationHolder<test.test>> String bindingToString(final @NonNull"
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());
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();
163 doReturn(impls).when(genType).getImplements();
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();
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);
178 doReturn(listMethodSign).when(genType).getMethodDefinitions();
180 final List<Type> impls = new ArrayList<>();
181 doReturn(impls).when(genType).getImplements();
185 private static CharSequence genToString(final GeneratedType genType) {
186 return new InterfaceTemplate(genType).generateBindingToString();
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();
195 final List<MethodSignature> listMethodSign = new ArrayList<>();
196 final MethodSignature methSign = mockMethSign(methodeName);
197 listMethodSign.add(methSign);
198 doReturn(listMethodSign).when(genType).getMethodDefinitions();
200 final List<Type> impls = new ArrayList<>();
201 doReturn(impls).when(genType).getImplements();
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();