-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add UnmarshalJSON and WithEmptyPayload methods with more tests
Signed-off-by: Jack Chen <[email protected]>
- Loading branch information
1 parent
4693839
commit 7ffbf86
Showing
7 changed files
with
924 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dtos | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v3/common" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/models" | ||
) | ||
|
||
var ( | ||
scheduleActionRecord = ScheduleActionRecord{ | ||
Id: TestUUID, | ||
JobName: jobName, | ||
Action: scheduleActionEdgeXMessageBus, | ||
Status: models.Missed, | ||
ScheduledAt: TestTimestamp, | ||
Created: TestTimestamp, | ||
} | ||
scheduleActionRecordModel = models.ScheduleActionRecord{ | ||
Id: TestUUID, | ||
JobName: jobName, | ||
Action: scheduleActionEdgeXMessageBusModel, | ||
Status: models.Missed, | ||
ScheduledAt: TestTimestamp, | ||
Created: TestTimestamp, | ||
} | ||
) | ||
|
||
func TestScheduleActionRecord_Validate(t *testing.T) { | ||
validScheduleActionRecord := scheduleActionRecord | ||
invalidId := scheduleActionRecord | ||
invalidId.Id = "123" | ||
emptyJobName := scheduleActionRecord | ||
emptyJobName.JobName = "" | ||
emptyAction := scheduleActionRecord | ||
emptyAction.Action = ScheduleAction{} | ||
invalidAction := scheduleActionRecord | ||
invalidAction.Action = ScheduleAction{ | ||
Type: common.ActionEdgeXMessageBus, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
} | ||
invalidStatus := scheduleActionRecord | ||
invalidStatus.Status = "xxx" | ||
|
||
tests := []struct { | ||
name string | ||
request ScheduleActionRecord | ||
expectedErr bool | ||
}{ | ||
{"valid ScheduleActionRecord", validScheduleActionRecord, false}, | ||
{"invalid ScheduleActionRecord, invalid ID", invalidId, true}, | ||
{"invalid ScheduleActionRecord, empty JobName", emptyJobName, true}, | ||
{"invalid ScheduleActionRecord, empty Action", emptyAction, true}, | ||
{"invalid ScheduleActionRecord, invalid Action", invalidAction, true}, | ||
{"invalid ScheduleActionRecord, invalid Status", invalidStatus, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.request.Validate() | ||
if tt.expectedErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToScheduleActionRecordModel(t *testing.T) { | ||
result := ToScheduleActionRecordModel(scheduleActionRecord) | ||
assert.Equal(t, scheduleActionRecordModel, result, "ToScheduleActionRecordModel did not result in ScheduleActionRecord model") | ||
} | ||
|
||
func TestFromScheduleActionRecordModelToDTO(t *testing.T) { | ||
result := FromScheduleActionRecordModelToDTO(scheduleActionRecordModel) | ||
assert.Equal(t, scheduleActionRecord, result, "FromScheduleActionRecordModelToDTO did not result in ScheduleActionRecord dto") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package dtos | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/edgexfoundry/go-mod-core-contracts/v3/common" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/models" | ||
) | ||
|
||
const ( | ||
jobName = "mock-job-name" | ||
payload = "eyJ0ZXN0I" | ||
topic = "mock-topic" | ||
crontab = "0 0 0 1 1 *" | ||
) | ||
|
||
var scheduleActionEdgeXMessageBus = ScheduleAction{ | ||
Type: common.ActionEdgeXMessageBus, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
EdgeXMessageBusAction: EdgeXMessageBusAction{ | ||
Topic: topic, | ||
}, | ||
} | ||
|
||
var scheduleActionEdgeXMessageBusModel = models.EdgeXMessageBusAction{ | ||
BaseScheduleAction: models.BaseScheduleAction{ | ||
Type: common.ActionEdgeXMessageBus, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
Topic: topic, | ||
} | ||
|
||
var scheduleActionRest = ScheduleAction{ | ||
Type: common.ActionREST, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
RESTAction: RESTAction{ | ||
Address: testPath, | ||
}, | ||
} | ||
|
||
var scheduleActionRestModel = models.RESTAction{ | ||
BaseScheduleAction: models.BaseScheduleAction{ | ||
Type: common.ActionREST, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
Address: testPath, | ||
} | ||
|
||
var scheduleActionDeviceControl = ScheduleAction{ | ||
Type: common.ActionDeviceControl, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
DeviceControlAction: DeviceControlAction{ | ||
DeviceName: TestDeviceName, | ||
SourceName: TestSourceName, | ||
}, | ||
} | ||
|
||
var scheduleActionDeviceControlModel = models.DeviceControlAction{ | ||
BaseScheduleAction: models.BaseScheduleAction{ | ||
Type: common.ActionDeviceControl, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
DeviceName: TestDeviceName, | ||
SourceName: TestSourceName, | ||
} | ||
|
||
var scheduleIntervalDef = ScheduleDef{ | ||
Type: common.DefInterval, | ||
IntervalScheduleDef: IntervalScheduleDef{ | ||
Interval: interval, | ||
}, | ||
} | ||
|
||
var scheduleIntervalDefModel = models.IntervalScheduleDef{ | ||
BaseScheduleDef: models.BaseScheduleDef{ | ||
Type: common.DefInterval, | ||
}, | ||
Interval: interval, | ||
} | ||
|
||
var scheduleCronDef = ScheduleDef{ | ||
Type: common.DefCron, | ||
CronScheduleDef: CronScheduleDef{ | ||
Crontab: crontab, | ||
}, | ||
} | ||
|
||
var scheduleCronDefModel = models.CronScheduleDef{ | ||
BaseScheduleDef: models.BaseScheduleDef{ | ||
Type: common.DefCron, | ||
}, | ||
Crontab: crontab, | ||
} | ||
|
||
var ( | ||
scheduleJob = ScheduleJob{ | ||
DBTimestamp: DBTimestamp{}, | ||
Id: TestUUID, | ||
Name: jobName, | ||
Definition: scheduleIntervalDef, | ||
Actions: []ScheduleAction{scheduleActionEdgeXMessageBus}, | ||
AdminState: testAdminState, | ||
} | ||
scheduleJobModel = models.ScheduleJob{ | ||
DBTimestamp: models.DBTimestamp{}, | ||
Id: TestUUID, | ||
Name: jobName, | ||
Definition: scheduleIntervalDefModel, | ||
Actions: []models.ScheduleAction{scheduleActionEdgeXMessageBusModel}, | ||
AdminState: models.AdminState(testAdminState), | ||
} | ||
) | ||
|
||
func TestScheduleJob_Validate(t *testing.T) { | ||
validScheduleJob := scheduleJob | ||
invalidId := scheduleJob | ||
invalidId.Id = "123" | ||
emptyName := scheduleJob | ||
emptyName.Name = "" | ||
emptyDef := scheduleJob | ||
emptyDef.Definition = ScheduleDef{} | ||
invalidIntervalDef := scheduleJob | ||
invalidIntervalDef.Definition = ScheduleDef{ | ||
Type: common.DefInterval, | ||
IntervalScheduleDef: IntervalScheduleDef{ | ||
Interval: "", | ||
}, | ||
} | ||
invalidCronDef := scheduleJob | ||
invalidCronDef.Definition = ScheduleDef{ | ||
Type: common.DefCron, | ||
CronScheduleDef: CronScheduleDef{ | ||
Crontab: "", | ||
}, | ||
} | ||
emptyActions := scheduleJob | ||
emptyActions.Actions = nil | ||
invalidEdgeXMessageBusAction := scheduleJob | ||
invalidEdgeXMessageBusAction.Actions = []ScheduleAction{ | ||
{ | ||
Type: common.ActionEdgeXMessageBus, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
} | ||
invalidRestAction := scheduleJob | ||
invalidRestAction.Actions = []ScheduleAction{ | ||
{ | ||
Type: common.ActionREST, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
} | ||
invalidDeviceControlAction := scheduleJob | ||
invalidDeviceControlAction.Actions = []ScheduleAction{ | ||
{ | ||
Type: common.ActionDeviceControl, | ||
ContentType: common.ContentTypeJSON, | ||
Payload: []byte(payload), | ||
}, | ||
} | ||
invalidAdminState := scheduleJob | ||
invalidAdminState.AdminState = "xxx" | ||
|
||
tests := []struct { | ||
name string | ||
request ScheduleJob | ||
expectedErr bool | ||
}{ | ||
{"valid ScheduleJob", validScheduleJob, false}, | ||
{"invalid ScheduleJob, invalid ID", invalidId, true}, | ||
{"invalid ScheduleJob, empty Name", emptyName, true}, | ||
{"invalid ScheduleJob, empty Definition", emptyDef, true}, | ||
{"invalid ScheduleJob, invalid Interval Definition", invalidIntervalDef, true}, | ||
{"invalid ScheduleJob, invalid Cron Definition", invalidCronDef, true}, | ||
{"invalid ScheduleJob, empty Actions", emptyActions, true}, | ||
{"invalid ScheduleJob, invalid EdgeXMessageBus Actions", invalidEdgeXMessageBusAction, true}, | ||
{"invalid ScheduleJob, invalid REST Actions", invalidRestAction, true}, | ||
{"invalid ScheduleJob, invalid DeviceControl Actions", invalidDeviceControlAction, true}, | ||
{"invalid ScheduleJob, invalid AdminState", invalidAdminState, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
err := tt.request.Validate() | ||
if tt.expectedErr { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestToScheduleJobModel(t *testing.T) { | ||
result := ToScheduleJobModel(scheduleJob) | ||
assert.Equal(t, scheduleJobModel, result, "ToScheduleJobModel did not result in ScheduleJob model") | ||
} | ||
|
||
func TestFromScheduleJobModelToDTO(t *testing.T) { | ||
result := FromScheduleJobModelToDTO(scheduleJobModel) | ||
assert.Equal(t, scheduleJob, result, "FromScheduleJobModelToDTO did not result in ScheduleJob dto") | ||
} | ||
|
||
func TestToScheduleDefModel(t *testing.T) { | ||
result := ToScheduleDefModel(scheduleIntervalDef) | ||
assert.Equal(t, scheduleIntervalDefModel, result, "ToScheduleDefModel did not result in Interval ScheduleDef model") | ||
|
||
result2 := ToScheduleDefModel(scheduleCronDef) | ||
assert.Equal(t, scheduleCronDefModel, result2, "ToScheduleDefModel did not result in Cron ScheduleDef model") | ||
} | ||
|
||
func TestFromScheduleDefModelToDTO(t *testing.T) { | ||
result := FromScheduleDefModelToDTO(scheduleIntervalDefModel) | ||
assert.Equal(t, scheduleIntervalDef, result, "FromScheduleDefModelToDTO did not result in Interval ScheduleDef dto") | ||
|
||
result2 := FromScheduleDefModelToDTO(scheduleCronDefModel) | ||
assert.Equal(t, scheduleCronDef, result2, "FromScheduleDefModelToDTO did not result in Cron ScheduleDef dto") | ||
} | ||
|
||
func TestToScheduleActionModel(t *testing.T) { | ||
result := ToScheduleActionModel(scheduleActionEdgeXMessageBus) | ||
assert.Equal(t, scheduleActionEdgeXMessageBusModel, result, "ToScheduleActionModel did not result in EdgeXMessageBus ScheduleAction model") | ||
|
||
result2 := ToScheduleActionModel(scheduleActionRest) | ||
assert.Equal(t, scheduleActionRestModel, result2, "ToScheduleActionModel did not result in REST ScheduleAction model") | ||
|
||
result3 := ToScheduleActionModel(scheduleActionDeviceControl) | ||
assert.Equal(t, scheduleActionDeviceControlModel, result3, "ToScheduleActionModel did not result in DeviceControl ScheduleAction model") | ||
} | ||
|
||
func TestFromScheduleActionModelToDTO(t *testing.T) { | ||
result := FromScheduleActionModelToDTO(scheduleActionEdgeXMessageBusModel) | ||
assert.Equal(t, scheduleActionEdgeXMessageBus, result, "FromScheduleActionModelToDTO did not result in EdgeXMessageBus ScheduleAction dto") | ||
|
||
result2 := FromScheduleActionModelToDTO(scheduleActionRestModel) | ||
assert.Equal(t, scheduleActionRest, result2, "FromScheduleActionModelToDTO did not result in REST ScheduleAction dto") | ||
|
||
result3 := FromScheduleActionModelToDTO(scheduleActionDeviceControlModel) | ||
assert.Equal(t, scheduleActionDeviceControl, result3, "FromScheduleActionModelToDTO did not result in DeviceControl ScheduleAction dto") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.