-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4768 from weichou1229/issue-4767
feat: Check the auto events before adding or updating the Device
- Loading branch information
Showing
3 changed files
with
118 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Copyright (C) 2024 IOTech Ltd | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package application | ||
|
||
import ( | ||
"github.com/edgexfoundry/edgex-go/internal/core/metadata/container" | ||
"testing" | ||
|
||
"github.com/edgexfoundry/edgex-go/internal/core/metadata/infrastructure/interfaces/mocks" | ||
"github.com/edgexfoundry/go-mod-bootstrap/v3/di" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/models" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestValidateAutoEvents(t *testing.T) { | ||
source1 := "source1" | ||
command1 := "command1" | ||
deviceProfile := models.DeviceProfile{ | ||
Name: "test-profile", | ||
DeviceResources: []models.DeviceResource{{Name: source1}, {Name: "resource2"}}, | ||
DeviceCommands: []models.DeviceCommand{{Name: command1}, {Name: "command2"}}, | ||
} | ||
|
||
dic := di.NewContainer(di.ServiceConstructorMap{}) | ||
dbClientMock := &mocks.DBClient{} | ||
dbClientMock.On("DeviceProfileByName", mock.Anything).Return(deviceProfile, nil) | ||
dic.Update(di.ServiceConstructorMap{ | ||
container.DBClientInterfaceName: func(get di.Get) interface{} { | ||
return dbClientMock | ||
}, | ||
}) | ||
|
||
tests := []struct { | ||
name string | ||
device models.Device | ||
errorExpected bool | ||
}{ | ||
{"resource exist", | ||
models.Device{ | ||
AutoEvents: []models.AutoEvent{{SourceName: source1, Interval: "1s"}}, | ||
}, | ||
false, | ||
}, | ||
{"command exist", | ||
models.Device{ | ||
AutoEvents: []models.AutoEvent{{SourceName: source1, Interval: "1s"}, {SourceName: command1, Interval: "1s"}}, | ||
}, | ||
false, | ||
}, | ||
{"resource not exist", | ||
models.Device{ | ||
AutoEvents: []models.AutoEvent{{SourceName: "notFoundSource", Interval: "1s"}}, | ||
}, | ||
true, | ||
}, | ||
{"interval format not valid", | ||
models.Device{ | ||
AutoEvents: []models.AutoEvent{{SourceName: source1, Interval: "1"}}, | ||
}, | ||
true, | ||
}, | ||
} | ||
for _, testCase := range tests { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
err := validateAutoEvent(dic, testCase.device) | ||
if testCase.errorExpected { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters