-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unit tests for provision refactor, update to latest Load fu…
…nction Signed-off-by: Elizabeth J Lee <elizabeth.j.lee@intel.com>
- Loading branch information
Showing
9 changed files
with
444 additions
and
56 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
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,41 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
// | ||
// # Copyright (C) 2023 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package provision | ||
|
||
import ( | ||
"github.com/edgexfoundry/device-sdk-go/v3/internal/cache" | ||
"github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/interfaces" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/requests" | ||
"github.com/stretchr/testify/assert" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func Test_processDevices(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
secretProvider interfaces.SecretProvider | ||
expectedNumProfiles int | ||
}{ | ||
{"valid load device from file", path.Join("..", "..", "example", "cmd", "device-simple", "res", "devices", "simple-device.yml"), nil, 1}, | ||
{"valid load device from uri", "https://github.com/raw/edgexfoundry/device-sdk-go/main/example/cmd/device-simple/res/devices/simple-device.yml", nil, 1}, | ||
{"invalid load device empty path", "", nil, 0}, | ||
{"invalid load device from file", path.Join("..", "..", "example", "cmd", "device-simple", "res", "devices", "bogus.yml"), nil, 0}, | ||
{"invalid load device invalid uri", "https://github.com/raw/edgexfoundry/device-sdk-go/main/example/cmd/device-simple/res/devices/bogus.yml", nil, 0}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var addDeviceRequests []requests.AddDeviceRequest | ||
lc := logger.MockLogger{} | ||
dic, _ := NewMockDIC() | ||
cache.InitCache(TestDeviceService, TestDeviceService, dic) | ||
addDeviceRequests = processDevices(tt.path, TestDeviceService, tt.secretProvider, lc, addDeviceRequests) | ||
assert.Equal(t, tt.expectedNumProfiles, len(addDeviceRequests)) | ||
}) | ||
} | ||
} |
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,119 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
// | ||
// # Copyright (C) 2023 Intel Corporation | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package provision | ||
|
||
import ( | ||
"context" | ||
"github.com/edgexfoundry/device-sdk-go/v3/internal/config" | ||
"github.com/edgexfoundry/device-sdk-go/v3/internal/container" | ||
bootstrapContainer "github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/container" | ||
"github.com/edgexfoundry/go-mod-bootstrap/v3/di" | ||
clientMocks "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/interfaces/mocks" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses" | ||
"github.com/edgexfoundry/go-mod-core-contracts/v3/models" | ||
) | ||
|
||
const ( | ||
TestDeviceService = "testDeviceService" | ||
TestDeviceWithTags = "testDeviceWithTags" | ||
TestDeviceWithoutTags = "testDeviceWithoutTags" | ||
TestProfile = "testProfile" | ||
TestDeviceResourceWithTags = "testResourceWithTags" | ||
TestDeviceResourceWithoutTags = "testResourceWithoutTags" | ||
TestDeviceCommandWithTags = "testCommandWithTags" | ||
TestDeviceCommandWithoutTags = "testCommandWithoutTags" | ||
TestResourceTagName = "testResourceTagName" | ||
TestResourceTagValue = "testResourceTagValue" | ||
TestCommandTagName = "testCommandTagName" | ||
TestCommandTagValue = "testCommandTagValue" | ||
TestDeviceTagName = "testDeviceTagName" | ||
TestDeviceTagValue = "testDeviceTagValue" | ||
TestDuplicateTagName = "testDuplicateTagName" | ||
) | ||
|
||
var profile = responses.DeviceProfileResponse{ | ||
Profile: dtos.DeviceProfile{ | ||
DeviceProfileBasicInfo: dtos.DeviceProfileBasicInfo{Name: TestProfile}, | ||
DeviceResources: []dtos.DeviceResource{ | ||
{ | ||
Name: TestDeviceResourceWithTags, | ||
Tags: dtos.Tags{ | ||
TestResourceTagName: TestResourceTagValue, | ||
}, | ||
}, | ||
{ | ||
Name: TestDeviceResourceWithoutTags, | ||
}, | ||
}, | ||
DeviceCommands: []dtos.DeviceCommand{ | ||
{ | ||
Name: TestDeviceCommandWithTags, | ||
Tags: dtos.Tags{ | ||
TestCommandTagName: TestCommandTagValue, | ||
TestDuplicateTagName: TestCommandTagValue, | ||
}, | ||
}, | ||
{ | ||
Name: TestDeviceCommandWithoutTags, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
func NewMockDIC() (*di.Container, *clientMocks.DeviceProfileClient) { | ||
configuration := &config.ConfigurationStruct{ | ||
Device: config.DeviceInfo{MaxCmdOps: 1}, | ||
} | ||
deviceService := &models.DeviceService{Name: TestDeviceService} | ||
|
||
devices := responses.MultiDevicesResponse{ | ||
Devices: []dtos.Device{ | ||
{ | ||
Name: TestDeviceWithTags, | ||
ProfileName: TestProfile, | ||
Tags: dtos.Tags{ | ||
TestDeviceTagName: TestDeviceTagValue, | ||
TestDuplicateTagName: TestDeviceTagValue, | ||
}, | ||
}, | ||
{ | ||
Name: TestDeviceWithoutTags, | ||
ProfileName: TestProfile, | ||
}, | ||
}, | ||
} | ||
dcMock := &clientMocks.DeviceClient{} | ||
dcMock.On("DevicesByServiceName", context.Background(), TestDeviceService, 0, -1).Return(devices, nil) | ||
|
||
dpcMock := &clientMocks.DeviceProfileClient{} | ||
dpcMock.On("DeviceProfileByName", context.Background(), TestProfile).Return(profile, nil) | ||
|
||
pwcMock := &clientMocks.ProvisionWatcherClient{} | ||
pwcMock.On("ProvisionWatchersByServiceName", context.Background(), TestDeviceService, 0, -1).Return(responses.MultiProvisionWatchersResponse{}, nil) | ||
|
||
return di.NewContainer(di.ServiceConstructorMap{ | ||
container.ConfigurationName: func(get di.Get) interface{} { | ||
return configuration | ||
}, | ||
container.DeviceServiceName: func(get di.Get) any { | ||
return deviceService | ||
}, | ||
bootstrapContainer.LoggingClientInterfaceName: func(get di.Get) interface{} { | ||
return logger.NewMockClient() | ||
}, | ||
bootstrapContainer.DeviceClientName: func(get di.Get) interface{} { | ||
return dcMock | ||
}, | ||
bootstrapContainer.DeviceProfileClientName: func(get di.Get) interface{} { | ||
return dpcMock | ||
}, | ||
bootstrapContainer.ProvisionWatcherClientName: func(get di.Get) interface{} { | ||
return pwcMock | ||
}, | ||
}), dpcMock | ||
} |
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
Oops, something went wrong.