Skip to content

Commit

Permalink
fix: Added validation for UpdateScheduleJobRequest
Browse files Browse the repository at this point in the history
refer to edgexfoundry/edgex-go#4925

Signed-off-by: Jack Chen <jack@iotechsys.com>
  • Loading branch information
jackchenjc committed Oct 7, 2024
1 parent 4ed9aff commit 9d7347a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 18 additions & 2 deletions dtos/requests/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,28 @@ type UpdateScheduleJobRequest struct {
}

// Validate satisfies the Validator interface
func (a *UpdateScheduleJobRequest) Validate() error {
err := common.Validate(a)
func (u *UpdateScheduleJobRequest) Validate() error {
err := common.Validate(u)
if err != nil {
return err
}

if u.ScheduleJob.Definition != nil {
err = u.ScheduleJob.Definition.Validate()
if err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid ScheduleDef.", err)
}
}

if u.ScheduleJob.Actions != nil {
for _, action := range u.ScheduleJob.Actions {
err = action.Validate()
if err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "invalid ScheduleAction.", err)
}
}
}

return nil
}

Expand Down
17 changes: 17 additions & 0 deletions dtos/requests/schedulejob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ func TestUpdateScheduleJobRequest_Validate(t *testing.T) {
unsupportedDefinitionType.ScheduleJob.Definition = &unsupportedDefinition
validWithoutDefinition := NewUpdateScheduleJobRequest(updateScheduleJobData())
validWithoutDefinition.ScheduleJob.Definition = nil
invalidStartAndEndTimestamp := NewUpdateScheduleJobRequest(updateScheduleJobData())
invalidStartAndEndTimestamp.ScheduleJob.Definition.StartTimestamp = 1727690062000
invalidStartAndEndTimestamp.ScheduleJob.Definition.EndTimestamp = 1727689822000
invalidEmptyDefinition := NewUpdateScheduleJobRequest(updateScheduleJobData())
emptyDefinition := dtos.ScheduleDef{}
invalidEmptyDefinition.ScheduleJob.Definition = &emptyDefinition
Expand All @@ -196,6 +199,18 @@ func TestUpdateScheduleJobRequest_Validate(t *testing.T) {
emptyLabels := NewUpdateScheduleJobRequest(updateScheduleJobData())
emptyLabels.ScheduleJob.Labels = []string{}

invalidActions := NewUpdateScheduleJobRequest(updateScheduleJobData())
invalidActions.ScheduleJob.Actions = []dtos.ScheduleAction{
{
Type: "invalid",
ContentType: common.ContentTypeJSON,
Payload: nil,
EdgeXMessageBusAction: dtos.EdgeXMessageBusAction{
Topic: "testTopic",
},
},
}

tests := []struct {
name string
req UpdateScheduleJobRequest
Expand All @@ -211,11 +226,13 @@ func TestUpdateScheduleJobRequest_Validate(t *testing.T) {
{"invalid, empty name", invalidEmptyName, true},
{"invalid, unsupported definition type", unsupportedDefinitionType, true},
{"valid, without definition", validWithoutDefinition, false},
{"invalid, endTimestamp must be greater than startTimestamp", invalidStartAndEndTimestamp, true},
{"invalid, empty definition", invalidEmptyDefinition, true},
{"valid, no actions", noActions, false},
{"valid, no labels", noLabels, false},
{"valid, empty actions", emptyActions, false},
{"valid, empty labels", emptyLabels, false},
{"invalid, invalid action type", invalidActions, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 9d7347a

Please sign in to comment.