Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check offset, limit, totalCount before querying transmissions #4995

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions internal/support/notifications/application/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ func TransmissionsByTimeRange(start int64, end int64, offset int, limit int, dic
// AllTransmissions queries transmissions by offset and limit
func AllTransmissions(offset, limit int, dic *di.Container) (transmissions []dtos.Transmission, totalCount uint32, err errors.EdgeX) {
dbClient := container.DBClientFrom(dic.Get)
models, err := dbClient.AllTransmissions(offset, limit)
if err == nil {
totalCount, err = dbClient.TransmissionTotalCount()

totalCount, err = dbClient.TransmissionTotalCount()
if err != nil {
return transmissions, totalCount, errors.NewCommonEdgeXWrapper(err)
}
cont, err := utils.CheckCountRange(totalCount, offset, limit)
if !cont {
return []dtos.Transmission{}, totalCount, err
}

models, err := dbClient.AllTransmissions(offset, limit)
if err != nil {
return transmissions, totalCount, errors.NewCommonEdgeXWrapper(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func TestAllTransmissions(t *testing.T) {
dbClientMock.On("TransmissionTotalCount").Return(expectedTransmissionCount, nil)
dbClientMock.On("AllTransmissions", 0, 20).Return(transmissions, nil)
dbClientMock.On("AllTransmissions", 1, 2).Return([]models.Transmission{transmissions[1], transmissions[2]}, nil)
dbClientMock.On("AllTransmissions", 4, 1).Return([]models.Transmission{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "query objects bounds out of range.", nil))
dbClientMock.On("AllTransmissions", 4, 1).Return([]models.Transmission{}, errors.NewCommonEdgeX(errors.KindRangeNotSatisfiable, "query objects bounds out of range.", nil))
dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand All @@ -213,7 +213,7 @@ func TestAllTransmissions(t *testing.T) {
}{
{"Valid - get transmissions without offset and limit", "", "", false, 3, expectedTransmissionCount, http.StatusOK},
{"Valid - get transmissions with offset and limit", "1", "2", false, 2, expectedTransmissionCount, http.StatusOK},
{"Invalid - offset out of range", "4", "1", true, 0, expectedTransmissionCount, http.StatusNotFound},
{"Invalid - offset out of range", "4", "1", true, 0, expectedTransmissionCount, http.StatusRequestedRangeNotSatisfiable},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
Expand Down
Loading