Skip to content

Commit

Permalink
feat: adding more test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Dec 10, 2024
1 parent 34f7f1d commit 31c5c11
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ import (
"io"
"net/http"
"os"
"strings"
"time"

"github.com/rudderlabs/rudder-go-kit/stats"

backendconfig "github.com/rudderlabs/rudder-server/backend-config"
"github.com/rudderlabs/rudder-server/jobsdb"
"github.com/rudderlabs/rudder-server/router/batchrouter/asyncdestinationmanager/common"
)
Expand All @@ -23,41 +21,6 @@ type ClevertapServiceImpl struct {
ConnectionConfig *ConnectionConfig
}

// GetCleverTapEndpoint returns the API endpoint for the given region
func (u *ClevertapServiceImpl) getCleverTapEndpoint(region string) (string, error) {
// Mapping of regions to endpoints
endpoints := map[string]string{
"IN": "in1.api.clevertap.com",
"SINGAPORE": "sg1.api.clevertap.com",
"US": "us1.api.clevertap.com",
"INDONESIA": "aps3.api.clevertap.com",
"UAE": "mec1.api.clevertap.com",
"EU": "api.clevertap.com",
}

// Normalize the region input to uppercase for case-insensitivity
region = strings.ToUpper(region)

// Check if the region exists in the map
if endpoint, exists := endpoints[region]; exists {
return endpoint, nil
}

// Return an error if the region is not recognized
return "", fmt.Errorf("unknown region: %s", region)
}

func (u *ClevertapServiceImpl) getBulkApi(destConfig DestinationConfig) *ClevertapServiceImpl {
endpoint, err := u.getCleverTapEndpoint(destConfig.Region)
if err != nil {
return nil
}
return &ClevertapServiceImpl{
BulkApi: fmt.Sprintf("https://%s/get_custom_list_segment_url", endpoint),
NotifyApi: fmt.Sprintf("https://%s/upload_custom_list_segment_completed", endpoint),
}
}

func (*ClevertapBulkUploader) Transform(job *jobsdb.JobT) (string, error) {
return common.GetMarshalledData(string(job.EventPayload), job.JobID)

Check warning on line 25 in router/batchrouter/asyncdestinationmanager/clevertap_segment/clevertapSegment.go

View check run for this annotation

Codecov / codecov/patch

router/batchrouter/asyncdestinationmanager/clevertap_segment/clevertapSegment.go#L24-L25

Added lines #L24 - L25 were not covered by tests
}
Expand Down Expand Up @@ -160,33 +123,6 @@ func (u *ClevertapBulkUploader) getPresignedS3URL(appKey, accessToken string) (s
return result.PresignedS3URL, nil
}

// Function to convert *backendconfig.Connection to ConnectionConfig using marshal and unmarshal
func (u *ClevertapServiceImpl) convertToConnectionConfig(conn *backendconfig.Connection) (*ConnectionConfig, error) {
if conn == nil {
return nil, fmt.Errorf("connection is nil")
}

// Marshal the backendconfig.Connection to JSON
data, err := json.Marshal(conn)
if err != nil {
return nil, fmt.Errorf("failed to marshal connection: %w", err)
}

// Unmarshal the JSON into ConnectionConfig
var connConfig ConnectionConfig
err = json.Unmarshal(data, &connConfig)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal to ConnectionConfig: %w", err)
}

// Set default SenderName if it is empty
if connConfig.Config.Destination.SenderName == "" {
connConfig.Config.Destination.SenderName = DEFAULT_SENDER_NAME
}

return &connConfig, nil
}

