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

     1  //
     2  // Copyright (C) 2020-2021 Unknown author
     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  	"path"
    13  	"testing"
    14  
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    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/requests"
    18  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    19  
    20  	"github.com/stretchr/testify/require"
    21  )
    22  
    23  func TestAddDevices(t *testing.T) {
    24  	ts := newTestServer(http.MethodPost, common.ApiDeviceRoute, []dtoCommon.BaseWithIdResponse{})
    25  	defer ts.Close()
    26  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    27  	res, err := client.Add(context.Background(), []requests.AddDeviceRequest{})
    28  	require.NoError(t, err)
    29  	require.IsType(t, []dtoCommon.BaseWithIdResponse{}, res)
    30  }
    31  
    32  func TestPatchDevices(t *testing.T) {
    33  	ts := newTestServer(http.MethodPatch, common.ApiDeviceRoute, []dtoCommon.BaseResponse{})
    34  	defer ts.Close()
    35  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    36  	res, err := client.Update(context.Background(), []requests.UpdateDeviceRequest{})
    37  	require.NoError(t, err)
    38  	require.IsType(t, []dtoCommon.BaseResponse{}, res)
    39  }
    40  
    41  func TestQueryAllDevices(t *testing.T) {
    42  	ts := newTestServer(http.MethodGet, common.ApiAllDeviceRoute, responses.MultiDevicesResponse{})
    43  	defer ts.Close()
    44  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    45  	res, err := client.AllDevices(context.Background(), []string{"label1", "label2"}, 1, 10)
    46  	require.NoError(t, err)
    47  	require.IsType(t, responses.MultiDevicesResponse{}, res)
    48  }
    49  
    50  func TestDeviceNameExists(t *testing.T) {
    51  	deviceName := "device"
    52  	path := path.Join(common.ApiDeviceRoute, common.Check, common.Name, deviceName)
    53  	ts := newTestServer(http.MethodGet, path, dtoCommon.BaseResponse{})
    54  	defer ts.Close()
    55  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    56  	res, err := client.DeviceNameExists(context.Background(), deviceName)
    57  	require.NoError(t, err)
    58  	require.IsType(t, dtoCommon.BaseResponse{}, res)
    59  }
    60  
    61  func TestQueryDeviceByName(t *testing.T) {
    62  	deviceName := "device"
    63  	path := path.Join(common.ApiDeviceRoute, common.Name, deviceName)
    64  	ts := newTestServer(http.MethodGet, path, responses.DeviceResponse{})
    65  	defer ts.Close()
    66  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    67  	res, err := client.DeviceByName(context.Background(), deviceName)
    68  	require.NoError(t, err)
    69  	require.IsType(t, responses.DeviceResponse{}, res)
    70  }
    71  
    72  func TestDeleteDeviceByName(t *testing.T) {
    73  	deviceName := "device"
    74  	path := path.Join(common.ApiDeviceRoute, common.Name, deviceName)
    75  	ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
    76  	defer ts.Close()
    77  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    78  	res, err := client.DeleteDeviceByName(context.Background(), deviceName)
    79  	require.NoError(t, err)
    80  	require.IsType(t, dtoCommon.BaseResponse{}, res)
    81  }
    82  
    83  func TestQueryDevicesByProfileName(t *testing.T) {
    84  	profileName := "profile"
    85  	urlPath := path.Join(common.ApiDeviceRoute, common.Profile, common.Name, profileName)
    86  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiDevicesResponse{})
    87  	defer ts.Close()
    88  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
    89  	res, err := client.DevicesByProfileName(context.Background(), profileName, 1, 10)
    90  	require.NoError(t, err)
    91  	require.IsType(t, responses.MultiDevicesResponse{}, res)
    92  }
    93  
    94  func TestQueryDevicesByServiceName(t *testing.T) {
    95  	serviceName := "service"
    96  	urlPath := path.Join(common.ApiDeviceRoute, common.Service, common.Name, serviceName)
    97  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiDevicesResponse{})
    98  	defer ts.Close()
    99  	client := NewDeviceClient(ts.URL, NewNullAuthenticationInjector(), false)
   100  	res, err := client.DevicesByServiceName(context.Background(), serviceName, 1, 10)
   101  	require.NoError(t, err)
   102  	require.IsType(t, responses.MultiDevicesResponse{}, res)
   103  }