Skip to content

Commit

Permalink
Merge pull request #16 from opsgenie/heimdall-4690
Browse files Browse the repository at this point in the history
Refactor Restriction struct to handle explicitly set 0 values
  • Loading branch information
metehanozturk authored Sep 11, 2019
2 parents ad123e4 + cbd4633 commit a5dd65f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 49 deletions.
1 change: 0 additions & 1 deletion contact/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
20 changes: 10 additions & 10 deletions notification/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
70 changes: 42 additions & 28 deletions og/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 22 additions & 10 deletions schedule/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -159,36 +159,48 @@ 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
rotation = rotation.WithParticipants(og.Participant{Type: og.Team, Name: "tram1"}).WithTimeRestriction(tr)
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
rotation = rotation.WithParticipants(og.Participant{Type: og.Team, Name: "tram1"}).WithTimeRestriction(tr)
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
rotation = rotation.WithParticipants(og.Participant{Type: og.Team, Name: "tram1"}).WithTimeRestriction(tr)
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)

}

Expand Down

0 comments on commit a5dd65f

Please sign in to comment.