Skip to content

Commit 2c62f53

Browse files
authored
Additional unit tests for AbstractOozieJobsTask (#1039)
* Add more tests * Use more descriptive names
1 parent 9a5ffbf commit 2c62f53

File tree

2 files changed

+83
-8
lines changed

2 files changed

+83
-8
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/hadoop/oozie/AbstractOozieJobsTask.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static java.nio.charset.StandardCharsets.UTF_8;
2020

21+
import com.fasterxml.jackson.core.JsonProcessingException;
2122
import com.fasterxml.jackson.databind.ObjectMapper;
2223
import com.google.common.annotations.VisibleForTesting;
2324
import com.google.common.base.Preconditions;
@@ -159,19 +160,26 @@ private static Object[] toCSVRecord(Object job, ImmutableList<String> header) th
159160
Object[] record = new Object[header.size()];
160161
BeanWrapper jobObjectWrapper = new BeanWrapperImpl(job);
161162
for (int i = 0; i < header.size(); i++) {
162-
record[i] = jobObjectWrapper.getPropertyValue(header.get(i));
163-
if (record[i] != null && record[i] instanceof Date) {
164-
// avoid date formats complexity and use milliseconds
165-
record[i] = ((Date) record[i]).getTime();
166-
}
167-
if (record[i] != null && record[i] instanceof List) {
168-
// write Actions arrays as json string in csv
169-
record[i] = objectMapper.writeValueAsString(record[i]);
163+
Object property = jobObjectWrapper.getPropertyValue(header.get(i));
164+
if (property != null) {
165+
record[i] = toRecordProperty(property);
170166
}
171167
}
172168
return record;
173169
}
174170

171+
static Object toRecordProperty(Object property) throws JsonProcessingException {
172+
if (property instanceof Date) {
173+
// avoid date formats complexity and use milliseconds
174+
return ((Date) property).getTime();
175+
} else if (property instanceof List) {
176+
// write Actions arrays as json string in csv
177+
return objectMapper.writeValueAsString(property);
178+
} else {
179+
return property;
180+
}
181+
}
182+
175183
@VisibleForTesting
176184
ZonedDateTime getStartDate() {
177185
return startDate;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2022-2025 Google LLC
3+
* Copyright 2013-2021 CompilerWorks
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.google.edwmigration.dumper.application.dumper.connector.hadoop.oozie;
18+
19+
import static org.junit.Assert.assertEquals;
20+
21+
import com.google.common.collect.ImmutableList;
22+
import java.text.SimpleDateFormat;
23+
import java.util.Date;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
@RunWith(JUnit4.class)
29+
public class AbstractOozieJobsTaskTest {
30+
31+
static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
32+
33+
@Test
34+
public void toRecordProperty_dateArgument_convertsToLong() throws Exception {
35+
Date christmas = dateFormat.parse("2025-12-25");
36+
37+
Object result = AbstractOozieJobsTask.toRecordProperty(christmas);
38+
39+
assertEquals(Long.class, result.getClass());
40+
assertEquals("2025-12-25", convertDate(result));
41+
}
42+
43+
@Test
44+
public void toRecordProperty_listArgument_success() throws Exception {
45+
ImmutableList<String> list = ImmutableList.of("abcde", "fghi");
46+
47+
Object result = AbstractOozieJobsTask.toRecordProperty(list);
48+
49+
assertEquals("[\"abcde\",\"fghi\"]", result);
50+
}
51+
52+
@Test
53+
public void toRecordProperty_otherArgument_nothingChanges() throws Exception {
54+
Object value = new Object();
55+
56+
Object result = AbstractOozieJobsTask.toRecordProperty(value);
57+
58+
assertEquals(value, result);
59+
}
60+
61+
private static String convertDate(Object date) {
62+
if (date instanceof Long) {
63+
return dateFormat.format(new Date((Long) date));
64+
}
65+
throw new RuntimeException();
66+
}
67+
}

0 commit comments

Comments
 (0)