Skip to content

Commit

Permalink
UML-3724 consuming event and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allenannom committed Jan 16, 2025
1 parent 070c2a3 commit 1632bf1
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 38 deletions.
10 changes: 0 additions & 10 deletions lambda-functions/event-receiver/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ import (
"os"
)

type AppConfig struct {
EventBusName string
}

func LoadConfig() AppConfig {
return AppConfig{
EventBusName: getEnv("EVENT_BUS_NAME", "default")
}
}

func getEnv(key, fallback string) string {
if value, exists := os.LookupEnv(key); exists {
return value
Expand Down
13 changes: 3 additions & 10 deletions lambda-functions/event-receiver/app/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ import (
)

type Factory interface {
GetAWSConfig() aws.GetAWSConfig
GetAWSConfig() aws.Config
GetLogger() Logger
GetConfig() AppConfig
}

type factory struct {
awsConfig aws.GetAWSConfig
awsConfig aws.Config
logger Logger
config AppConfig
}

func NewFactory(cfg aws.Config, logger Logger, config AppConfig) Factory {
func NewFactory(cfg aws.Config, logger Logger) Factory {
return &factory{
awsConfig: cfg,
logger: logger,
config: config,
}
}

Expand All @@ -31,7 +28,3 @@ func (f *factory) GetAWSConfig() aws.Config {
func (f *factory) GetLogger() Logger {
return f.logger
}

func (f *factory) GetConfig() AppConfig {
return f.config
}
27 changes: 27 additions & 0 deletions lambda-functions/event-receiver/app/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/stretchr/testify/assert"
"github.com/ministryofjustice/opg-use-an-lpa/app/mocks"
)

func TestFactory_GetLogger(t *testing.T) {
mockLogger := new(mocks.Logger)
mockAWSConfig := aws.Config{}

factory := NewFactory(mockAWSConfig, mockLogger)

assert.Equal(t, mockLogger, factory.GetLogger())
}

func TestFactory_GetAWSConfig(t *testing.T) {
mockLogger := new(mocks.Logger)
mockAWSConfig := aws.Config{}

factory := NewFactory(mockAWSConfig, mockLogger)

assert.Equal(t, mockAWSConfig, factory.GetAWSConfig())
}
23 changes: 14 additions & 9 deletions lambda-functions/event-receiver/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/aws/aws-lambda-go/events"
)

type CloudWatchHandler interfact {
Handle(ctx context.Context, event events.CloudWatchHandler) error
type CloudWatchHandler interface {
Handle(ctx context.Context, event events.CloudWatchEvent) error
}

type cloudWatchHandler struct {
Expand All @@ -24,17 +24,22 @@ func NewCloudWatchHandler(factory Factory, logger Logger) CloudWatchHandler {
}
}

func (h *cloudWatchHandler) Handle(ctx context.Context, event events.CloudWatchHandler) error {
func (h *cloudWatchHandler) Handle(ctx context.Context, event events.CloudWatchEvent) error {
var parsedEvent events.CloudWatchEvent

h.logger.Info("Received CloudWatch event", "source", event.Source)

switch event.Source {
case "opg.poas.makeregister":
h.logger.Info("Handling 'makeregister' event", "detailType", event.DetailType)
err := json.Unmarshal(event.Detail, &parsedEvent)
if err != nil {
h.logger.Error("Failed to unmarshal event detail", err)
return fmt.Errorf("failed to unmarshal event: %w", err)
}

switch parsedEvent.Source {
case "opg.poas.makeregister":
h.logger.Info("Handling 'makeregister' event", "detailType", parsedEvent.DetailType)
default:
eventData, _ := json.Marshal(event)
h.logger.Warn("Unhandled event source", fmt.Errorf("unknown source: %s", event.Source), "event", string(eventData))
return fmt.Errorf("unknown event source: %s", event.Source)
h.logger.Warn("Unhandled event source: " + parsedEvent.Source, err)
}

return nil
Expand Down
12 changes: 12 additions & 0 deletions lambda-functions/event-receiver/app/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"
"context"
"encoding/json"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/assert"
"github.com/ministryofjustice/opg-use-an-lpa/app/mocks"
"github.com/aws/aws-lambda-go/events"
)
6 changes: 3 additions & 3 deletions lambda-functions/event-receiver/app/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
)

type Logger interface {
Info(message String, args ...interface{})
Warn(message String, err error, args ...interface{})
Error(message String, err error)
Info(message string, args ...interface{})
Warn(message string, err error, args ...interface{})
Error(message string, err error)
}

type defaultLogger struct{}
Expand Down
38 changes: 38 additions & 0 deletions lambda-functions/event-receiver/app/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"testing"

"github.com/stretchr/testify/mock"
"github.com/ministryofjustice/opg-use-an-lpa/app/mocks"
)

func TestLogger_Info(t *testing.T) {
mockLogger := new(mocks.Logger)

mockLogger.On("Info", "Test info log").Return()

mockLogger.Info("Test info log")

mockLogger.AssertExpectations(t)
}

func TestLogger_Warn(t *testing.T) {
mockLogger := new(mocks.Logger)

mockLogger.On("Warn", "Test warn log", mock.Anything).Return()

mockLogger.Warn("Test warn log", nil)

mockLogger.AssertExpectations(t)
}

func TestLogger_Error(t *testing.T) {
mockLogger := new(mocks.Logger)

mockLogger.On("Error", "Test error log", mock.Anything).Return()

mockLogger.Error("Test error log", nil)

mockLogger.AssertExpectations(t)
}
13 changes: 8 additions & 5 deletions lambda-functions/event-receiver/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ package main
import (
"context"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/aws"
)

var (
cfg aws.Config
)

func main() {
Expand All @@ -16,13 +21,11 @@ func main() {

cfg, err = config.LoadDefaultConfig(ctx)
if err != nil {
logger.ErrorContext(ctx, "failed to load default config", slog.Any("err", err))
logger.Error("failed to load default config", err)
return
}

appConfig := LoadConfig()

appFactory := NewFactory(cfg, logger, appConfig)
appFactory := NewFactory(cfg, logger)

handler := NewCloudWatchHandler(appFactory, logger)

Expand Down
47 changes: 47 additions & 0 deletions lambda-functions/event-receiver/app/mocks/CloudWatchHandler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions lambda-functions/event-receiver/app/mocks/Factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions lambda-functions/event-receiver/app/mocks/Logger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1632bf1

Please sign in to comment.