Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIFI-2829: Fixed PutSQL time unit test. #2082

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public class PutSQL extends AbstractSessionFactoryProcessor {
private static final String FRAGMENT_INDEX_ATTR = FragmentAttributes.FRAGMENT_INDEX.key();
private static final String FRAGMENT_COUNT_ATTR = FragmentAttributes.FRAGMENT_COUNT.key();

private static final Pattern LONG_PATTERN = Pattern.compile("^\\d{1,19}$");
private static final Pattern LONG_PATTERN = Pattern.compile("^-?\\d{1,19}$");

@Override
protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
import java.sql.Types;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -60,7 +61,6 @@
import org.mockito.Mockito;

import javax.xml.bind.DatatypeConverter;
import org.junit.Ignore;

public class TestPutSQL {
private static final String createPersons = "CREATE TABLE PERSONS (id integer primary key, name varchar(100), code integer)";
Expand Down Expand Up @@ -460,7 +460,6 @@ public void testUsingTimestampValuesWithFormatAttribute() throws InitializationE
}
}

@Ignore("this test needs fixing due to TestPutSQL.testUsingDateTimeValuesWithFormatAttribute:551 expected:<1012608000000> but was:<1012521600000>")
@Test
public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationException, ProcessException, SQLException, IOException, ParseException {
final TestRunner runner = TestRunners.newTestRunner(PutSQL.class);
Expand All @@ -474,8 +473,8 @@ public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationEx
runner.enableControllerService(service);
runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");

final String dateStr = "2002-02-02";
final String timeStr = "12:02:02";
final String dateStr = "2002-03-04";
final String timeStr = "02:03:04";

final String timeFormatString = "HH:mm:ss";
final String dateFormatString ="yyyy-MM-dd";
Expand All @@ -492,19 +491,7 @@ public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationEx
final long expectedTimeInLong = expectedTime.getTime();
final long expectedDateInLong = expectedDate.getTime();

//test with time zone GMT to avoid negative value unmatched with long pattern problem.
SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString);
timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
java.util.Date parsedTimeGMT = timeFormat.parse(timeStr);

SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
java.util.Date parsedDateGMT = dateFormat.parse(dateStr);

Calendar gmtCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));


//test with ISO LOCAL format attribute
// test with ISO LOCAL format attribute
Map<String, String> attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", timeStr);
Expand All @@ -515,22 +502,31 @@ public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationEx

runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (1, ?, ?)".getBytes(), attributes);

//test Long pattern without format attribute
// Since Derby database which is used for unit test does not have timezone in DATE and TIME type,
// and PutSQL converts date string into long representation using local timezone,
// we need to use local timezone.
SimpleDateFormat timeFormat = new SimpleDateFormat(timeFormatString);
java.util.Date parsedLocalTime = timeFormat.parse(timeStr);

SimpleDateFormat dateFormat = new SimpleDateFormat(dateFormatString);
java.util.Date parsedLocalDate = dateFormat.parse(dateStr);

// test Long pattern without format attribute
attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", Long.toString(parsedTimeGMT.getTime()));
attributes.put("sql.args.1.value", Long.toString(parsedLocalTime.getTime()));
attributes.put("sql.args.2.type", String.valueOf(Types.DATE));
attributes.put("sql.args.2.value", Long.toString(parsedDateGMT.getTime()));
attributes.put("sql.args.2.value", Long.toString(parsedLocalDate.getTime()));

runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (2, ?, ?)".getBytes(), attributes);

//test with format attribute
// test with format attribute
attributes = new HashMap<>();
attributes.put("sql.args.1.type", String.valueOf(Types.TIME));
attributes.put("sql.args.1.value", "120202000");
attributes.put("sql.args.1.value", "020304000");
attributes.put("sql.args.1.format", "HHmmssSSS");
attributes.put("sql.args.2.type", String.valueOf(Types.DATE));
attributes.put("sql.args.2.value", "20020202");
attributes.put("sql.args.2.value", "20020304");
attributes.put("sql.args.2.format", "yyyyMMdd");

runner.enqueue("INSERT INTO TIMESTAMPTEST3 (ID, ts1, ts2) VALUES (3, ?, ?)".getBytes(), attributes);
Expand All @@ -549,8 +545,8 @@ public void testUsingDateTimeValuesWithFormatAttribute() throws InitializationEx

assertTrue(rs.next());
assertEquals(2, rs.getInt(1));
assertEquals(parsedTimeGMT.getTime(), rs.getTime(2).getTime());
assertEquals(parsedDateGMT.getTime(), rs.getDate(3,gmtCalendar).getTime());
assertEquals(parsedLocalTime.getTime(), rs.getTime(2).getTime());
assertEquals(parsedLocalDate.getTime(), rs.getDate(3).getTime());

assertTrue(rs.next());
assertEquals(3, rs.getInt(1));
Expand Down Expand Up @@ -692,11 +688,10 @@ public void testUsingTimeValuesEpochAndString() throws InitializationException,
runner.enableControllerService(service);
runner.setProperty(PutSQL.CONNECTION_POOL, "dbcp");

final String arg2TS = "00:01:01";
final String art3TS = "12:02:02";
final String arg2TS = "00:01:02";
final String art3TS = "02:03:04";
final String timeFormatString = "HH:mm:ss";
SimpleDateFormat dateFormat = new SimpleDateFormat(timeFormatString);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
java.util.Date parsedDate = dateFormat.parse(arg2TS);


Expand Down