diff --git a/cmd/aliases.go b/cmd/aliases.go index d175d2bc..c1779f6d 100644 --- a/cmd/aliases.go +++ b/cmd/aliases.go @@ -17,12 +17,25 @@ var aliasesCmd = &cobra.Command{ } var aliasListCmd = &cobra.Command{ - Use: "list ", + Use: "list ...", Short: "Lists all aliases for a resource", - Args: cobra.MinimumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - if len(args) != 0 { - resource, ok := resources.GetResourceByName(args[0]) + + resourcesToPrint := args + + if len(resourcesToPrint) == 0 { + resourcesToPrint = resources.GetSingularResourceNames() + } + + sort.Strings(resourcesToPrint) + if len(resourcesToPrint) != 1 { + fmt.Printf("%45s || %100s || Values\n", "Resource Type", "Alias Name") + } else { + fmt.Printf("%45s || Values\n", "Alias Name") + } + + for _, resourceName := range resourcesToPrint { + resource, ok := resources.GetResourceByName(resourceName) if !ok { return fmt.Errorf("could not find resource information for resource: %s", args[0]) } @@ -37,10 +50,14 @@ var aliasListCmd = &cobra.Command{ sort.Strings(sortedAliasNames) - fmt.Printf("%40s || Values\n", "Alias Name") - for _, alias := range sortedAliasNames { - fmt.Printf("%40s => ID: %s", alias, aliases[alias].Id) + + if len(resourcesToPrint) != 1 { + fmt.Printf("%45s %100s => ID: %s", resourceName, alias, aliases[alias].Id) + } else { + fmt.Printf("%45s => ID: %s", alias, aliases[alias].Id) + } + if aliases[alias].Sku != "" { fmt.Printf(" Sku: %10s", aliases[alias].Sku) } @@ -51,10 +68,10 @@ var aliasListCmd = &cobra.Command{ fmt.Println() } - - return nil } - return fmt.Errorf("you must supply a resource type to the aliases command") + + return nil + }, ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { diff --git a/external/aliases/aliases.go b/external/aliases/aliases.go index d20e72a4..074a64c6 100644 --- a/external/aliases/aliases.go +++ b/external/aliases/aliases.go @@ -11,6 +11,7 @@ import ( "io/ioutil" "os" "path/filepath" + "regexp" "strconv" "strings" "sync" @@ -42,6 +43,12 @@ func ClearAllAliasesForJsonApiType(jsonApiType string) error { } +// Used to determine index of element in array. +var arrayPathPattern = regexp.MustCompile("^\\.data\\[([0-9]+)]$") + +// Used to determine name of relationship +var relationshipPattern = regexp.MustCompile("^\\.data(?:\\[[0-9]+])?\\.relationships\\.([^.]+)\\.data") + func GetAliasesForJsonApiType(jsonApiType string) map[string]*id.IdableAttributes { profileDirectory := getAliasDataDirectory() aliasFile := getAliasFileForJsonApiType(profileDirectory, jsonApiType) @@ -90,7 +97,7 @@ func SaveAliasesForResources(jsonTxt string) { } results := map[string]map[string]*id.IdableAttributes{} - visitResources(jsonStruct, "", results) + visitResources(jsonStruct, "", "", map[string]*id.IdableAttributes{}, results) log.Tracef("All aliases: %v", results) @@ -201,12 +208,16 @@ func saveAliasesForResource(jsonApiType string, newAliases map[string]*id.Idable }) } -func visitResources(data map[string]interface{}, prefix string, results map[string]map[string]*id.IdableAttributes) { +func visitResources(data map[string]interface{}, prefix string, parentAliasType string, parentAliases map[string]*id.IdableAttributes, results map[string]map[string]*id.IdableAttributes) { + aliases := map[string]*id.IdableAttributes{} + myType := "" if typeObj, typeKeyExists := data["type"]; typeKeyExists { if idObj, idKeyExists := data["id"]; idKeyExists { if typeKeyValue, typeKeyIsString := typeObj.(string); typeKeyIsString { if idKeyValue, idKeyIsString := idObj.(string); idKeyIsString { - aliases := generateAliasesForStruct(typeKeyValue, idKeyValue, data) + + myType = typeKeyValue + aliases = generateAliasesForStruct(prefix, parentAliasType, parentAliases, typeKeyValue, idKeyValue, data) log.Tracef("Found a type and id pair %s => %s under prefix %s, aliases %v", typeKeyValue, idKeyValue, prefix, aliases) @@ -222,16 +233,26 @@ func visitResources(data map[string]interface{}, prefix string, results map[stri } } + parentAliasesToUse := parentAliases + + if len(aliases) > 0 { + parentAliasesToUse = aliases + } + + parentAliasTypeToUse := parentAliasType + if myType != "" { + parentAliasTypeToUse = myType + } // Recursively descend over each element for key, val := range data { if mapType, ok := val.(map[string]interface{}); ok { - visitResources(mapType, prefix+"."+key, results) + visitResources(mapType, prefix+"."+key, parentAliasTypeToUse, parentAliasesToUse, results) } if arrayType, ok := val.([]interface{}); ok { for idx, value := range arrayType { if mapValue, ok := value.(map[string]interface{}); ok { - visitResources(mapValue, prefix+"."+key+"["+strconv.Itoa(idx)+"]", results) + visitResources(mapValue, prefix+"."+key+"["+strconv.Itoa(idx)+"]", parentAliasTypeToUse, parentAliasesToUse, results) } } @@ -242,7 +263,7 @@ func visitResources(data map[string]interface{}, prefix string, results map[stri return } -func generateAliasesForStruct(typeKey string, idKey string, data map[string]interface{}) map[string]*id.IdableAttributes { +func generateAliasesForStruct(prefix string, parentAliasType string, parentAliases map[string]*id.IdableAttributes, typeKey string, idKey string, data map[string]interface{}) map[string]*id.IdableAttributes { result := id.IdableAttributes{ Id: idKey, } @@ -252,6 +273,26 @@ func generateAliasesForStruct(typeKey string, idKey string, data map[string]inte "id=" + idKey: &result, } + if prefix == ".data" { + results["last_read=entity"] = &result + } + + if arrayPathPattern.MatchString(prefix) { + matches := arrayPathPattern.FindStringSubmatch(prefix) + results["last_read=array["+matches[1]+"]"] = &result + } + + if relationshipPattern.MatchString(prefix) { + matches := relationshipPattern.FindStringSubmatch(prefix) + //related_buz_for_foo_id_123 + keyPrefix := "related_" + matches[1] + "_for_" + parentAliasType + "_" + + for k := range parentAliases { + results[keyPrefix+k] = &result + } + + } + jsonObjectsToInspect := make([]map[string]interface{}, 0) jsonObjectsToInspect = append(jsonObjectsToInspect, data) diff --git a/external/aliases/aliases_test.go b/external/aliases/aliases_test.go index 50b9c3c9..95488284 100644 --- a/external/aliases/aliases_test.go +++ b/external/aliases/aliases_test.go @@ -1,6 +1,7 @@ package aliases import ( + "github.com/stretchr/testify/assert" "io/ioutil" "os" "testing" @@ -32,14 +33,15 @@ func TestSavedAliasIsReturnedInAllAliasesForSingleResponse(t *testing.T) { aliases := GetAliasesForJsonApiType("foo") // Verification - - if len(aliases) != 1 { + if len(aliases) != 2 { t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) } func TestSavedAliasAppendsAndPreservesPreviousUnrelatedAliases(t *testing.T) { @@ -74,17 +76,18 @@ func TestSavedAliasAppendsAndPreservesPreviousUnrelatedAliases(t *testing.T) { // Verification - if len(aliases) != 2 { + if len(aliases) != 3 { t.Errorf("There should be two aliases for the type foo, not %d", len(aliases)) } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=456"] != nil && aliases["id=456"].Id != "456" { - t.Errorf("Alias should exist for id=456") - } + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "456", aliases["last_read=entity"].Id) } func TestSavedAliasIsReplacedWhenNewEntityHasTheSameAttributeValue(t *testing.T) { @@ -121,21 +124,21 @@ func TestSavedAliasIsReplacedWhenNewEntityHasTheSameAttributeValue(t *testing.T) // Verification - if len(aliases) != 3 { + if len(aliases) != 4 { t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=456"] != nil && aliases["id=456"].Id != "456" { - t.Errorf("Alias should exist for id=456") - } + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) - if aliases["name=Alpha"] != nil && aliases["name=Alpha"].Id != "456" { - t.Errorf("Alias should exist for id=456") - } + assert.Contains(t, aliases, "name=Alpha") + assert.Equal(t, "456", aliases["name=Alpha"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "456", aliases["last_read=entity"].Id) } func TestSavedAliasIsReplacedWhenSameEntityHasANewValue(t *testing.T) { @@ -172,17 +175,17 @@ func TestSavedAliasIsReplacedWhenSameEntityHasANewValue(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be two aliases for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["name=Beta"] != nil && aliases["name=Beta"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "name=Beta") + assert.Equal(t, "123", aliases["name=Beta"].Id) + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) } func TestDeleteAliasByIdDeletesAnAlias(t *testing.T) { @@ -219,13 +222,15 @@ func TestDeleteAliasByIdDeletesAnAlias(t *testing.T) { // Verification - if len(aliases) != 1 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 2 { + t.Errorf("There should be two alias for the type foo, not %d", len(aliases)) } - if aliases["id=456"] != nil && aliases["id=456"].Id != "456" { - t.Errorf("Alias should exist for id=456") - } + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "456", aliases["last_read=entity"].Id) } func TestAllAliasesAreReturnedInAllAliasesForArrayResponse(t *testing.T) { @@ -256,17 +261,21 @@ func TestAllAliasesAreReturnedInAllAliasesForArrayResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 4 { + t.Errorf("There should be four aliases for the type foo, not %d", len(aliases)) } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=456"] != nil && aliases["id=456"].Id != "456" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) + + assert.Contains(t, aliases, "last_read=array[0]") + assert.Equal(t, "123", aliases["last_read=array[0]"].Id) + + assert.Contains(t, aliases, "last_read=array[1]") + assert.Equal(t, "456", aliases["last_read=array[1]"].Id) } func TestSavedAliasIsReturnedForAnEmailInLegacyObjectResponse(t *testing.T) { @@ -293,17 +302,18 @@ func TestSavedAliasIsReturnedForAnEmailInLegacyObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three alias for the type foo, not %d", len(aliases)) } - if aliases["email=test@test.com"] != nil && aliases["email=test@test.com"].Id != "123" { - t.Errorf("Alias should exist for email=test@test.com") - } + assert.Contains(t, aliases, "email=test@test.com") + assert.Equal(t, "123", aliases["email=test@test.com"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) } func TestSavedAliasIsReturnedForAnSkuInLegacyObjectResponse(t *testing.T) { @@ -330,25 +340,27 @@ func TestSavedAliasIsReturnedForAnSkuInLegacyObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["sku=test"] != nil && aliases["sku=test"].Id != "123" { - t.Errorf("Alias should exist for sku=test") - } + assert.Contains(t, aliases, "sku=test") + assert.Equal(t, "123", aliases["sku=test"].Id) - if aliases["sku=test"] != nil && aliases["sku=test"].Sku != "test" { - t.Errorf("Alias should exist for sku=test and have sku test") - } + assert.Contains(t, aliases, "sku=test") + assert.Equal(t, "test", aliases["sku=test"].Sku) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Sku != "test" { - t.Errorf("Alias should exist for id=123 and have a sku of test") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "test", aliases["id=123"].Sku) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "test", aliases["last_read=entity"].Sku) } func TestSavedAliasIsReturnedForASlugInLegacyObjectResponse(t *testing.T) { @@ -375,25 +387,27 @@ func TestSavedAliasIsReturnedForASlugInLegacyObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["slug=test"] != nil && aliases["slug=test"].Id != "123" { - t.Errorf("Alias should exist for slug=test") - } + assert.Contains(t, aliases, "slug=test") + assert.Equal(t, "123", aliases["slug=test"].Id) - if aliases["slug=test"] != nil && aliases["slug=test"].Slug != "test" { - t.Errorf("Alias should exist for slug=test and have slug value of test") - } + assert.Contains(t, aliases, "slug=test") + assert.Equal(t, "test", aliases["slug=test"].Slug) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Slug != "test" { - t.Errorf("Alias should exist for id=123 and have a slug of test") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "test", aliases["id=123"].Slug) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "test", aliases["last_read=entity"].Slug) } func TestSavedAliasIsReturnedForANameInLegacyObjectResponse(t *testing.T) { @@ -420,17 +434,19 @@ func TestSavedAliasIsReturnedForANameInLegacyObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["name=Test_Testerson"] != nil && aliases["name=Test_Testerson"].Id != "123" { - t.Errorf("Alias should exist for name=Test_Testerson") - } + assert.Contains(t, aliases, "name=Test_Testerson") + assert.Equal(t, "123", aliases["name=Test_Testerson"].Id) + + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } } func TestSavedAliasIsReturnedForAnEmailInComplaintObjectResponse(t *testing.T) { @@ -459,17 +475,19 @@ func TestSavedAliasIsReturnedForAnEmailInComplaintObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["email=test@test.com"] != nil && aliases["email=test@test.com"].Id != "123" { - t.Errorf("Alias should exist for email=test@test.com") - } + assert.Contains(t, aliases, "email=test@test.com") + assert.Equal(t, "123", aliases["email=test@test.com"].Id) + + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } } func TestSavedAliasIsReturnedForAnSkuInComplaintObjectResponse(t *testing.T) { @@ -498,25 +516,27 @@ func TestSavedAliasIsReturnedForAnSkuInComplaintObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three alias for the type foo, not %d", len(aliases)) } - if aliases["sku=test"] != nil && aliases["sku=test"].Id != "123" { - t.Errorf("Alias should exist for sku=test") - } + assert.Contains(t, aliases, "sku=test") + assert.Equal(t, "123", aliases["sku=test"].Id) - if aliases["sku=test"] != nil && aliases["sku=test"].Sku != "test" { - t.Errorf("Alias should exist for sku=test and have a sku of test") - } + assert.Contains(t, aliases, "sku=test") + assert.Equal(t, "test", aliases["sku=test"].Sku) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Sku != "test" { - t.Errorf("Alias should exist for id=123 and have a sku of test") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "test", aliases["id=123"].Sku) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "test", aliases["last_read=entity"].Sku) } func TestSavedAliasIsReturnedForASlugInComplaintObjectResponse(t *testing.T) { @@ -545,25 +565,27 @@ func TestSavedAliasIsReturnedForASlugInComplaintObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three aliases for the type foo, not %d", len(aliases)) } - if aliases["slug=test"] != nil && aliases["slug=test"].Id != "123" { - t.Errorf("Alias should exist for slug=test") - } + assert.Contains(t, aliases, "slug=test") + assert.Equal(t, "123", aliases["slug=test"].Id) - if aliases["slug=test"] != nil && aliases["slug=test"].Slug != "test" { - t.Errorf("Alias should exist for slug=test and have a slug of test") - } + assert.Contains(t, aliases, "slug=test") + assert.Equal(t, "test", aliases["slug=test"].Slug) - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) - if aliases["id=123"] != nil && aliases["id=123"].Slug != "test" { - t.Errorf("Alias should exist for id=123 and have a slug of test") - } + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "test", aliases["id=123"].Slug) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "test", aliases["last_read=entity"].Slug) } func TestSavedAliasIsReturnedForANameInComplaintObjectResponse(t *testing.T) { @@ -592,17 +614,135 @@ func TestSavedAliasIsReturnedForANameInComplaintObjectResponse(t *testing.T) { // Verification - if len(aliases) != 2 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 3 { + t.Errorf("There should be three alias for the type foo, not %d", len(aliases)) } - if aliases["name=Test_Testerson"] != nil && aliases["name=Test_Testerson"].Id != "123" { - t.Errorf("Alias should exist for name=Test_Testerson") + assert.Contains(t, aliases, "name=Test_Testerson") + assert.Equal(t, "123", aliases["name=Test_Testerson"].Id) + + assert.Contains(t, aliases, "id=123") + assert.Equal(t, "123", aliases["id=123"].Id) + + assert.Contains(t, aliases, "last_read=entity") + assert.Equal(t, "123", aliases["last_read=entity"].Id) + +} + +func TestSavedAliasIsReturnedForARelationshipObjectInArrayResponse(t *testing.T) { + err := ClearAllAliases() + if err != nil { + t.Fatalf("Could not clear aliases") } - if aliases["id=123"] != nil && aliases["id=123"].Id != "123" { - t.Errorf("Alias should exist for id=123") + // Execute SUT + SaveAliasesForResources( + // language=JSON + ` +{ + "data": [{ + "id": "123", + "type": "foo", + "name": "Test Testerson", + "relationships": { + "buz": { + "data": { + "type": "bar", + "id": "abc" + } + } + } + },{ + "id": "456", + "type": "foo", + "name": "Bob Robertson", + "relationships": { + "buz": { + "data": { + "type": "bar", + "id": "def" + } + } + } + }] + +}`) + + aliases := GetAliasesForJsonApiType("bar") + + if len(aliases) != 8 { + t.Errorf("There should be eight aliases for the type bar, not %d", len(aliases)) } + + assert.Contains(t, aliases, "id=abc") + assert.Equal(t, "abc", aliases["id=abc"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_id=123") + assert.Equal(t, "abc", aliases["related_buz_for_foo_id=123"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_last_read=array[0]") + assert.Equal(t, "abc", aliases["related_buz_for_foo_last_read=array[0]"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_name=Test_Testerson") + assert.Equal(t, "abc", aliases["related_buz_for_foo_name=Test_Testerson"].Id) + + assert.Contains(t, aliases, "id=def") + assert.Equal(t, "def", aliases["id=def"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_id=456") + assert.Equal(t, "def", aliases["related_buz_for_foo_id=456"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_last_read=array[1]") + assert.Equal(t, "def", aliases["related_buz_for_foo_last_read=array[1]"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_name=Bob_Robertson") + assert.Equal(t, "def", aliases["related_buz_for_foo_name=Bob_Robertson"].Id) +} + +func TestSavedAliasIsReturnedForARelationshipObjectInSingleResponse(t *testing.T) { + err := ClearAllAliases() + if err != nil { + t.Fatalf("Could not clear aliases") + } + + // Execute SUT + SaveAliasesForResources( + // language=JSON + ` +{ + "data": { + "id": "123", + "type": "foo", + "name": "Test Testerson", + "relationships": { + "buz": { + "data": { + "type": "bar", + "id": "456" + } + } + } + } + +}`) + + aliases := GetAliasesForJsonApiType("bar") + + if len(aliases) != 4 { + t.Errorf("There should be four aliases for the type bar, not %d", len(aliases)) + } + + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_id=123") + assert.Equal(t, "456", aliases["related_buz_for_foo_id=123"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_last_read=entity") + assert.Equal(t, "456", aliases["related_buz_for_foo_last_read=entity"].Id) + + assert.Contains(t, aliases, "related_buz_for_foo_name=Test_Testerson") + assert.Equal(t, "456", aliases["related_buz_for_foo_name=Test_Testerson"].Id) } func TestResolveAliasValuesReturnsAliasForMatchingValue(t *testing.T) { @@ -780,8 +920,8 @@ func TestClearAllAliasesForJsonTypeOnlyClearsJsonType(t *testing.T) { t.Errorf("There should be zero alias for the type foo, not %d", len(fooAliases)) } - if len(barAliases) != 1 { - t.Errorf("There should be one alias for the type bar, not %d", len(barAliases)) + if len(barAliases) != 2 { + t.Errorf("There should be two alias for the type bar, not %d", len(barAliases)) } } @@ -866,12 +1006,11 @@ func TestThatCorruptAliasFileDoesntCrashProgramWhenSavingAliases(t *testing.T) { aliases := GetAliasesForJsonApiType("foo") // Verification - if len(aliases) != 1 { - t.Errorf("There should be one alias for the type foo, not %d", len(aliases)) + if len(aliases) != 2 { + t.Errorf("There should be two aliases for the type foo, not %d", len(aliases)) } - if aliases["id=456"] != nil && aliases["id=456"].Id != "456" { - t.Errorf("Alias should exist for id=456") - } + assert.Contains(t, aliases, "id=456") + assert.Equal(t, "456", aliases["id=456"].Id) } diff --git a/external/resources/resources.yaml b/external/resources/resources.yaml index 303b6f78..07d5ddc4 100644 --- a/external/resources/resources.yaml +++ b/external/resources/resources.yaml @@ -323,7 +323,7 @@ currencies: enabled: type: BOOL customer-authentication-settings: - singular-name: "customer-authentication-settings" + singular-name: "customer-authentication-setting" json-api-type: "customer-authentication-settings" json-api-format: "legacy" docs: "https://documentation.elasticpath.com/commerce-cloud/docs/api/advanced/settings/customer-authentication-settings/index.html" @@ -332,7 +332,7 @@ customer-authentication-settings: url: "/v2/settings/customer-authentication" get-entity: docs: "https://documentation.elasticpath.com/commerce-cloud/docs/api/advanced/settings/customer-authentication-settings/get-customer-authentication-settings.html" - url: "/v2/settings/customer-authentication" + url: "/v2/settings/customer-authentication/{customer_authentication_settings}" customers: singular-name: "customer" json-api-type: "customer" diff --git a/go.mod b/go.mod index 6bf0591f..538a4303 100644 --- a/go.mod +++ b/go.mod @@ -19,9 +19,13 @@ require ( ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/itchyny/timefmt-go v0.1.3 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.4.0 // indirect + github.com/stretchr/testify v1.8.0 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/net v0.0.0-20210813160813-60bc85c4be6d // indirect golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect diff --git a/go.sum b/go.sum index f6ea0106..e2098c13 100644 --- a/go.sum +++ b/go.sum @@ -118,10 +118,15 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/thediveo/enumflag v0.10.1 h1:DB3Ag69VZ7BCv6jzKECrZ0ebZrHLzFRMIFYt96s4OxM= github.com/thediveo/enumflag v0.10.1/go.mod h1:KyVhQUPzreSw85oJi2uSjFM0ODLKXBH0rPod7zc2pmI= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=