99 "encoding/json"
1010 "errors"
1111 "fmt"
12+ "math"
1213 "net/http"
1314 "net/http/httptest"
1415 "strings"
@@ -183,6 +184,8 @@ func TestAddDevice(t *testing.T) {
183184 dbClientMock .On ("AddDevice" , emptyProtocolsModel ).Return (emptyProtocolsModel , nil )
184185 invalidProtocols := testDevice
185186 invalidProtocols .Device .Protocols = map [string ]dtos.ProtocolProperties {"others" : {}}
187+ ownParent := testDevice
188+ ownParent .Device .Parent = ownParent .Device .Name
186189
187190 dic .Update (di.ServiceConstructorMap {
188191 container .DBClientInterfaceName : func (get di.Get ) interface {} {
@@ -217,6 +220,7 @@ func TestAddDevice(t *testing.T) {
217220 {"Invalid - not found device service" , []requests.AddDeviceRequest {notFoundService }, http .StatusMultiStatus , http .StatusBadRequest , false , false , false },
218221 {"Invalid - device service unavailable" , []requests.AddDeviceRequest {valid }, http .StatusMultiStatus , http .StatusServiceUnavailable , true , false , false },
219222 {"Valid - force add device" , []requests.AddDeviceRequest {validForceAdd }, http .StatusMultiStatus , http .StatusCreated , false , true , true },
223+ {"Invalid - own parent" , []requests.AddDeviceRequest {ownParent }, http .StatusMultiStatus , http .StatusBadRequest , false , false , false },
220224 }
221225 for _ , testCase := range tests {
222226 t .Run (testCase .name , func (t * testing.T ) {
@@ -319,14 +323,18 @@ func TestDeleteDeviceByName(t *testing.T) {
319323 device := dtos .ToDeviceModel (buildTestDeviceRequest ().Device )
320324 noName := ""
321325 notFoundName := "notFoundName"
326+ deviceParent := device
327+ deviceParent .Name = "someOtherName"
322328
323329 dic := mockDic ()
324330 dbClientMock := & dbMock.DBClient {}
331+ dbClientMock .On ("DeviceTree" , device .Name , 1 , 0 , 1 , []string (nil )).Return (uint32 (0 ), nil , nil )
325332 dbClientMock .On ("DeleteDeviceByName" , device .Name ).Return (nil )
326333 dbClientMock .On ("DeleteDeviceByName" , notFoundName ).Return (edgexErr .NewCommonEdgeX (edgexErr .KindEntityDoesNotExist , "device doesn't exist in the database" , nil ))
327334 dbClientMock .On ("DeviceByName" , notFoundName ).Return (device , edgexErr .NewCommonEdgeX (edgexErr .KindEntityDoesNotExist , "device doesn't exist in the database" , nil ))
328335 dbClientMock .On ("DeviceByName" , device .Name ).Return (device , nil )
329- dbClientMock .On ("DeviceServiceByName" , device .ServiceName ).Return (models.DeviceService {BaseAddress : testBaseAddress }, nil )
336+ dbClientMock .On ("DeviceByName" , deviceParent .Name ).Return (device , nil )
337+ dbClientMock .On ("DeviceTree" , deviceParent .Name , 1 , 0 , 1 , []string (nil )).Return (uint32 (1 ), []models.Device {device }, nil )
330338 dic .Update (di.ServiceConstructorMap {
331339 container .DBClientInterfaceName : func (get di.Get ) interface {} {
332340 return dbClientMock
@@ -344,6 +352,7 @@ func TestDeleteDeviceByName(t *testing.T) {
344352 {"Valid - delete device by name" , device .Name , http .StatusOK },
345353 {"Invalid - name parameter is empty" , noName , http .StatusBadRequest },
346354 {"Invalid - device not found by name" , notFoundName , http .StatusNotFound },
355+ {"Invalid - device has children" , deviceParent .Name , http .StatusConflict },
347356 }
348357 for _ , testCase := range tests {
349358 t .Run (testCase .name , func (t * testing.T ) {
@@ -603,14 +612,17 @@ func TestPatchDevice(t *testing.T) {
603612 notFoundService .Device .ServiceName = & notFoundServiceName
604613 dbClientMock .On ("DeviceServiceNameExists" , * notFoundService .Device .ServiceName ).Return (false , nil )
605614
606- notFountProfileName := "notFoundProfile"
615+ notFoundProfileName := "notFoundProfile"
607616 notFoundProfile := testReq
608- notFoundProfile .Device .ProfileName = & notFountProfileName
617+ notFoundProfile .Device .ProfileName = & notFoundProfileName
609618 notFoundProfileDeviceModel := dsModels
610- notFoundProfileDeviceModel .ProfileName = notFountProfileName
619+ notFoundProfileDeviceModel .ProfileName = notFoundProfileName
611620 dbClientMock .On ("UpdateDevice" , notFoundProfileDeviceModel ).Return (
612621 edgexErr .NewCommonEdgeX (edgexErr .KindEntityDoesNotExist ,
613- fmt .Sprintf ("device profile '%s' does not exists" , notFountProfileName ), nil ))
622+ fmt .Sprintf ("device profile '%s' does not exists" , notFoundProfileName ), nil ))
623+
624+ ownParent := testReq
625+ ownParent .Device .Parent = ownParent .Device .Name
614626
615627 dic .Update (di.ServiceConstructorMap {
616628 container .DBClientInterfaceName : func (get di.Get ) interface {} {
@@ -642,7 +654,8 @@ func TestPatchDevice(t *testing.T) {
642654 {"Invalid - invalid protocols" , []requests.UpdateDeviceRequest {invalidProtocols }, http .StatusMultiStatus , http .StatusInternalServerError , true , false },
643655 {"Invalid - not found device service" , []requests.UpdateDeviceRequest {notFoundService }, http .StatusMultiStatus , http .StatusBadRequest , false , false },
644656 {"Invalid - device service unavailable" , []requests.UpdateDeviceRequest {valid }, http .StatusMultiStatus , http .StatusServiceUnavailable , true , false },
645- {"Valid - empty profile" , []requests.UpdateDeviceRequest {emptyProfile }, http .StatusMultiStatus , http .StatusOK , true , true }}
657+ {"Valid - empty profile" , []requests.UpdateDeviceRequest {emptyProfile }, http .StatusMultiStatus , http .StatusOK , true , true },
658+ {"Invalid - own parent" , []requests.UpdateDeviceRequest {ownParent }, http .StatusMultiStatus , http .StatusBadRequest , false , false }}
646659 for _ , testCase := range tests {
647660 t .Run (testCase .name , func (t * testing.T ) {
648661 e := echo .New ()
@@ -749,6 +762,8 @@ func TestAllDevices(t *testing.T) {
749762 dbClientMock .On ("AllDevices" , 0 , 5 , testDeviceLabels ).Return ([]models.Device {devices [0 ], devices [1 ]}, nil )
750763 dbClientMock .On ("AllDevices" , 1 , 2 , []string (nil )).Return ([]models.Device {devices [1 ], devices [2 ]}, nil )
751764 dbClientMock .On ("AllDevices" , 4 , 1 , testDeviceLabels ).Return ([]models.Device {}, edgexErr .NewCommonEdgeX (edgexErr .KindRangeNotSatisfiable , "query objects bounds out of range." , nil ))
765+ dbClientMock .On ("DeviceTree" , "foo" , 4 , 0 , 10 , []string (nil )).Return (uint32 (expectedDeviceTotalCount ), devices , nil )
766+ dbClientMock .On ("DeviceTree" , "foo" , math .MaxInt32 , 0 , 10 , testDeviceLabels ).Return (uint32 (expectedDeviceTotalCount ), devices , nil )
752767 dic .Update (di.ServiceConstructorMap {
753768 container .DBClientInterfaceName : func (get di.Get ) interface {} {
754769 return dbClientMock
@@ -762,15 +777,20 @@ func TestAllDevices(t *testing.T) {
762777 offset string
763778 limit string
764779 labels string
780+ descendantsOf string
781+ maxLevels string
765782 errorExpected bool
766783 expectedCount int
767784 expectedTotalCount uint32
768785 expectedStatusCode int
769786 }{
770- {"Valid - get devices without labels" , "0" , "10" , "" , false , 3 , expectedDeviceTotalCount , http .StatusOK },
771- {"Valid - get devices with labels" , "0" , "5" , strings .Join (testDeviceLabels , "," ), false , 2 , expectedDeviceTotalCount , http .StatusOK },
772- {"Valid - get devices with offset and no labels" , "1" , "2" , "" , false , 2 , expectedDeviceTotalCount , http .StatusOK },
773- {"Invalid - offset out of range" , "4" , "1" , strings .Join (testDeviceLabels , "," ), true , 0 , expectedDeviceTotalCount , http .StatusRequestedRangeNotSatisfiable },
787+ {"Valid - get devices without labels" , "0" , "10" , "" , "" , "" , false , 3 , expectedDeviceTotalCount , http .StatusOK },
788+ {"Valid - get devices with labels" , "0" , "5" , strings .Join (testDeviceLabels , "," ), "" , "" , false , 2 , expectedDeviceTotalCount , http .StatusOK },
789+ {"Valid - get devices with offset and no labels" , "1" , "2" , "" , "" , "" , false , 2 , expectedDeviceTotalCount , http .StatusOK },
790+ {"Invalid - offset out of range" , "4" , "1" , strings .Join (testDeviceLabels , "," ), "" , "" , true , 0 , expectedDeviceTotalCount , http .StatusRequestedRangeNotSatisfiable },
791+ {"Valid - get tree without labels" , "0" , "10" , "" , "foo" , "4" , false , 3 , expectedDeviceTotalCount , http .StatusOK },
792+ {"Valid - get tree with labels" , "0" , "10" , strings .Join (testDeviceLabels , "," ), "foo" , "-1" , false , 3 , expectedDeviceTotalCount , http .StatusOK },
793+ {"Invalid - maxLevels bad integer" , "4" , "1" , strings .Join (testDeviceLabels , "," ), "foo" , "bar" , true , 0 , 0 , http .StatusBadRequest },
774794 }
775795 for _ , testCase := range tests {
776796 t .Run (testCase .name , func (t * testing.T ) {
@@ -782,6 +802,12 @@ func TestAllDevices(t *testing.T) {
782802 if len (testCase .labels ) > 0 {
783803 query .Add (common .Labels , testCase .labels )
784804 }
805+ if testCase .descendantsOf != "" {
806+ query .Add (common .DescendantsOf , testCase .descendantsOf )
807+ }
808+ if testCase .maxLevels != "" {
809+ query .Add (common .MaxLevels , testCase .maxLevels )
810+ }
785811 req .URL .RawQuery = query .Encode ()
786812 require .NoError (t , err )
787813
0 commit comments