Use FileGenerator for java-api-generator
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / SpecializingLeafrefTest.java
1 /*
2  * Copyright (c) 2020 Pantheon Technologies, s.r.o.  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.hamcrest.CoreMatchers.containsString;
11 import static org.hamcrest.CoreMatchers.not;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.DOUBLE_TAB;
16 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.TAB;
17 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.TRIPLE_TAB;
18 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.doubleTab;
19 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.getFiles;
20 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.tab;
21 import static org.opendaylight.mdsal.binding.java.api.generator.FileSearchUtil.tripleTab;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.URISyntaxException;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.List;
29 import java.util.Map;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
34 import org.opendaylight.mdsal.binding.model.api.MethodSignature;
35 import org.opendaylight.mdsal.binding.model.api.Type;
36 import org.opendaylight.mdsal.binding.model.util.Types;
37
38 public class SpecializingLeafrefTest extends BaseCompilationTest {
39     private static final Type LIST_STRING_TYPE  = Types.listTypeFor(Types.STRING);
40
41     public static final String BAR_CONT = "BarCont";
42     public static final String BOOLEAN_CONT = "BooleanCont";
43
44     private static final String BAR_LST = "BarLst";
45     private static final String BAZ_GRP = "BazGrp";
46     private static final String FOO_GRP = "FooGrp";
47     private static final String RESOLVED_LEAF_GRP = "ResolvedLeafGrp";
48     private static final String RESOLVED_LEAFLIST_GRP = "ResolvedLeafListGrp";
49     private static final String TRANSITIVE_GROUP = "TransitiveGroup";
50     private static final String UNRESOLVED_GROUPING = "UnresolvedGrouping";
51
52     private static final String GET_LEAF1_NAME = "getLeaf1";
53     private static final String GET_LEAFLIST1_NAME = "getLeafList1";
54
55     private static final String GET_LEAF1_TYPE_OBJECT = "    Object getLeaf1();";
56     private static final String GET_LEAF1_TYPE_STRING = "    String getLeaf1();";
57     private static final String GET_LEAFLIST1_WILDCARD = "    @Nullable List<?> getLeafList1();";
58     private static final String GET_LEAFLIST1_STRING = "    @Nullable List<String> getLeafList1();";
59     private static final String GET_LEAFLIST1_DECLARATION = " getLeafList1();";
60     private static final String GET_LEAF1_DECLARATION = " getLeaf1();";
61
62     private static final char CLOSING_METHOD_BRACE = '}';
63     private static final String TAB_CLOSING_METHOD_BRACE = TAB + CLOSING_METHOD_BRACE;
64     private static final String DTAB_CLOSING_METHOD_BRACE = DOUBLE_TAB + CLOSING_METHOD_BRACE;
65
66     private static final String FOO_GRP_REF = "FooGrp";
67     private static final String RESOLVED_LEAF_GRP_REF = "ResolvedLeafGrp";
68
69     private static final String UNRESOLVED_GROUPING_REF =
70             "UnresolvedGrouping";
71
72     private static final String ARG_AS_FOO_GRP = "((" + FOO_GRP_REF + ")arg)";
73
74     private static final String LEAF2_ASSIGNMENT = "this._leaf2 = arg.getLeaf2();";
75
76     private static final String TAB_FIELDS_FROM_SIGNATURE = TAB + "public void fieldsFrom(DataObject arg) {";
77     private static final String TTAB_SET_IS_VALID_ARG_TRUE = TRIPLE_TAB + "isValidArg = true;";
78     private static final String DTAB_INIT_IS_VALID_ARG_FALSE = DOUBLE_TAB + "boolean isValidArg = false;";
79
80     private File sourcesOutputDir;
81     private File compiledOutputDir;
82     private List<GeneratedType> types;
83     private Map<String, File> files;
84
85     @Before
86     public void before() throws IOException, URISyntaxException {
87         sourcesOutputDir = CompilationTestUtils.generatorOutput("mdsal426");
88         compiledOutputDir = CompilationTestUtils.compilerOutput("mdsal426");
89         types = generateTestSources("/compilation/mdsal426", sourcesOutputDir);
90         CompilationTestUtils.testCompilation(sourcesOutputDir, compiledOutputDir);
91         files = getFiles(sourcesOutputDir);
92     }
93
94     @After
95     public void after() {
96         CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
97     }
98
99     @Test
100     public void testGroupingWithUnresolvedLeafRefs() throws IOException {
101         verifyReturnType(FOO_GRP, GET_LEAF1_NAME, Types.objectType());
102         verifyReturnType(FOO_GRP, GET_LEAFLIST1_NAME, Types.listTypeWildcard());
103
104         final String content = getFileContent(FOO_GRP);
105
106         assertThat(content, containsString(GET_LEAF1_TYPE_OBJECT));
107         assertThat(content, containsString(GET_LEAFLIST1_WILDCARD));
108     }
109
110     @Test
111     public void testLeafLeafrefPointsLeaf() throws IOException {
112         verifyReturnType(RESOLVED_LEAF_GRP, GET_LEAF1_NAME, Types.STRING);
113
114         final String content = getFileContent(RESOLVED_LEAF_GRP);
115
116         assertThat(content, containsString(GET_LEAF1_TYPE_STRING));
117     }
118
119     @Test
120     public void testLeafLeafrefPointsLeafList() throws IOException {
121         verifyReturnType(RESOLVED_LEAFLIST_GRP, GET_LEAF1_NAME, Types.STRING);
122
123         final String content = getFileContent(RESOLVED_LEAF_GRP);
124
125         assertThat(content, containsString(GET_LEAF1_TYPE_STRING));
126     }
127
128     @Test
129     public void testLeafListLeafrefPointsLeaf() throws IOException {
130         verifyReturnType(RESOLVED_LEAF_GRP, GET_LEAFLIST1_NAME, LIST_STRING_TYPE);
131
132         final String content = getFileContent(RESOLVED_LEAF_GRP);
133
134         assertOverriddenGetter(content, GET_LEAFLIST1_STRING);
135     }
136
137     @Test
138     public void testLeafListLeafrefPointsLeafList() throws IOException {
139         verifyReturnType(RESOLVED_LEAFLIST_GRP, GET_LEAFLIST1_NAME, LIST_STRING_TYPE);
140
141         final String content = getFileContent(RESOLVED_LEAFLIST_GRP);
142
143         assertOverriddenGetter(content, GET_LEAFLIST1_STRING);
144     }
145
146     @Test
147     public void testGroupingWhichInheritUnresolvedLeafrefAndDoesNotDefineIt() throws IOException {
148         verifyMethodAbsence(TRANSITIVE_GROUP, GET_LEAF1_NAME);
149         verifyMethodAbsence(TRANSITIVE_GROUP, GET_LEAFLIST1_NAME);
150
151         final String content = getFileContent(TRANSITIVE_GROUP);
152
153         assertThat(content, not(containsString(GET_LEAF1_DECLARATION)));
154         assertThat(content, not(containsString(GET_LEAFLIST1_DECLARATION)));
155     }
156
157     @Test
158     public void testLeafrefWhichPointsBoolean() throws IOException {
159         verifyReturnType(UNRESOLVED_GROUPING, GET_LEAF1_NAME, Types.objectType());
160         verifyReturnType(BOOLEAN_CONT, GET_LEAF1_NAME, Types.BOOLEAN);
161
162         final String unresolvedGrouping = getFileContent(UNRESOLVED_GROUPING);
163         final String booleanCont = getFileContent(BOOLEAN_CONT);
164
165         assertNotOverriddenGetter(unresolvedGrouping, GET_LEAF1_TYPE_OBJECT);
166         assertThat(booleanCont, containsString(GET_LEAF1_DECLARATION));
167     }
168
169     @Test
170     public void testGroupingsUsageWhereLeafrefAlreadyResolved() throws IOException {
171         leafList1AndLeaf1Absence(BAR_CONT);
172         leafList1AndLeaf1Absence(BAR_LST);
173         leafList1AndLeaf1Absence(BAZ_GRP);
174     }
175
176     private void leafList1AndLeaf1Absence(final String typeName) throws IOException {
177         verifyMethodAbsence(typeName, GET_LEAF1_NAME);
178         verifyMethodAbsence(typeName, GET_LEAFLIST1_NAME);
179
180         final String content = getFileContent(typeName);
181
182         assertThat(content, not(containsString(GET_LEAF1_DECLARATION)));
183         assertThat(content, not(containsString(GET_LEAFLIST1_DECLARATION)));
184     }
185
186     private static void assertNotOverriddenGetter(final String fileContent, final String getterString) {
187         assertThat(fileContent, not(containsString("@Override" + System.lineSeparator() + getterString)));
188         assertThat(fileContent, containsString(getterString));
189     }
190
191     private static void assertOverriddenGetter(final String fileContent, final String getterString) {
192         assertThat(fileContent, containsString("@Override" + System.lineSeparator() + getterString));
193     }
194
195     @Test
196     public void barContBuilderDataObjectTest() throws IOException {
197         final File file = files.get(getJavaBuilderFileName(BAR_CONT));
198         final String content = Files.readString(file.toPath());
199
200         barContBuilderConstructorResolvedLeafGrpTest(file, content);
201         barContBuilderConstructorFooGrpTest(file, content);
202         barContBuilderFieldsFromTest(file, content);
203     }
204
205     private static void barContBuilderConstructorResolvedLeafGrpTest(final File file, final String content) {
206         FileSearchUtil.assertFileContainsConsecutiveLines(file, content,
207                 tab("public BarContBuilder(" + RESOLVED_LEAF_GRP_REF + " arg) {"),
208                 doubleTab("this._leaf1 = arg.getLeaf1();"),
209                 doubleTab("this._leafList1 = arg.getLeafList1();"),
210                 doubleTab("this._name = arg.getName();"),
211                 doubleTab(LEAF2_ASSIGNMENT),
212                 TAB_CLOSING_METHOD_BRACE);
213     }
214
215     private static void barContBuilderFieldsFromTest(final File file, final String content) {
216         FileSearchUtil.assertFileContainsConsecutiveLines(file, content,
217                 TAB_FIELDS_FROM_SIGNATURE,
218                 DTAB_INIT_IS_VALID_ARG_FALSE,
219                 doubleTab("if (arg instanceof " + FOO_GRP_REF + ") {"),
220                 tripleTab("this._leaf1 = CodeHelpers.checkFieldCast(String.class, \"leaf1\", "
221                     + ARG_AS_FOO_GRP + ".getLeaf1());"),
222                 tripleTab("this._leafList1 = CodeHelpers.checkListFieldCast(String.class, \"leafList1\", "
223                     + ARG_AS_FOO_GRP + ".getLeafList1());"),
224                 tripleTab("this._leaf2 = " + ARG_AS_FOO_GRP + ".getLeaf2();"),
225                 TTAB_SET_IS_VALID_ARG_TRUE,
226                 DTAB_CLOSING_METHOD_BRACE,
227                 doubleTab("if (arg instanceof " + RESOLVED_LEAF_GRP_REF + ") {"),
228                 tripleTab("this._name = ((" + RESOLVED_LEAF_GRP_REF + ")arg).getName();"),
229                 TTAB_SET_IS_VALID_ARG_TRUE,
230                 DTAB_CLOSING_METHOD_BRACE,
231                 doubleTab("CodeHelpers.validValue(isValidArg, arg, \"[" + FOO_GRP_REF + ", " + RESOLVED_LEAF_GRP_REF
232                     + "]\");"),
233                 TAB_CLOSING_METHOD_BRACE);
234     }
235
236     private static void barContBuilderConstructorFooGrpTest(final File file, final String content) {
237         FileSearchUtil.assertFileContainsConsecutiveLines(file, content,
238                 tab("public BarContBuilder(" + FOO_GRP_REF + " arg) {"),
239                 doubleTab("this._leaf1 = CodeHelpers.checkFieldCast(String.class, \"leaf1\", "
240                     + "arg.getLeaf1());"),
241                 doubleTab("this._leafList1 = CodeHelpers.checkListFieldCast(String.class, \"leafList1\", "
242                     + "arg.getLeafList1());"),
243                 doubleTab(LEAF2_ASSIGNMENT),
244                 TAB_CLOSING_METHOD_BRACE);
245     }
246
247     @Test
248     public void booleanContBuilderDataObjectTest() throws IOException {
249         final File file = files.get(getJavaBuilderFileName(BOOLEAN_CONT));
250         final String content = Files.readString(file.toPath());
251
252         booleanContBuilderFieldsFromTest(file, content);
253         booleanContBuilderConstructorTest(file, content);
254     }
255
256     private static void booleanContBuilderFieldsFromTest(final File file, final String content) {
257         FileSearchUtil.assertFileContainsConsecutiveLines(file, content,
258                 TAB_FIELDS_FROM_SIGNATURE,
259                 DTAB_INIT_IS_VALID_ARG_FALSE,
260                 doubleTab("if (arg instanceof " + UNRESOLVED_GROUPING_REF + ") {"),
261                 tripleTab("this._leaf1 = CodeHelpers.checkFieldCast(Boolean.class, \"leaf1\", (("
262                     + UNRESOLVED_GROUPING_REF + ")arg).getLeaf1());"),
263                 TTAB_SET_IS_VALID_ARG_TRUE,
264                 DTAB_CLOSING_METHOD_BRACE,
265                 doubleTab("CodeHelpers.validValue(isValidArg, arg, \"[" + UNRESOLVED_GROUPING_REF + "]\");"),
266                 TAB_CLOSING_METHOD_BRACE);
267     }
268
269     private static void booleanContBuilderConstructorTest(final File file, final String content) {
270         FileSearchUtil.assertFileContainsConsecutiveLines(file, content,
271                 tab("public BooleanContBuilder(" + UNRESOLVED_GROUPING_REF + " arg) {"),
272                 doubleTab("this._leaf1 = CodeHelpers.checkFieldCast(Boolean.class, \"leaf1\", "
273                     + "arg.getLeaf1());"),
274                 TAB_CLOSING_METHOD_BRACE);
275     }
276
277     private static String getJavaFileName(final String name) {
278         return name + ".java";
279     }
280
281     private static String getJavaBuilderFileName(final String name) {
282         return getJavaFileName(name + "Builder");
283     }
284
285     private String getFileContent(final String fileName) throws IOException {
286         final File file = files.get(getJavaFileName(fileName));
287         assertNotNull(Files.lines(file.toPath()).findFirst());
288         final String content = Files.readString(Path.of(file.getAbsolutePath()));
289         assertNotNull(content);
290         return content;
291     }
292
293     private void verifyMethodAbsence(final String typeName, final String getterName) {
294         verifyReturnType(typeName, getterName, null);
295     }
296
297     private void verifyReturnType(final String typeName, final String getterName, final Type returnType) {
298         final GeneratedType generated = typeByName(types, typeName);
299         assertNotNull(generated);
300         assertEquals(returnType, returnTypeByMethodName(generated, getterName));
301     }
302
303     private static GeneratedType typeByName(final List<GeneratedType> types, final String name) {
304         for (final GeneratedType type : types) {
305             if (type.getName().equals(name)) {
306                 return type;
307             }
308         }
309         return null;
310     }
311
312     private static Type returnTypeByMethodName(final GeneratedType type, final String name) {
313         for (final MethodSignature m : type.getMethodDefinitions()) {
314             if (m.getName().equals(name)) {
315                 return m.getReturnType();
316             }
317         }
318         return null;
319     }
320 }