func (u *ClevertapBulkUploader) namingSegment(presignedURL, csvFilePath, appKey, accessToken string) error {
url := u.notifyEndpoint

Expand Down Expand Up @@ -283,7 +219,7 @@ func (u *ClevertapBulkUploader) Upload(asyncDestStruct *common.AsyncDestinationS
AbortCount: len(asyncDestStruct.ImportingJobIDs),
DestinationID: asyncDestStruct.Destination.ID,
AbortJobIDs: asyncDestStruct.ImportingJobIDs,
AbortReason: fmt.Sprintf("%s %v", "Error while fetching presigned url", err.Error()),
AbortReason: fmt.Sprintf("%s %v", "Error while fetching presigned url", urlErr),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,57 @@ var _ = Describe("CLEVERTAP_SEGMENT test", func() {
Expect(received).To(Equal(expected))
})

It("TestClevertapErrorWhilePreSignedURLFetch", func() {
initClevertap()
ctrl := gomock.NewController(GinkgoT())
defer ctrl.Finish()

clevertapService := mocks.NewMockClevertapService(ctrl)
clevertapServiceImpl := ClevertapSegment.ClevertapServiceImpl{
BulkApi: "https://api.clevertap.com/get_custom_list_segment_url",
NotifyApi: "https://api.clevertap.com/upload_custom_list_segment_completed",
ConnectionConfig: &ClevertapSegment.ConnectionConfig{
SourceId: "source123",
DestinationId: "destination456",
Enabled: true,
Config: ClevertapSegment.ConnConfig{
Destination: ClevertapSegment.Destination{
SchemaVersion: "v1.0",
SegmentName: "User Segment A",
AdminEmail: "admin@example.com",
SenderName: "Rudderstack",
},
},
},
}

bulkUploader := common.SimpleAsyncDestinationManager{UploaderAndTransformer: ClevertapSegment.NewClevertapBulkUploader(logger.NOP, stats.NOP, "CLEVERTAP_SEGMENT", destination.Config["clevertapAccountKey"].(string), destination.Config["clevertapAccountId"].(string), &clevertapServiceImpl, clevertapService, clevertapServiceImpl.ConnectionConfig)}

asyncDestination := common.AsyncDestinationStruct{
ImportingJobIDs: []int64{1, 2, 3, 4},
FailedJobIDs: []int64{},
FileName: filepath.Join(currentDir, "testdata/uploadData.txt"),
Destination: &destination,
}

// Mock handling for MakeHTTPRequest
clevertapService.EXPECT().
MakeHTTPRequest(gomock.Any()).
Return([]byte(`{"error": "Invalid Credentials", "status": "fail", "code": 401}`), 401, nil).
Times(1)

expected := common.AsyncUploadOutput{
AbortReason: "Error while fetching presigned url Error while fetching preSignedUrl: Invalid Credentials",
ImportingJobIDs: nil,
AbortJobIDs: []int64{1, 2, 3, 4},
ImportingCount: 0,
AbortCount: 4,
}

received := bulkUploader.Upload(&asyncDestination)
Expect(received).To(Equal(expected))
})

It("TestSuccessfulClevertapUpload", func() {
initClevertap()
ctrl := gomock.NewController(GinkgoT())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package clevertapSegment
import (
"bufio"
"encoding/csv"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/google/uuid"
jsoniter "github.com/json-iterator/go"

"github.com/rudderlabs/rudder-go-kit/stats"

backendconfig "github.com/rudderlabs/rudder-server/backend-config"
"github.com/rudderlabs/rudder-server/utils/misc"
)

Expand Down Expand Up @@ -153,3 +156,65 @@ func (u *ClevertapBulkUploader) createCSVFile(existingFilePath string) (*ActionF

return actionFile, nil
}

// GetCleverTapEndpoint returns the API endpoint for the given region
func (u *ClevertapServiceImpl) getCleverTapEndpoint(region string) (string, error) {
// Mapping of regions to endpoints
endpoints := map[string]string{
"IN": "in1.api.clevertap.com",
"SINGAPORE": "sg1.api.clevertap.com",
"US": "us1.api.clevertap.com",
"INDONESIA": "aps3.api.clevertap.com",
"UAE": "mec1.api.clevertap.com",
"EU": "api.clevertap.com",
}

// Normalize the region input to uppercase for case-insensitivity
region = strings.ToUpper(region)

// Check if the region exists in the map
if endpoint, exists := endpoints[region]; exists {
return endpoint, nil
}

// Return an error if the region is not recognized
return "", fmt.Errorf("unknown region: %s", region)
}

func (u *ClevertapServiceImpl) getBulkApi(destConfig DestinationConfig) *ClevertapServiceImpl {
endpoint, err := u.getCleverTapEndpoint(destConfig.Region)
if err != nil {
return nil
}

Check warning on line 188 in router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go

View check run for this annotation

Codecov / codecov/patch

router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go#L187-L188

Added lines #L187 - L188 were not covered by tests
return &ClevertapServiceImpl{
BulkApi: fmt.Sprintf("https://%s/get_custom_list_segment_url", endpoint),
NotifyApi: fmt.Sprintf("https://%s/upload_custom_list_segment_completed", endpoint),
}
}

// Function to convert *backendconfig.Connection to ConnectionConfig using marshal and unmarshal
func (u *ClevertapServiceImpl) convertToConnectionConfig(conn *backendconfig.Connection) (*ConnectionConfig, error) {
if conn == nil {
return nil, fmt.Errorf("connection is nil")
}

// Marshal the backendconfig.Connection to JSON
data, err := json.Marshal(conn)
if err != nil {
return nil, fmt.Errorf("failed to marshal connection: %w", err)
}

Check warning on line 205 in router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go

View check run for this annotation

Codecov / codecov/patch

router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go#L204-L205

Added lines #L204 - L205 were not covered by tests

// Unmarshal the JSON into ConnectionConfig
var connConfig ConnectionConfig
err = json.Unmarshal(data, &connConfig)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal to ConnectionConfig: %w", err)
}

Check warning on line 212 in router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go

View check run for this annotation

Codecov / codecov/patch

router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go#L211-L212

Added lines #L211 - L212 were not covered by tests

// Set default SenderName if it is empty
if connConfig.Config.Destination.SenderName == "" {
connConfig.Config.Destination.SenderName = DEFAULT_SENDER_NAME
}

Check warning on line 217 in router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go

View check run for this annotation

Codecov / codecov/patch

router/batchrouter/asyncdestinationmanager/clevertap_segment/utils.go#L216-L217

Added lines #L216 - L217 were not covered by tests

return &connConfig, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package clevertapSegment

import (
"testing"

backendconfig "github.com/rudderlabs/rudder-server/backend-config"
)

func TestGetCleverTapEndpoint(t *testing.T) {
tests := []struct {
region string
expectErr bool
expectURL string
}{
{"IN", false, "in1.api.clevertap.com"},
{"SINGAPORE", false, "sg1.api.clevertap.com"},
{"US", false, "us1.api.clevertap.com"},
{"UNKNOWN", true, ""},
}

for _, test := range tests {
t.Run(test.region, func(t *testing.T) {
service := &ClevertapServiceImpl{}
endpoint, err := service.getCleverTapEndpoint(test.region)

if test.expectErr && err == nil {
t.Errorf("expected an error for region %s, got none", test.region)
}
if !test.expectErr && err != nil {
t.Errorf("did not expect an error for region %s, got: %v", test.region, err)
}
if endpoint != test.expectURL {
t.Errorf("expected URL %s, got %s", test.expectURL, endpoint)
}
})
}
}

func TestGetBulkApi(t *testing.T) {
service := &ClevertapServiceImpl{}
destConfig := DestinationConfig{Region: "IN"}

bulkApi := service.getBulkApi(destConfig)
if bulkApi == nil {
t.Fatal("expected a non-nil bulk API service")
}
if bulkApi.BulkApi == "" {
t.Error("expected a non-empty BulkApi URL")
}
}

func TestConvertToConnectionConfig(t *testing.T) {
tests := []struct {
conn *backendconfig.Connection
expectErr bool
}{
{nil, true},
{&backendconfig.Connection{
SourceID: "source123",
DestinationID: "destination456",
Enabled: true,
Config: map[string]interface{}{
"Destination": map[string]interface{}{
"SchemaVersion": "v1.0",
"SegmentName": "User Segment A",
"AdminEmail": "admin@example.com",
"SenderName": "Rudderstack",
},
},
}, false},
}

for _, test := range tests {
t.Run("", func(t *testing.T) {
service := &ClevertapServiceImpl{}
connConfig, err := service.convertToConnectionConfig(test.conn)

if test.expectErr && err == nil {
t.Error("expected an error, got none")
}
if !test.expectErr && err != nil {
t.Errorf("did not expect an error, got: %v", err)
}
if !test.expectErr && connConfig.Config.Destination.SenderName != DEFAULT_SENDER_NAME {
t.Errorf("expected SenderName to be set to default, got: %s", connConfig.Config.Destination.SenderName)
}
})
}
}

0 comments on commit 31c5c11

Please sign in to comment.