Skip to content

Commit

Permalink
chore: Tests and modified change tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
NSSPKrishna committed May 9, 2024
1 parent 21e8f75 commit 98ddce9
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/entities/command_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ import (

"github.com/spf13/cobra"

"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"

"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-cli/internal/output"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-cli/internal/utils/validation"
"github.com/newrelic/newrelic-client-go/v2/pkg/changetracking"
"github.com/newrelic/newrelic-client-go/v2/pkg/common"

"github.com/newrelic/newrelic-cli/internal/client"
"github.com/newrelic/newrelic-client-go/v2/pkg/nrtime"
)

var (
Expand Down Expand Up @@ -68,6 +67,10 @@ The deployment command marks a change for a New Relic entity
log.Fatal("--version cannot be empty")
}

if _, err := validation.DecodeEntityGuid(entityGUID); err != nil {
log.Fatal(err)
}

var (
attrs map[string]interface{}
err error
Expand Down
72 changes: 72 additions & 0 deletions internal/utils/validation/entity_guid_validator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package validation

import (
"encoding/base64"
"fmt"
"strconv"
"strings"
"testing"
)

func TestDecodeEntityGuid_Valid(t *testing.T) {
entity := GenericEntity{
AccountId: 12345,
Domain: "test_domain",
EntityType: "user",
DomainId: "abc123",
}
encodedGuid := encodeEntity(entity)

decodedEntity, err := DecodeEntityGuid(encodedGuid)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if decodedEntity.AccountId != entity.AccountId ||
decodedEntity.Domain != entity.Domain ||
decodedEntity.EntityType != entity.EntityType ||
decodedEntity.DomainId != entity.DomainId {
t.Errorf("Decoded entity does not match original entity")
}
}

func encodeEntity(entity GenericEntity) string {
parts := []string{
strconv.FormatInt(entity.AccountId, 10),
entity.Domain,
entity.EntityType,
entity.DomainId,
}
return base64.StdEncoding.EncodeToString([]byte(strings.Join(parts, DELIMITER)))
}

func TestDecodeEntityGuid_MissingDelimiter(t *testing.T) {
invalidGuid := "invalidentityguid"

_, err := DecodeEntityGuid(invalidGuid)

if err != invalidEntityGuidErr {
t.Errorf("Expected error 'invalid entity GUID format', got %v", err)
}
}

func TestDecodeEntityGuid_LessThanFourParts(t *testing.T) {
invalidGuid := base64.StdEncoding.EncodeToString([]byte("account|domain"))

_, err := DecodeEntityGuid(invalidGuid)

expectedErrorMessage := fmt.Sprintf("invalid entity GUID format: expected at least 4 parts delimited by '%s': %s", DELIMITER, invalidGuid)
if err.Error() != expectedErrorMessage {
t.Errorf("Expected error message: %s, got %v", expectedErrorMessage, err)
}
}

func TestDecodeEntityGuid_EmptyEntityType(t *testing.T) {
encodedGuid := base64.StdEncoding.EncodeToString([]byte("12345|domain||domainId"))

_, err := DecodeEntityGuid(encodedGuid)

if err != emptyEntityTypeErr {
t.Errorf("Expected error 'entity type is required', got %v", err)
}
}

0 comments on commit 98ddce9

Please sign in to comment.