diff --git a/packages/core/lib/messages/MetadataFiller.spec.ts b/packages/core/lib/messages/MetadataFiller.spec.ts new file mode 100644 index 00000000..43915b31 --- /dev/null +++ b/packages/core/lib/messages/MetadataFiller.spec.ts @@ -0,0 +1,59 @@ +import { CommonMetadataFiller } from './MetadataFiller' + +const TEST_MESSAGE = { + type: 'test.message', +} + +const EVENT_DEFINITION = { + schemaVersion: '0.0.0' +} + +const SERVICE_ID = 'myServiceId' + +describe('MetadataFiller', () => { + describe('produceMetadata', () => { + let filler: CommonMetadataFiller + + beforeAll(() => { + filler = new CommonMetadataFiller({ serviceId: SERVICE_ID }) + }) + + it('Autofills metadata if not provided', () => { + // When + const metadata = filler.produceMetadata(TEST_MESSAGE, EVENT_DEFINITION) + + // Then + expect(metadata.schemaVersion).toEqual(EVENT_DEFINITION.schemaVersion) + expect(metadata.producedBy).toEqual(SERVICE_ID) + expect(metadata.originatedFrom).toEqual(SERVICE_ID) + expect(metadata.correlationId).toEqual(expect.any(String)) + }) + + it('Autofills metadata if not provided and fallsback to default schema version if not in event definition', () => { + // When + const metadata = filler.produceMetadata(TEST_MESSAGE, {}) + + // Then + expect(metadata.schemaVersion).toEqual('1.0.0') + expect(metadata.producedBy).toEqual(SERVICE_ID) + expect(metadata.originatedFrom).toEqual(SERVICE_ID) + expect(metadata.correlationId).toEqual(expect.any(String)) + }) + + it('Applies provided metadata', () => { + // Given + const providedMetadata = { + schemaVersion: '2.0.0', + producedBy: 'producer', + originatedFrom: 'source', + correlationId: 'myCorrelationId', + } + + // When + const metadata = filler.produceMetadata(TEST_MESSAGE, EVENT_DEFINITION, providedMetadata) + + // Then + expect(metadata).toEqual(providedMetadata) + }) + }) +}) diff --git a/packages/core/lib/messages/MetadataFiller.ts b/packages/core/lib/messages/MetadataFiller.ts index d3b1bc16..6b2cb57e 100644 --- a/packages/core/lib/messages/MetadataFiller.ts +++ b/packages/core/lib/messages/MetadataFiller.ts @@ -52,9 +52,11 @@ export class CommonMetadataFiller implements MetadataFiller { precedingMessageMetadata?: MessageMetadataType, ): MessageMetadataType { return { - producedBy: this.serviceId, + producedBy: precedingMessageMetadata?.producedBy ?? this.serviceId, originatedFrom: precedingMessageMetadata?.originatedFrom ?? this.serviceId, - schemaVersion: eventDefinition.schemaVersion ?? this.defaultVersion, + schemaVersion: precedingMessageMetadata?.schemaVersion + ?? eventDefinition.schemaVersion + ?? this.defaultVersion, correlationId: precedingMessageMetadata?.correlationId ?? this.produceId(), } }