-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LC-171 - Ensure Date support in cloud connectors (#47)
- Loading branch information
1 parent
6db51c8
commit bc0ba7f
Showing
9 changed files
with
317 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...c/test/scala/io/lenses/streamreactor/connect/cloud/common/sink/conversion/TimeUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.lenses.streamreactor.connect.cloud.common.sink.conversion | ||
|
||
import java.time.temporal.ChronoUnit | ||
import java.time.LocalDate | ||
import java.time.ZoneId | ||
import java.util.Calendar | ||
import java.util.Date | ||
|
||
object TimeUtils { | ||
|
||
def dateWithTimeFieldsOnly(hour: Int, minute: Int, second: Int, millis: Int): Date = { | ||
val calendar = Calendar.getInstance() | ||
|
||
// Set date fields to epoch start (January 1, 1970) | ||
calendar.set(Calendar.YEAR, 1970) | ||
calendar.set(Calendar.MONTH, Calendar.JANUARY) | ||
calendar.set(Calendar.DAY_OF_MONTH, 1) | ||
|
||
// Set time fields | ||
calendar.set(Calendar.HOUR_OF_DAY, hour) | ||
calendar.set(Calendar.MINUTE, minute) | ||
calendar.set(Calendar.SECOND, second) | ||
calendar.set(Calendar.MILLISECOND, millis) | ||
|
||
calendar.getTime | ||
} | ||
|
||
def toLocalDate(date: Date): LocalDate = | ||
date.toInstant.atZone(ZoneId.systemDefault).toLocalDate | ||
|
||
def daysSinceEpoch(date: Date): Long = { | ||
val localDate = toLocalDate(date) | ||
val epoch = LocalDate.ofEpochDay(0) | ||
ChronoUnit.DAYS.between(epoch, localDate) | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...o/lenses/streamreactor/connect/cloud/common/sink/conversion/ToAvroDataConverterTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.lenses.streamreactor.connect.cloud.common.sink.conversion | ||
|
||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.DateSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.TimeSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.TimestampSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.sink.conversion.TimeUtils.dateWithTimeFieldsOnly | ||
import io.lenses.streamreactor.connect.cloud.common.sink.conversion.TimeUtils.daysSinceEpoch | ||
import org.scalatest.funsuite.AnyFunSuiteLike | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Date | ||
|
||
class ToAvroDataConverterTest extends AnyFunSuiteLike with Matchers { | ||
|
||
test("should convert date") { | ||
val date = Date.from(Instant.now().truncatedTo(ChronoUnit.DAYS)) | ||
val daysSince: Long = daysSinceEpoch(date) | ||
val converted = ToAvroDataConverter.convertToGenericRecord(DateSinkData(date)) | ||
checkValueAndSchema(converted, daysSince) | ||
} | ||
|
||
test("should convert time") { | ||
val asDate: Date = dateWithTimeFieldsOnly(12, 30, 45, 450) | ||
val converted = ToAvroDataConverter.convertToGenericRecord(TimeSinkData(asDate)) | ||
checkValueAndSchema(converted, asDate.getTime) | ||
} | ||
|
||
test("should convert timestamp") { | ||
val date = Date.from(Instant.now()) | ||
val converted = ToAvroDataConverter.convertToGenericRecord(TimestampSinkData(date)) | ||
checkValueAndSchema(converted, date.getTime) | ||
} | ||
|
||
private def checkValueAndSchema(converted: Any, expectedValue: Long): Any = | ||
converted match { | ||
case nonRecordContainer: Long => | ||
nonRecordContainer should be(expectedValue) | ||
case _ => fail("not a non-record container") | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
...o/lenses/streamreactor/connect/cloud/common/sink/conversion/ToJsonDataConverterTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2017-2024 Lenses.io Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.lenses.streamreactor.connect.cloud.common.sink.conversion | ||
|
||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.DateSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.TimeSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.formats.writer.TimestampSinkData | ||
import io.lenses.streamreactor.connect.cloud.common.model.Topic | ||
import io.lenses.streamreactor.connect.cloud.common.sink.conversion.TimeUtils.dateWithTimeFieldsOnly | ||
import io.lenses.streamreactor.connect.cloud.common.sink.conversion.TimeUtils.daysSinceEpoch | ||
import org.apache.kafka.connect.json.DecimalFormat | ||
import org.apache.kafka.connect.json.JsonConverter | ||
import org.apache.kafka.connect.json.JsonConverterConfig | ||
import org.scalatest.funsuite.AnyFunSuiteLike | ||
import org.scalatest.matchers.should.Matchers | ||
|
||
import java.time.Instant | ||
import java.time.temporal.ChronoUnit | ||
import java.util.Date | ||
import scala.jdk.CollectionConverters.MapHasAsJava | ||
|
||
class ToJsonDataConverterTest extends AnyFunSuiteLike with Matchers { | ||
|
||
private val ExampleTopic: Topic = Topic("myTopic") | ||
|
||
private val Converter = new JsonConverter() | ||
Converter.configure( | ||
Map("schemas.enable" -> "false", JsonConverterConfig.DECIMAL_FORMAT_CONFIG -> DecimalFormat.NUMERIC.name()).asJava, | ||
false, | ||
) | ||
|
||
test("should convert date") { | ||
val date = Date.from(Instant.now().truncatedTo(ChronoUnit.DAYS)) | ||
val daysSince: Long = daysSinceEpoch(date) | ||
val converted = ToJsonDataConverter.convertMessageValueToByteArray(Converter, ExampleTopic, DateSinkData(date)) | ||
checkValueAndSchema(converted, daysSince.toString) | ||
} | ||
|
||
test("should convert time") { | ||
val asDate: Date = dateWithTimeFieldsOnly(12, 30, 45, 450) | ||
val converted = ToJsonDataConverter.convertMessageValueToByteArray(Converter, ExampleTopic, TimeSinkData(asDate)) | ||
checkValueAndSchema(converted, asDate.getTime.toString) | ||
} | ||
|
||
test("should convert timestamp") { | ||
val date = Date.from(Instant.now()) | ||
val converted = ToJsonDataConverter.convertMessageValueToByteArray(Converter, ExampleTopic, TimestampSinkData(date)) | ||
checkValueAndSchema(converted, date.getTime.toString) | ||
} | ||
|
||
private def checkValueAndSchema(converted: Any, expectedValue: String): Any = | ||
converted match { | ||
case nonRecordContainer: Array[Byte] => | ||
new String(nonRecordContainer) should be(expectedValue) | ||
case x => fail(s"not a byte array ($converted) but a ${x.getClass.getName}") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.