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 10, 2024
1 parent 21e8f75 commit 8fe42a7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 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
22 changes: 14 additions & 8 deletions internal/utils/validation/entity_guid_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@ const (
DELIMITER = "|"
)

var (
invalidEntityGuidErr = errors.New("invalid entity GUID format")
emptyEntityTypeErr = errors.New("entity type is required")
emptyDomainIdErr = errors.New("domain ID is required")
)
var EntityGUIDValidationErrorTypes = struct {
INVALID_ENTITY_GUID_ERROR EntityGUIDValidationError
EMPTY_ENTITY_TYPE_ERROR EntityGUIDValidationError
EMPTY_DOMAIN_ID_ERROR EntityGUIDValidationError
}{
INVALID_ENTITY_GUID_ERROR: errors.New("invalid entity GUID format"),
EMPTY_ENTITY_TYPE_ERROR: errors.New("entity type is required"),
EMPTY_DOMAIN_ID_ERROR: errors.New("domain ID is required"),
}

type EntityGUIDValidationError error

// GenericEntity represents the decoded entity information
type GenericEntity struct {
Expand All @@ -30,7 +36,7 @@ type GenericEntity struct {
func DecodeEntityGuid(entityGuid string) (*GenericEntity, error) {
decodedGuid, err := base64.StdEncoding.DecodeString(entityGuid)
if err != nil {
return nil, invalidEntityGuidErr
return nil, EntityGUIDValidationErrorTypes.INVALID_ENTITY_GUID_ERROR
}

parts := strings.Split(string(decodedGuid), "|")
Expand All @@ -48,11 +54,11 @@ func DecodeEntityGuid(entityGuid string) (*GenericEntity, error) {
domainId := parts[3]

if entityType == "" {
return nil, emptyEntityTypeErr
return nil, EntityGUIDValidationErrorTypes.EMPTY_ENTITY_TYPE_ERROR
}

if domainId == "" {
return nil, emptyDomainIdErr
return nil, EntityGUIDValidationErrorTypes.EMPTY_DOMAIN_ID_ERROR
}

return &GenericEntity{
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 != EntityGUIDValidationErrorTypes.INVALID_ENTITY_GUID_ERROR {
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 != EntityGUIDValidationErrorTypes.EMPTY_ENTITY_TYPE_ERROR {
t.Errorf("Expected error 'entity type is required', got %v", err)
}
}

0 comments on commit 8fe42a7

Please sign in to comment.