Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SendFilesReadyCommand #49

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions datasetIngestor/sendFilesReadyCommand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
Expand All @@ -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)
Expand Down
48 changes: 48 additions & 0 deletions datasetIngestor/sendFilesReadyCommand_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading