Skip to content

Commit

Permalink
Merge pull request #4941 from jinlinGuan/issue-4937
Browse files Browse the repository at this point in the history
fix: Delete device profile in use should return 409
  • Loading branch information
cloudxxx8 authored Oct 7, 2024
2 parents 77e0adc + d76678a commit eff0073
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
16 changes: 16 additions & 0 deletions internal/core/metadata/application/deviceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func DeleteDeviceProfileByName(name string, ctx context.Context, dic *di.Contain
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
// Check the associated Device and ProvisionWatcher existence
devices, edgeXErr := dbClient.DevicesByProfileName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(devices) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated device exists", nil)
}
provisionWatchers, edgeXErr := dbClient.ProvisionWatchersByProfileName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(provisionWatchers) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device profile when associated provisionWatcher exists", nil)
}

err = dbClient.DeleteDeviceProfileByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
Expand Down
16 changes: 16 additions & 0 deletions internal/core/metadata/application/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ func DeleteDeviceServiceByName(name string, ctx context.Context, dic *di.Contain
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
}
// Check the associated Device and ProvisionWatcher existence
devices, edgeXErr := dbClient.DevicesByServiceName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(devices) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated device exists", nil)
}
provisionWatchers, edgeXErr := dbClient.ProvisionWatchersByServiceName(0, 1, name)
if edgeXErr != nil {
return errors.NewCommonEdgeXWrapper(edgeXErr)
}
if len(provisionWatchers) > 0 {
return errors.NewCommonEdgeX(errors.KindStatusConflict, "fail to delete the device service when associated provisionWatcher exists", nil)
}

err = dbClient.DeleteDeviceServiceByName(name)
if err != nil {
return errors.NewCommonEdgeXWrapper(err)
Expand Down
18 changes: 10 additions & 8 deletions internal/core/metadata/controller/http/deviceprofile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,18 +1113,20 @@ func TestDeleteDeviceProfileByName(t *testing.T) {

dic := mockDic()
dbClientMock := &mocks.DBClient{}
dbClientMock.On("DeviceProfileByName", deviceProfile.Name).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, deviceProfile.Name).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, deviceProfile.Name).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceProfileByName", deviceProfile.Name).Return(nil)
dbClientMock.On("DevicesByProfileName", 0, 1, notFoundName).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, notFoundName).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceProfileByName", notFoundName).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device profile doesn't exist in the database", nil))
dbClientMock.On("DeleteDeviceProfileByName", deviceExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device profile when associated device exists", nil))
dbClientMock.On("DeleteDeviceProfileByName", provisionWatcherExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device profile when associated provisionWatcher exists", nil))

dbClientMock.On("DeviceProfileByName", notFoundName).Return(models.DeviceProfile{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "not found", nil))

dbClientMock.On("DeviceProfileByName", deviceExists).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, deviceExists).Return([]models.Device{{ServiceName: testDeviceServiceName}}, nil)

dbClientMock.On("DeviceProfileByName", provisionWatcherExists).Return(models.DeviceProfile{}, nil)
dbClientMock.On("DevicesByProfileName", 0, 1, provisionWatcherExists).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByProfileName", 0, 1, provisionWatcherExists).Return([]models.ProvisionWatcher{models.ProvisionWatcher{}}, nil)
dbClientMock.On("DeviceProfileByName", mock.Anything).Return(models.DeviceProfile{}, nil)

dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand Down
18 changes: 10 additions & 8 deletions internal/core/metadata/controller/http/deviceservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,18 +489,20 @@ func TestDeleteDeviceServiceByName(t *testing.T) {

dic := mockDic()
dbClientMock := &dbMock.DBClient{}
dbClientMock.On("DeviceServiceByName", deviceService.Name).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceService.Name).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, deviceService.Name).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceServiceByName", deviceService.Name).Return(nil)
dbClientMock.On("DevicesByServiceName", 0, 1, notFoundName).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, notFoundName).Return([]models.ProvisionWatcher{}, nil)
dbClientMock.On("DeleteDeviceServiceByName", notFoundName).Return(errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device service doesn't exist in the database", nil))
dbClientMock.On("DeleteDeviceServiceByName", deviceExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device service when associated device exists", nil))
dbClientMock.On("DeleteDeviceServiceByName", provisionWatcherExists).Return(errors.NewCommonEdgeX(
errors.KindStatusConflict, "fail to delete the device service when associated provisionWatcher exists", nil))

dbClientMock.On("DeviceServiceByName", notFoundName).Return(models.DeviceService{}, errors.NewCommonEdgeX(errors.KindEntityDoesNotExist, "device service doesn't exist in the database", nil))

dbClientMock.On("DeviceServiceByName", deviceExists).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, deviceExists).Return([]models.Device{{ServiceName: testDeviceServiceName}}, nil)

dbClientMock.On("DeviceServiceByName", provisionWatcherExists).Return(models.DeviceService{}, nil)
dbClientMock.On("DevicesByServiceName", 0, 1, provisionWatcherExists).Return([]models.Device{}, nil)
dbClientMock.On("ProvisionWatchersByServiceName", 0, 1, provisionWatcherExists).Return([]models.ProvisionWatcher{models.ProvisionWatcher{}}, nil)
dbClientMock.On("DeviceServiceByName", mock.Anything).Return(models.DeviceService{}, nil)

dic.Update(di.ServiceConstructorMap{
container.DBClientInterfaceName: func(get di.Get) interface{} {
return dbClientMock
Expand Down

0 comments on commit eff0073

Please sign in to comment.