From 5fa7ddc87c86f15c5a57cacc0c356a222429ae34 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Tue, 23 Apr 2024 15:31:34 +0200 Subject: [PATCH 1/2] Add docstring --- datasetIngestor/sendFilesReadyCommand.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/datasetIngestor/sendFilesReadyCommand.go b/datasetIngestor/sendFilesReadyCommand.go index 9b17067..2712d0b 100644 --- a/datasetIngestor/sendFilesReadyCommand.go +++ b/datasetIngestor/sendFilesReadyCommand.go @@ -3,12 +3,28 @@ package datasetIngestor import ( "bytes" "encoding/json" - "io/ioutil" + "io" "log" "net/http" "strings" ) +/* +SendFilesReadyCommand is a function that sends a PUT request to a specified API server to update the dataset lifecycle status. + +Parameters: +- client: An *http.Client object, used to send the HTTP request. +- APIServer: A string representing the URL of the API server. +- datasetId: A string representing the ID of the dataset to be updated. +- user: A map[string]string containing user information, specifically the access token. + +The function constructs a metadata map with the dataset lifecycle status set to "datasetCreated" and archivable set to true. +This metadata is then converted to JSON and sent in the body of the PUT request. +The URL for the request is constructed using the APIServer and datasetId parameters, and the user's access token is appended as a query parameter. + +If the request is successful (HTTP status code 200), the function logs a success message along with the response body. +If the request fails, the function logs a failure message along with the status code and metadata map. +*/ func SendFilesReadyCommand(client *http.Client, APIServer string, datasetId string, user map[string]string) { var metaDataMap = map[string]interface{}{} metaDataMap["datasetlifecycle"] = map[string]interface{}{} @@ -28,7 +44,7 @@ func SendFilesReadyCommand(client *http.Client, APIServer string, datasetId stri } defer resp.Body.Close() if resp.StatusCode == 200 { - body, _ := ioutil.ReadAll(resp.Body) + body, _ := io.ReadAll(resp.Body) log.Printf("Successfully updated %v\n", string(body)) } else { log.Fatalf("SendFilesReadyCommand: Failed to update datasetLifecycle %v %v\n", resp.StatusCode, metaDataMap) From 0d2211960c811acdee68c9cbd57726635e34a702 Mon Sep 17 00:00:00 2001 From: "Ali R. Vahdati" <3798865+kavir1698@users.noreply.github.com> Date: Tue, 23 Apr 2024 16:43:18 +0200 Subject: [PATCH 2/2] Add tests for `SendFilesReadyCommand` --- datasetIngestor/sendFilesReadyCommand_test.go | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 datasetIngestor/sendFilesReadyCommand_test.go diff --git a/datasetIngestor/sendFilesReadyCommand_test.go b/datasetIngestor/sendFilesReadyCommand_test.go new file mode 100644 index 0000000..b41247e --- /dev/null +++ b/datasetIngestor/sendFilesReadyCommand_test.go @@ -0,0 +1,48 @@ +package datasetIngestor + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestSendFilesReadyCommand(t *testing.T) { + // Create a mock server + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { + // Test method and path + if req.URL.String() != "/Datasets/testDatasetId?access_token=testToken" { + t.Errorf("Expected URL '/Datasets/testDatasetId?access_token=testToken', got '%s'", req.URL.String()) + } + if req.Method != "PUT" { + t.Errorf("Expected method 'PUT', got '%s'", req.Method) + } + + // Test headers + if req.Header.Get("Content-Type") != "application/json" { + t.Errorf("Expected header 'Content-Type: application/json', got '%s'", req.Header.Get("Content-Type")) + } + + // Test body + body, _ := io.ReadAll(req.Body) + expectedBody := `{"datasetlifecycle":{"archivable":true,"archiveStatusMessage":"datasetCreated"}}` + if strings.TrimSpace(string(body)) != expectedBody { + t.Errorf("Expected body '%s', got '%s'", expectedBody, strings.TrimSpace(string(body))) + } + + rw.Write([]byte(`OK`)) + })) + // Close the server when test finishes + defer server.Close() + + // Create a map for user info + user := make(map[string]string) + user["accessToken"] = "testToken" + + // Create a http client + client := &http.Client{} + + // Call the function + SendFilesReadyCommand(client, server.URL, "testDatasetId", user) +}