dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/deviceprofile_test.go (about) 1 // 2 // Copyright (C) 2021-2023 IOTech Ltd 3 // 4 // SPDX-License-Identifier: Apache-2.0 5 6 package dtos 7 8 import ( 9 "testing" 10 11 "gopkg.in/yaml.v3" 12 13 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 14 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models" 15 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 var testLabels = []string{"MODBUS", "TEMP"} 21 var testAttributes = map[string]interface{}{ 22 "TestAttribute": "TestAttributeValue", 23 } 24 var testMappings = map[string]string{"0": "off", "1": "on"} 25 var testTags = map[string]any{"TestTagsKey": "TestTagsValue"} 26 var testOptional = map[string]any{"isVirtual": false} 27 28 var testDeviceProfile = models.DeviceProfile{ 29 Name: TestDeviceProfileName, 30 Manufacturer: TestManufacturer, 31 Description: TestDescription, 32 Model: TestModel, 33 Labels: testLabels, 34 DeviceResources: []models.DeviceResource{{ 35 Name: TestDeviceResourceName, 36 Description: TestDescription, 37 Attributes: testAttributes, 38 Properties: models.ResourceProperties{ 39 ValueType: common.ValueTypeInt16, 40 ReadWrite: common.ReadWrite_RW, 41 Optional: testOptional, 42 }, 43 Tags: testTags, 44 }}, 45 DeviceCommands: []models.DeviceCommand{{ 46 Name: TestDeviceCommandName, 47 ReadWrite: common.ReadWrite_RW, 48 ResourceOperations: []models.ResourceOperation{{ 49 DeviceResource: TestDeviceResourceName, 50 Mappings: testMappings, 51 }}, 52 Tags: testTags, 53 }}, 54 } 55 56 func profileData() DeviceProfile { 57 return DeviceProfile{ 58 DeviceProfileBasicInfo: DeviceProfileBasicInfo{ 59 Name: TestDeviceProfileName, 60 Manufacturer: TestManufacturer, 61 Description: TestDescription, 62 Model: TestModel, 63 Labels: testLabels, 64 }, 65 DeviceResources: []DeviceResource{{ 66 Name: TestDeviceResourceName, 67 Description: TestDescription, 68 Attributes: testAttributes, 69 Properties: ResourceProperties{ 70 ValueType: common.ValueTypeInt16, 71 ReadWrite: common.ReadWrite_RW, 72 Optional: testOptional, 73 }, 74 Tags: testTags, 75 }}, 76 DeviceCommands: []DeviceCommand{{ 77 Name: TestDeviceCommandName, 78 ReadWrite: common.ReadWrite_RW, 79 ResourceOperations: []ResourceOperation{{ 80 DeviceResource: TestDeviceResourceName, 81 Mappings: testMappings, 82 }}, 83 Tags: testTags, 84 }}, 85 } 86 } 87 88 func TestFromDeviceProfileModelToDTO(t *testing.T) { 89 result := FromDeviceProfileModelToDTO(testDeviceProfile) 90 assert.Equal(t, profileData(), result, "FromDeviceProfileModelToDTO did not result in expected device profile DTO.") 91 } 92 93 func TestDeviceProfileDTOValidation(t *testing.T) { 94 valid := profileData() 95 duplicatedDeviceResource := profileData() 96 duplicatedDeviceResource.DeviceResources = append( 97 duplicatedDeviceResource.DeviceResources, DeviceResource{Name: TestDeviceResourceName}) 98 duplicatedDeviceCommand := profileData() 99 duplicatedDeviceCommand.DeviceCommands = append( 100 duplicatedDeviceCommand.DeviceCommands, DeviceCommand{Name: TestDeviceCommandName}) 101 mismatchedResource := profileData() 102 mismatchedResource.DeviceCommands[0].ResourceOperations = append( 103 mismatchedResource.DeviceCommands[0].ResourceOperations, ResourceOperation{DeviceResource: "missMatchedResource"}) 104 invalidReadWrite := profileData() 105 invalidReadWrite.DeviceResources[0].Properties.ReadWrite = common.ReadWrite_R 106 binaryWithWritePermission := profileData() 107 binaryWithWritePermission.DeviceResources[0].Properties.ValueType = common.ValueTypeBinary 108 binaryWithWritePermission.DeviceResources[0].Properties.ReadWrite = common.ReadWrite_RW 109 110 tests := []struct { 111 name string 112 profile DeviceProfile 113 expectError bool 114 }{ 115 {"valid device profile", valid, false}, 116 {"duplicated device resource", duplicatedDeviceResource, true}, 117 {"duplicated device command", duplicatedDeviceCommand, true}, 118 {"mismatched resource", mismatchedResource, true}, 119 {"invalid ReadWrite permission", invalidReadWrite, true}, 120 {"write permission not support Binary value type", binaryWithWritePermission, true}, 121 } 122 123 for _, tt := range tests { 124 t.Run(tt.name, func(t *testing.T) { 125 err := tt.profile.Validate() 126 if tt.expectError { 127 require.Error(t, err) 128 } else { 129 require.NoError(t, err) 130 } 131 }) 132 } 133 } 134 135 func TestAddDeviceProfile_UnmarshalYAML(t *testing.T) { 136 valid := profileData() 137 resultTestBytes, _ := yaml.Marshal(profileData()) 138 type args struct { 139 data []byte 140 } 141 tests := []struct { 142 name string 143 expected DeviceProfile 144 args args 145 wantErr bool 146 }{ 147 {"valid", valid, args{resultTestBytes}, false}, 148 {"invalid", DeviceProfile{}, args{[]byte("Invalid DeviceProfile")}, true}, 149 } 150 for _, tt := range tests { 151 t.Run(tt.name, func(t *testing.T) { 152 var dp DeviceProfile 153 err := yaml.Unmarshal(tt.args.data, &dp) 154 if tt.wantErr { 155 require.Error(t, err) 156 } else { 157 require.NoError(t, err) 158 assert.Equal(t, tt.expected, dp, "Unmarshal did not result in expected DeviceProfileRequest.") 159 } 160 }) 161 } 162 }