dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/interval_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  	"path"
    13  	"testing"
    14  
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    16  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    17  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    18  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/requests"
    19  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestAddIntervals(t *testing.T) {
    26  	ts := newTestServer(http.MethodPost, common.ApiIntervalRoute, []dtoCommon.BaseWithIdResponse{})
    27  	defer ts.Close()
    28  	dto := dtos.NewInterval(TestIntervalName, TestFrequency)
    29  	request := []requests.AddIntervalRequest{requests.NewAddIntervalRequest(dto)}
    30  	client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false)
    31  
    32  	res, err := client.Add(context.Background(), request)
    33  
    34  	require.NoError(t, err)
    35  	assert.IsType(t, []dtoCommon.BaseWithIdResponse{}, res)
    36  }
    37  
    38  func TestPatchIntervals(t *testing.T) {
    39  	ts := newTestServer(http.MethodPatch, common.ApiIntervalRoute, []dtoCommon.BaseResponse{})
    40  	defer ts.Close()
    41  	dto := dtos.NewUpdateInterval(TestIntervalName)
    42  	request := []requests.UpdateIntervalRequest{requests.NewUpdateIntervalRequest(dto)}
    43  	client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false)
    44  
    45  	res, err := client.Update(context.Background(), request)
    46  
    47  	require.NoError(t, err)
    48  	assert.IsType(t, []dtoCommon.BaseResponse{}, res)
    49  }
    50  
    51  func TestQueryAllIntervals(t *testing.T) {
    52  	ts := newTestServer(http.MethodGet, common.ApiAllIntervalRoute, responses.MultiIntervalsResponse{})
    53  	defer ts.Close()
    54  	client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false)
    55  
    56  	res, err := client.AllIntervals(context.Background(), 0, 10)
    57  
    58  	require.NoError(t, err)
    59  	assert.IsType(t, responses.MultiIntervalsResponse{}, res)
    60  }
    61  
    62  func TestQueryIntervalByName(t *testing.T) {
    63  	path := path.Join(common.ApiIntervalRoute, common.Name, TestIntervalName)
    64  	ts := newTestServer(http.MethodGet, path, responses.DeviceResponse{})
    65  	defer ts.Close()
    66  
    67  	client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false)
    68  
    69  	res, err := client.IntervalByName(context.Background(), TestIntervalName)
    70  	require.NoError(t, err)
    71  	assert.IsType(t, responses.IntervalResponse{}, res)
    72  }
    73  
    74  func TestDeleteIntervalByName(t *testing.T) {
    75  	path := path.Join(common.ApiIntervalRoute, common.Name, TestIntervalName)
    76  	ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{})
    77  	defer ts.Close()
    78  	client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false)
    79  
    80  	res, err := client.DeleteIntervalByName(context.Background(), TestIntervalName)
    81  
    82  	require.NoError(t, err)
    83  	assert.IsType(t, dtoCommon.BaseResponse{}, res)
    84  }