Merged PR 680123: fix: Remove unnecessary interface pointers

Related work items: #697212
This commit is contained in:
Christian Schaible 2024-11-14 13:30:25 +00:00
parent 568c5cdb91
commit cc53ba4126
19 changed files with 208 additions and 199 deletions

View file

@ -64,7 +64,7 @@ var ErrUnsupportedObjectIdentifierType = errors.New("unsupported object identifi
var ErrUnsupportedRoutableType = errors.New("unsupported routable type")
func validateAndSerializePartially(
validator *ProtobufValidator,
validator ProtobufValidator,
event *auditV1.AuditLogEntry,
visibility auditV1.Visibility,
routableIdentifier *RoutableIdentifier,
@ -86,7 +86,7 @@ func validateAndSerializePartially(
}
func newValidatedRoutableAuditEvent(
validator *ProtobufValidator,
validator ProtobufValidator,
event *auditV1.AuditLogEntry,
visibility auditV1.Visibility,
routableIdentifier *RoutableIdentifier) (*auditV1.RoutableAuditEvent, error) {
@ -109,7 +109,7 @@ func newValidatedRoutableAuditEvent(
Data: &auditV1.RoutableAuditEvent_UnencryptedData{UnencryptedData: &payload},
}
err = (*validator).Validate(&routableEvent)
err = validator.Validate(&routableEvent)
if err != nil {
return nil, err
}
@ -117,7 +117,7 @@ func newValidatedRoutableAuditEvent(
}
func validateAuditLogEntry(
validator *ProtobufValidator,
validator ProtobufValidator,
event *auditV1.AuditLogEntry,
visibility auditV1.Visibility,
routableIdentifier *RoutableIdentifier,
@ -132,7 +132,7 @@ func validateAuditLogEntry(
}
// Validate the actual event
err := (*validator).Validate(event)
err := validator.Validate(event)
if err != nil {
return err
}
@ -169,8 +169,8 @@ func validateAuditLogEntry(
// Send implements AuditApi.Send
func send(
topicNameResolver *TopicNameResolver,
messagingApi *messaging.Api,
topicNameResolver TopicNameResolver,
messagingApi messaging.Api,
ctx context.Context,
routableIdentifier *RoutableIdentifier,
cloudEvent *CloudEvent,
@ -198,7 +198,7 @@ func send(
return err
}
topic, err := (*topicNameResolver).Resolve(routableIdentifier)
topic, err := topicNameResolver.Resolve(routableIdentifier)
if err != nil {
return err
}
@ -235,11 +235,11 @@ func send(
applicationAttributes["cloudEvents:sdkhttpversion"] = auditGoHttpVersion
}
return (*messagingApi).Send(
return messagingApi.Send(
ctx,
topic,
(*cloudEvent).Data,
(*cloudEvent).DataContentType,
cloudEvent.Data,
cloudEvent.DataContentType,
applicationAttributes)
}

View file

@ -68,7 +68,7 @@ func Test_ValidateAndSerializePartially_EventNil(t *testing.T) {
validator := NewValidator(t)
_, err := validateAndSerializePartially(
&validator, nil, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
validator, nil, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
assert.ErrorIs(t, err, ErrEventNil)
}
@ -80,7 +80,7 @@ func Test_ValidateAndSerializePartially_AuditEventValidationFailed(t *testing.T)
event.LogName = ""
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.EqualError(t, err, "validation error:\n - log_name: value is required [required]")
}
@ -89,7 +89,7 @@ func Test_ValidateAndSerializePartially_RoutableEventValidationFailed(t *testing
validator := NewValidator(t)
event, objectIdentifier := newOrganizationAuditEvent(nil)
_, err := validateAndSerializePartially(&validator, event, 3, NewRoutableIdentifier(objectIdentifier))
_, err := validateAndSerializePartially(validator, event, 3, NewRoutableIdentifier(objectIdentifier))
assert.EqualError(t, err, "validation error:\n - visibility: value must be one of the defined enum values [enum.defined_only]")
}
@ -101,28 +101,28 @@ func Test_ValidateAndSerializePartially_CheckVisibility_Event(t *testing.T) {
t.Run("Visibility public - object identifier nil", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
assert.ErrorIs(t, err, ErrObjectIdentifierNil)
})
t.Run("Visibility private - object identifier nil", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, nil)
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, nil)
assert.ErrorIs(t, err, ErrObjectIdentifierNil)
})
t.Run("Visibility public - object identifier system", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, RoutableSystemIdentifier)
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, RoutableSystemIdentifier)
assert.ErrorIs(t, err, ErrObjectIdentifierVisibilityMismatch)
})
t.Run("Visibility public - object identifier set", func(t *testing.T) {
routableEvent, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.NoError(t, err)
assert.NotNil(t, routableEvent)
@ -130,14 +130,14 @@ func Test_ValidateAndSerializePartially_CheckVisibility_Event(t *testing.T) {
t.Run("Visibility private - object identifier system", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
assert.ErrorIs(t, err, ErrAttributeIdentifierInvalid)
})
t.Run("Visibility private - object identifier set", func(t *testing.T) {
routableEvent, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, NewRoutableIdentifier(objectIdentifier))
assert.NoError(t, err)
assert.NotNil(t, routableEvent)
@ -151,28 +151,28 @@ func Test_ValidateAndSerializePartially_CheckVisibility_SystemEvent(t *testing.T
t.Run("Visibility public - object identifier nil", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, nil)
assert.ErrorIs(t, err, ErrObjectIdentifierNil)
})
t.Run("Visibility private - object identifier nil", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, nil)
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, nil)
assert.ErrorIs(t, err, ErrObjectIdentifierNil)
})
t.Run("Visibility public - object identifier system", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, RoutableSystemIdentifier)
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, RoutableSystemIdentifier)
assert.ErrorIs(t, err, ErrObjectIdentifierVisibilityMismatch)
})
t.Run("Visibility public - object identifier set", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(
&auditV1.ObjectIdentifier{Identifier: uuid.NewString(), Type: string(ObjectTypeOrganization)}))
assert.ErrorIs(t, err, ErrInvalidRoutableIdentifierForSystemEvent)
@ -180,7 +180,7 @@ func Test_ValidateAndSerializePartially_CheckVisibility_SystemEvent(t *testing.T
t.Run("Visibility private - object identifier system", func(t *testing.T) {
routableEvent, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
assert.NoError(t, err)
assert.NotNil(t, routableEvent)
@ -188,7 +188,7 @@ func Test_ValidateAndSerializePartially_CheckVisibility_SystemEvent(t *testing.T
t.Run("Visibility private - object identifier set", func(t *testing.T) {
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, NewRoutableIdentifier(
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, NewRoutableIdentifier(
&auditV1.ObjectIdentifier{Identifier: uuid.NewString(), Type: string(ObjectTypeOrganization)}))
assert.ErrorIs(t, err, ErrInvalidRoutableIdentifierForSystemEvent)
@ -202,7 +202,7 @@ func Test_ValidateAndSerializePartially_UnsupportedIdentifierType(t *testing.T)
objectIdentifier.Type = "invalid"
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrUnsupportedRoutableType)
}
@ -217,7 +217,7 @@ func Test_ValidateAndSerializePartially_LogNameIdentifierMismatch(t *testing.T)
t.Run("LogName type mismatch", func(t *testing.T) {
event.LogName = fmt.Sprintf("projects/%s/logs/admin-activity", identifier)
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrAttributeTypeInvalid)
})
@ -225,7 +225,7 @@ func Test_ValidateAndSerializePartially_LogNameIdentifierMismatch(t *testing.T)
t.Run("LogName identifier mismatch", func(t *testing.T) {
event.LogName = fmt.Sprintf("folders/%s/logs/admin-activity", uuid.NewString())
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrAttributeIdentifierInvalid)
})
@ -241,7 +241,7 @@ func Test_ValidateAndSerializePartially_ResourceNameIdentifierMismatch(t *testin
t.Run("ResourceName type mismatch", func(t *testing.T) {
event.ProtoPayload.ResourceName = fmt.Sprintf("projects/%s", identifier)
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrAttributeTypeInvalid)
})
@ -249,7 +249,7 @@ func Test_ValidateAndSerializePartially_ResourceNameIdentifierMismatch(t *testin
t.Run("ResourceName identifier mismatch", func(t *testing.T) {
event.ProtoPayload.ResourceName = fmt.Sprintf("folders/%s", uuid.NewString())
_, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
validator, event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrAttributeIdentifierInvalid)
})
@ -261,7 +261,7 @@ func Test_ValidateAndSerializePartially_SystemEvent(t *testing.T) {
event := newSystemAuditEvent(nil)
routableEvent, err := validateAndSerializePartially(
&validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
validator, event, auditV1.Visibility_VISIBILITY_PRIVATE, RoutableSystemIdentifier)
assert.NoError(t, err)
assert.Equal(t, event.LogName, fmt.Sprintf("system/%s/logs/%s", SystemIdentifier.Identifier, EventTypeSystemEvent))
@ -283,13 +283,13 @@ func Test_Send_TopicNameResolutionError(t *testing.T) {
var cloudEvent = CloudEvent{}
var messagingApi messaging.Api = &messaging.AmqpApi{}
err := send(&topicNameResolver, &messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent)
err := send(topicNameResolver, messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent)
assert.ErrorIs(t, err, expectedError)
}
func Test_Send_MessagingApiNil(t *testing.T) {
var topicNameResolver TopicNameResolver = &LegacyTopicNameResolver{topicName: "test"}
err := send(&topicNameResolver, nil, context.Background(), nil, nil)
err := send(topicNameResolver, nil, context.Background(), nil, nil)
assert.ErrorIs(t, err, ErrMessagingApiNil)
}
@ -297,7 +297,7 @@ func Test_Send_CloudEventNil(t *testing.T) {
var topicNameResolver TopicNameResolver = &LegacyTopicNameResolver{topicName: "test"}
var messagingApi messaging.Api = &messaging.AmqpApi{}
err := send(&topicNameResolver, &messagingApi, context.Background(), nil, nil)
err := send(topicNameResolver, messagingApi, context.Background(), nil, nil)
assert.ErrorIs(t, err, ErrCloudEventNil)
}
@ -306,7 +306,7 @@ func Test_Send_ObjectIdentifierNil(t *testing.T) {
var messagingApi messaging.Api = &messaging.AmqpApi{}
var cloudEvent = CloudEvent{}
err := send(&topicNameResolver, &messagingApi, context.Background(), nil, &cloudEvent)
err := send(topicNameResolver, messagingApi, context.Background(), nil, &cloudEvent)
assert.ErrorIs(t, err, ErrObjectIdentifierNil)
}
@ -316,7 +316,7 @@ func Test_Send_UnsupportedObjectIdentifierType(t *testing.T) {
var cloudEvent = CloudEvent{}
var objectIdentifier = auditV1.ObjectIdentifier{Identifier: uuid.NewString(), Type: "unsupported"}
err := send(&topicNameResolver, &messagingApi, context.Background(), NewRoutableIdentifier(&objectIdentifier), &cloudEvent)
err := send(topicNameResolver, messagingApi, context.Background(), NewRoutableIdentifier(&objectIdentifier), &cloudEvent)
assert.ErrorIs(t, err, ErrUnsupportedRoutableType)
}
@ -330,7 +330,7 @@ func Test_Send(t *testing.T) {
var messagingApi messaging.Api = &messagingApiMock
var cloudEvent = CloudEvent{}
assert.NoError(t, send(&topicNameResolver, &messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.NoError(t, send(topicNameResolver, messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.True(t, messagingApiMock.AssertNumberOfCalls(t, "Send", 1))
}
@ -357,13 +357,16 @@ func Test_SendAllHeadersSet(t *testing.T) {
TraceParent: &traceParent,
TraceState: &traceState,
}
assert.NoError(t, send(&topicNameResolver, &messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.NoError(t, send(topicNameResolver, messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.True(t, messagingApiMock.AssertNumberOfCalls(t, "Send", 1))
arguments := messagingApiMock.Calls[0].Arguments
topic := arguments.Get(1).(string)
assert.Equal(t, "topic", topic)
contentType := arguments.Get(3).(string)
assert.Equal(t, ContentTypeCloudEventsProtobuf, contentType)
applicationProperties := arguments.Get(4).(map[string]any)
assert.Equal(t, "1.0", applicationProperties["cloudEvents:specversion"])
assert.Equal(t, "resourcemanager", applicationProperties["cloudEvents:source"])
@ -396,13 +399,16 @@ func Test_SendWithoutOptionalHeadersSet(t *testing.T) {
DataType: "type",
Subject: "subject",
}
assert.NoError(t, send(&topicNameResolver, &messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.NoError(t, send(topicNameResolver, messagingApi, context.Background(), RoutableSystemIdentifier, &cloudEvent))
assert.True(t, messagingApiMock.AssertNumberOfCalls(t, "Send", 1))
arguments := messagingApiMock.Calls[0].Arguments
topic := arguments.Get(1).(string)
assert.Equal(t, "topic", topic)
contentType := arguments.Get(3).(string)
assert.Equal(t, ContentTypeCloudEventsProtobuf, contentType)
applicationProperties := arguments.Get(4).(map[string]any)
assert.Equal(t, "1.0", applicationProperties["cloudEvents:specversion"])
assert.Equal(t, "resourcemanager", applicationProperties["cloudEvents:source"])

View file

@ -34,20 +34,20 @@ type LegacyTopicNameConfig struct {
//
// Note: The implementation will be deprecated and replaced with the "routableAuditApi" once the new audit log routing is implemented
type LegacyAuditApi struct {
messagingApi *messaging.Api
topicNameResolver *TopicNameResolver
messagingApi messaging.Api
topicNameResolver TopicNameResolver
tracer trace.Tracer
validator *ProtobufValidator
validator ProtobufValidator
}
// NewLegacyAuditApi can be used to initialize the audit log api.
//
// Note: The NewLegacyAuditApi method will be deprecated and replaced with "newRoutableAuditApi" once the new audit log routing is implemented
func NewLegacyAuditApi(
messagingApi *messaging.Api,
messagingApi messaging.Api,
topicNameConfig LegacyTopicNameConfig,
validator ProtobufValidator,
) (*AuditApi, error) {
) (AuditApi, error) {
if messagingApi == nil {
return nil, ErrMessagingApiNil
@ -62,12 +62,12 @@ func NewLegacyAuditApi(
// Audit api
var auditApi AuditApi = &LegacyAuditApi{
messagingApi: messagingApi,
topicNameResolver: &topicNameResolver,
topicNameResolver: topicNameResolver,
tracer: otel.Tracer("legacy-audit-api"),
validator: &validator,
validator: validator,
}
return &auditApi, nil
return auditApi, nil
}
// Log implements AuditApi.Log

View file

@ -26,18 +26,18 @@ var ErrTopicNameEmpty = errors.New("empty topic name provided")
//
// Note: The implementation will be deprecated and replaced with the "routableAuditApi" once the new audit log routing is implemented
type DynamicLegacyAuditApi struct {
messagingApi *messaging.Api
messagingApi messaging.Api
tracer trace.Tracer
validator *ProtobufValidator
validator ProtobufValidator
}
// NewDynamicLegacyAuditApi can be used to initialize the audit log api.
//
// Note: The NewLegacyAuditApi method will be deprecated and replaced with "newRoutableAuditApi" once the new audit log routing is implemented
func NewDynamicLegacyAuditApi(
messagingApi *messaging.Api,
messagingApi messaging.Api,
validator ProtobufValidator,
) (*AuditApi, error) {
) (AuditApi, error) {
if messagingApi == nil {
return nil, ErrMessagingApiNil
@ -47,10 +47,10 @@ func NewDynamicLegacyAuditApi(
var auditApi AuditApi = &DynamicLegacyAuditApi{
messagingApi: messagingApi,
tracer: otel.Tracer("dynamic-legacy-audit-api"),
validator: &validator,
validator: validator,
}
return &auditApi, nil
return auditApi, nil
}
// Log implements AuditApi.Log
@ -147,5 +147,5 @@ func (a *DynamicLegacyAuditApi) Send(
ctx, span := a.tracer.Start(ctx, "send")
defer span.End()
return send(&topicNameResolver, a.messagingApi, ctx, routableIdentifier, cloudEvent)
return send(topicNameResolver, a.messagingApi, ctx, routableIdentifier, cloudEvent)
}

View file

@ -66,7 +66,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.ErrorIs(t, (*auditApi).Log(
assert.ErrorIs(t, auditApi.Log(
ctx,
event,
visibility,
@ -99,7 +99,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -136,7 +136,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -174,7 +174,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -211,7 +211,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -249,7 +249,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -286,7 +286,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -324,7 +324,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -380,7 +380,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -436,7 +436,7 @@ func TestDynamicLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
ctx := context.WithValue(ctx, ContextKeyTopic, topicName)
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -465,7 +465,7 @@ func TestDynamicLegacyAuditApi_ValidateAndSerialize_ValidationFailed(t *testing.
auditApi := DynamicLegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)
@ -482,7 +482,7 @@ func TestDynamicLegacyAuditApi_Log_ValidationFailed(t *testing.T) {
auditApi := DynamicLegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)
@ -509,7 +509,7 @@ func TestDynamicLegacyAuditApi_ConvertAndSerializeIntoLegacyFormatInvalidObjectI
auditApi := DynamicLegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
_, err := auditApi.ValidateAndSerialize(context.Background(), event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrUnsupportedRoutableType)

View file

@ -67,7 +67,7 @@ func TestLegacyAuditApi(t *testing.T) {
event.LogName = strings.Replace(event.LogName, string(EventTypeAdminActivity), string(EventTypeDataAccess), 1)
// Log the event to solace
assert.ErrorIs(t, (*auditApi).Log(
assert.ErrorIs(t, auditApi.Log(
ctx,
event,
auditV1.Visibility_VISIBILITY_PUBLIC,
@ -100,7 +100,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -137,7 +137,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -175,7 +175,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -212,7 +212,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -250,7 +250,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -287,7 +287,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -325,7 +325,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -381,7 +381,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -437,7 +437,7 @@ func TestLegacyAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -604,7 +604,7 @@ func TestLegacyAuditApi_ValidateAndSerialize_ValidationFailed(t *testing.T) {
auditApi := LegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)
@ -621,7 +621,7 @@ func TestLegacyAuditApi_Log_ValidationFailed(t *testing.T) {
auditApi := LegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)
@ -648,7 +648,7 @@ func TestLegacyAuditApi_ConvertAndSerializeIntoLegacyFormatInvalidObjectIdentifi
auditApi := LegacyAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
_, err := auditApi.ValidateAndSerialize(context.Background(), event, auditV1.Visibility_VISIBILITY_PUBLIC, NewRoutableIdentifier(objectIdentifier))
assert.ErrorIs(t, err, ErrUnsupportedRoutableType)

View file

@ -17,10 +17,10 @@ import (
// MockAuditApi is an implementation of AuditApi that does nothing and has no dependency to external systems.
type MockAuditApi struct {
tracer trace.Tracer
validator *ProtobufValidator
validator ProtobufValidator
}
func NewMockAuditApi() (*AuditApi, error) {
func NewMockAuditApi() (AuditApi, error) {
validator, err := protovalidate.New()
if err != nil {
return nil, err
@ -28,9 +28,9 @@ func NewMockAuditApi() (*AuditApi, error) {
var protobufValidator ProtobufValidator = validator
var auditApi AuditApi = &MockAuditApi{
tracer: otel.Tracer("mock-audit-api"),
validator: &protobufValidator,
validator: protobufValidator,
}
return &auditApi, nil
return auditApi, nil
}
// Log implements AuditApi.Log.

View file

@ -21,7 +21,7 @@ func TestMockAuditApi_Log(t *testing.T) {
// Test
t.Run("Log", func(t *testing.T) {
assert.Nil(t, (*auditApi).Log(
assert.Nil(t, auditApi.Log(
context.Background(), event, auditV1.Visibility_VISIBILITY_PUBLIC, routableObjectIdentifier))
})
@ -30,14 +30,14 @@ func TestMockAuditApi_Log(t *testing.T) {
event.LogName = strings.Replace(event.LogName, string(EventTypeAdminActivity), string(EventTypeDataAccess), 1)
routableObjectIdentifier := NewRoutableIdentifier(objectIdentifier)
assert.ErrorIs(t, (*auditApi).Log(
assert.ErrorIs(t, auditApi.Log(
context.Background(), event, auditV1.Visibility_VISIBILITY_PUBLIC, routableObjectIdentifier),
ErrUnsupportedEventTypeDataAccess)
})
t.Run("ValidateAndSerialize", func(t *testing.T) {
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
cloudEvent, err := (*auditApi).ValidateAndSerialize(
cloudEvent, err := auditApi.ValidateAndSerialize(
context.Background(), event, visibility, routableObjectIdentifier)
assert.NoError(t, err)
@ -48,7 +48,7 @@ func TestMockAuditApi_Log(t *testing.T) {
t.Run("ValidateAndSerialize event nil", func(t *testing.T) {
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
_, err := (*auditApi).ValidateAndSerialize(context.Background(), nil, visibility, routableObjectIdentifier)
_, err := auditApi.ValidateAndSerialize(context.Background(), nil, visibility, routableObjectIdentifier)
assert.ErrorIs(t, err, ErrEventNil)
})
@ -56,6 +56,6 @@ func TestMockAuditApi_Log(t *testing.T) {
t.Run("Send", func(t *testing.T) {
var cloudEvent = CloudEvent{}
assert.Nil(t, (*auditApi).Send(context.Background(), routableObjectIdentifier, &cloudEvent))
assert.Nil(t, auditApi.Send(context.Background(), routableObjectIdentifier, &cloudEvent))
})
}

View file

@ -57,18 +57,18 @@ type topicNameConfig struct {
// Warning: It is only there for local (compatibility) testing.
// DO NOT USE IT!
type routableAuditApi struct {
messagingApi *messaging.Api
topicNameResolver *TopicNameResolver
messagingApi messaging.Api
topicNameResolver TopicNameResolver
tracer trace.Tracer
validator *ProtobufValidator
validator ProtobufValidator
}
// NewRoutableAuditApi can be used to initialize the audit log api.
func newRoutableAuditApi(
messagingApi *messaging.Api,
messagingApi messaging.Api,
topicNameConfig topicNameConfig,
validator ProtobufValidator,
) (*AuditApi, error) {
) (AuditApi, error) {
if messagingApi == nil {
return nil, errors.New("messaging api nil")
@ -98,12 +98,12 @@ func newRoutableAuditApi(
// Audit api
var auditApi AuditApi = &routableAuditApi{
messagingApi: messagingApi,
topicNameResolver: &topicNameResolver,
topicNameResolver: topicNameResolver,
tracer: otel.Tracer("routable-audit-api"),
validator: &validator,
validator: validator,
}
return &auditApi, nil
return auditApi, nil
}
// Log implements AuditApi.Log

View file

@ -73,7 +73,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.ErrorIs(t, (*auditApi).Log(
assert.ErrorIs(t, auditApi.Log(
ctx,
event,
visibility,
@ -94,7 +94,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -130,7 +130,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -164,7 +164,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -198,7 +198,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -232,7 +232,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -265,7 +265,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -299,7 +299,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -349,7 +349,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PRIVATE
assert.NoError(t,
(*auditApi).Log(
auditApi.Log(
ctx,
event,
visibility,
@ -399,7 +399,7 @@ func TestRoutableAuditApi(t *testing.T) {
// Log the event to solace
visibility := auditV1.Visibility_VISIBILITY_PUBLIC
assert.NoError(t, (*auditApi).Log(
assert.NoError(t, auditApi.Log(
ctx,
event,
visibility,
@ -502,7 +502,7 @@ func TestRoutableAuditApi_ValidateAndSerialize_ValidationFailed(t *testing.T) {
auditApi := routableAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)
@ -519,7 +519,7 @@ func TestRoutableAuditApi_Log_ValidationFailed(t *testing.T) {
auditApi := routableAuditApi{
tracer: otel.Tracer("test"),
validator: &protobufValidator,
validator: protobufValidator,
}
event := newSystemAuditEvent(nil)

View file

@ -378,7 +378,7 @@ func (builder *AuditLogEntryBuilder) Build(ctx context.Context, sequenceNumber S
type AuditEventBuilder struct {
// The audit api used to validate, serialize and send events
api *AuditApi
api AuditApi
// The audit log entry builder which is used to build the actual protobuf message
auditLogEntryBuilder *AuditLogEntryBuilder
@ -387,7 +387,7 @@ type AuditEventBuilder struct {
built bool
// Sequence number generator providing sequential increasing numbers for the insert IDs
sequenceNumberGenerator *utils.SequenceNumberGenerator
sequenceNumberGenerator utils.SequenceNumberGenerator
// Opentelemetry tracer
tracer trace.Tracer
@ -400,10 +400,10 @@ type AuditEventBuilder struct {
// validates input and returns a cloud event that can be sent to the audit log system.
func NewAuditEventBuilder(
// The audit api used to validate, serialize and send events
api *AuditApi,
api AuditApi,
// The sequence number generator can be used to get and revert sequence numbers to build audit log events
sequenceNumberGenerator *utils.SequenceNumberGenerator,
sequenceNumberGenerator utils.SequenceNumberGenerator,
// The service name in lowercase (allowed characters are [a-z-]).
serviceName string,
@ -429,12 +429,12 @@ func NewAuditEventBuilder(
// NextSequenceNumber returns the next sequence number from utils.SequenceNumberGenerator.
// In case of an error RevertSequenceNumber must be called to prevent gaps in the sequence of numbers.
func (builder *AuditEventBuilder) NextSequenceNumber() SequenceNumber {
return SequenceNumber((*builder.sequenceNumberGenerator).Next())
return SequenceNumber(builder.sequenceNumberGenerator.Next())
}
// RevertSequenceNumber can be called to decrease the sequence number on the utils.SequenceNumberGenerator in case of an error
func (builder *AuditEventBuilder) RevertSequenceNumber(number SequenceNumber) {
(*builder.sequenceNumberGenerator).Revert(uint64(number))
builder.sequenceNumberGenerator.Revert(uint64(number))
}
func (builder *AuditEventBuilder) AsSystemEvent() *AuditEventBuilder {
@ -646,7 +646,7 @@ func (builder *AuditEventBuilder) Build(ctx context.Context, sequenceNumber Sequ
}
// Validate and serialize the protobuf event into a cloud event
cloudEvent, err := (*builder.api).ValidateAndSerialize(ctx, auditLogEntry, visibility, routingIdentifier)
cloudEvent, err := builder.api.ValidateAndSerialize(ctx, auditLogEntry, visibility, routingIdentifier)
if err != nil {
return nil, nil, err
}

View file

@ -1036,7 +1036,7 @@ func Test_AuditEventBuilder(t *testing.T) {
assert.NoError(t, err)
})
t.Run("with responsebody unserialized", func(t *testing.T) {
t.Run("with response body unserialized", func(t *testing.T) {
api, _ := NewMockAuditApi()
sequenceNumberGenerator := utils.NewDefaultSequenceNumberGenerator()

View file

@ -75,7 +75,7 @@ func Test_LogEvent(t *testing.T) {
Type: ObjectTypeProject,
}
routableEvent, err := validateAndSerializePartially(&protoValidator, entry, auditV1.Visibility_VISIBILITY_PUBLIC, &routableIdentifier)
routableEvent, err := validateAndSerializePartially(protoValidator, entry, auditV1.Visibility_VISIBILITY_PUBLIC, &routableIdentifier)
assert.NoError(t, err)
legacyBytes, err := convertAndSerializeIntoLegacyFormat(entry, routableEvent)

View file

@ -764,7 +764,10 @@ func parseClaimsFromAuthorizationHeader(authorizationHeader string) (map[string]
// Collect user-friendly filtered subset of claims
filteredClaims := make(map[string]interface{})
_ = json.Unmarshal(decodedString, &filteredClaims)
err = json.Unmarshal(decodedString, &filteredClaims)
if err != nil {
return parsedClaims, nil, err
}
keysToDelete := make([]string, 0)
for key := range filteredClaims {
if key != "aud" && key != "email" && key != "iss" && key != "jti" && key != "sub" {

View file

@ -39,36 +39,36 @@ type Api interface {
// MutexApi is wrapper around an API implementation that controls mutual exclusive access to the api.
type MutexApi struct {
mutex *sync.Mutex
api *Api
mutex sync.Mutex
api Api
}
var _ Api = &MutexApi{}
func NewMutexApi(api *Api) (*Api, error) {
func NewMutexApi(api Api) (Api, error) {
if api == nil {
return nil, errors.New("api is nil")
}
mutexApi := MutexApi{
mutex: &sync.Mutex{},
mutex: sync.Mutex{},
api: api,
}
var genericApi Api = &mutexApi
return &genericApi, nil
return genericApi, nil
}
// Send implements Api.Send
func (m *MutexApi) Send(ctx context.Context, topic string, data []byte, contentType string, applicationProperties map[string]any) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return (*m.api).Send(ctx, topic, data, contentType, applicationProperties)
return m.api.Send(ctx, topic, data, contentType, applicationProperties)
}
func (m *MutexApi) Close(ctx context.Context) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return (*m.api).Close(ctx)
return m.api.Close(ctx)
}
// AmqpConfig provides AMQP connection related parameters.
@ -80,7 +80,7 @@ type AmqpConfig struct {
// AmqpSession is an abstraction providing a subset of the methods of amqp.Session
type AmqpSession interface {
NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (*AmqpSender, error)
NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (AmqpSender, error)
Close(ctx context.Context) error
}
@ -88,10 +88,10 @@ type AmqpSessionWrapper struct {
session *amqp.Session
}
func (w AmqpSessionWrapper) NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (*AmqpSender, error) {
func (w AmqpSessionWrapper) NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (AmqpSender, error) {
sender, err := w.session.NewSender(ctx, target, opts)
var amqpSender AmqpSender = sender
return &amqpSender, err
return amqpSender, err
}
func (w AmqpSessionWrapper) Close(ctx context.Context) error {
@ -108,12 +108,12 @@ type AmqpSender interface {
type AmqpApi struct {
config AmqpConfig
connection *amqp.Conn
session *AmqpSession
session AmqpSession
}
var _ Api = &AmqpApi{}
func NewAmqpApi(amqpConfig AmqpConfig) (*Api, error) {
func NewAmqpApi(amqpConfig AmqpConfig) (Api, error) {
amqpApi := &AmqpApi{config: amqpConfig}
err := amqpApi.connect()
@ -122,7 +122,7 @@ func NewAmqpApi(amqpConfig AmqpConfig) (*Api, error) {
}
var messagingApi Api = amqpApi
return &messagingApi, nil
return messagingApi, nil
}
// connect opens a new connection and session to the AMQP messaging system.
@ -161,8 +161,8 @@ func (a *AmqpApi) connect() error {
return err
}
var amqpSession AmqpSession = AmqpSessionWrapper{session: session}
a.session = &amqpSession
var amqpSession AmqpSession = &AmqpSessionWrapper{session: session}
a.session = amqpSession
return nil
}
@ -196,7 +196,7 @@ func (a *AmqpApi) trySend(ctx context.Context, topic string, data []byte, conten
)
}
sender, err := (*a.session).NewSender(ctx, topic, nil)
sender, err := a.session.NewSender(ctx, topic, nil)
if err != nil {
return err
}
@ -214,9 +214,9 @@ func (a *AmqpApi) trySend(ctx context.Context, topic string, data []byte, conten
Data: bytes,
}
err = (*sender).Send(ctx, &message, nil)
err = sender.Send(ctx, &message, nil)
if err != nil {
_ = (*sender).Close(ctx)
_ = sender.Close(ctx)
return err
}
@ -225,7 +225,7 @@ func (a *AmqpApi) trySend(ctx context.Context, topic string, data []byte, conten
// resetConnection closes the current session and connection and reconnects to the messaging system.
func (a *AmqpApi) resetConnection(ctx context.Context) error {
_ = (*a.session).Close(ctx)
_ = a.session.Close(ctx)
err := a.connection.Close()
if err != nil {
log.AuditLogger.Error("failed to close audit messaging connection", err)
@ -237,7 +237,7 @@ func (a *AmqpApi) resetConnection(ctx context.Context) error {
// Close implements Api.Close
func (a *AmqpApi) Close(ctx context.Context) error {
log.AuditLogger.Info("close audit messaging connection")
_ = (*a.session).Close(ctx)
_ = a.session.Close(ctx)
err := a.connection.Close()
return err
}

View file

@ -15,11 +15,11 @@ type AmqpSessionMock struct {
mock.Mock
}
func (m *AmqpSessionMock) NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (*AmqpSender, error) {
func (m *AmqpSessionMock) NewSender(ctx context.Context, target string, opts *amqp.SenderOptions) (AmqpSender, error) {
args := m.Called(ctx, target, opts)
var sender *AmqpSender = nil
var sender AmqpSender = nil
if args.Get(0) != nil {
sender = args.Get(0).(*AmqpSender)
sender = args.Get(0).(AmqpSender)
}
err := args.Error(1)
return sender, err
@ -65,10 +65,29 @@ func Test_AmqpMessagingApi_Send(t *testing.T) {
api, err := NewAmqpApi(AmqpConfig{URL: solaceContainer.AmqpConnectionString})
assert.NoError(t, err)
err = (*api).Send(ctx, "topic-name", []byte{}, "application/json", make(map[string]any))
err = api.Send(ctx, "topic-name", []byte{}, "application/json", make(map[string]any))
assert.EqualError(t, err, "topic \"topic-name\" name lacks mandatory prefix \"topic://\"")
})
t.Run("Close connection without errors", func(t *testing.T) {
defer solaceContainer.StopOnError()
// Initialize the solace queue
topicSubscriptionTopicPattern := "auditlog/>"
queueName := "close-connection-without-error"
assert.NoError(t, solaceContainer.QueueCreate(ctx, queueName))
assert.NoError(t, solaceContainer.TopicSubscriptionCreate(ctx, queueName, topicSubscriptionTopicPattern))
topicName := fmt.Sprintf("topic://auditlog/%s", "amqp-close-connection")
assert.NoError(t, solaceContainer.ValidateTopicName(topicSubscriptionTopicPattern, topicName))
api := &AmqpApi{config: AmqpConfig{URL: solaceContainer.AmqpConnectionString}}
err := api.connect()
assert.NoError(t, err)
err = api.Close(ctx)
assert.NoError(t, err)
})
t.Run("New sender call returns error", func(t *testing.T) {
defer solaceContainer.StopOnError()
@ -92,13 +111,13 @@ func Test_AmqpMessagingApi_Send(t *testing.T) {
sessionMock.On("Close", mock.Anything).Return(nil)
var amqpSession AmqpSession = &sessionMock
api.session = &amqpSession
api.session = amqpSession
// It's expected that the test succeeds.
// First the session is closed as it returns the expected error
// Then the retry mechanism restarts the connection and successfully sends the data
value := "test"
err = (*api).Send(ctx, topicName, []byte(value), "application/json", make(map[string]any))
err = api.Send(ctx, topicName, []byte(value), "application/json", make(map[string]any))
assert.NoError(t, err)
// Check that the mock was called
@ -140,13 +159,13 @@ func Test_AmqpMessagingApi_Send(t *testing.T) {
sessionMock.On("Close", mock.Anything).Return(nil)
var amqpSession AmqpSession = &sessionMock
api.session = &amqpSession
api.session = amqpSession
// It's expected that the test succeeds.
// First the sender and session are closed as the sender returns the expected error
// Then the retry mechanism restarts the connection and successfully sends the data
value := "test"
err = (*api).Send(ctx, topicName, []byte(value), "application/json", make(map[string]any))
err = api.Send(ctx, topicName, []byte(value), "application/json", make(map[string]any))
assert.NoError(t, err)
// Check that the mocks were called
@ -160,23 +179,4 @@ func Test_AmqpMessagingApi_Send(t *testing.T) {
assert.Equal(t, value, string(message.Data[0]))
assert.Equal(t, topicName, *message.Properties.To)
})
t.Run("Close connection without errors", func(t *testing.T) {
defer solaceContainer.StopOnError()
// Initialize the solace queue
topicSubscriptionTopicPattern := "auditlog/>"
queueName := "close-connection-without-error"
assert.NoError(t, solaceContainer.QueueCreate(ctx, queueName))
assert.NoError(t, solaceContainer.TopicSubscriptionCreate(ctx, queueName, topicSubscriptionTopicPattern))
topicName := fmt.Sprintf("topic://auditlog/%s", "amqp-close-connection")
assert.NoError(t, solaceContainer.ValidateTopicName(topicSubscriptionTopicPattern, topicName))
api := &AmqpApi{config: AmqpConfig{URL: solaceContainer.AmqpConnectionString}}
err := api.connect()
assert.NoError(t, err)
err = (*api).Close(ctx)
assert.NoError(t, err)
})
}

View file

@ -24,13 +24,13 @@ type DefaultSequenceNumberGenerator struct {
// NewDefaultSequenceNumberGenerator returns an instance of DefaultSequenceNumberGenerator as pointer
// of SequenceNumberGenerator.
func NewDefaultSequenceNumberGenerator() *SequenceNumberGenerator {
func NewDefaultSequenceNumberGenerator() SequenceNumberGenerator {
var generator SequenceNumberGenerator = &DefaultSequenceNumberGenerator{
backlog: make([]uint64, 0),
sequenceNumber: 0,
sequenceNumberLock: sync.Mutex{},
}
return &generator
return generator
}
// Next implements SequenceNumberGenerator.Next

View file

@ -9,50 +9,50 @@ func Test_DefaultSequenceNumberGenerator(t *testing.T) {
t.Run("next", func(t *testing.T) {
var sequenceGenerator = NewDefaultSequenceNumberGenerator()
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(0), sequenceGenerator.Next())
})
t.Run("revert", func(t *testing.T) {
var sequenceGenerator = NewDefaultSequenceNumberGenerator()
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
(*sequenceGenerator).Revert(uint64(1))
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
assert.Equal(t, uint64(0), sequenceGenerator.Next())
assert.Equal(t, uint64(1), sequenceGenerator.Next())
sequenceGenerator.Revert(uint64(1))
assert.Equal(t, uint64(1), sequenceGenerator.Next())
})
t.Run("revert first", func(t *testing.T) {
var sequenceGenerator = NewDefaultSequenceNumberGenerator()
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
(*sequenceGenerator).Revert(uint64(0))
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(0), sequenceGenerator.Next())
assert.Equal(t, uint64(1), sequenceGenerator.Next())
sequenceGenerator.Revert(uint64(0))
assert.Equal(t, uint64(0), sequenceGenerator.Next())
})
t.Run("revert same value multiple times", func(t *testing.T) {
var sequenceGenerator = NewDefaultSequenceNumberGenerator()
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
assert.Equal(t, uint64(2), (*sequenceGenerator).Next())
(*sequenceGenerator).Revert(uint64(1))
(*sequenceGenerator).Revert(uint64(1))
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
assert.Equal(t, uint64(3), (*sequenceGenerator).Next())
assert.Equal(t, uint64(0), sequenceGenerator.Next())
assert.Equal(t, uint64(1), sequenceGenerator.Next())
assert.Equal(t, uint64(2), sequenceGenerator.Next())
sequenceGenerator.Revert(uint64(1))
sequenceGenerator.Revert(uint64(1))
assert.Equal(t, uint64(1), sequenceGenerator.Next())
assert.Equal(t, uint64(3), sequenceGenerator.Next())
})
t.Run("get and revert multiple", func(t *testing.T) {
var sequenceGenerator = NewDefaultSequenceNumberGenerator()
assert.Equal(t, uint64(0), (*sequenceGenerator).Next())
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
(*sequenceGenerator).Revert(uint64(1))
assert.Equal(t, uint64(1), (*sequenceGenerator).Next())
assert.Equal(t, uint64(2), (*sequenceGenerator).Next())
assert.Equal(t, uint64(3), (*sequenceGenerator).Next())
assert.Equal(t, uint64(4), (*sequenceGenerator).Next())
(*sequenceGenerator).Revert(uint64(2))
(*sequenceGenerator).Revert(uint64(3))
assert.Equal(t, uint64(2), (*sequenceGenerator).Next())
assert.Equal(t, uint64(3), (*sequenceGenerator).Next())
assert.Equal(t, uint64(5), (*sequenceGenerator).Next())
assert.Equal(t, uint64(6), (*sequenceGenerator).Next())
assert.Equal(t, uint64(0), sequenceGenerator.Next())
assert.Equal(t, uint64(1), sequenceGenerator.Next())
sequenceGenerator.Revert(uint64(1))
assert.Equal(t, uint64(1), sequenceGenerator.Next())
assert.Equal(t, uint64(2), sequenceGenerator.Next())
assert.Equal(t, uint64(3), sequenceGenerator.Next())
assert.Equal(t, uint64(4), sequenceGenerator.Next())
sequenceGenerator.Revert(uint64(2))
sequenceGenerator.Revert(uint64(3))
assert.Equal(t, uint64(2), sequenceGenerator.Next())
assert.Equal(t, uint64(3), sequenceGenerator.Next())
assert.Equal(t, uint64(5), sequenceGenerator.Next())
assert.Equal(t, uint64(6), sequenceGenerator.Next())
})
}

View file

@ -5,7 +5,7 @@ import (
"log/slog"
)
var AuditLogger Logger = SlogLogger{logger: slog.Default()}
var AuditLogger Logger = &SlogLogger{logger: slog.Default()}
type Logger interface {
Debug(msg string, err ...error)