package docinitrunner import ( "context" "encoding/json" "fmt" "testing" "queryorchestration/internal/client" "queryorchestration/internal/database/repository" "queryorchestration/internal/document" documentinit "queryorchestration/internal/document/init" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/queue/documentsync" queuemock "queryorchestration/mocks/queue" "github.com/aws/aws-sdk-go-v2/service/sqs" "github.com/aws/aws-sdk-go-v2/service/sqs/types" "github.com/go-playground/validator/v10" "github.com/google/uuid" "github.com/pashagolub/pgxmock/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" ) type DocInitConfig struct { serviceconfig.BaseConfig documentsync.DocSyncConfig } func TestDocInitRunner(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocInitConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS runner := New(validator.New(), &Services{ Document: documentinit.New(cfg), }) assert.NotNil(t, runner) t.Run("valid", func(t *testing.T) { clientId := uuid.New() bucketName := "bucketName" location := fmt.Sprintf("%s/aaa", clientId) docinfo := document.DocumentSummary{ ID: uuid.New(), ClientID: clientId, Hash: "example_hash", } doc := S3EventNotification{ Records: []S3EventRecord{ { EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: location, ETag: docinfo.Hash, }, }, }, }, } bodyBytes, err := json.Marshal(doc) require.NoError(t, err) body := string(bodyBytes) msg := &types.Message{ Body: &body, } pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, clientId).WillReturnRows( pgxmock.NewRows([]string{"id"}), ) pool.ExpectBegin() pool.ExpectQuery("name: CreateDocument :one").WithArgs(clientId, docinfo.Hash). WillReturnRows( pgxmock.NewRows([]string{"id"}). AddRow(docinfo.ID), ) pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location). WillReturnResult(pgxmock.NewResult("", 1)) pool.ExpectCommit() pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID). WillReturnRows( pgxmock.NewRows([]string{"id", "clientId", "hash"}). AddRow(docinfo.ID, docinfo.ClientID, "example"), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) assert.True(t, runner.Process(ctx, msg)) }) t.Run("invalid body", func(t *testing.T) { body := "definitely invalid" msgId := "id" msg := &types.Message{ Body: &body, MessageId: &msgId, } assert.True(t, runner.Process(ctx, msg)) }) } func TestProcessRecord(t *testing.T) { ctx := context.Background() pool, err := pgxmock.NewPool() require.NoError(t, err) cfg := &DocInitConfig{} cfg.DBPool = pool cfg.DBQueries = repository.New(pool) mockSQS := queuemock.NewMockSQSClient(t) cfg.QueueClient = mockSQS runner := New(validator.New(), &Services{ Document: documentinit.New(cfg), }) assert.NotNil(t, runner) t.Run("valid", func(t *testing.T) { j := client.Client{ ID: uuid.New(), } bucketName := "bucketName" location := fmt.Sprintf("%s/aaa", j.ID.String()) docinfo := document.DocumentSummary{ ID: uuid.New(), ClientID: j.ID, Hash: "example_hash", } record := S3EventRecord{ EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Bucket: S3Bucket{ Name: bucketName, }, Object: S3Object{ Key: location, ETag: docinfo.Hash, }, }, } pool.ExpectQuery("name: GetDocumentIDByHash :one").WithArgs(docinfo.Hash, j.ID).WillReturnRows( pgxmock.NewRows([]string{"id"}), ) pool.ExpectBegin() pool.ExpectQuery("name: CreateDocument :one").WithArgs(j.ID, docinfo.Hash). WillReturnRows( pgxmock.NewRows([]string{"id"}). AddRow(docinfo.ID), ) pool.ExpectExec("name: AddDocumentEntry :exec").WithArgs(docinfo.ID, bucketName, location). WillReturnResult(pgxmock.NewResult("", 1)) pool.ExpectCommit() pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(docinfo.ID). WillReturnRows( pgxmock.NewRows([]string{"id", "clientId", "hash"}). AddRow(docinfo.ID, docinfo.ClientID, "example"), ) mockSQS.EXPECT(). SendMessage( mock.Anything, mock.MatchedBy(func(in *sqs.SendMessageInput) bool { return *in.QueueUrl == cfg.DocumentSyncURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", docinfo.ID.String()) }), mock.Anything, ). Return(&sqs.SendMessageOutput{}, nil) err = runner.processRecord(ctx, record) require.NoError(t, err) }) t.Run("invalid_event", func(t *testing.T) { record := S3EventRecord{ EventName: "invalid_event", } err = runner.processRecord(ctx, record) require.NoError(t, err) }) t.Run("invalid id", func(t *testing.T) { location := "cc/aaa" record := S3EventRecord{ EventName: EventS3ObjectCreatedPut, S3: S3EventRecordDetails{ Object: S3Object{ Key: location, }, }, } err = runner.processRecord(ctx, record) require.NoError(t, err) }) } func TestIsSupportedEvent(t *testing.T) { runner := Runner{} t.Run("put", func(t *testing.T) { assert.True(t, runner.isSupportedEvent(EventS3ObjectCreatedPut)) }) t.Run("invalid", func(t *testing.T) { assert.False(t, runner.isSupportedEvent("invalid")) }) }