From 82df6aa76d7e5e8e8165c23ce0e8560af0818afb Mon Sep 17 00:00:00 2001 From: Ginny Guan Date: Thu, 7 Nov 2024 18:29:36 +0800 Subject: [PATCH] fix: check offset, limit, totalCount before querying transmissions close #4988 Signed-off-by: Ginny Guan --- .../notifications/application/transmission.go | 13 ++++++++++--- .../controller/http/transmission_test.go | 4 ++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/support/notifications/application/transmission.go b/internal/support/notifications/application/transmission.go index 418ad7dc85..c1669c09b7 100644 --- a/internal/support/notifications/application/transmission.go +++ b/internal/support/notifications/application/transmission.go @@ -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) } diff --git a/internal/support/notifications/controller/http/transmission_test.go b/internal/support/notifications/controller/http/transmission_test.go index 162e1f825d..a20b94c48d 100644 --- a/internal/support/notifications/controller/http/transmission_test.go +++ b/internal/support/notifications/controller/http/transmission_test.go @@ -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 @@ -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) {