From 8fe42a77dafeebc62b66a97384adf0b127b93379 Mon Sep 17 00:00:00 2001 From: NSSPKrishna Date: Fri, 10 May 2024 03:24:27 +0530 Subject: [PATCH] chore: Tests and modified change tracking --- internal/entities/command_deployment.go | 11 +-- .../utils/validation/entity_guid_validator.go | 22 +++--- .../validation/entity_guid_validator_test.go | 72 +++++++++++++++++++ 3 files changed, 93 insertions(+), 12 deletions(-) create mode 100644 internal/utils/validation/entity_guid_validator_test.go diff --git a/internal/entities/command_deployment.go b/internal/entities/command_deployment.go index 0bf940303..94574964a 100644 --- a/internal/entities/command_deployment.go +++ b/internal/entities/command_deployment.go @@ -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 ( @@ -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 diff --git a/internal/utils/validation/entity_guid_validator.go b/internal/utils/validation/entity_guid_validator.go index 3ff48ebd5..0baceea9c 100644 --- a/internal/utils/validation/entity_guid_validator.go +++ b/internal/utils/validation/entity_guid_validator.go @@ -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 { @@ -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), "|") @@ -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{ diff --git a/internal/utils/validation/entity_guid_validator_test.go b/internal/utils/validation/entity_guid_validator_test.go new file mode 100644 index 000000000..a80f25ca0 --- /dev/null +++ b/internal/utils/validation/entity_guid_validator_test.go @@ -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) + } +}