2020-09-24 06:52:10 +00:00
|
|
|
package eventstore
|
|
|
|
|
2020-09-30 08:00:05 +00:00
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
|
|
|
)
|
|
|
|
|
2020-10-05 17:09:26 +00:00
|
|
|
// testEvent implements the Event interface
|
2020-09-30 08:00:05 +00:00
|
|
|
type testEvent struct {
|
|
|
|
description string
|
|
|
|
shouldCheckPrevious bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *testEvent) CheckPrevious() bool {
|
|
|
|
return e.shouldCheckPrevious
|
|
|
|
}
|
|
|
|
|
2020-10-05 17:09:26 +00:00
|
|
|
func (e *testEvent) EditorService() string {
|
|
|
|
return "editorService"
|
|
|
|
}
|
|
|
|
func (e *testEvent) EditorUser() string {
|
|
|
|
return "editorUser"
|
|
|
|
}
|
|
|
|
func (e *testEvent) Type() EventType {
|
|
|
|
return "test.event"
|
|
|
|
}
|
|
|
|
func (e *testEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *testEvent) PreviousSequence() uint64 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-09-30 08:00:05 +00:00
|
|
|
func testPushMapper(Event) (*repository.Event, error) {
|
|
|
|
return &repository.Event{AggregateID: "aggregateID"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_eventstore_RegisterPushEventMapper(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventMapper map[EventType]eventTypeInterceptors
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
eventType EventType
|
|
|
|
mapper func(Event) (*repository.Event, error)
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
event *repository.Event
|
|
|
|
isErr func(error) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no event type",
|
|
|
|
args: args{
|
|
|
|
eventType: "",
|
|
|
|
mapper: testPushMapper,
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no event mapper",
|
|
|
|
args: args{
|
|
|
|
eventType: "event.type",
|
|
|
|
mapper: nil,
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "new interceptor",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "new.event",
|
|
|
|
mapper: testPushMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &repository.Event{AggregateID: "aggregateID"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing interceptor new push mapper",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{
|
|
|
|
"existing": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "new.event",
|
|
|
|
mapper: testPushMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &repository.Event{AggregateID: "aggregateID"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing interceptor existing push mapper",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{
|
|
|
|
"existing": {
|
|
|
|
pushMapper: func(Event) (*repository.Event, error) {
|
|
|
|
return nil, errors.ThrowUnimplemented(nil, "V2-1qPvn", "unimplemented")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "new.event",
|
|
|
|
mapper: testPushMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &repository.Event{AggregateID: "aggregateID"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
es := &Eventstore{
|
|
|
|
eventMapper: tt.fields.eventMapper,
|
|
|
|
}
|
|
|
|
err := es.RegisterPushEventMapper(tt.args.eventType, tt.args.mapper)
|
|
|
|
if (tt.res.isErr != nil && !tt.res.isErr(err)) || (tt.res.isErr == nil && err != nil) {
|
|
|
|
t.Errorf("wrong error got: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tt.res.isErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper := es.eventMapper[tt.args.eventType]
|
|
|
|
event, err := mapper.pushMapper(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.res.event, event) {
|
|
|
|
t.Errorf("events should be deep equal. \ngot %v\nwant %v", event, tt.res.event)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFilterMapper(*repository.Event) (Event, error) {
|
|
|
|
return &testEvent{description: "hodor"}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_eventstore_RegisterFilterEventMapper(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
eventMapper map[EventType]eventTypeInterceptors
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
eventType EventType
|
|
|
|
mapper func(*repository.Event) (Event, error)
|
|
|
|
}
|
|
|
|
type res struct {
|
|
|
|
event Event
|
|
|
|
isErr func(error) bool
|
|
|
|
}
|
2020-09-24 06:52:10 +00:00
|
|
|
|
|
|
|
tests := []struct {
|
2020-09-30 08:00:05 +00:00
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
res res
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no event type",
|
|
|
|
args: args{
|
|
|
|
eventType: "",
|
|
|
|
mapper: testFilterMapper,
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no event mapper",
|
|
|
|
args: args{
|
|
|
|
eventType: "event.type",
|
|
|
|
mapper: nil,
|
|
|
|
},
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
isErr: errors.IsErrorInvalidArgument,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "new interceptor",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "event.type",
|
|
|
|
mapper: testFilterMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &testEvent{description: "hodor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing interceptor new filter mapper",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{
|
|
|
|
"event.type": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "new.event",
|
|
|
|
mapper: testFilterMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &testEvent{description: "hodor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "existing interceptor existing filter mapper",
|
|
|
|
fields: fields{
|
|
|
|
eventMapper: map[EventType]eventTypeInterceptors{
|
|
|
|
"event.type": {
|
|
|
|
filterMapper: func(*repository.Event) (Event, error) {
|
|
|
|
return nil, errors.ThrowUnimplemented(nil, "V2-1qPvn", "unimplemented")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
eventType: "new.event",
|
|
|
|
mapper: testFilterMapper,
|
|
|
|
},
|
|
|
|
res: res{
|
|
|
|
event: &testEvent{description: "hodor"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-09-24 06:52:10 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-09-30 08:00:05 +00:00
|
|
|
es := &Eventstore{
|
|
|
|
eventMapper: tt.fields.eventMapper,
|
|
|
|
}
|
|
|
|
err := es.RegisterFilterEventMapper(tt.args.eventType, tt.args.mapper)
|
|
|
|
if (tt.res.isErr != nil && !tt.res.isErr(err)) || (tt.res.isErr == nil && err != nil) {
|
|
|
|
t.Errorf("wrong error got: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tt.res.isErr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mapper := es.eventMapper[tt.args.eventType]
|
|
|
|
event, err := mapper.filterMapper(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(tt.res.event, event) {
|
|
|
|
t.Errorf("events should be deep equal. \ngot %v\nwant %v", event, tt.res.event)
|
|
|
|
}
|
2020-09-24 06:52:10 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|