Skip to content

Commit

Permalink
fix: check offset, limit, totalCount before querying transmissions
Browse files Browse the repository at this point in the history
close #4988

Signed-off-by: Ginny Guan <[email protected]>
  • Loading branch information
jinlinGuan committed Nov 7, 2024
1 parent 44a947b commit 82df6aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
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

0 comments on commit 82df6aa

Please sign in to comment.