package bsl_test import ( "context" "encoding/json" "testing" "time" "github.com/stretchr/testify/assert" "sigs.k8s.io/controller-runtime/pkg/client/fake" "edge-infra.dev/pkg/edge/api/graph/model" "edge-infra.dev/pkg/edge/api/utils" . "edge-infra.dev/pkg/edge/bsl" //nolint:revive //TODO: dont be lazy fakebsl "edge-infra.dev/pkg/edge/bsl/fake" "edge-infra.dev/pkg/edge/constants/api/banner" ) func TestSetID(t *testing.T) { bslInfo := &BSLInfo{ ID: "id-1", } bslInfo.SetID("id-2") assert.Equal(t, "id-2", bslInfo.ID) } func TestSetSiteName(t *testing.T) { bslInfo := &BSLInfo{ SiteName: "test-site-1", } bslInfo.SetSiteName("test-site-2") assert.Equal(t, "test-site-2", bslInfo.SiteName) } func TestSetEnterpriseUnitName(t *testing.T) { bslInfo := &BSLInfo{ EnterpriseUnitName: "test-store-1", } bslInfo.SetEnterpriseUnitName("test-store-2") assert.Equal(t, "test-store-2", bslInfo.EnterpriseUnitName) } func TestSetLatitude(t *testing.T) { bslInfo := &BSLInfo{ Coordinates: Coordinates{ Latitude: 90.0, }, } bslInfo.SetLatitude(92.3) assert.Equal(t, 92.3, bslInfo.Coordinates.Latitude) } func TestSetLongitude(t *testing.T) { bslInfo := &BSLInfo{ Coordinates: Coordinates{ Longitude: 180.0, }, } bslInfo.SetLongitude(153.3) assert.Equal(t, 153.3, bslInfo.Coordinates.Longitude) } func TestSetAddress(t *testing.T) { bslInfo := &BSLInfo{ Address: Address{ Street: "111 Test Street", City: "Atlanta", State: "Georgia", Country: "US", PostalCode: "30301", }, } newAddress := Address{ Street: "220 Testing Street", City: "Oklahoma City", State: "Oklahoma", Country: "US", PostalCode: "73089", } bslInfo.SetAddress(newAddress.Street, newAddress.City, newAddress.State, newAddress.Country, newAddress.PostalCode) assert.Equal(t, newAddress, bslInfo.Address) } func TestSetContact(t *testing.T) { bslInfo := &BSLInfo{ Contact: Contact{ ContactPerson: "Test Person", PhoneNumber: "2345678910", PhoneNumberCountryCode: "+1", }, } newContact := Contact{ ContactPerson: "New Person", PhoneNumber: "0987654321", PhoneNumberCountryCode: "+1", } bslInfo.SetContact(newContact.ContactPerson, newContact.PhoneNumber, newContact.PhoneNumberCountryCode) assert.Equal(t, newContact, bslInfo.Contact) } func TestSetCustomAttributes(t *testing.T) { bslInfo := &BSLInfo{ CustomAttributeSets: []CustomAttributeSet{ { TypeName: "test-type", Attributes: []Attribute{ { Key: "test-key", Value: "test-value", }, }, }, }, } newCustomAttributes := []CustomAttributeSet{ { TypeName: "test-type-1", Attributes: []Attribute{ { Key: "test-key-1", Value: "test-value-1", }, }, }, } bslInfo.SetCustomAttributes(newCustomAttributes) assert.Equal(t, newCustomAttributes, bslInfo.CustomAttributeSets) } func TestAddCustomAttributes(t *testing.T) { bslInfo := &BSLInfo{ CustomAttributeSets: []CustomAttributeSet{ { TypeName: "test-type", Attributes: []Attribute{ { Key: "test-key", Value: "test-value", }, }, }, }, } newCustomAttributes := []CustomAttributeSet{ { TypeName: "test-type-1", Attributes: []Attribute{ { Key: "test-key-1", Value: "test-value-1", }, }, }, } bslInfo.AddCustomAttributes(newCustomAttributes) for _, customAttribute := range newCustomAttributes { assert.Contains(t, bslInfo.CustomAttributeSets, customAttribute) } } func TestAddAttributeToCustomAttributeSet(t *testing.T) { bslInfo := &BSLInfo{ CustomAttributeSets: []CustomAttributeSet{ { TypeName: "test-type", Attributes: []Attribute{ { Key: "test-key", Value: "test-value", }, }, }, { TypeName: "test-type-1", Attributes: []Attribute{ { Key: "test-key-1", Value: "test-value-1", }, }, }, }, } newAttribute := Attribute{ Key: "test-key-2", Value: "test-value-2", } bslInfo.AddAttributeToCustomAttributeSet("test-type", newAttribute) for _, customAttribute := range bslInfo.CustomAttributeSets { if customAttribute.TypeName == "test-type" { assert.Contains(t, customAttribute.Attributes, newAttribute) } } } func TestSetCurrency(t *testing.T) { bslInfo := &BSLInfo{ Currency: "USD", } bslInfo.SetCurrency("GBP") assert.Equal(t, "GBP", bslInfo.Currency) } func TestSetCreatedOn(t *testing.T) { currentTime := time.Now().UTC() bslInfo := &BSLInfo{ CreatedOn: currentTime.Local().String(), } newTime := currentTime.Add(2 * time.Hour) bslInfo.SetCreatedOn(newTime.Local().String()) assert.Equal(t, newTime.Local().String(), bslInfo.CreatedOn) } func TestSetDescription(t *testing.T) { bslInfo := &BSLInfo{ Description: "Test Description", } bslInfo.SetDescription("New Description") assert.Equal(t, "New Description", bslInfo.Description) } func TestSetEnterpriseSettings(t *testing.T) { bslInfo := &BSLInfo{ EnterpriseSettings: []EnterpriseSetting{ { EnterpriseUnitID: "ent-unit-1", ConfigurationSetID: ConfigurationSetID{ Name: "test-config", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-1", Value: "value-1", }, }, }, }, } newEnterprisesettings := []EnterpriseSetting{ { EnterpriseUnitID: "ent-unit-2", ConfigurationSetID: ConfigurationSetID{ Name: "test-config-2", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-2", Value: "value-2", }, }, }, } bslInfo.SetEnterpriseSettings(newEnterprisesettings) assert.Equal(t, newEnterprisesettings, bslInfo.EnterpriseSettings) } func TestAddEnterpriseSetting(t *testing.T) { bslInfo := &BSLInfo{ EnterpriseSettings: []EnterpriseSetting{ { EnterpriseUnitID: "ent-unit-1", ConfigurationSetID: ConfigurationSetID{ Name: "test-config", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-1", Value: "value-1", }, }, }, }, } newEnterpriseSetting := EnterpriseSetting{ EnterpriseUnitID: "ent-unit-2", ConfigurationSetID: ConfigurationSetID{ Name: "test-config-2", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-2", Value: "value-2", }, }, } bslInfo.AddEnterpriseSetting(newEnterpriseSetting) assert.Contains(t, bslInfo.EnterpriseSettings, newEnterpriseSetting) } func TestAddEnterpriseConfigurationSetting(t *testing.T) { bslInfo := &BSLInfo{ EnterpriseSettings: []EnterpriseSetting{ { EnterpriseUnitID: "ent-unit-1", ConfigurationSetID: ConfigurationSetID{ Name: "test-config", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-1", Value: "value-1", }, }, }, { EnterpriseUnitID: "ent-unit-2", ConfigurationSetID: ConfigurationSetID{ Name: "test-config-2", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-2", Value: "value-2", }, }, }, }, } newConfigurationSetting := ConfigurationSetting{ Key: "key-2", Value: "value-2", } bslInfo.AddEnterpriseConfigurationSetting("ent-unit-1", newConfigurationSetting) for _, enterpriseSetting := range bslInfo.EnterpriseSettings { if enterpriseSetting.EnterpriseUnitID == "ent-unit-1" { assert.Contains(t, enterpriseSetting.ConfigurationSettings, newConfigurationSetting) } } } func TestSetLastModifiedOn(t *testing.T) { currentTime := time.Now().UTC() bslInfo := &BSLInfo{ LastModifiedOn: currentTime.Local().String(), } newTime := currentTime.Add(2 * time.Hour) bslInfo.SetLastModifiedOn(newTime.Local().String()) assert.Equal(t, newTime.Local().String(), bslInfo.LastModifiedOn) } func TestSetLocked(t *testing.T) { bslInfo := &BSLInfo{ Locked: false, } bslInfo.SetLocked(true) assert.True(t, bslInfo.Locked) } func TestSetStatus(t *testing.T) { bslInfo := &BSLInfo{ Status: Inactive.String(), } bslInfo.SetStatus(Active) assert.Equal(t, Active.String(), bslInfo.Status) } func TestSetDayparts(t *testing.T) { bslInfo := &BSLInfo{ DayParts: []DayParts{ { Name: "Office Hours", Description: "Test Dayparts", Day: Daily.String(), StartTime: "6:00 AM", EndTime: "5:00 PM", }, }, } newSchedule := DayParts{ Name: "Office Hours", Description: "Test Dayparts", Day: Monday.String(), StartTime: "8:00 AM", EndTime: "4:00 PM", } bslInfo.SetDayparts(newSchedule.Name, newSchedule.Description, newSchedule.StartTime, newSchedule.EndTime, newSchedule.Day) assert.Contains(t, bslInfo.DayParts, newSchedule) } func TestSetOrganizationName(t *testing.T) { bslInfo := &BSLInfo{ OrganizationName: "test-org", } bslInfo.SetOrganizationName("test-org-2") assert.Equal(t, "test-org-2", bslInfo.OrganizationName) } func TestSetParentEnterpriseUnitID(t *testing.T) { bslInfo := &BSLInfo{ ParentEnterpriseUnitID: "ent-1", } bslInfo.SetParentEnterpriseUnitID("ent-2") assert.Equal(t, "ent-2", bslInfo.ParentEnterpriseUnitID) } func TestSetReferenceID(t *testing.T) { bslInfo := &BSLInfo{ ReferenceID: "ref-1", } bslInfo.SetReferenceID("ref-2") assert.Equal(t, "ref-2", bslInfo.ReferenceID) } func TestSetTimeZone(t *testing.T) { bslInfo := &BSLInfo{ TimeZone: "CST", } bslInfo.SetTimeZone("EST") assert.Equal(t, "EST", bslInfo.TimeZone) } func TestLockedToString(t *testing.T) { bslInfo := &BSLInfo{ Locked: true, } result := bslInfo.LockedToString() assert.Equal(t, "true", result) } func TestCoordinateLatitudeToString(t *testing.T) { bslInfo := &BSLInfo{ Coordinates: Coordinates{ Latitude: 93.45, }, } result := bslInfo.CoordinateLatitudeToString() assert.Equal(t, "93.450000", result) } func TestCoordinateLongitdeToString(t *testing.T) { bslInfo := &BSLInfo{ Coordinates: Coordinates{ Longitude: 153.45, }, } result := bslInfo.CoordinateLongitdeToString() assert.Equal(t, "153.450000", result) } func TestCustomAttributeSetsToString(t *testing.T) { bslInfo := &BSLInfo{ CustomAttributeSets: []CustomAttributeSet{ { TypeName: "test-type", Attributes: []Attribute{ { Key: "test-key", Value: "test-value", }, }, }, }, } response, err := json.Marshal(bslInfo.CustomAttributeSets) assert.NoError(t, err) result := bslInfo.CustomAttributeSetsToString() assert.Equal(t, string(response), result) } func TestEnterpriseSettingsToString(t *testing.T) { bslInfo := &BSLInfo{ EnterpriseSettings: []EnterpriseSetting{ { EnterpriseUnitID: "ent-unit-1", ConfigurationSetID: ConfigurationSetID{ Name: "test-config", }, ConfigurationSettings: []ConfigurationSetting{ { Key: "key-1", Value: "value-1", }, }, }, }, } response, err := json.Marshal(bslInfo.EnterpriseSettings) assert.NoError(t, err) result := bslInfo.EnterpriseSettingsToString() assert.Equal(t, string(response), result) } func TestNewBSLInfoConfigMap(t *testing.T) { bslInfo := fakebsl.GetBSLInfo() cfg := bslInfo.NewBSLInfoConfigMap() bslInfoFromConfigMap, err := FromConfigMap(cfg) assert.NoError(t, err) assert.Equal(t, bslInfo, bslInfoFromConfigMap) } func TestAddBSLOrgID(t *testing.T) { bslInfo := fakebsl.GetBSLInfo() b := bslInfo.AddBSLOrgID(&model.Banner{BannerBSLId: "banner bsl id", BannerType: string(banner.EU)}, &model.Tenant{TenantBSLId: "tenant bsl id"}) assert.Equal(t, b.OrganizationID, "tenant bsl id") b = bslInfo.AddBSLOrgID(&model.Banner{BannerBSLId: "banner bsl id", BannerType: string(banner.Org)}, &model.Tenant{TenantBSLId: "tenant bsl id"}) assert.Equal(t, b.OrganizationID, "banner bsl id") } func TestConfigMapToString(t *testing.T) { bslInfo := fakebsl.GetBSLInfo() bslInfoConfigMap := bslInfo.NewBSLInfoConfigMap() bslInfoConfigMapCompare := fakebsl.GetBSLInfoConfigMap() bytes, err := json.Marshal(bslInfoConfigMapCompare) assert.NoError(t, err) result := utils.ToBase64(bytes) configMapString, err := ConfigMapToString(bslInfoConfigMap) assert.NoError(t, err) assert.Equal(t, result, configMapString) } func TestFromClient(t *testing.T) { ctx := context.Background() cl := fake.NewClientBuilder().Build() expected := fakebsl.GetBSLInfo() // not yet created edgeInfo, err := FromClient(ctx, cl) assert.Error(t, err) assert.Nil(t, edgeInfo) // valid cfg created cfg := fakebsl.GetBSLInfoConfigMap() assert.NoError(t, cl.Create(ctx, cfg)) edgeInfo, err = FromClient(ctx, cl) assert.NoError(t, err) assert.Equal(t, expected, edgeInfo) // invalid cfg updated := fakebsl.GetBSLInfoConfigMap() updated.Data[BSLSiteCoordinateLatitude] = "invalid value, number only" assert.NoError(t, cl.Update(ctx, updated)) edgeInfo, err = FromClient(ctx, cl) assert.Error(t, err) assert.Nil(t, edgeInfo) } func TestIsBslInfoConfigMap(t *testing.T) { cfg := fakebsl.GetBSLInfoConfigMap() assert.True(t, IsBslInfoConfigMap(cfg.Name, cfg.Namespace)) } func TestBuildConfigMap(t *testing.T) { bslInfo := fakebsl.GetBSLInfo() expected := fakebsl.GetBSLInfoConfigMap() cfg := BuildConfigMap(bslInfo.SiteName, bslInfo.EnterpriseUnitName, bslInfo.OrganizationName, bslInfo.Coordinates.Latitude, bslInfo.Coordinates.Longitude, nil) assert.Equal(t, expected.Data[BSLSiteName], cfg.Data[BSLSiteName]) assert.Equal(t, expected.Data[BSLSiteEnterpriseUnitName], cfg.Data[BSLSiteEnterpriseUnitName]) assert.Equal(t, expected.Data[BSLSiteOrganizationName], cfg.Data[BSLSiteOrganizationName]) assert.Equal(t, expected.Data[BSLSiteCoordinateLatitude], cfg.Data[BSLSiteCoordinateLatitude]) assert.Equal(t, expected.Data[BSLSiteCoordinateLongitude], cfg.Data[BSLSiteCoordinateLongitude]) assert.Empty(t, cfg.Data[BSLSiteID]) cfg = BuildConfigMap(bslInfo.SiteName, bslInfo.EnterpriseUnitName, bslInfo.OrganizationName, bslInfo.Coordinates.Latitude, bslInfo.Coordinates.Longitude, &bslInfo.ID) assert.Equal(t, expected.Data[BSLSiteName], cfg.Data[BSLSiteName]) assert.Equal(t, expected.Data[BSLSiteEnterpriseUnitName], cfg.Data[BSLSiteEnterpriseUnitName]) assert.Equal(t, expected.Data[BSLSiteOrganizationName], cfg.Data[BSLSiteOrganizationName]) assert.Equal(t, expected.Data[BSLSiteCoordinateLatitude], cfg.Data[BSLSiteCoordinateLatitude]) assert.Equal(t, expected.Data[BSLSiteCoordinateLongitude], cfg.Data[BSLSiteCoordinateLongitude]) assert.Equal(t, expected.Data[BSLSiteID], cfg.Data[BSLSiteID]) } func TestAddOrganizationID(t *testing.T) { cfg := fakebsl.GetBSLInfoConfigMap() orgID := "123456789" AddOrganizationID(orgID, cfg) assert.Equal(t, orgID, cfg.Data[BSLOrganizationID]) } // Tests that zero valued slices have toStrings that make sense // For example, make sure that nil slices will not be JSON marshalled to "null". func TestSliceToStrings(t *testing.T) { bslInfo := fakebsl.GetBSLInfo() cfg := BuildConfigMap(bslInfo.SiteName, bslInfo.EnterpriseUnitName, bslInfo.OrganizationName, bslInfo.Coordinates.Latitude, bslInfo.Coordinates.Longitude, nil) assert.Equal(t, "[]", cfg.Data[BSLSiteHours]) assert.Equal(t, "[]", cfg.Data[BSLSiteDayParts]) assert.Equal(t, "[]", cfg.Data[BSLSiteEnterpriseSettings]) assert.Equal(t, "[]", cfg.Data[BSLSiteCustomAttributeSets]) } func TestSetEndpoint(t *testing.T) { bslInfo := &BSLInfo{ Endpoint: "https://api.ncr.test", } endpoint := "https://gateway-staging.ncr.test" bslInfo.SetEndpoint(endpoint) assert.Equal(t, endpoint, bslInfo.Endpoint) }