-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Support json and binary avro values (#114)
- decode message header trying both formats - message payloads are then decoded using the advertised format in the header
- Loading branch information
Showing
4 changed files
with
146 additions
and
9 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
74 changes: 74 additions & 0 deletions
74
modules/events/src/test/scala/io/renku/search/events/MessageHeaderSpec.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,74 @@ | ||
/* | ||
* Copyright 2024 Swiss Data Science Center (SDSC) | ||
* A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and | ||
* Eidgenössische Technische Hochschule Zürich (ETHZ). | ||
* | ||
* 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.renku.search.events | ||
|
||
import java.time.Instant | ||
|
||
import io.renku.avro.codec.AvroEncoder | ||
import io.renku.avro.codec.AvroWriter | ||
import io.renku.avro.codec.all.given | ||
import io.renku.events.Header as HeaderV2 | ||
import io.renku.events.v1.Header as HeaderV1 | ||
import io.renku.search.model.Timestamp | ||
import munit.* | ||
import scodec.bits.ByteVector | ||
|
||
class MessageHeaderSpec extends FunSuite: | ||
|
||
DataContentType.values.foreach { ct => | ||
test(s"read v1 header: $ct"): | ||
val now = Instant.now | ||
val hv1 = HeaderV1("the-source", "type2", ct.mimeType, "v1", now, "req1") | ||
val bv1 = AvroWriter(HeaderV1.SCHEMA$).writeData(ct, Seq(hv1)) | ||
val h = MessageHeader.fromByteVector(bv1).fold(throw _, identity) | ||
assertEquals( | ||
h, | ||
MessageHeader( | ||
MessageSource("the-source"), | ||
ct, | ||
SchemaVersion.V1, | ||
Timestamp(now), | ||
RequestId("req1") | ||
) | ||
) | ||
} | ||
|
||
DataContentType.values.foreach { ct => | ||
test(s"read v2 header: $ct"): | ||
val now = Instant.now | ||
val hv2 = HeaderV2("the-source", "type2", ct.mimeType, "v1", now, "req1") | ||
val bv1 = AvroWriter(HeaderV2.SCHEMA$).writeData(ct, Seq(hv2)) | ||
val h = MessageHeader.fromByteVector(bv1).fold(throw _, identity) | ||
assertEquals( | ||
h, | ||
MessageHeader( | ||
MessageSource("the-source"), | ||
ct, | ||
SchemaVersion.V1, | ||
Timestamp(now), | ||
RequestId("req1") | ||
) | ||
) | ||
} | ||
|
||
extension (self: AvroWriter) | ||
def writeData[A: AvroEncoder](ct: DataContentType, values: Seq[A]): ByteVector = | ||
ct match | ||
case DataContentType.Binary => self.write[A](values) | ||
case DataContentType.Json => self.writeJson[A](values) |
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