From cbd4633da625d06645db2e9f5096ee0ed09f7fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eren=20K=C4=B1z=C4=B1lay?= Date: Wed, 11 Sep 2019 11:26:07 +0300 Subject: [PATCH] Refactor Restriction struct to handle explicitly set 0 values --- contact/result.go | 1 - notification/notification_test.go | 20 ++++----- og/entity.go | 70 ++++++++++++++++++------------- schedule/schedule_test.go | 32 +++++++++----- 4 files changed, 74 insertions(+), 49 deletions(-) diff --git a/contact/result.go b/contact/result.go index 74bcdd0..0f469cf 100644 --- a/contact/result.go +++ b/contact/result.go @@ -25,7 +25,6 @@ type GetResult struct { Id string `json:"id"` MethodOfContact string `json:"method"` To string `json:"to,omitempty"` - Enabled bool `json:"enabled"` Status Status `json:"status,omitempty"` } diff --git a/notification/notification_test.go b/notification/notification_test.go index 746da52..4eb0e79 100644 --- a/notification/notification_test.go +++ b/notification/notification_test.go @@ -277,17 +277,17 @@ func TestCreateRuleRequest_Validate(t *testing.T) { err = createRequest.Validate() assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18} - createRequest.TimeRestriction = &og.TimeRestriction{Type: og.TimeOfDay, Restriction: og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18}} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18)} + createRequest.TimeRestriction = &og.TimeRestriction{Type: og.TimeOfDay, Restriction: og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18)}} err = createRequest.Validate() assert.Nil(t, err) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18)} createRequest.TimeRestriction = &og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} err = createRequest.Validate() - assert.Equal(t, err.Error(), errors.New("startDay, startHour, startMin, endDay, endHour, endMin cannot be empty.").Error()) + assert.Equal(t, err.Error(), errors.New("startDay, endDay cannot be empty.").Error()) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18, StartDay: og.Wednesday, EndDay: og.Friday} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18), StartDay: og.Wednesday, EndDay: og.Friday} createRequest.TimeRestriction = &og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} err = createRequest.Validate() assert.Nil(t, err) @@ -409,17 +409,17 @@ func TestUpdateRuleRequest_Validate(t *testing.T) { err = updateRuleRequest.Validate() assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18} - updateRuleRequest.TimeRestriction = &og.TimeRestriction{Type: og.TimeOfDay, Restriction: og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18}} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18)} + updateRuleRequest.TimeRestriction = &og.TimeRestriction{Type: og.TimeOfDay, Restriction: og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(18)}} err = updateRuleRequest.Validate() assert.Nil(t, err) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(12)} updateRuleRequest.TimeRestriction = &og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} err = updateRuleRequest.Validate() - assert.Equal(t, err.Error(), errors.New("startDay, startHour, startMin, endDay, endHour, endMin cannot be empty.").Error()) + assert.Equal(t, err.Error(), errors.New("startDay, endDay cannot be empty.").Error()) - restrictions[0] = og.Restriction{StartMin: 12, StartHour: 1, EndHour: 1, EndMin: 18, StartDay: og.Wednesday, EndDay: og.Friday} + restrictions[0] = og.Restriction{StartMin: og.Minute(12), StartHour: og.Hour(1), EndHour: og.Hour(1), EndMin: og.Minute(12), StartDay: og.Wednesday, EndDay: og.Friday} updateRuleRequest.TimeRestriction = &og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} err = updateRuleRequest.Validate() assert.Nil(t, err) diff --git a/og/entity.go b/og/entity.go index b908c20..05c59af 100644 --- a/og/entity.go +++ b/og/entity.go @@ -160,36 +160,50 @@ func ValidateConditions(conditions []Condition) error { } func ValidateRestrictions(timeRestriction *TimeRestriction) error { - if timeRestriction.Type == "" { - return errors.New("Type of time restriction must be time-of-day or weekday-and-time-of-day.") - } - if timeRestriction.Type == WeekdayAndTimeOfDay && timeRestriction.RestrictionList == nil { - return errors.New("Restrictions cannot be empty.") - } - if len(timeRestriction.RestrictionList) != 0 { - for _, restriction := range timeRestriction.RestrictionList { - if timeRestriction.Type == "weekday-and-time-of-day" && - (restriction.EndMin < 0 || - restriction.EndHour <= 0 || - restriction.EndDay == "" || - restriction.StartDay == "" || - restriction.StartHour <= 0 || - restriction.StartMin < 0) { - return errors.New("startDay, startHour, startMin, endDay, endHour, endMin cannot be empty.") + if timeRestriction.Type == WeekdayAndTimeOfDay { + if len(timeRestriction.RestrictionList) != 0 { + for _, restriction := range timeRestriction.RestrictionList { + err := validateRestriction(restriction, WeekdayAndTimeOfDay) + if err != nil { + return err + } } + return nil + } else { + return errors.New("Restrictions cannot be empty.") } + } else if timeRestriction.Type == TimeOfDay { + return validateRestriction(timeRestriction.Restriction, TimeOfDay) } - if timeRestriction.Type == TimeOfDay && - (timeRestriction.Restriction.EndMin < 0 || - timeRestriction.Restriction.EndHour <= 0 || - timeRestriction.Restriction.StartHour <= 0 || - timeRestriction.Restriction.StartMin < 0) { + + return errors.New("Type of time restriction must be time-of-day or weekday-and-time-of-day.") +} + +func validateRestriction(restriction Restriction, restrictionType RestrictionType) error { + if restriction.EndHour == nil || restriction.StartHour == nil || restriction.StartMin == nil || restriction.EndMin == nil { return errors.New("startHour, startMin, endHour, endMin cannot be empty.") } - + if restrictionType == WeekdayAndTimeOfDay && (restriction.EndDay == "" || restriction.StartDay == "") { + return errors.New("startDay, endDay cannot be empty.") + } + if *restriction.EndHour > 24 { + return errors.New("restriction end hour should between 0 and 24.") + } + if *restriction.StartHour > 24 { + return errors.New("restriction start hour should between 0 and 24.") + } + //minutes converted nearest 0 or 30 automatically so do not need to validate return nil } +func Hour(hour uint32) *uint32 { + return &hour +} + +func Minute(minute uint32) *uint32 { + return &minute +} + type RotationType string type ParticipantType string type Day string @@ -273,12 +287,12 @@ type TimeRestriction struct { } type Restriction struct { - StartDay Day `json:"startDay,omitempty"` - StartHour uint32 `json:"startHour,omitempty"` - StartMin uint32 `json:"startMin,omitempty"` - EndHour uint32 `json:"endHour,omitempty"` - EndDay Day `json:"endDay,omitempty"` - EndMin uint32 `json:"endMin,omitempty"` + StartDay Day `json:"startDay,omitempty"` + StartHour *uint32 `json:"startHour,omitempty"` + StartMin *uint32 `json:"startMin,omitempty"` + EndHour *uint32 `json:"endHour,omitempty"` + EndDay Day `json:"endDay,omitempty"` + EndMin *uint32 `json:"endMin,omitempty"` } type Filter struct { diff --git a/schedule/schedule_test.go b/schedule/schedule_test.go index 79c3e2a..c38f81d 100644 --- a/schedule/schedule_test.go +++ b/schedule/schedule_test.go @@ -15,8 +15,8 @@ func TestBuildCreateRequest(t *testing.T) { participants[0] = *participant1 participants[1] = *participant2 - restriction1 := og.Restriction{StartDay: og.Saturday, StartHour: 5, StartMin: 3, EndDay: og.Friday, EndMin: 5, EndHour: 2} - restriction2 := og.Restriction{StartDay: og.Monday, StartHour: 12, StartMin: 33, EndDay: og.Friday, EndMin: 6, EndHour: 20} + restriction1 := og.Restriction{StartDay: og.Saturday, StartHour: og.Hour(5), StartMin: og.Minute(3), EndDay: og.Friday, EndMin: og.Minute(3), EndHour: og.Hour(2)} + restriction2 := og.Restriction{StartDay: og.Monday, StartHour: og.Hour(12), StartMin: og.Minute(33), EndDay: og.Friday, EndMin: og.Minute(6), EndHour: og.Hour(20)} restrictions := make([]og.Restriction, 2) restrictions[0] = restriction1 restrictions[1] = restriction2 @@ -126,7 +126,7 @@ func TestCreateRequest_Validate(t *testing.T) { assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1}, + og.Restriction{EndMin: og.Minute(1)}, } tr = og.TimeRestriction{Type: og.TimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -137,7 +137,7 @@ func TestCreateRequest_Validate(t *testing.T) { assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1, StartHour: 5}, + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(5)}, } tr = og.TimeRestriction{Type: og.TimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -148,7 +148,7 @@ func TestCreateRequest_Validate(t *testing.T) { assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1, StartHour: 5, StartMin: 1}, + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(5), StartMin: og.Minute(1)}, } tr = og.TimeRestriction{Type: og.TimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -159,7 +159,7 @@ func TestCreateRequest_Validate(t *testing.T) { assert.Equal(t, err.Error(), errors.New("startHour, startMin, endHour, endMin cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1, StartHour: 5, StartMin: 1, EndHour: 1}, + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(22), StartMin: og.Minute(1), EndHour: og.Hour(23)}, } tr = og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -167,10 +167,10 @@ func TestCreateRequest_Validate(t *testing.T) { createRequest.Rotations = nil createRequest.WithRotation(rotation) err = createRequest.Validate() - assert.Equal(t, err.Error(), errors.New("startDay, startHour, startMin, endDay, endHour, endMin cannot be empty.").Error()) + assert.Equal(t, err.Error(), errors.New("startDay, endDay cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1, StartHour: 5, StartMin: 1, EndHour: 1, EndDay: og.Monday}, + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(5), StartMin: og.Minute(1), EndHour: og.Hour(4), EndDay: og.Monday}, } tr = og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -178,10 +178,10 @@ func TestCreateRequest_Validate(t *testing.T) { createRequest.Rotations = nil createRequest.WithRotation(rotation) err = createRequest.Validate() - assert.Equal(t, err.Error(), errors.New("startDay, startHour, startMin, endDay, endHour, endMin cannot be empty.").Error()) + assert.Equal(t, err.Error(), errors.New("startDay, endDay cannot be empty.").Error()) restrictions = []og.Restriction{ - og.Restriction{EndMin: 1, StartHour: 5, StartMin: 1, EndHour: 1, EndDay: og.Monday, StartDay: og.Monday}, + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(55), StartMin: og.Minute(1), EndHour: og.Hour(1), EndDay: og.Monday, StartDay: og.Monday}, } tr = og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} rotation.Participants = nil @@ -189,6 +189,18 @@ func TestCreateRequest_Validate(t *testing.T) { createRequest.Rotations = nil createRequest.WithRotation(rotation) err = createRequest.Validate() + assert.Equal(t, err.Error(), errors.New("restriction start hour should between 0 and 24.").Error()) + + restrictions = []og.Restriction{ + og.Restriction{EndMin: og.Minute(1), StartHour: og.Hour(5), StartMin: og.Minute(1), EndHour: og.Hour(1), EndDay: og.Monday, StartDay: og.Monday}, + } + tr = og.TimeRestriction{Type: og.WeekdayAndTimeOfDay, RestrictionList: restrictions} + rotation.Participants = nil + rotation = rotation.WithParticipants(og.Participant{Type: og.Team, Name: "tram1"}).WithTimeRestriction(tr) + createRequest.Rotations = nil + createRequest.WithRotation(rotation) + err = createRequest.Validate() + assert.Nil(t, err) }