2025-03-17 18:14:15 +00:00
|
|
|
package queryapitest
|
|
|
|
|
|
|
|
|
|
import (
|
2025-05-27 15:28:46 +00:00
|
|
|
"bytes"
|
2025-03-17 18:14:15 +00:00
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-03-19 11:54:14 +00:00
|
|
|
"log/slog"
|
2025-05-27 15:28:46 +00:00
|
|
|
"mime/multipart"
|
2025-05-05 09:31:21 +00:00
|
|
|
"net/http"
|
2025-03-17 18:14:15 +00:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-05-06 13:49:49 +00:00
|
|
|
"queryorchestration/internal/test"
|
2025-05-27 15:28:46 +00:00
|
|
|
|
2025-03-17 18:14:15 +00:00
|
|
|
queryapi "queryorchestration/pkg/queryAPI"
|
|
|
|
|
|
2025-05-05 09:31:21 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
|
"github.com/oapi-codegen/runtime/types"
|
2025-03-17 18:14:15 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
2025-05-27 15:28:46 +00:00
|
|
|
type File struct {
|
|
|
|
|
ClientID queryapi.ClientID
|
|
|
|
|
Filename string
|
|
|
|
|
Content []byte
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CreateFile(t testing.TB, client queryapi.ClientWithResponsesInterface, file File) {
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
fileBody := multipart.NewWriter(&buf)
|
|
|
|
|
|
|
|
|
|
name := file.Filename
|
|
|
|
|
if name == "" {
|
|
|
|
|
name = "helloworld"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
write, err := fileBody.CreateFormFile("file", name)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
_, err = write.Write(file.Content)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
err = fileBody.Close()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
body := bytes.NewReader(buf.Bytes())
|
|
|
|
|
|
|
|
|
|
uploadRes, err := client.UploadDocumentWithBodyWithResponse(t.Context(), file.ClientID, fileBody.FormDataContentType(), body)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, http.StatusOK, uploadRes.StatusCode())
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
func CreateClientWithSync(t testing.TB, client queryapi.ClientWithResponsesInterface) queryapi.ClientID {
|
2025-03-17 18:14:15 +00:00
|
|
|
t.Helper()
|
|
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
clientCreateRes, err := client.CreateClientWithResponse(t.Context(), queryapi.ClientCreate{
|
2025-03-17 18:14:15 +00:00
|
|
|
Name: "example_name",
|
|
|
|
|
Id: "ID",
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
2025-08-20 19:01:13 +00:00
|
|
|
require.NotNil(t, clientCreateRes.JSON201, "expected 201 response but got status %d", clientCreateRes.StatusCode())
|
|
|
|
|
require.NotEmpty(t, clientCreateRes.JSON201.Id, "client ID should not be empty")
|
2025-03-17 18:14:15 +00:00
|
|
|
|
|
|
|
|
canSync := true
|
2025-05-06 01:59:52 +00:00
|
|
|
_, err = client.UpdateClientWithResponse(t.Context(), clientCreateRes.JSON201.Id, queryapi.ClientUpdate{
|
2025-03-17 18:14:15 +00:00
|
|
|
CanSync: &canSync,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
return clientCreateRes.JSON201.Id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetQueryForClient(t testing.TB, client queryapi.ClientWithResponsesInterface, clientId queryapi.ClientID, queryId uuid.UUID) {
|
|
|
|
|
t.Helper()
|
2025-03-17 18:14:15 +00:00
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
newActiveVersion := int32(1)
|
|
|
|
|
_, err := client.SetCollectorByClientIdWithResponse(t.Context(), clientId, queryapi.CollectorSet{
|
|
|
|
|
ActiveVersion: &newActiveVersion,
|
|
|
|
|
Fields: &[]queryapi.CollectorField{
|
|
|
|
|
{
|
|
|
|
|
Name: "JSON_QUERY",
|
|
|
|
|
QueryId: queryId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
2025-03-17 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WaitForClientStatus(t testing.TB, ctx context.Context, service queryapi.ClientWithResponsesInterface, id string, status queryapi.ClientStatus) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
2025-03-19 11:54:14 +00:00
|
|
|
timeout := time.After(60 * time.Second)
|
2025-05-05 09:31:21 +00:00
|
|
|
ticker := time.NewTicker(100 * time.Millisecond)
|
2025-03-17 18:14:15 +00:00
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case <-timeout:
|
|
|
|
|
require.NoError(t, fmt.Errorf("Timeout waiting for client status to become %s", status))
|
|
|
|
|
case <-ticker.C:
|
|
|
|
|
jRes, err := service.GetStatusByClientIdWithResponse(ctx, id)
|
|
|
|
|
if err != nil {
|
2025-03-19 11:54:14 +00:00
|
|
|
slog.Error("error getting status", "error", err.Error())
|
|
|
|
|
require.NoError(t, err)
|
2025-03-17 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if jRes.JSON200.Status == status {
|
|
|
|
|
assert.Equal(t, status, jRes.JSON200.Status)
|
2025-05-05 09:31:21 +00:00
|
|
|
slog.Info("returned client status", "status", jRes.JSON200.Status)
|
2025-03-17 18:14:15 +00:00
|
|
|
return
|
|
|
|
|
}
|
2025-03-19 11:54:14 +00:00
|
|
|
|
|
|
|
|
slog.Error("unexpected status", "status", jRes.JSON200.Status)
|
2025-03-17 18:14:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-05 09:31:21 +00:00
|
|
|
|
2025-05-06 01:59:52 +00:00
|
|
|
func CreateDependentQueries(t testing.TB, client *queryapi.ClientWithResponses) (uuid.UUID, uuid.UUID) {
|
2025-05-05 09:31:21 +00:00
|
|
|
contextQueryRes, err := client.CreateQueryWithResponse(t.Context(), queryapi.QueryCreate{
|
|
|
|
|
Type: queryapi.CONTEXTFULL,
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
2025-05-06 13:49:49 +00:00
|
|
|
test.AssertStatus(t, http.StatusCreated, contextQueryRes.HTTPResponse)
|
2025-05-05 09:31:21 +00:00
|
|
|
jcfg := `{"path":"keyone"}`
|
|
|
|
|
jsonQueryRes, err := client.CreateQueryWithResponse(t.Context(), queryapi.QueryCreate{
|
|
|
|
|
Type: queryapi.JSONEXTRACTOR,
|
|
|
|
|
Config: &jcfg,
|
|
|
|
|
RequiredQueries: &[]types.UUID{contextQueryRes.JSON201.Id},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
2025-05-06 13:49:49 +00:00
|
|
|
test.AssertStatus(t, http.StatusCreated, jsonQueryRes.HTTPResponse)
|
2025-05-05 09:31:21 +00:00
|
|
|
|
|
|
|
|
return contextQueryRes.JSON201.Id, jsonQueryRes.JSON201.Id
|
|
|
|
|
}
|