Skip to content

Commit a8498c3

Browse files
fix(protocol): fix random generation of CommandDistributionRecords
The CommandDistributionRecord contains a ValueType and a UnifiedRecordValue. We must make sure during the random generation of this record that the UnifiedRecordValue matches the ValueType. This is achieved by creating a custom randomizer for this specific class. A downside of this implementation is that the ProtocolFactory is now aware of this specific Record. When we make changes in the Record we must change them in this randomizer as well.
1 parent aa17b0f commit a8498c3

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

protocol-impl/src/main/java/io/camunda/zeebe/protocol/impl/record/value/distribution/CommandDistributionRecord.java

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public final class CommandDistributionRecord extends UnifiedRecordValue
3434
RECORDS_BY_TYPE.put(ValueType.DEPLOYMENT, DeploymentRecord::new);
3535
}
3636

37+
/*
38+
NOTE! When adding a new property here it must also be added to the ProtocolFactory! This class
39+
contains a randomizer implementation which is used to generate a random
40+
CommandDistributionRecord. The new property must be added there. Without it we won't generate a
41+
complete record.
42+
*/
3743
private final IntegerProperty partitionIdProperty = new IntegerProperty("partitionId");
3844
private final EnumProperty<ValueType> valueTypeProperty =
3945
new EnumProperty<>("valueType", ValueType.class);

protocol-jackson/src/main/java/io/camunda/zeebe/protocol/jackson/RecordMixin.java

+4
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@ abstract class RecordMixin<T extends RecordValue> {
3434
@JsonTypeInfo(use = Id.CUSTOM, include = As.EXTERNAL_PROPERTY, property = "valueType")
3535
@JsonTypeIdResolver(RecordValueTypeIdResolver.class)
3636
private T value;
37+
38+
@JsonTypeInfo(use = Id.CUSTOM, include = As.EXTERNAL_PROPERTY, property = "valueType")
39+
@JsonTypeIdResolver(RecordValueTypeIdResolver.class)
40+
private T commandValue;
3741
}

protocol-jackson/src/main/java/io/camunda/zeebe/protocol/jackson/ZeebeProtocolModule.java

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.databind.module.SimpleModule;
1212
import io.camunda.zeebe.protocol.record.ImmutableRecord;
1313
import io.camunda.zeebe.protocol.record.Record;
14+
import io.camunda.zeebe.protocol.record.value.ImmutableCommandDistributionRecordValue;
1415

1516
/**
1617
* A Jackson module which enables your {@link ObjectMapper} to serialize and deserialize Zeebe
@@ -34,6 +35,7 @@
3435
public final class ZeebeProtocolModule extends SimpleModule {
3536
public ZeebeProtocolModule() {
3637
setMixInAnnotation(ImmutableRecord.Builder.class, RecordMixin.class);
38+
setMixInAnnotation(ImmutableCommandDistributionRecordValue.Builder.class, RecordMixin.class);
3739
}
3840

3941
@Override

protocol-test-util/src/main/java/io/camunda/zeebe/test/broker/protocol/ProtocolFactory.java

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.camunda.zeebe.protocol.record.RecordValue;
1616
import io.camunda.zeebe.protocol.record.ValueType;
1717
import io.camunda.zeebe.protocol.record.ValueTypeMapping;
18+
import io.camunda.zeebe.protocol.record.value.ImmutableCommandDistributionRecordValue;
1819
import io.github.classgraph.ClassGraph;
1920
import io.github.classgraph.ClassInfo;
2021
import io.github.classgraph.ClassInfoList;
@@ -231,6 +232,18 @@ private void registerRandomizers() {
231232
final var recordTypes = EnumSet.complementOf(excludedRecordTypes);
232233
randomizerRegistry.registerRandomizer(
233234
RecordType.class, new EnumRandomizer<>(getSeed(), recordTypes.toArray(RecordType[]::new)));
235+
236+
randomizerRegistry.registerRandomizer(
237+
ImmutableCommandDistributionRecordValue.class,
238+
() -> {
239+
final var valueType = random.nextObject(ValueType.class);
240+
final var typeInfo = ValueTypeMapping.get(valueType);
241+
return ImmutableCommandDistributionRecordValue.builder()
242+
.withPartitionId(random.nextInt())
243+
.withValueType(valueType)
244+
.withCommandValue(generateObject(typeInfo.getValueClass()))
245+
.build();
246+
});
234247
}
235248

236249
private void registerProtocolType(final ClassInfo abstractType) {

0 commit comments

Comments
 (0)