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.lang.reflect.Field;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
22 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
23 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
24 import org.opendaylight.mdsal.binding.model.api.Type;
26 public class BuilderGeneratorTest {
28 private static final String PROPERTIES_FIELD_NAME = "properties";
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);
41 assertEquals("@Override\n" +
42 "public java.lang.String toString() {\n" +
43 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
44 " CodeHelpers.appendValue(helper, \"_test\", _test);\n" +
45 " return helper.toString();\n" +
46 "}\n", genToString(genType).toString());
50 public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
51 assertEquals("@Override\n" +
52 "public java.lang.String toString() {\n" +
53 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
54 " return helper.toString();\n" +
55 "}\n", genToString(mockGenType(TEST)).toString());
59 public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
60 assertEquals("@Override\n" +
61 "public java.lang.String toString() {\n" +
62 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
63 " CodeHelpers.appendValue(helper, \"_test1\", _test1);\n" +
64 " CodeHelpers.appendValue(helper, \"_test2\", _test2);\n" +
65 " return helper.toString();\n" +
66 "}\n", genToString(mockGenTypeMoreMeth("get" + TEST)).toString());
70 public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
71 assertEquals("@Override\n" +
72 "public java.lang.String toString() {\n" +
73 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
74 " CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n" +
75 " return helper.toString();\n" +
76 "}\n", genToString(mockAugment(mockGenType(TEST))).toString());
80 public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
81 assertEquals("@Override\n" +
82 "public java.lang.String toString() {\n" +
83 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
84 " CodeHelpers.appendValue(helper, \"_test\", _test);\n" +
85 " CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n" +
86 " return helper.toString();\n" +
87 "}\n", genToString(mockAugment(mockGenType("get" + TEST))).toString());
91 public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
92 assertEquals("@Override\n" +
93 "public java.lang.String toString() {\n" +
94 " final MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(\"test\");\n" +
95 " CodeHelpers.appendValue(helper, \"_test1\", _test1);\n" +
96 " CodeHelpers.appendValue(helper, \"_test2\", _test2);\n" +
97 " CodeHelpers.appendValue(helper, \"augmentation\", augmentation.values());\n" +
98 " return helper.toString();\n" +
99 "}\n", genToString(mockAugment(mockGenTypeMoreMeth("get" + TEST))).toString());
102 private static GeneratedType mockAugment(final GeneratedType genType) {
103 final List<Type> impls = new ArrayList<>();
104 final Type impl = mock(Type.class);
105 doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
107 doReturn(impls).when(genType).getImplements();
111 private static GeneratedType mockGenTypeMoreMeth(final String methodeName) {
112 final GeneratedType genType = spy(GeneratedType.class);
113 doReturn(TYPE_NAME).when(genType).getIdentifier();
114 doReturn(TEST).when(genType).getName();
115 doReturn(TEST).when(genType).getPackageName();
117 final List<MethodSignature> listMethodSign = new ArrayList<>();
118 for (int i = 0; i < 2; i++) {
119 final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
120 listMethodSign.add(methSign);
122 doReturn(listMethodSign).when(genType).getMethodDefinitions();
124 final List<Type> impls = new ArrayList<>();
125 doReturn(impls).when(genType).getImplements();
129 @SuppressWarnings("unchecked")
130 private static CharSequence genToString(final GeneratedType genType) {
132 final BuilderTemplate bt = new BuilderTemplate(genType);
133 final Field propertiesField = bt.getClass().getDeclaredField(PROPERTIES_FIELD_NAME);
134 propertiesField.setAccessible(true);
135 return bt.generateToString((Collection<GeneratedProperty>) propertiesField.get(bt));
136 } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
137 throw new RuntimeException(e);
141 private static GeneratedType mockGenType(final String methodeName) {
142 final GeneratedType genType = spy(GeneratedType.class);
143 doReturn(TYPE_NAME).when(genType).getIdentifier();
144 doReturn(TEST).when(genType).getName();
145 doReturn(TEST).when(genType).getPackageName();
147 final List<MethodSignature> listMethodSign = new ArrayList<>();
148 final MethodSignature methSign = mockMethSign(methodeName);
149 listMethodSign.add(methSign);
150 doReturn(listMethodSign).when(genType).getMethodDefinitions();
152 final List<Type> impls = new ArrayList<>();
153 doReturn(impls).when(genType).getImplements();
157 private static MethodSignature mockMethSign(final String methodeName) {
158 final MethodSignature methSign = mock(MethodSignature.class);
159 doReturn(methodeName).when(methSign).getName();
160 final Type methType = mock(Type.class);
161 doReturn(TYPE_NAME).when(methType).getIdentifier();
162 doReturn(methType).when(methSign).getReturnType();