1 package bsl_test
2
3 import (
4 "context"
5 "encoding/json"
6 "testing"
7 "time"
8
9 "github.com/stretchr/testify/assert"
10 "sigs.k8s.io/controller-runtime/pkg/client/fake"
11
12 "edge-infra.dev/pkg/edge/api/graph/model"
13 "edge-infra.dev/pkg/edge/api/utils"
14 . "edge-infra.dev/pkg/edge/bsl"
15 fakebsl "edge-infra.dev/pkg/edge/bsl/fake"
16 "edge-infra.dev/pkg/edge/constants/api/banner"
17 )
18
19 func TestSetID(t *testing.T) {
20 bslInfo := &BSLInfo{
21 ID: "id-1",
22 }
23 bslInfo.SetID("id-2")
24 assert.Equal(t, "id-2", bslInfo.ID)
25 }
26
27 func TestSetSiteName(t *testing.T) {
28 bslInfo := &BSLInfo{
29 SiteName: "test-site-1",
30 }
31 bslInfo.SetSiteName("test-site-2")
32 assert.Equal(t, "test-site-2", bslInfo.SiteName)
33 }
34
35 func TestSetEnterpriseUnitName(t *testing.T) {
36 bslInfo := &BSLInfo{
37 EnterpriseUnitName: "test-store-1",
38 }
39 bslInfo.SetEnterpriseUnitName("test-store-2")
40 assert.Equal(t, "test-store-2", bslInfo.EnterpriseUnitName)
41 }
42
43 func TestSetLatitude(t *testing.T) {
44 bslInfo := &BSLInfo{
45 Coordinates: Coordinates{
46 Latitude: 90.0,
47 },
48 }
49 bslInfo.SetLatitude(92.3)
50 assert.Equal(t, 92.3, bslInfo.Coordinates.Latitude)
51 }
52
53 func TestSetLongitude(t *testing.T) {
54 bslInfo := &BSLInfo{
55 Coordinates: Coordinates{
56 Longitude: 180.0,
57 },
58 }
59 bslInfo.SetLongitude(153.3)
60 assert.Equal(t, 153.3, bslInfo.Coordinates.Longitude)
61 }
62
63 func TestSetAddress(t *testing.T) {
64 bslInfo := &BSLInfo{
65 Address: Address{
66 Street: "111 Test Street",
67 City: "Atlanta",
68 State: "Georgia",
69 Country: "US",
70 PostalCode: "30301",
71 },
72 }
73 newAddress := Address{
74 Street: "220 Testing Street",
75 City: "Oklahoma City",
76 State: "Oklahoma",
77 Country: "US",
78 PostalCode: "73089",
79 }
80 bslInfo.SetAddress(newAddress.Street, newAddress.City, newAddress.State, newAddress.Country, newAddress.PostalCode)
81 assert.Equal(t, newAddress, bslInfo.Address)
82 }
83
84 func TestSetContact(t *testing.T) {
85 bslInfo := &BSLInfo{
86 Contact: Contact{
87 ContactPerson: "Test Person",
88 PhoneNumber: "2345678910",
89 PhoneNumberCountryCode: "+1",
90 },
91 }
92 newContact := Contact{
93 ContactPerson: "New Person",
94 PhoneNumber: "0987654321",
95 PhoneNumberCountryCode: "+1",
96 }
97 bslInfo.SetContact(newContact.ContactPerson, newContact.PhoneNumber, newContact.PhoneNumberCountryCode)
98 assert.Equal(t, newContact, bslInfo.Contact)
99 }
100
101 func TestSetCustomAttributes(t *testing.T) {
102 bslInfo := &BSLInfo{
103 CustomAttributeSets: []CustomAttributeSet{
104 {
105 TypeName: "test-type",
106 Attributes: []Attribute{
107 {
108 Key: "test-key",
109 Value: "test-value",
110 },
111 },
112 },
113 },
114 }
115 newCustomAttributes := []CustomAttributeSet{
116 {
117 TypeName: "test-type-1",
118 Attributes: []Attribute{
119 {
120 Key: "test-key-1",
121 Value: "test-value-1",
122 },
123 },
124 },
125 }
126 bslInfo.SetCustomAttributes(newCustomAttributes)
127 assert.Equal(t, newCustomAttributes, bslInfo.CustomAttributeSets)
128 }
129
130 func TestAddCustomAttributes(t *testing.T) {
131 bslInfo := &BSLInfo{
132 CustomAttributeSets: []CustomAttributeSet{
133 {
134 TypeName: "test-type",
135 Attributes: []Attribute{
136 {
137 Key: "test-key",
138 Value: "test-value",
139 },
140 },
141 },
142 },
143 }
144 newCustomAttributes := []CustomAttributeSet{
145 {
146 TypeName: "test-type-1",
147 Attributes: []Attribute{
148 {
149 Key: "test-key-1",
150 Value: "test-value-1",
151 },
152 },
153 },
154 }
155 bslInfo.AddCustomAttributes(newCustomAttributes)
156 for _, customAttribute := range newCustomAttributes {
157 assert.Contains(t, bslInfo.CustomAttributeSets, customAttribute)
158 }
159 }
160
161 func TestAddAttributeToCustomAttributeSet(t *testing.T) {
162 bslInfo := &BSLInfo{
163 CustomAttributeSets: []CustomAttributeSet{
164 {
165 TypeName: "test-type",
166 Attributes: []Attribute{
167 {
168 Key: "test-key",
169 Value: "test-value",
170 },
171 },
172 },
173 {
174 TypeName: "test-type-1",
175 Attributes: []Attribute{
176 {
177 Key: "test-key-1",
178 Value: "test-value-1",
179 },
180 },
181 },
182 },
183 }
184
185 newAttribute := Attribute{
186 Key: "test-key-2",
187 Value: "test-value-2",
188 }
189
190 bslInfo.AddAttributeToCustomAttributeSet("test-type", newAttribute)
191 for _, customAttribute := range bslInfo.CustomAttributeSets {
192 if customAttribute.TypeName == "test-type" {
193 assert.Contains(t, customAttribute.Attributes, newAttribute)
194 }
195 }
196 }
197
198 func TestSetCurrency(t *testing.T) {
199 bslInfo := &BSLInfo{
200 Currency: "USD",
201 }
202 bslInfo.SetCurrency("GBP")
203 assert.Equal(t, "GBP", bslInfo.Currency)
204 }
205
206 func TestSetCreatedOn(t *testing.T) {
207 currentTime := time.Now().UTC()
208 bslInfo := &BSLInfo{
209 CreatedOn: currentTime.Local().String(),
210 }
211 newTime := currentTime.Add(2 * time.Hour)
212 bslInfo.SetCreatedOn(newTime.Local().String())
213 assert.Equal(t, newTime.Local().String(), bslInfo.CreatedOn)
214 }
215
216 func TestSetDescription(t *testing.T) {
217 bslInfo := &BSLInfo{
218 Description: "Test Description",
219 }
220 bslInfo.SetDescription("New Description")
221 assert.Equal(t, "New Description", bslInfo.Description)
222 }
223
224 func TestSetEnterpriseSettings(t *testing.T) {
225 bslInfo := &BSLInfo{
226 EnterpriseSettings: []EnterpriseSetting{
227 {
228 EnterpriseUnitID: "ent-unit-1",
229 ConfigurationSetID: ConfigurationSetID{
230 Name: "test-config",
231 },
232 ConfigurationSettings: []ConfigurationSetting{
233 {
234 Key: "key-1",
235 Value: "value-1",
236 },
237 },
238 },
239 },
240 }
241 newEnterprisesettings := []EnterpriseSetting{
242 {
243 EnterpriseUnitID: "ent-unit-2",
244 ConfigurationSetID: ConfigurationSetID{
245 Name: "test-config-2",
246 },
247 ConfigurationSettings: []ConfigurationSetting{
248 {
249 Key: "key-2",
250 Value: "value-2",
251 },
252 },
253 },
254 }
255 bslInfo.SetEnterpriseSettings(newEnterprisesettings)
256 assert.Equal(t, newEnterprisesettings, bslInfo.EnterpriseSettings)
257 }
258
259 func TestAddEnterpriseSetting(t *testing.T) {
260 bslInfo := &BSLInfo{
261 EnterpriseSettings: []EnterpriseSetting{
262 {
263 EnterpriseUnitID: "ent-unit-1",
264 ConfigurationSetID: ConfigurationSetID{
265 Name: "test-config",
266 },
267 ConfigurationSettings: []ConfigurationSetting{
268 {
269 Key: "key-1",
270 Value: "value-1",
271 },
272 },
273 },
274 },
275 }
276 newEnterpriseSetting := EnterpriseSetting{
277 EnterpriseUnitID: "ent-unit-2",
278 ConfigurationSetID: ConfigurationSetID{
279 Name: "test-config-2",
280 },
281 ConfigurationSettings: []ConfigurationSetting{
282 {
283 Key: "key-2",
284 Value: "value-2",
285 },
286 },
287 }
288 bslInfo.AddEnterpriseSetting(newEnterpriseSetting)
289 assert.Contains(t, bslInfo.EnterpriseSettings, newEnterpriseSetting)
290 }
291
292 func TestAddEnterpriseConfigurationSetting(t *testing.T) {
293 bslInfo := &BSLInfo{
294 EnterpriseSettings: []EnterpriseSetting{
295 {
296 EnterpriseUnitID: "ent-unit-1",
297 ConfigurationSetID: ConfigurationSetID{
298 Name: "test-config",
299 },
300 ConfigurationSettings: []ConfigurationSetting{
301 {
302 Key: "key-1",
303 Value: "value-1",
304 },
305 },
306 },
307 {
308 EnterpriseUnitID: "ent-unit-2",
309 ConfigurationSetID: ConfigurationSetID{
310 Name: "test-config-2",
311 },
312 ConfigurationSettings: []ConfigurationSetting{
313 {
314 Key: "key-2",
315 Value: "value-2",
316 },
317 },
318 },
319 },
320 }
321 newConfigurationSetting := ConfigurationSetting{
322 Key: "key-2",
323 Value: "value-2",
324 }
325 bslInfo.AddEnterpriseConfigurationSetting("ent-unit-1", newConfigurationSetting)
326 for _, enterpriseSetting := range bslInfo.EnterpriseSettings {
327 if enterpriseSetting.EnterpriseUnitID == "ent-unit-1" {
328 assert.Contains(t, enterpriseSetting.ConfigurationSettings, newConfigurationSetting)
329 }
330 }
331 }
332
333 func TestSetLastModifiedOn(t *testing.T) {
334 currentTime := time.Now().UTC()
335 bslInfo := &BSLInfo{
336 LastModifiedOn: currentTime.Local().String(),
337 }
338 newTime := currentTime.Add(2 * time.Hour)
339 bslInfo.SetLastModifiedOn(newTime.Local().String())
340 assert.Equal(t, newTime.Local().String(), bslInfo.LastModifiedOn)
341 }
342
343 func TestSetLocked(t *testing.T) {
344 bslInfo := &BSLInfo{
345 Locked: false,
346 }
347 bslInfo.SetLocked(true)
348 assert.True(t, bslInfo.Locked)
349 }
350
351 func TestSetStatus(t *testing.T) {
352 bslInfo := &BSLInfo{
353 Status: Inactive.String(),
354 }
355 bslInfo.SetStatus(Active)
356 assert.Equal(t, Active.String(), bslInfo.Status)
357 }
358
359 func TestSetDayparts(t *testing.T) {
360 bslInfo := &BSLInfo{
361 DayParts: []DayParts{
362 {
363 Name: "Office Hours",
364 Description: "Test Dayparts",
365 Day: Daily.String(),
366 StartTime: "6:00 AM",
367 EndTime: "5:00 PM",
368 },
369 },
370 }
371 newSchedule := DayParts{
372 Name: "Office Hours",
373 Description: "Test Dayparts",
374 Day: Monday.String(),
375 StartTime: "8:00 AM",
376 EndTime: "4:00 PM",
377 }
378 bslInfo.SetDayparts(newSchedule.Name, newSchedule.Description, newSchedule.StartTime, newSchedule.EndTime, newSchedule.Day)
379 assert.Contains(t, bslInfo.DayParts, newSchedule)
380 }
381
382 func TestSetOrganizationName(t *testing.T) {
383 bslInfo := &BSLInfo{
384 OrganizationName: "test-org",
385 }
386 bslInfo.SetOrganizationName("test-org-2")
387 assert.Equal(t, "test-org-2", bslInfo.OrganizationName)
388 }
389
390 func TestSetParentEnterpriseUnitID(t *testing.T) {
391 bslInfo := &BSLInfo{
392 ParentEnterpriseUnitID: "ent-1",
393 }
394 bslInfo.SetParentEnterpriseUnitID("ent-2")
395 assert.Equal(t, "ent-2", bslInfo.ParentEnterpriseUnitID)
396 }
397
398 func TestSetReferenceID(t *testing.T) {
399 bslInfo := &BSLInfo{
400 ReferenceID: "ref-1",
401 }
402 bslInfo.SetReferenceID("ref-2")
403 assert.Equal(t, "ref-2", bslInfo.ReferenceID)
404 }
405
406 func TestSetTimeZone(t *testing.T) {
407 bslInfo := &BSLInfo{
408 TimeZone: "CST",
409 }
410 bslInfo.SetTimeZone("EST")
411 assert.Equal(t, "EST", bslInfo.TimeZone)
412 }
413
414 func TestLockedToString(t *testing.T) {
415 bslInfo := &BSLInfo{
416 Locked: true,
417 }
418 result := bslInfo.LockedToString()
419 assert.Equal(t, "true", result)
420 }
421
422 func TestCoordinateLatitudeToString(t *testing.T) {
423 bslInfo := &BSLInfo{
424 Coordinates: Coordinates{
425 Latitude: 93.45,
426 },
427 }
428 result := bslInfo.CoordinateLatitudeToString()
429 assert.Equal(t, "93.450000", result)
430 }
431
432 func TestCoordinateLongitdeToString(t *testing.T) {
433 bslInfo := &BSLInfo{
434 Coordinates: Coordinates{
435 Longitude: 153.45,
436 },
437 }
438 result := bslInfo.CoordinateLongitdeToString()
439 assert.Equal(t, "153.450000", result)
440 }
441
442 func TestCustomAttributeSetsToString(t *testing.T) {
443 bslInfo := &BSLInfo{
444 CustomAttributeSets: []CustomAttributeSet{
445 {
446 TypeName: "test-type",
447 Attributes: []Attribute{
448 {
449 Key: "test-key",
450 Value: "test-value",
451 },
452 },
453 },
454 },
455 }
456 response, err := json.Marshal(bslInfo.CustomAttributeSets)
457 assert.NoError(t, err)
458 result := bslInfo.CustomAttributeSetsToString()
459 assert.Equal(t, string(response), result)
460 }
461
462 func TestEnterpriseSettingsToString(t *testing.T) {
463 bslInfo := &BSLInfo{
464 EnterpriseSettings: []EnterpriseSetting{
465 {
466 EnterpriseUnitID: "ent-unit-1",
467 ConfigurationSetID: ConfigurationSetID{
468 Name: "test-config",
469 },
470 ConfigurationSettings: []ConfigurationSetting{
471 {
472 Key: "key-1",
473 Value: "value-1",
474 },
475 },
476 },
477 },
478 }
479 response, err := json.Marshal(bslInfo.EnterpriseSettings)
480 assert.NoError(t, err)
481 result := bslInfo.EnterpriseSettingsToString()
482 assert.Equal(t, string(response), result)
483 }
484
485 func TestNewBSLInfoConfigMap(t *testing.T) {
486 bslInfo := fakebsl.GetBSLInfo()
487 cfg := bslInfo.NewBSLInfoConfigMap()
488 bslInfoFromConfigMap, err := FromConfigMap(cfg)
489 assert.NoError(t, err)
490 assert.Equal(t, bslInfo, bslInfoFromConfigMap)
491 }
492
493 func TestAddBSLOrgID(t *testing.T) {
494 bslInfo := fakebsl.GetBSLInfo()
495 b := bslInfo.AddBSLOrgID(&model.Banner{BannerBSLId: "banner bsl id", BannerType: string(banner.EU)}, &model.Tenant{TenantBSLId: "tenant bsl id"})
496 assert.Equal(t, b.OrganizationID, "tenant bsl id")
497 b = bslInfo.AddBSLOrgID(&model.Banner{BannerBSLId: "banner bsl id", BannerType: string(banner.Org)}, &model.Tenant{TenantBSLId: "tenant bsl id"})
498 assert.Equal(t, b.OrganizationID, "banner bsl id")
499 }
500
501 func TestConfigMapToString(t *testing.T) {
502 bslInfo := fakebsl.GetBSLInfo()
503 bslInfoConfigMap := bslInfo.NewBSLInfoConfigMap()
504 bslInfoConfigMapCompare := fakebsl.GetBSLInfoConfigMap()
505 bytes, err := json.Marshal(bslInfoConfigMapCompare)
506 assert.NoError(t, err)
507 result := utils.ToBase64(bytes)
508 configMapString, err := ConfigMapToString(bslInfoConfigMap)
509 assert.NoError(t, err)
510 assert.Equal(t, result, configMapString)
511 }
512
513 func TestFromClient(t *testing.T) {
514 ctx := context.Background()
515 cl := fake.NewClientBuilder().Build()
516 expected := fakebsl.GetBSLInfo()
517
518
519 edgeInfo, err := FromClient(ctx, cl)
520 assert.Error(t, err)
521 assert.Nil(t, edgeInfo)
522
523
524 cfg := fakebsl.GetBSLInfoConfigMap()
525 assert.NoError(t, cl.Create(ctx, cfg))
526 edgeInfo, err = FromClient(ctx, cl)
527 assert.NoError(t, err)
528 assert.Equal(t, expected, edgeInfo)
529
530
531 updated := fakebsl.GetBSLInfoConfigMap()
532 updated.Data[BSLSiteCoordinateLatitude] = "invalid value, number only"
533 assert.NoError(t, cl.Update(ctx, updated))
534 edgeInfo, err = FromClient(ctx, cl)
535 assert.Error(t, err)
536 assert.Nil(t, edgeInfo)
537 }
538
539 func TestIsBslInfoConfigMap(t *testing.T) {
540 cfg := fakebsl.GetBSLInfoConfigMap()
541 assert.True(t, IsBslInfoConfigMap(cfg.Name, cfg.Namespace))
542 }
543
544 func TestBuildConfigMap(t *testing.T) {
545 bslInfo := fakebsl.GetBSLInfo()
546 expected := fakebsl.GetBSLInfoConfigMap()
547 cfg := BuildConfigMap(bslInfo.SiteName,
548 bslInfo.EnterpriseUnitName,
549 bslInfo.OrganizationName,
550 bslInfo.Coordinates.Latitude,
551 bslInfo.Coordinates.Longitude,
552 nil)
553 assert.Equal(t, expected.Data[BSLSiteName], cfg.Data[BSLSiteName])
554 assert.Equal(t, expected.Data[BSLSiteEnterpriseUnitName], cfg.Data[BSLSiteEnterpriseUnitName])
555 assert.Equal(t, expected.Data[BSLSiteOrganizationName], cfg.Data[BSLSiteOrganizationName])
556 assert.Equal(t, expected.Data[BSLSiteCoordinateLatitude], cfg.Data[BSLSiteCoordinateLatitude])
557 assert.Equal(t, expected.Data[BSLSiteCoordinateLongitude], cfg.Data[BSLSiteCoordinateLongitude])
558 assert.Empty(t, cfg.Data[BSLSiteID])
559
560 cfg = BuildConfigMap(bslInfo.SiteName,
561 bslInfo.EnterpriseUnitName,
562 bslInfo.OrganizationName,
563 bslInfo.Coordinates.Latitude,
564 bslInfo.Coordinates.Longitude,
565 &bslInfo.ID)
566 assert.Equal(t, expected.Data[BSLSiteName], cfg.Data[BSLSiteName])
567 assert.Equal(t, expected.Data[BSLSiteEnterpriseUnitName], cfg.Data[BSLSiteEnterpriseUnitName])
568 assert.Equal(t, expected.Data[BSLSiteOrganizationName], cfg.Data[BSLSiteOrganizationName])
569 assert.Equal(t, expected.Data[BSLSiteCoordinateLatitude], cfg.Data[BSLSiteCoordinateLatitude])
570 assert.Equal(t, expected.Data[BSLSiteCoordinateLongitude], cfg.Data[BSLSiteCoordinateLongitude])
571 assert.Equal(t, expected.Data[BSLSiteID], cfg.Data[BSLSiteID])
572 }
573
574 func TestAddOrganizationID(t *testing.T) {
575 cfg := fakebsl.GetBSLInfoConfigMap()
576 orgID := "123456789"
577 AddOrganizationID(orgID, cfg)
578 assert.Equal(t, orgID, cfg.Data[BSLOrganizationID])
579 }
580
581
582
583 func TestSliceToStrings(t *testing.T) {
584 bslInfo := fakebsl.GetBSLInfo()
585 cfg := BuildConfigMap(bslInfo.SiteName,
586 bslInfo.EnterpriseUnitName,
587 bslInfo.OrganizationName,
588 bslInfo.Coordinates.Latitude,
589 bslInfo.Coordinates.Longitude,
590 nil)
591
592 assert.Equal(t, "[]", cfg.Data[BSLSiteHours])
593 assert.Equal(t, "[]", cfg.Data[BSLSiteDayParts])
594 assert.Equal(t, "[]", cfg.Data[BSLSiteEnterpriseSettings])
595 assert.Equal(t, "[]", cfg.Data[BSLSiteCustomAttributeSets])
596 }
597
598 func TestSetEndpoint(t *testing.T) {
599 bslInfo := &BSLInfo{
600 Endpoint: "https://api.ncr.test",
601 }
602 endpoint := "https://gateway-staging.ncr.test"
603 bslInfo.SetEndpoint(endpoint)
604 assert.Equal(t, endpoint, bslInfo.Endpoint)
605 }
606
View as plain text