dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/deviceservicecommand_test.go (about)

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  // Copyright (C) 2023 Intel Corporation
     4  //
     5  // SPDX-License-Identifier: Apache-2.0
     6  
     7  package http
     8  
     9  import (
    10  	"context"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    16  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    17  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    18  
    19  	"github.com/google/uuid"
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  const (
    25  	TestUUID              = "7a1707f0-166f-4c4b-bc9d-1d54c74e0137"
    26  	TestTimestamp         = 1594963842
    27  	TestCommandName       = "TestCommand"
    28  	TestDeviceName        = "TestDevice"
    29  	TestDeviceProfileName = "TestDeviceProfileName"
    30  )
    31  
    32  var testEventDTO = dtos.Event{
    33  	Versionable: dtoCommon.Versionable{ApiVersion: common.ApiVersion},
    34  	Id:          TestUUID,
    35  	DeviceName:  TestDeviceName,
    36  	ProfileName: TestDeviceProfileName,
    37  	Origin:      TestTimestamp,
    38  	Tags: map[string]interface{}{
    39  		"GatewayID": "Houston-0001",
    40  		"Latitude":  "29.630771",
    41  		"Longitude": "-95.377603",
    42  	},
    43  }
    44  
    45  func TestGetCommand(t *testing.T) {
    46  	requestId := uuid.New().String()
    47  	expectedResponse := responses.NewEventResponse(requestId, "", http.StatusOK, testEventDTO)
    48  	ts := newTestServer(http.MethodGet, common.ApiDeviceRoute+"/"+common.Name+"/"+TestDeviceName+"/"+TestCommandName, expectedResponse)
    49  	defer ts.Close()
    50  
    51  	client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
    52  	res, err := client.GetCommand(context.Background(), ts.URL, TestDeviceName, TestCommandName, "")
    53  
    54  	require.NoError(t, err)
    55  	assert.Equal(t, expectedResponse, *res)
    56  }
    57  
    58  func TestSetCommand(t *testing.T) {
    59  	requestId := uuid.New().String()
    60  	expectedResponse := dtoCommon.NewBaseResponse(requestId, "", http.StatusOK)
    61  	ts := newTestServer(http.MethodPut, common.ApiDeviceRoute+"/"+common.Name+"/"+TestDeviceName+"/"+TestCommandName, expectedResponse)
    62  	defer ts.Close()
    63  
    64  	client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
    65  	res, err := client.SetCommand(context.Background(), ts.URL, TestDeviceName, TestCommandName, "", nil)
    66  
    67  	require.NoError(t, err)
    68  	assert.Equal(t, requestId, res.RequestId)
    69  }
    70  
    71  func TestSetCommandWithObject(t *testing.T) {
    72  	requestId := uuid.New().String()
    73  	expectedResponse := dtoCommon.NewBaseResponse(requestId, "", http.StatusOK)
    74  	ts := newTestServer(http.MethodPut, common.ApiDeviceRoute+"/"+common.Name+"/"+TestDeviceName+"/"+TestCommandName, expectedResponse)
    75  	defer ts.Close()
    76  	settings := map[string]interface{}{
    77  		"SwitchButton": map[string]interface{}{
    78  			"kind":  "button",
    79  			"value": "on",
    80  		},
    81  	}
    82  
    83  	client := NewDeviceServiceCommandClient(NewNullAuthenticationInjector(), false)
    84  	res, err := client.SetCommandWithObject(context.Background(), ts.URL, TestDeviceName, TestCommandName, "", settings)
    85  
    86  	require.NoError(t, err)
    87  	assert.Equal(t, requestId, res.RequestId)
    88  }