Skip to content

Commit 8fcdfe8

Browse files
committed
Add message type.
1 parent ac318be commit 8fcdfe8

4 files changed

Lines changed: 334 additions & 0 deletions

File tree

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import java.util.UUID;
4+
import java.util.function.Function;
5+
6+
/**
7+
* Represents a message with payload, metadata, and schema information.
8+
*/
9+
public final class Message {
10+
/**
11+
* An empty message instance.
12+
*/
13+
public static final Message EMPTY = new Message();
14+
15+
private final Object value;
16+
private final Metadata metadata;
17+
private final UUID recordId;
18+
private final SchemaDataFormat dataFormat;
19+
20+
/**
21+
* Initializes a new instance of the Message class with default values.
22+
*/
23+
public Message() {
24+
this(null, new Metadata(), UUID.randomUUID(), SchemaDataFormat.JSON);
25+
}
26+
27+
/**
28+
* Initializes a new instance of the Message class.
29+
*
30+
* @param value The message payload.
31+
* @param metadata The message metadata.
32+
* @param recordId The assigned record id.
33+
* @param dataFormat The format of the schema associated with the message.
34+
*/
35+
public Message(Object value, Metadata metadata, UUID recordId, SchemaDataFormat dataFormat) {
36+
this.value = value;
37+
this.metadata = metadata != null ? metadata : new Metadata();
38+
this.recordId = recordId != null ? recordId : UUID.randomUUID();
39+
this.dataFormat = dataFormat != null ? dataFormat : SchemaDataFormat.JSON;
40+
}
41+
42+
/**
43+
* Gets the message payload.
44+
*
45+
* @return The message payload.
46+
*/
47+
public Object getValue() {
48+
return value;
49+
}
50+
51+
/**
52+
* Gets the message metadata.
53+
*
54+
* @return The message metadata.
55+
*/
56+
public Metadata getMetadata() {
57+
return metadata;
58+
}
59+
60+
/**
61+
* Gets the assigned record id.
62+
*
63+
* @return The assigned record id.
64+
*/
65+
public UUID getRecordId() {
66+
return recordId;
67+
}
68+
69+
/**
70+
* Gets the format of the schema associated with the message.
71+
*
72+
* @return The schema data format.
73+
*/
74+
public SchemaDataFormat getDataFormat() {
75+
return dataFormat;
76+
}
77+
78+
/**
79+
* Creates a new Message builder.
80+
*
81+
* @return A new Message builder.
82+
*/
83+
public static Builder builder() {
84+
return new Builder(null, new Metadata(), null, SchemaDataFormat.JSON);
85+
}
86+
87+
/**
88+
* Builder for creating Message instances.
89+
*/
90+
public static class Builder {
91+
private final Object value;
92+
private final Metadata metadata;
93+
private final UUID recordId;
94+
private final SchemaDataFormat dataFormat;
95+
96+
private Builder(Object value, Metadata metadata, UUID recordId, SchemaDataFormat dataFormat) {
97+
this.value = value;
98+
this.metadata = metadata;
99+
this.recordId = recordId;
100+
this.dataFormat = dataFormat;
101+
}
102+
103+
/**
104+
* Sets the message payload.
105+
*
106+
* @param value The message payload.
107+
* @return This builder instance.
108+
*/
109+
public Builder value(Object value) {
110+
return new Builder(value, this.metadata, this.recordId, this.dataFormat);
111+
}
112+
113+
/**
114+
* Sets the message metadata.
115+
*
116+
* @param metadata The message metadata.
117+
* @return This builder instance.
118+
*/
119+
public Builder metadata(Metadata metadata) {
120+
assert metadata != null : "Metadata cannot be null";
121+
return new Builder(this.value, metadata, this.recordId, this.dataFormat);
122+
}
123+
124+
/**
125+
* Sets the assigned record id.
126+
*
127+
* @param recordId The assigned record id.
128+
* @return This builder instance.
129+
*/
130+
public Builder recordId(UUID recordId) {
131+
assert recordId != null : "Record ID cannot be null";
132+
return new Builder(this.value, this.metadata, recordId, this.dataFormat);
133+
}
134+
135+
/**
136+
* Sets the format of the schema associated with the message.
137+
*
138+
* @param dataFormat The schema data format.
139+
* @return This builder instance.
140+
*/
141+
public Builder dataFormat(SchemaDataFormat dataFormat) {
142+
return new Builder(this.value, this.metadata, this.recordId, dataFormat);
143+
}
144+
145+
public Builder when(boolean condition, Function<Builder, Builder> func) {
146+
return condition ? func.apply(this) : this;
147+
}
148+
149+
/**
150+
* Builds a new Message instance.
151+
*
152+
* @return A new Message instance.
153+
*/
154+
public Message build() {
155+
return new Message(this.value, this.metadata, this.recordId != null ? this.recordId : UUID.randomUUID(), this.dataFormat);
156+
}
157+
}
158+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
/**
4+
* Specifies the format of schema data.
5+
*/
6+
public enum SchemaDataFormat {
7+
/**
8+
* Unspecified format.
9+
*/
10+
UNSPECIFIED(0),
11+
12+
/**
13+
* JSON format.
14+
*/
15+
JSON(1),
16+
17+
/**
18+
* Protocol Buffers format.
19+
*/
20+
PROTOBUF(2),
21+
22+
/**
23+
* Apache Avro format.
24+
*/
25+
AVRO(3),
26+
27+
/**
28+
* Raw bytes format.
29+
*/
30+
BYTES(4);
31+
32+
private final int value;
33+
34+
SchemaDataFormat(int value) {
35+
this.value = value;
36+
}
37+
38+
/**
39+
* Gets the integer value of the enum.
40+
*
41+
* @return The integer value.
42+
*/
43+
public int getValue() {
44+
return value;
45+
}
46+
47+
/**
48+
* Gets a SchemaDataFormat from its integer value.
49+
*
50+
* @param value The integer value.
51+
* @return The corresponding SchemaDataFormat, or UNSPECIFIED if not found.
52+
*/
53+
public static SchemaDataFormat fromValue(int value) {
54+
for (SchemaDataFormat format : SchemaDataFormat.values()) {
55+
if (format.value == value) {
56+
return format;
57+
}
58+
}
59+
return UNSPECIFIED;
60+
}
61+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.UUID;
7+
8+
public class MessageTests {
9+
10+
@Test
11+
public void testEmptyMessage() {
12+
Message empty = Message.EMPTY;
13+
14+
Assertions.assertNull(empty.getValue());
15+
Assertions.assertNotNull(empty.getMetadata());
16+
Assertions.assertEquals(0, empty.getMetadata().size());
17+
Assertions.assertNotNull(empty.getRecordId());
18+
Assertions.assertEquals(SchemaDataFormat.JSON, empty.getDataFormat());
19+
}
20+
21+
@Test
22+
public void testMessageConstructor() {
23+
Metadata metadata = new Metadata();
24+
metadata.put("key1", "value1");
25+
26+
UUID recordId = UUID.randomUUID();
27+
28+
Message message = new Message("test payload", metadata, recordId, SchemaDataFormat.PROTOBUF);
29+
30+
Assertions.assertEquals("test payload", message.getValue());
31+
Assertions.assertSame(metadata, message.getMetadata());
32+
Assertions.assertEquals(1, message.getMetadata().size());
33+
Assertions.assertEquals("value1", message.getMetadata().get("key1"));
34+
Assertions.assertEquals(recordId, message.getRecordId());
35+
Assertions.assertEquals(SchemaDataFormat.PROTOBUF, message.getDataFormat());
36+
}
37+
38+
@Test
39+
public void testMessageBuilder() {
40+
Metadata metadata = new Metadata();
41+
metadata.put("key1", "value1");
42+
43+
UUID recordId = UUID.randomUUID();
44+
45+
Message message = Message.builder()
46+
.value("test payload")
47+
.metadata(metadata)
48+
.recordId(recordId)
49+
.dataFormat(SchemaDataFormat.AVRO)
50+
.build();
51+
52+
Assertions.assertEquals("test payload", message.getValue());
53+
Assertions.assertSame(metadata, message.getMetadata());
54+
Assertions.assertEquals(1, message.getMetadata().size());
55+
Assertions.assertEquals("value1", message.getMetadata().get("key1"));
56+
Assertions.assertEquals(recordId, message.getRecordId());
57+
Assertions.assertEquals(SchemaDataFormat.AVRO, message.getDataFormat());
58+
}
59+
60+
@Test
61+
public void testMessageBuilderWithDefaults() {
62+
Message message = Message.builder()
63+
.value("test payload")
64+
.build();
65+
66+
Assertions.assertEquals("test payload", message.getValue());
67+
Assertions.assertNotNull(message.getMetadata());
68+
Assertions.assertEquals(0, message.getMetadata().size());
69+
Assertions.assertNotNull(message.getRecordId());
70+
Assertions.assertEquals(SchemaDataFormat.JSON, message.getDataFormat());
71+
}
72+
73+
@Test
74+
public void testMessageBuilderWithNullValues() {
75+
Message message = Message.builder()
76+
.value(null)
77+
.build();
78+
79+
Assertions.assertNull(message.getValue());
80+
Assertions.assertNotNull(message.getMetadata());
81+
Assertions.assertEquals(0, message.getMetadata().size());
82+
Assertions.assertNotNull(message.getRecordId());
83+
Assertions.assertEquals(SchemaDataFormat.JSON, message.getDataFormat());
84+
}
85+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class SchemaDataFormatTests {
7+
8+
@Test
9+
public void testEnumValues() {
10+
// Verify enum values
11+
Assertions.assertEquals(0, SchemaDataFormat.UNSPECIFIED.getValue());
12+
Assertions.assertEquals(1, SchemaDataFormat.JSON.getValue());
13+
Assertions.assertEquals(2, SchemaDataFormat.PROTOBUF.getValue());
14+
Assertions.assertEquals(3, SchemaDataFormat.AVRO.getValue());
15+
Assertions.assertEquals(4, SchemaDataFormat.BYTES.getValue());
16+
}
17+
18+
@Test
19+
public void testFromValue() {
20+
// Verify fromValue method
21+
Assertions.assertEquals(SchemaDataFormat.UNSPECIFIED, SchemaDataFormat.fromValue(0));
22+
Assertions.assertEquals(SchemaDataFormat.JSON, SchemaDataFormat.fromValue(1));
23+
Assertions.assertEquals(SchemaDataFormat.PROTOBUF, SchemaDataFormat.fromValue(2));
24+
Assertions.assertEquals(SchemaDataFormat.AVRO, SchemaDataFormat.fromValue(3));
25+
Assertions.assertEquals(SchemaDataFormat.BYTES, SchemaDataFormat.fromValue(4));
26+
27+
// Test invalid value
28+
Assertions.assertEquals(SchemaDataFormat.UNSPECIFIED, SchemaDataFormat.fromValue(99));
29+
}
30+
}

0 commit comments

Comments
 (0)