Skip to content

Commit

Permalink
feat: use StructKeys with missing properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pcvolkmer committed Aug 16, 2024
1 parent 7911bb5 commit ba3cdc3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.miracum.streams.ume.obdstofhir.serde;

import java.nio.charset.StandardCharsets;
import java.util.StringJoiner;
import java.util.regex.Pattern;
import org.apache.kafka.common.serialization.Deserializer;
import org.apache.kafka.common.serialization.Serde;
Expand All @@ -11,19 +12,26 @@ public class StructKeyStringSerde implements Serde<StructKey> {

private static final Pattern structPattern =
Pattern.compile(
"Struct\\{REFERENZ_NUMMER=\"(?<referenznummer>.*)\",\\s*TUMOR_ID=\"(?<tumorid>.*)\"\\}");
"Struct\\{(REFERENZ_NUMMER=\"(?<referenznummer>[^\"]*)\")?,?\\s*(TUMOR_ID=\"(?<tumorid>[^\"]*)\")?\\}");

@Override
public Serializer<StructKey> serializer() {
return (s, structKey) -> {
if (null == structKey) {
return null;
}
return String.format(
"Struct{REFERENZ_NUMMER=\"%s\",TUMOR_ID=\"%s\"}",
null == structKey.referenzNummer() ? "" : structKey.referenzNummer(),
null == structKey.tumorId() ? "" : structKey.tumorId())
.getBytes(StandardCharsets.UTF_8);

var values = new StringJoiner(",");

if (null != structKey.referenzNummer()) {
values.add(String.format("REFERENZ_NUMMER=\"%s\"", structKey.referenzNummer()));
}

if (null != structKey.tumorId()) {
values.add(String.format("TUMOR_ID=\"%s\"", structKey.tumorId()));
}

return String.format("Struct{%s}", values).getBytes(StandardCharsets.UTF_8);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ void shouldSerializeStructKey() {
"Struct{REFERENZ_NUMMER=\"01234\",TUMOR_ID=\"1\"}".getBytes(StandardCharsets.UTF_8));
}

@Test
void shouldSerializePartialStructKeyWithRefNumOnly() {
var actual = serde.serializer().serialize("any", new StructKey("01234", null));
assertThat(actual)
.isEqualTo("Struct{REFERENZ_NUMMER=\"01234\"}".getBytes(StandardCharsets.UTF_8));
}

@Test
void shouldSerializeStructKeyWithTumIdOnly() {
var actual = serde.serializer().serialize("any", new StructKey(null, "1"));
assertThat(actual).isEqualTo("Struct{TUMOR_ID=\"1\"}".getBytes(StandardCharsets.UTF_8));
}

@Test
void shouldDeserializeStructKey() {
var actual =
Expand All @@ -36,6 +49,25 @@ void shouldDeserializeStructKey() {
assertThat(actual).isEqualTo(new StructKey("01234", "1"));
}

@Test
void shouldDeserializePartialStructKeyWithRefNumOnly() {
var actual =
serde
.deserializer()
.deserialize(
"any", "Struct{REFERENZ_NUMMER=\"01234\"}".getBytes(StandardCharsets.UTF_8));
assertThat(actual).isEqualTo(new StructKey("01234", null));
}

@Test
void shouldDeserializePartialStructKeyWithTumIdOnly() {
var actual =
serde
.deserializer()
.deserialize("any", "Struct{TUMOR_ID=\"1\"}".getBytes(StandardCharsets.UTF_8));
assertThat(actual).isEqualTo(new StructKey(null, "1"));
}

@Test
void shouldUseLombokBuilderForStructKeyRecord() {
var expected = new StructKey("01234", "1");
Expand Down

0 comments on commit ba3cdc3

Please sign in to comment.