Skip to content

Commit 60a496d

Browse files
committed
add registered schema type.
1 parent fa3072f commit 60a496d

File tree

2 files changed

+256
-0
lines changed

2 files changed

+256
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
package io.kurrent.dbclient.v2;
2+
3+
import java.time.OffsetDateTime;
4+
import java.util.Objects;
5+
6+
/**
7+
* Represents a registered schema with its metadata.
8+
*/
9+
public class RegisteredSchema {
10+
/**
11+
* Represents an empty or unspecified registered schema.
12+
*/
13+
public static final RegisteredSchema NONE = new RegisteredSchema();
14+
15+
private final String schemaName;
16+
private final SchemaDataFormat dataFormat;
17+
private final String schemaVersionId;
18+
private final String definition;
19+
private final int versionNumber;
20+
private final OffsetDateTime createdAt;
21+
22+
/**
23+
* Creates a default instance with null or default values.
24+
*/
25+
private RegisteredSchema() {
26+
this.schemaName = null;
27+
this.dataFormat = SchemaDataFormat.UNSPECIFIED;
28+
this.schemaVersionId = null;
29+
this.definition = null;
30+
this.versionNumber = 0;
31+
this.createdAt = OffsetDateTime.now();
32+
}
33+
34+
/**
35+
* Creates a new instance of RegisteredSchema with the specified values.
36+
*
37+
* @param schemaName The name of the schema.
38+
* @param dataFormat The data format of the schema.
39+
* @param schemaVersionId The version ID of the schema.
40+
* @param definition The definition of the schema.
41+
* @param versionNumber The version number of the schema.
42+
* @param createdAt The creation timestamp of the schema.
43+
*/
44+
public RegisteredSchema(
45+
String schemaName,
46+
SchemaDataFormat dataFormat,
47+
String schemaVersionId,
48+
String definition,
49+
int versionNumber,
50+
OffsetDateTime createdAt) {
51+
this.schemaName = schemaName;
52+
this.dataFormat = dataFormat;
53+
this.schemaVersionId = schemaVersionId;
54+
this.definition = definition;
55+
this.versionNumber = versionNumber;
56+
this.createdAt = createdAt;
57+
}
58+
59+
/**
60+
* Gets the schema name.
61+
*
62+
* @return The schema name.
63+
*/
64+
public String getSchemaName() {
65+
return schemaName;
66+
}
67+
68+
/**
69+
* Gets the data format.
70+
*
71+
* @return The data format.
72+
*/
73+
public SchemaDataFormat getDataFormat() {
74+
return dataFormat;
75+
}
76+
77+
/**
78+
* Gets the schema version ID.
79+
*
80+
* @return The schema version ID.
81+
*/
82+
public String getSchemaVersionId() {
83+
return schemaVersionId;
84+
}
85+
86+
/**
87+
* Gets the schema definition.
88+
*
89+
* @return The schema definition.
90+
*/
91+
public String getDefinition() {
92+
return definition;
93+
}
94+
95+
/**
96+
* Gets the version number.
97+
*
98+
* @return The version number.
99+
*/
100+
public int getVersionNumber() {
101+
return versionNumber;
102+
}
103+
104+
/**
105+
* Gets the creation timestamp.
106+
*
107+
* @return The creation timestamp.
108+
*/
109+
public OffsetDateTime getCreatedAt() {
110+
return createdAt;
111+
}
112+
113+
/**
114+
* Converts this registered schema to a SchemaInfo object.
115+
*
116+
* @return A new SchemaInfo instance.
117+
*/
118+
public SchemaInfo toSchemaInfo() {
119+
return new SchemaInfo(schemaName, dataFormat);
120+
}
121+
122+
@Override
123+
public boolean equals(Object o) {
124+
if (this == o) return true;
125+
if (o == null || getClass() != o.getClass()) return false;
126+
RegisteredSchema that = (RegisteredSchema) o;
127+
return versionNumber == that.versionNumber &&
128+
Objects.equals(schemaName, that.schemaName) &&
129+
dataFormat == that.dataFormat &&
130+
Objects.equals(schemaVersionId, that.schemaVersionId) &&
131+
Objects.equals(definition, that.definition) &&
132+
Objects.equals(createdAt, that.createdAt);
133+
}
134+
135+
@Override
136+
public int hashCode() {
137+
return Objects.hash(schemaName, dataFormat, schemaVersionId, definition, versionNumber, createdAt);
138+
}
139+
140+
@Override
141+
public String toString() {
142+
return "RegisteredSchema{" +
143+
"schemaName='" + schemaName + '\'' +
144+
", dataFormat=" + dataFormat +
145+
", schemaVersionId='" + schemaVersionId + '\'' +
146+
", definition='" + definition + '\'' +
147+
", versionNumber=" + versionNumber +
148+
", createdAt=" + createdAt +
149+
'}';
150+
}
151+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.time.OffsetDateTime;
7+
8+
public class RegisteredSchemaTests {
9+
10+
@Test
11+
public void testNoneRegisteredSchema() {
12+
RegisteredSchema none = RegisteredSchema.NONE;
13+
14+
Assertions.assertNull(none.getSchemaName());
15+
Assertions.assertEquals(SchemaDataFormat.UNSPECIFIED, none.getDataFormat());
16+
Assertions.assertNull(none.getSchemaVersionId());
17+
Assertions.assertNull(none.getDefinition());
18+
Assertions.assertEquals(0, none.getVersionNumber());
19+
Assertions.assertNotNull(none.getCreatedAt());
20+
}
21+
22+
@Test
23+
public void testRegisteredSchemaConstructor() {
24+
String schemaName = "test-schema";
25+
SchemaDataFormat dataFormat = SchemaDataFormat.JSON;
26+
String schemaVersionId = "schema-version-123";
27+
String definition = "{\"type\":\"object\",\"properties\":{}}";
28+
int versionNumber = 1;
29+
OffsetDateTime createdAt = OffsetDateTime.now();
30+
31+
RegisteredSchema schema = new RegisteredSchema(
32+
schemaName,
33+
dataFormat,
34+
schemaVersionId,
35+
definition,
36+
versionNumber,
37+
createdAt
38+
);
39+
40+
Assertions.assertEquals(schemaName, schema.getSchemaName());
41+
Assertions.assertEquals(dataFormat, schema.getDataFormat());
42+
Assertions.assertEquals(schemaVersionId, schema.getSchemaVersionId());
43+
Assertions.assertEquals(definition, schema.getDefinition());
44+
Assertions.assertEquals(versionNumber, schema.getVersionNumber());
45+
Assertions.assertEquals(createdAt, schema.getCreatedAt());
46+
}
47+
48+
@Test
49+
public void testToSchemaInfo() {
50+
String schemaName = "test-schema";
51+
SchemaDataFormat dataFormat = SchemaDataFormat.JSON;
52+
53+
RegisteredSchema schema = new RegisteredSchema(
54+
schemaName,
55+
dataFormat,
56+
"schema-version-123",
57+
"{\"type\":\"object\",\"properties\":{}}",
58+
1,
59+
OffsetDateTime.now()
60+
);
61+
62+
SchemaInfo schemaInfo = schema.toSchemaInfo();
63+
64+
Assertions.assertEquals(schemaName, schemaInfo.getSchemaName());
65+
Assertions.assertEquals(dataFormat, schemaInfo.getDataFormat());
66+
}
67+
68+
@Test
69+
public void testEqualsAndHashCode() {
70+
OffsetDateTime now = OffsetDateTime.now();
71+
72+
RegisteredSchema schema1 = new RegisteredSchema(
73+
"test-schema",
74+
SchemaDataFormat.JSON,
75+
"schema-version-123",
76+
"{\"type\":\"object\",\"properties\":{}}",
77+
1,
78+
now
79+
);
80+
81+
RegisteredSchema schema2 = new RegisteredSchema(
82+
"test-schema",
83+
SchemaDataFormat.JSON,
84+
"schema-version-123",
85+
"{\"type\":\"object\",\"properties\":{}}",
86+
1,
87+
now
88+
);
89+
90+
RegisteredSchema schema3 = new RegisteredSchema(
91+
"different-schema",
92+
SchemaDataFormat.PROTOBUF,
93+
"schema-version-456",
94+
"message Test {}",
95+
2,
96+
now
97+
);
98+
99+
Assertions.assertEquals(schema1, schema2);
100+
Assertions.assertEquals(schema1.hashCode(), schema2.hashCode());
101+
102+
Assertions.assertNotEquals(schema1, schema3);
103+
Assertions.assertNotEquals(schema1.hashCode(), schema3.hashCode());
104+
}
105+
}

0 commit comments

Comments
 (0)