package endtoend_test import ( "sync" "testing" "queryorchestration/internal/serviceconfig" "queryorchestration/internal/serviceconfig/aws" "queryorchestration/internal/serviceconfig/objectstore" "queryorchestration/internal/test" queryapitest "queryorchestration/internal/test/queryAPI" queryapi "queryorchestration/pkg/queryAPI" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) type Config struct { serviceconfig.BaseConfig aws.AWSConfig objectstore.ObjectStoreConfig } func TestProcess(t *testing.T) { t.Run("basic upload", func(t *testing.T) { t.Parallel() cfg := &Config{} net, clean := test.CreateFullNetwork(t, t.Context(), cfg) defer clean() var wg sync.WaitGroup var clientId queryapi.ClientID var jsonId queryapi.QueryID var textractExpectation test.MockExpectation textractBody := "Hello World" wg.Add(2) go func(t testing.TB) { _, jsonId = queryapitest.CreateDependentQueries(t, net.Client) wg.Done() }(t) go func(t testing.TB) { clientId = queryapitest.CreateClientWithSync(t, net.Client) wg.Done() }(t) wg.Wait() wg.Add(3) go func(t testing.TB) { textractExpectation = test.CreateDetectDocumentTextExpectation( t, net.Dependencies.MockServer, textractBody, ) wg.Done() }(t) go func(t testing.TB) { queryapitest.SetQueryForClient(t, net.Client, clientId, jsonId) wg.Done() }(t) go func(t testing.TB) { queryapitest.CreateFile(t, net.Client, queryapitest.File{ Filename: "helloworld.pdf", Content: []byte(pdfHelloWorld), ClientID: clientId, }) wg.Done() }(t) wg.Wait() test.WaitForMockEndpoint(t, net.Dependencies.MockServer, textractExpectation.Request) queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.INSYNC) docs, err := net.Client.ListDocumentsByClientIdWithResponse(t.Context(), clientId) require.NoError(t, err) assert.Len(t, *docs.JSON200, 1) doc := (*docs.JSON200)[0] expectedDoc := queryapi.Document{ Id: doc.Id, Hash: doc.Hash, ClientId: clientId, Fields: map[string]any{ "JSON_QUERY": "valueone", }, } docRes, err := net.Client.GetDocumentWithResponse(t.Context(), doc.Id) require.NoError(t, err) require.NotNil(t, docRes) require.NotNil(t, docRes.JSON200) assert.EqualExportedValues(t, expectedDoc, *docRes.JSON200) }) t.Run("update config", func(t *testing.T) { t.Parallel() cfg := &Config{} net, clean := test.CreateFullNetwork(t, t.Context(), cfg) defer clean() var wg sync.WaitGroup var clientId queryapi.ClientID var jsonId queryapi.QueryID var textractExpectation test.MockExpectation textractBody := "Hello World" wg.Add(2) go func(t testing.TB) { _, jsonId = queryapitest.CreateDependentQueries(t, net.Client) wg.Done() }(t) go func(t testing.TB) { clientId = queryapitest.CreateClientWithSync(t, net.Client) wg.Done() }(t) wg.Wait() wg.Add(3) go func(t testing.TB) { queryapitest.SetQueryForClient(t, net.Client, clientId, jsonId) wg.Done() }(t) go func(t testing.TB) { queryapitest.CreateFile(t, net.Client, queryapitest.File{ Filename: "helloworld.pdf", Content: []byte(pdfHelloWorld), ClientID: clientId, }) wg.Done() }(t) go func(t testing.TB) { textractExpectation = test.CreateDetectDocumentTextExpectation( t, net.Dependencies.MockServer, textractBody, ) wg.Done() }(t) wg.Wait() test.WaitForMockEndpoint(t, net.Dependencies.MockServer, textractExpectation.Request) queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.INSYNC) jcfg := `{"path":"keytwo"}` av := int32(2) _, err := net.Client.UpdateQueryWithResponse(t.Context(), jsonId, queryapi.QueryUpdate{ ActiveVersion: &av, Config: &jcfg, }) require.NoError(t, err) queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.INSYNC) docs, err := net.Client.ListDocumentsByClientIdWithResponse(t.Context(), clientId) require.NoError(t, err) assert.Len(t, *docs.JSON200, 1) doc := (*docs.JSON200)[0] expectedDoc := queryapi.Document{ Id: doc.Id, Hash: doc.Hash, ClientId: clientId, Fields: map[string]any{ "JSON_QUERY": "valuetwo", }, } docRes, err := net.Client.GetDocumentWithResponse(t.Context(), doc.Id) require.NoError(t, err) require.NotNil(t, docRes) require.NotNil(t, docRes.JSON200) assert.EqualExportedValues(t, expectedDoc, *docRes.JSON200) }) t.Run("test multiple versions", func(t *testing.T) { t.Parallel() cfg := &Config{} net, clean := test.CreateFullNetwork(t, t.Context(), cfg) defer clean() var wg sync.WaitGroup var clientId queryapi.ClientID var jsonId queryapi.QueryID var textractExpectation test.MockExpectation textractBody := "Hello World" wg.Add(2) go func(t testing.TB) { _, jsonId = queryapitest.CreateDependentQueries(t, net.Client) jcfg := `{"path":"keytwo"}` _, err := net.Client.UpdateQueryWithResponse(t.Context(), jsonId, queryapi.QueryUpdate{ Config: &jcfg, }) require.NoError(t, err) wg.Done() }(t) go func(t testing.TB) { clientId = queryapitest.CreateClientWithSync(t, net.Client) wg.Done() }(t) wg.Wait() wg.Add(3) go func(t testing.TB) { textractExpectation = test.CreateDetectDocumentTextExpectation( t, net.Dependencies.MockServer, textractBody, ) wg.Done() }(t) go func(t testing.TB) { queryapitest.SetQueryForClient(t, net.Client, clientId, jsonId) wg.Done() }(t) go func(t testing.TB) { queryapitest.CreateFile(t, net.Client, queryapitest.File{ Filename: "helloworld.pdf", Content: []byte(pdfHelloWorld), ClientID: clientId, }) wg.Done() }(t) wg.Wait() test.WaitForMockEndpoint(t, net.Dependencies.MockServer, textractExpectation.Request) queryapitest.WaitForClientStatus(t, t.Context(), net.Client, clientId, queryapi.INSYNC) docs, err := net.Client.ListDocumentsByClientIdWithResponse(t.Context(), clientId) require.NoError(t, err) assert.Len(t, *docs.JSON200, 1) doc := (*docs.JSON200)[0] wg.Add(2) go func(t testing.TB) { testRes, err := net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ QueryVersion: 1, DocumentId: doc.Id, }) require.NoError(t, err) assert.Equal(t, "valueone", testRes.JSON200.Value) wg.Done() }(t) go func(t testing.TB) { testRes, err := net.Client.TestQueryWithResponse(t.Context(), jsonId, queryapi.QueryTestRequest{ QueryVersion: 2, DocumentId: doc.Id, }) require.NoError(t, err) assert.Equal(t, "valuetwo", testRes.JSON200.Value) wg.Done() }(t) wg.Wait() }) } 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`