Skip to content

Commit

Permalink
Fix MetadataFiller to use provided schemaVersion and producedBy (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariacm authored Jun 19, 2024
1 parent d749387 commit 817bdb9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
59 changes: 59 additions & 0 deletions packages/core/lib/messages/MetadataFiller.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
6 changes: 4 additions & 2 deletions packages/core/lib/messages/MetadataFiller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down

0 comments on commit 817bdb9

Please sign in to comment.