2025-02-07 12:12:51 +00:00
|
|
|
package doccleanrunner_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-02-28 13:11:53 +00:00
|
|
|
"io"
|
2025-05-23 00:20:01 +00:00
|
|
|
"regexp"
|
2025-03-05 12:05:46 +00:00
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2025-04-03 19:17:24 +00:00
|
|
|
"time"
|
2025-03-05 12:05:46 +00:00
|
|
|
|
2025-02-07 12:12:51 +00:00
|
|
|
doccleanrunner "queryorchestration/api/docCleanRunner"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
documentclean "queryorchestration/internal/document/clean"
|
2025-04-02 18:50:03 +00:00
|
|
|
"queryorchestration/internal/server/runner"
|
2025-02-07 12:12:51 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
2025-06-10 12:57:30 +00:00
|
|
|
"queryorchestration/internal/serviceconfig/queue/documenttext"
|
2025-05-20 12:47:22 +00:00
|
|
|
"queryorchestration/internal/test"
|
2025-02-07 12:12:51 +00:00
|
|
|
objectstoremock "queryorchestration/mocks/objectstore"
|
|
|
|
|
|
2025-02-28 13:11:53 +00:00
|
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
2025-02-07 12:12:51 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
2025-03-04 15:51:03 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2025-02-07 12:12:51 +00:00
|
|
|
)
|
|
|
|
|
|
2025-02-07 14:15:06 +00:00
|
|
|
type DocCleanConfig struct {
|
2025-04-02 18:50:03 +00:00
|
|
|
runner.BaseConfig[doccleanrunner.Body]
|
2025-06-10 12:57:30 +00:00
|
|
|
documenttext.DocTextConfig
|
2025-02-07 12:12:51 +00:00
|
|
|
objectstore.ObjectStoreConfig
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDocCleanRunner(t *testing.T) {
|
2025-02-07 14:15:06 +00:00
|
|
|
cfg := &DocCleanConfig{}
|
2025-05-23 00:20:01 +00:00
|
|
|
test.CreateDB(t, cfg)
|
|
|
|
|
acfg := test.CreateAWSContainer(t, cfg)
|
|
|
|
|
|
|
|
|
|
test.SetQueueClient(t, t.Context(), cfg, acfg.ExternalEndpoint)
|
2025-06-10 12:57:30 +00:00
|
|
|
cfg.DocumentTextURL = test.CreateQueue(t, cfg, test.DocTextRunnerName)
|
2025-05-20 12:47:22 +00:00
|
|
|
|
2025-02-07 12:12:51 +00:00
|
|
|
mockStore := objectstoremock.NewMockS3Client(t)
|
|
|
|
|
cfg.StoreClient = mockStore
|
|
|
|
|
|
2025-04-22 14:40:16 +00:00
|
|
|
runner := doccleanrunner.New(&doccleanrunner.Services{
|
2025-03-11 18:15:49 +00:00
|
|
|
Clean: documentclean.New(cfg),
|
2025-02-07 12:12:51 +00:00
|
|
|
})
|
|
|
|
|
|
2025-05-20 12:47:22 +00:00
|
|
|
err := cfg.GetDBQueries().CreateClient(t.Context(), &repository.CreateClientParams{
|
|
|
|
|
Clientid: "clientid",
|
|
|
|
|
Name: "client_name",
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
docId, err := cfg.GetDBQueries().CreateDocument(t.Context(), &repository.CreateDocumentParams{
|
|
|
|
|
Clientid: "clientid",
|
|
|
|
|
Hash: "hash",
|
2025-03-05 19:49:03 +00:00
|
|
|
})
|
2025-05-20 12:47:22 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
part := uint16(1)
|
|
|
|
|
filetype := "pdf"
|
|
|
|
|
key := objectstore.BucketKey{
|
|
|
|
|
CreatedAt: time.Now().UTC(),
|
|
|
|
|
ClientID: "clientid",
|
|
|
|
|
EntityID: uuid.New(),
|
|
|
|
|
Location: objectstore.Import,
|
|
|
|
|
Part: &part,
|
|
|
|
|
FileType: &filetype,
|
|
|
|
|
}
|
|
|
|
|
err = cfg.GetDBQueries().AddDocumentEntry(t.Context(), &repository.AddDocumentEntryParams{
|
|
|
|
|
Documentid: docId,
|
|
|
|
|
Bucket: "bucket",
|
|
|
|
|
Key: key.String(),
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
mimeType := "application/pdf"
|
|
|
|
|
mockStore.EXPECT().
|
|
|
|
|
HeadObject(
|
|
|
|
|
mock.Anything,
|
|
|
|
|
mock.MatchedBy(func(in *s3.HeadObjectInput) bool {
|
|
|
|
|
return *in.Bucket == "bucket" && *in.Key == key.String()
|
|
|
|
|
}),
|
|
|
|
|
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 == key.String()
|
|
|
|
|
}),
|
|
|
|
|
mock.Anything,
|
|
|
|
|
).
|
|
|
|
|
Return(&s3.GetObjectOutput{
|
|
|
|
|
Body: bodyMsg,
|
|
|
|
|
}, nil)
|
|
|
|
|
|
|
|
|
|
bod := doccleanrunner.Body{
|
|
|
|
|
DocumentID: docId,
|
|
|
|
|
}
|
|
|
|
|
assert.True(t, runner.Process(t.Context(), bod))
|
2025-05-23 00:20:01 +00:00
|
|
|
|
2025-06-10 12:57:30 +00:00
|
|
|
test.AssertMessageBody(t, cfg, cfg.GetDocumentTextURL(), regexp.MustCompile(fmt.Sprintf("{\"id\":\"%s\"}", docId.String())))
|
2025-02-07 12:12:51 +00:00
|
|
|
}
|
2025-02-28 13:11:53 +00:00
|
|
|
|
|
|
|
|
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`
|