dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/devicecommand_test.go (about)

     1  //
     2  // Copyright (C) 2022 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package requests
     7  
     8  import (
     9  	"encoding/json"
    10  	"testing"
    11  
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    13  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    14  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  var emptyString = " "
    22  
    23  var testDeviceCommand = AddDeviceCommandRequest{
    24  	BaseRequest: dtoCommon.BaseRequest{
    25  		RequestId:   ExampleUUID,
    26  		Versionable: dtoCommon.NewVersionable(),
    27  	},
    28  	ProfileName: TestDeviceProfileName,
    29  	DeviceCommand: dtos.DeviceCommand{
    30  		Name:      TestDeviceCommandName,
    31  		ReadWrite: common.ReadWrite_RW,
    32  		ResourceOperations: []dtos.ResourceOperation{{
    33  			DeviceResource: TestDeviceResourceName,
    34  		}},
    35  	},
    36  }
    37  
    38  var testUpdateDeviceCommand = UpdateDeviceCommandRequest{
    39  	BaseRequest: dtoCommon.BaseRequest{
    40  		RequestId:   ExampleUUID,
    41  		Versionable: dtoCommon.NewVersionable(),
    42  	},
    43  	ProfileName:   TestDeviceProfileName,
    44  	DeviceCommand: mockUpdateDeviceCommandDTO(),
    45  }
    46  
    47  func mockUpdateDeviceCommandDTO() dtos.UpdateDeviceCommand {
    48  	testIsHidden := true
    49  	testName := TestDeviceCommandName
    50  	dc := dtos.UpdateDeviceCommand{}
    51  	dc.Name = &testName
    52  	dc.IsHidden = &testIsHidden
    53  	return dc
    54  }
    55  
    56  func TestAddDeviceCommandRequest_Validate(t *testing.T) {
    57  	valid := testDeviceCommand
    58  	noProfileName := testDeviceCommand
    59  	noProfileName.ProfileName = emptyString
    60  	noDeviceCommandName := testDeviceCommand
    61  	noDeviceCommandName.DeviceCommand.Name = emptyString
    62  	invalidReadWrite := testDeviceCommand
    63  	invalidReadWrite.DeviceCommand.ReadWrite = "invalid"
    64  	noResourceOperations := testDeviceCommand
    65  	noResourceOperations.DeviceCommand.ResourceOperations = nil
    66  
    67  	tests := []struct {
    68  		name        string
    69  		request     AddDeviceCommandRequest
    70  		expectedErr bool
    71  	}{
    72  		{"valid AddDeviceCommandRequest", valid, false},
    73  		{"invalid AddDeviceCommandRequest, no ProfileName", noProfileName, true},
    74  		{"invalid AddDeviceCommandRequest, no DeviceCommand Name", noDeviceCommandName, true},
    75  		{"invalid AddDeviceCommandRequest, invalid ReadWrite", invalidReadWrite, true},
    76  		{"invalid AddDeviceCommandRequest, no ResourceOperations", noResourceOperations, true},
    77  	}
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			err := tt.request.Validate()
    81  			if tt.expectedErr {
    82  				assert.Error(t, err)
    83  			} else {
    84  				assert.NoError(t, err)
    85  			}
    86  		})
    87  	}
    88  }
    89  
    90  func TestAddDeviceCommandRequest_UnmarshalJSON(t *testing.T) {
    91  	valid := testDeviceCommand
    92  	resultTestBytes, _ := json.Marshal(testDeviceCommand)
    93  	type args struct {
    94  		data []byte
    95  	}
    96  
    97  	tests := []struct {
    98  		name        string
    99  		request     AddDeviceCommandRequest
   100  		args        args
   101  		expectedErr bool
   102  	}{
   103  		{"unmarshal AddDeviceCommandRequest with success", valid, args{resultTestBytes}, false},
   104  		{"unmarshal invalid AddDeviceCommandRequest, empty data", AddDeviceCommandRequest{}, args{[]byte{}}, true},
   105  		{"unmarshal invalid AddDeviceCommandRequest, string data", AddDeviceCommandRequest{}, args{[]byte("Invalid AddDeviceCommandRequest")}, true},
   106  	}
   107  	for _, tt := range tests {
   108  		t.Run(tt.name, func(t *testing.T) {
   109  			var expected = tt.request
   110  			err := tt.request.UnmarshalJSON(tt.args.data)
   111  			if tt.expectedErr {
   112  				require.Error(t, err)
   113  			} else {
   114  				require.NoError(t, err)
   115  				assert.Equal(t, expected, tt.request, "Unmarshal did not result in expected AddDeviceCommandRequest.")
   116  			}
   117  		})
   118  	}
   119  }
   120  
   121  func TestUpdateDeviceCommandRequest_Validate(t *testing.T) {
   122  	valid := testUpdateDeviceCommand
   123  	noProfileName := testUpdateDeviceCommand
   124  	noProfileName.ProfileName = emptyString
   125  	noDeviceCommandName := testUpdateDeviceCommand
   126  	noDeviceCommandName.DeviceCommand.Name = &emptyString
   127  
   128  	tests := []struct {
   129  		name        string
   130  		request     UpdateDeviceCommandRequest
   131  		expectedErr bool
   132  	}{
   133  		{"valid UpdateDeviceCommandRequest", valid, false},
   134  		{"invalid UpdateDeviceCommandRequest, no profile name", noProfileName, true},
   135  		{"invalid UpdateDeviceCommandRequest, no device command name", noDeviceCommandName, true},
   136  	}
   137  	for _, tt := range tests {
   138  		t.Run(tt.name, func(t *testing.T) {
   139  			err := tt.request.Validate()
   140  			if tt.expectedErr {
   141  				assert.Error(t, err)
   142  			} else {
   143  				assert.NoError(t, err)
   144  			}
   145  		})
   146  	}
   147  }
   148  
   149  func TestUpdateDeviceCommandRequest_UnmarshalJSON_NilField(t *testing.T) {
   150  	reqJson := `{
   151  	    "apiVersion" : "v3",
   152          "requestId":"7a1707f0-166f-4c4b-bc9d-1d54c74e0137",
   153          "profileName": "TestProfile",
   154  		"deviceCommand":{"name":"TestCommand"}
   155  	}`
   156  	var req UpdateDeviceCommandRequest
   157  
   158  	err := req.UnmarshalJSON([]byte(reqJson))
   159  
   160  	require.NoError(t, err)
   161  	// Nil field checking is used to update with patch
   162  	assert.Nil(t, req.DeviceCommand.IsHidden)
   163  }
   164  
   165  func TestReplaceDeviceCommandModelFieldsWithDTO(t *testing.T) {
   166  	command := models.DeviceCommand{
   167  		Name:      TestDeviceCommandName,
   168  		ReadWrite: common.ReadWrite_R,
   169  		IsHidden:  false,
   170  		ResourceOperations: []models.ResourceOperation{{
   171  			DeviceResource: TestDeviceResourceName,
   172  		}},
   173  	}
   174  
   175  	patch := mockUpdateDeviceCommandDTO()
   176  
   177  	ReplaceDeviceCommandModelFieldsWithDTO(&command, patch)
   178  	assert.Equal(t, common.ReadWrite_R, command.ReadWrite)
   179  	assert.Equal(t, true, command.IsHidden)
   180  	assert.Equal(t, TestDeviceResourceName, command.ResourceOperations[0].DeviceResource)
   181  }