ee776d2681
Single Mock Server * mockserver * mockserver * reqs * mockserver * slowrunner * someoptimisedqueries * passedfullsuite * passedfullsuite
208 lines
4.7 KiB
Go
208 lines
4.7 KiB
Go
package doccleanrunner_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
doccleanrunner "queryorchestration/api/docCleanRunner"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/document"
|
|
documentclean "queryorchestration/internal/document/clean"
|
|
"queryorchestration/internal/server/runner"
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
|
"queryorchestration/internal/serviceconfig/queue/documenttexttrigger"
|
|
objectstoremock "queryorchestration/mocks/objectstore"
|
|
queuemock "queryorchestration/mocks/queue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/aws/aws-sdk-go-v2/service/sqs"
|
|
"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 DocCleanConfig struct {
|
|
runner.BaseConfig[doccleanrunner.Body]
|
|
documenttexttrigger.DocTextTriggerConfig
|
|
objectstore.ObjectStoreConfig
|
|
}
|
|
|
|
func TestDocCleanRunner(t *testing.T) {
|
|
ctx := context.Background()
|
|
|
|
pool, err := pgxmock.NewPool()
|
|
require.NoError(t, err)
|
|
|
|
cfg := &DocCleanConfig{}
|
|
cfg.DBPool = pool
|
|
cfg.DBQueries = repository.New(pool)
|
|
mockStore := objectstoremock.NewMockS3Client(t)
|
|
cfg.StoreClient = mockStore
|
|
mockSQS := queuemock.NewMockSQSClient(t)
|
|
cfg.QueueClient = mockSQS
|
|
cfg.DocumentTextTriggerURL = "/i/am/here"
|
|
|
|
runner := doccleanrunner.New(&doccleanrunner.Services{
|
|
Clean: documentclean.New(cfg),
|
|
})
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
bod := doccleanrunner.Body{
|
|
DocumentID: uuid.New(),
|
|
}
|
|
|
|
doc := document.DocumentSummary{
|
|
ID: bod.DocumentID,
|
|
ClientID: "hello",
|
|
Hash: "example_hash",
|
|
}
|
|
bucket := "hello"
|
|
key := objectstore.BucketKey{
|
|
CreatedAt: time.Now().UTC(),
|
|
ClientID: "hi",
|
|
EntityID: uuid.New(),
|
|
Location: objectstore.Import,
|
|
}
|
|
keyStr := key.String()
|
|
dbid := doc.ID
|
|
|
|
pool.ExpectQuery("name: HasDocumentCleanEntry :one").WithArgs(dbid).WillReturnRows(
|
|
pgxmock.NewRows([]string{"isclean"}).
|
|
AddRow(false),
|
|
)
|
|
pool.ExpectQuery("name: GetDocumentSummary :one").WithArgs(dbid).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "clientId", "hash"}).
|
|
AddRow(dbid, doc.ClientID, doc.Hash),
|
|
)
|
|
pool.ExpectQuery("name: GetDocumentEntry :one").WithArgs(dbid).WillReturnRows(
|
|
pgxmock.NewRows([]string{"documentId", "bucket", "key"}).
|
|
AddRow(dbid, bucket, keyStr),
|
|
)
|
|
mimeType := "application/pdf"
|
|
dbmimetype := repository.NullCleanmimetype{
|
|
Valid: true,
|
|
Cleanmimetype: repository.Cleanmimetype(mimeType),
|
|
}
|
|
pool.ExpectBegin()
|
|
cleanid := uuid.New()
|
|
pool.ExpectQuery("name: GetMostRecentDocumentCleanEntry :one").WithArgs(dbid).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id", "documentId", "bucket", "key", "version", "hash", "mimetype", "fail"}),
|
|
)
|
|
pool.ExpectQuery("name: AddDocumentClean :one").WithArgs(dbid, &bucket, &keyStr, &doc.Hash, dbmimetype, repository.NullCleanfailtype{}).
|
|
WillReturnRows(
|
|
pgxmock.NewRows([]string{"id"}).
|
|
AddRow(cleanid),
|
|
)
|
|
pool.ExpectExec("name: AddDocumentCleanEntry :exec").WithArgs(cleanid, pgxmock.AnyArg()).
|
|
WillReturnResult(pgxmock.NewResult("", 1))
|
|
pool.ExpectCommit()
|
|
|
|
mockSQS.EXPECT().
|
|
SendMessage(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *sqs.SendMessageInput) bool {
|
|
return *in.QueueUrl == cfg.DocumentTextTriggerURL && *in.MessageBody == fmt.Sprintf("{\"id\":\"%s\"}", doc.ID.String())
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&sqs.SendMessageOutput{}, nil)
|
|
|
|
mockStore.EXPECT().
|
|
HeadObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
|
return *in.Bucket == bucket && *in.Key == keyStr
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.HeadObjectOutput{
|
|
ContentType: &mimeType,
|
|
}, nil)
|
|
|
|
bodyMsg := io.NopCloser(strings.NewReader(pdfHelloWorld))
|
|
mockStore.EXPECT().
|
|
GetObject(
|
|
mock.Anything,
|
|
mock.MatchedBy(func(in *s3.GetObjectInput) bool {
|
|
return *in.Bucket == bucket && *in.Key == keyStr
|
|
}),
|
|
mock.Anything,
|
|
).
|
|
Return(&s3.GetObjectOutput{
|
|
Body: bodyMsg,
|
|
}, nil)
|
|
|
|
assert.True(t, runner.Process(ctx, bod))
|
|
})
|
|
}
|
|
|
|
const pdfHelloWorld = `%PDF-1.4
|
|
%����
|
|
1 0 obj
|
|
<<
|
|
/Type /Catalog
|
|
/Pages 2 0 R
|
|
/Version /1.4
|
|
>>
|
|
endobj
|
|
2 0 obj
|
|
<<
|
|
/Type /Pages
|
|
/Kids [3 0 R]
|
|
/Count 1
|
|
>>
|
|
endobj
|
|
3 0 obj
|
|
<<
|
|
/Type /Page
|
|
/Parent 2 0 R
|
|
/MediaBox [0 0 612 792]
|
|
/Resources <<
|
|
/Font <<
|
|
/F1 <<
|
|
/Type /Font
|
|
/Subtype /Type1
|
|
/BaseFont /Helvetica
|
|
>>
|
|
>>
|
|
>>
|
|
/Contents 4 0 R
|
|
>>
|
|
endobj
|
|
4 0 obj
|
|
<<
|
|
/Length
|
|
44
|
|
>>
|
|
stream
|
|
BT
|
|
/F1 24 Tf
|
|
100 700 Td
|
|
(Hello World!) Tj
|
|
ET
|
|
endstream
|
|
endobj
|
|
xref
|
|
0 5
|
|
0000000000 65535 f
|
|
0000000015 00000 n
|
|
0000000086 00000 n
|
|
0000000151 00000 n
|
|
0000000376 00000 n
|
|
trailer
|
|
<<
|
|
/Size 5
|
|
/Root 1 0 R
|
|
>>
|
|
startxref
|
|
472
|
|
%%EOF`
|