Bug 3147 - Binding spec v1: auto generated code by YANGTOOLS could
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / yangtools / sal / 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.yangtools.sal.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.lang.reflect.Field;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19
20 import org.junit.Test;
21 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.sal.binding.model.api.MethodSignature;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25
26 @SuppressWarnings("deprecation")
27 public class BuilderGeneratorTest {
28
29     private static final String PROPERTIES_FIELD_NAME = "properties";
30     private static final String GEN_TO_STRING_FIRST_PART =
31             "@Override\npublic java.lang.String toString() {\n    java.lang.StringBuilder builder = new java.lang.StringBuilder ("
32                     + "\"test [\");";
33     private static final String GEN_TO_STRING_LAST_PART = "\n    return builder.append(']').toString();\n}\n";
34     private static final String GEN_TO_STRING_AUGMENT_PART =
35             "\n    builder.append(\"augmentation=\");\n    builder.append(augmentation.values());";
36     private static final String APPEND_COMMA = "builder.append(\", \");";
37     private static final String APPEND_COMMA_AUGMENT = "int builderLength = builder.length();\n"
38             + "    if (builderLength > 2 && !builder.substring(builderLength - 2, builderLength).equals(\", \")) {\n"
39             + "        " + APPEND_COMMA + "\n" + "    }";
40     private static final String TEST = "test";
41
42     @Test
43     public void basicTest() throws Exception {
44         assertEquals("", new BuilderGenerator().generate(mock(Type.class)));
45     }
46
47     @Test
48     public void builderTemplateGenerateToStringWithPropertyTest() throws Exception {
49         final GeneratedType genType = mockGenType("get" + TEST);
50         final String generateToString = genToString(genType).toString();
51         final String expected = GEN_TO_STRING_FIRST_PART
52                 + "\n    if (_test != null) {\n        builder.append(\"_test=\");\n        builder.append(_test);\n    }"
53                 + GEN_TO_STRING_LAST_PART;
54         assertEquals(expected, generateToString);
55     }
56
57     @Test
58     public void builderTemplateGenerateToStringWithoutAnyPropertyTest() throws Exception {
59         final GeneratedType genType = mockGenType(TEST);
60         final String generateToString = genToString(genType).toString();
61         final String expected = GEN_TO_STRING_FIRST_PART + GEN_TO_STRING_LAST_PART;
62         assertEquals(expected, generateToString);
63     }
64
65     @Test
66     public void builderTemplateGenerateToStringWithMorePropertiesTest() throws Exception {
67         final GeneratedType genType = mockGenTypeMoreMeth("get" + TEST);
68         final String generateToString = genToString(genType).toString();
69         final String expected = GEN_TO_STRING_FIRST_PART
70                 + "\n    if (_test1 != null) {\n        builder.append(\"_test1=\");\n        builder.append(_test1);"
71                 + "\n        " + APPEND_COMMA + "\n    }"
72                 + "\n    if (_test2 != null) {\n        builder.append(\"_test2=\");\n        builder.append(_test2);\n    }"
73                 + GEN_TO_STRING_LAST_PART;
74         assertEquals(expected, generateToString);
75     }
76
77     @Test
78     public void builderTemplateGenerateToStringWithoutPropertyWithAugmentTest() throws Exception {
79         final GeneratedType genType = mockGenType(TEST);
80         mockAugment(genType);
81         final String generateToString = genToString(genType).toString();
82         final String expected = GEN_TO_STRING_FIRST_PART + GEN_TO_STRING_AUGMENT_PART + GEN_TO_STRING_LAST_PART;
83         assertEquals(expected, generateToString);
84     }
85
86     @Test
87     public void builderTemplateGenerateToStringWithPropertyWithAugmentTest() throws Exception {
88         final GeneratedType genType = mockGenType("get" + TEST);
89         mockAugment(genType);
90         final String generateToString = genToString(genType).toString();
91         final String expected = GEN_TO_STRING_FIRST_PART
92                 + "\n    if (_test != null) {\n        builder.append(\"_test=\");\n        builder.append(_test);\n    }"
93                 + "\n    " + APPEND_COMMA_AUGMENT + GEN_TO_STRING_AUGMENT_PART + GEN_TO_STRING_LAST_PART;
94         assertEquals(expected, generateToString);
95     }
96
97     @Test
98     public void builderTemplateGenerateToStringWithMorePropertiesWithAugmentTest() throws Exception {
99         final GeneratedType genType = mockGenTypeMoreMeth("get" + TEST);
100         mockAugment(genType);
101         final String generateToString = genToString(genType).toString();
102         final String expected = GEN_TO_STRING_FIRST_PART
103                 + "\n    if (_test1 != null) {\n        builder.append(\"_test1=\");\n        builder.append(_test1);\n        "
104                 + APPEND_COMMA + "\n    }"
105                 + "\n    if (_test2 != null) {\n        builder.append(\"_test2=\");\n        builder.append(_test2);\n    }"
106                 + "\n    " + APPEND_COMMA_AUGMENT + GEN_TO_STRING_AUGMENT_PART + GEN_TO_STRING_LAST_PART;
107         assertEquals(expected, generateToString);
108     }
109
110     private void mockAugment(final GeneratedType genType) {
111         final List<Type> impls = new ArrayList<>();
112         final Type impl = mock(Type.class);
113         doReturn("org.opendaylight.yangtools.yang.binding.Augmentable").when(impl).getFullyQualifiedName();
114         impls.add(impl);
115         doReturn(impls).when(genType).getImplements();
116     }
117
118     private GeneratedType mockGenTypeMoreMeth(final String methodeName) {
119         final GeneratedType genType = spy(GeneratedType.class);
120         doReturn(TEST).when(genType).getName();
121         doReturn(TEST).when(genType).getPackageName();
122
123         final List<MethodSignature> listMethodSign = new ArrayList<>();
124         for (int i = 0; i < 2; i++) {
125             final MethodSignature methSign = mockMethSign(methodeName + (i + 1));
126             listMethodSign.add(methSign);
127         }
128         doReturn(listMethodSign).when(genType).getMethodDefinitions();
129
130         final List<Type> impls = new ArrayList<>();
131         doReturn(impls).when(genType).getImplements();
132         return genType;
133     }
134
135     @SuppressWarnings("unchecked")
136     private CharSequence genToString(final GeneratedType genType)
137             throws NoSuchFieldException, IllegalAccessException {
138         final BuilderTemplate bt = new BuilderTemplate(genType);
139         final Field propertiesField = bt.getClass().getDeclaredField(PROPERTIES_FIELD_NAME);
140         propertiesField.setAccessible(true);
141         return bt.generateToString((Collection<GeneratedProperty>) propertiesField.get(bt));
142     }
143
144     private GeneratedType mockGenType(final String methodeName) {
145         final GeneratedType genType = spy(GeneratedType.class);
146         doReturn(TEST).when(genType).getName();
147         doReturn(TEST).when(genType).getPackageName();
148
149         final List<MethodSignature> listMethodSign = new ArrayList<>();
150         final MethodSignature methSign = mockMethSign(methodeName);
151         listMethodSign.add(methSign);
152         doReturn(listMethodSign).when(genType).getMethodDefinitions();
153
154         final List<Type> impls = new ArrayList<>();
155         doReturn(impls).when(genType).getImplements();
156         return genType;
157     }
158
159     private MethodSignature mockMethSign(final String methodeName) {
160         final MethodSignature methSign = mock(MethodSignature.class);
161         doReturn(methodeName).when(methSign).getName();
162         final Type methType = mock(Type.class);
163         doReturn(TEST).when(methType).getName();
164         doReturn(TEST).when(methType).getPackageName();
165         doReturn(methType).when(methSign).getReturnType();
166         return methSign;
167     }
168 }