2 * Copyright (c) 2020 Pantheon Technologies, s.r.o. 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.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;
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;
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;
38 public class SpecializingLeafrefTest extends BaseCompilationTest {
39 private static final Type LIST_STRING_TYPE = Types.listTypeFor(Types.STRING);
41 public static final String BAR_CONT = "BarCont";
42 public static final String BOOLEAN_CONT = "BooleanCont";
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";
52 private static final String GET_LEAF1_NAME = "getLeaf1";
53 private static final String GET_LEAFLIST1_NAME = "getLeafList1";
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();";
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;
66 private static final String FOO_GRP_REF = "FooGrp";
67 private static final String RESOLVED_LEAF_GRP_REF = "ResolvedLeafGrp";
69 private static final String UNRESOLVED_GROUPING_REF =
72 private static final String ARG_AS_FOO_GRP = "((" + FOO_GRP_REF + ")arg)";
74 private static final String LEAF2_ASSIGNMENT = "this._leaf2 = arg.getLeaf2();";
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;";
80 private File sourcesOutputDir;
81 private File compiledOutputDir;
82 private List<GeneratedType> types;
83 private Map<String, File> files;
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);
96 CompilationTestUtils.cleanUp(sourcesOutputDir, compiledOutputDir);
100 public void testGroupingWithUnresolvedLeafRefs() throws IOException {
101 verifyReturnType(FOO_GRP, GET_LEAF1_NAME, Types.objectType());
102 verifyReturnType(FOO_GRP, GET_LEAFLIST1_NAME, Types.listTypeWildcard());
104 final String content = getFileContent(FOO_GRP);
106 assertThat(content, containsString(GET_LEAF1_TYPE_OBJECT));
107 assertThat(content, containsString(GET_LEAFLIST1_WILDCARD));
111 public void testLeafLeafrefPointsLeaf() throws IOException {
112 verifyReturnType(RESOLVED_LEAF_GRP, GET_LEAF1_NAME, Types.STRING);
114 final String content = getFileContent(RESOLVED_LEAF_GRP);
116 assertThat(content, containsString(GET_LEAF1_TYPE_STRING));
120 public void testLeafLeafrefPointsLeafList() throws IOException {
121 verifyReturnType(RESOLVED_LEAFLIST_GRP, GET_LEAF1_NAME, Types.STRING);
123 final String content = getFileContent(RESOLVED_LEAF_GRP);
125 assertThat(content, containsString(GET_LEAF1_TYPE_STRING));
129 public void testLeafListLeafrefPointsLeaf() throws IOException {
130 verifyReturnType(RESOLVED_LEAF_GRP, GET_LEAFLIST1_NAME, LIST_STRING_TYPE);
132 final String content = getFileContent(RESOLVED_LEAF_GRP);
134 assertOverriddenGetter(content, GET_LEAFLIST1_STRING);
138 public void testLeafListLeafrefPointsLeafList() throws IOException {
139 verifyReturnType(RESOLVED_LEAFLIST_GRP, GET_LEAFLIST1_NAME, LIST_STRING_TYPE);
141 final String content = getFileContent(RESOLVED_LEAFLIST_GRP);
143 assertOverriddenGetter(content, GET_LEAFLIST1_STRING);
147 public void testGroupingWhichInheritUnresolvedLeafrefAndDoesNotDefineIt() throws IOException {
148 verifyMethodAbsence(TRANSITIVE_GROUP, GET_LEAF1_NAME);
149 verifyMethodAbsence(TRANSITIVE_GROUP, GET_LEAFLIST1_NAME);
151 final String content = getFileContent(TRANSITIVE_GROUP);
153 assertThat(content, not(containsString(GET_LEAF1_DECLARATION)));
154 assertThat(content, not(containsString(GET_LEAFLIST1_DECLARATION)));
158 public void testLeafrefWhichPointsBoolean() throws IOException {
159 verifyReturnType(UNRESOLVED_GROUPING, GET_LEAF1_NAME, Types.objectType());
160 verifyReturnType(BOOLEAN_CONT, GET_LEAF1_NAME, Types.BOOLEAN);
162 final String unresolvedGrouping = getFileContent(UNRESOLVED_GROUPING);
163 final String booleanCont = getFileContent(BOOLEAN_CONT);
165 assertNotOverriddenGetter(unresolvedGrouping, GET_LEAF1_TYPE_OBJECT);
166 assertThat(booleanCont, containsString(GET_LEAF1_DECLARATION));
170 public void testGroupingsUsageWhereLeafrefAlreadyResolved() throws IOException {
171 leafList1AndLeaf1Absence(BAR_CONT);
172 leafList1AndLeaf1Absence(BAR_LST);
173 leafList1AndLeaf1Absence(BAZ_GRP);
176 private void leafList1AndLeaf1Absence(final String typeName) throws IOException {
177 verifyMethodAbsence(typeName, GET_LEAF1_NAME);
178 verifyMethodAbsence(typeName, GET_LEAFLIST1_NAME);
180 final String content = getFileContent(typeName);
182 assertThat(content, not(containsString(GET_LEAF1_DECLARATION)));
183 assertThat(content, not(containsString(GET_LEAFLIST1_DECLARATION)));
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));
191 private static void assertOverriddenGetter(final String fileContent, final String getterString) {
192 assertThat(fileContent, containsString("@Override" + System.lineSeparator() + getterString));
196 public void barContBuilderDataObjectTest() throws IOException {
197 final File file = files.get(getJavaBuilderFileName(BAR_CONT));
198 final String content = Files.readString(file.toPath());
200 barContBuilderConstructorResolvedLeafGrpTest(file, content);
201 barContBuilderConstructorFooGrpTest(file, content);
202 barContBuilderFieldsFromTest(file, content);
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);
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
233 TAB_CLOSING_METHOD_BRACE);
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);
248 public void booleanContBuilderDataObjectTest() throws IOException {
249 final File file = files.get(getJavaBuilderFileName(BOOLEAN_CONT));
250 final String content = Files.readString(file.toPath());
252 booleanContBuilderFieldsFromTest(file, content);
253 booleanContBuilderConstructorTest(file, content);
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);
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);
277 private static String getJavaFileName(final String name) {
278 return name + ".java";
281 private static String getJavaBuilderFileName(final String name) {
282 return getJavaFileName(name + "Builder");
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);
293 private void verifyMethodAbsence(final String typeName, final String getterName) {
294 verifyReturnType(typeName, getterName, null);
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));
303 private static GeneratedType typeByName(final List<GeneratedType> types, final String name) {
304 for (final GeneratedType type : types) {
305 if (type.getName().equals(name)) {
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();