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(meta): Delete DS API should check the associated Device existence #3054

Merged
merged 1 commit into from
Jan 20, 2021
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
11 changes: 10 additions & 1 deletion internal/core/metadata/v2/application/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@ func DeleteDeviceServiceByName(name string, ctx context.Context, dic *di.Contain
return errors.NewCommonEdgeX(errors.KindContractInvalid, "name is empty", nil)
}
dbClient := v2MetadataContainer.DBClientFrom(dic.Get)
err := dbClient.DeleteDeviceServiceByName(name)

// Check the associated Device existence
devices, err := dbClient.DevicesByServiceName(0, 1, name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
if len(devices) > 0 {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "fail to delete the device service when associated device exists", nil)
}
err = dbClient.DeleteDeviceServiceByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,15 @@ func TestDeleteDeviceServiceByName(t *testing.T) {
deviceService := dtos.ToDeviceServiceModel(buildTestDeviceServiceRequest().Service)
noName := ""
notFoundName := "notFoundName"
deviceExists := "deviceExists"

dic := mockDic()
dbClientMock := &dbMock.DBClient{}
dbClientMock.On("DeleteDeviceServiceByName", deviceService.Name).Return(nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceService.Name).Return([]models.Device{}, nil)
dbClientMock.On("DeleteDeviceServiceByName", notFoundName).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device service doesn't exist in the database", nil))
dbClientMock.On("DevicesByServiceName", 0, 1, notFoundName).Return([]models.Device{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceExists).Return([]models.Device{models.Device{}}, nil)
dic.Update(di.ServiceConstructorMap{
v2MetadataContainer.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand All @@ -489,6 +493,7 @@ func TestDeleteDeviceServiceByName(t *testing.T) {
{"Valid - delete device service by name", deviceService.Name, false, http.StatusOK},
{"Invalid - name parameter is empty", noName, true, http.StatusBadRequest},
{"Invalid - device service not found by name", notFoundName, true, http.StatusNotFound},
{"Invalid - associated device exists", deviceExists, true, http.StatusBadRequest},
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
Expand Down