dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/clients/http/deviceprofile_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  	"encoding/json"
    12  	"errors"
    13  	"net/http"
    14  	"net/http/httptest"
    15  	"os"
    16  	"path"
    17  	"path/filepath"
    18  	"runtime"
    19  	"testing"
    20  
    21  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    22  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    23  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/requests"
    24  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/responses"
    25  	edgexErrors "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    26  
    27  	"github.com/google/uuid"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestAddDeviceProfiles(t *testing.T) {
    33  	requestId := uuid.New().String()
    34  
    35  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    36  		if r.Method != http.MethodPost {
    37  			w.WriteHeader(http.StatusMethodNotAllowed)
    38  			return
    39  		}
    40  		if r.URL.EscapedPath() != common.ApiDeviceProfileRoute {
    41  			w.WriteHeader(http.StatusBadRequest)
    42  			return
    43  		}
    44  
    45  		w.WriteHeader(http.StatusMultiStatus)
    46  		br := dtoCommon.NewBaseWithIdResponse(requestId, "", http.StatusMultiStatus, uuid.New().String())
    47  		res, _ := json.Marshal([]dtoCommon.BaseWithIdResponse{br})
    48  		_, _ = w.Write(res)
    49  	}))
    50  	defer ts.Close()
    51  
    52  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
    53  	res, err := client.Add(context.Background(), []requests.DeviceProfileRequest{})
    54  	require.NoError(t, err)
    55  	require.NotNil(t, res)
    56  	require.Equal(t, requestId, res[0].RequestId)
    57  }
    58  
    59  func TestPutDeviceProfiles(t *testing.T) {
    60  	requestId := uuid.New().String()
    61  
    62  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    63  		if r.Method != http.MethodPut {
    64  			w.WriteHeader(http.StatusMethodNotAllowed)
    65  			return
    66  		}
    67  		if r.URL.EscapedPath() != common.ApiDeviceProfileRoute {
    68  			w.WriteHeader(http.StatusBadRequest)
    69  			return
    70  		}
    71  
    72  		w.WriteHeader(http.StatusMultiStatus)
    73  		br := dtoCommon.NewBaseResponse(requestId, "", http.StatusMultiStatus)
    74  		res, _ := json.Marshal([]dtoCommon.BaseResponse{br})
    75  		_, _ = w.Write(res)
    76  	}))
    77  	defer ts.Close()
    78  
    79  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
    80  	res, err := client.Update(context.Background(), []requests.DeviceProfileRequest{})
    81  	require.NoError(t, err)
    82  	require.NotNil(t, res)
    83  	require.Equal(t, requestId, res[0].RequestId)
    84  }
    85  
    86  func TestAddDeviceProfileByYaml(t *testing.T) {
    87  	requestId := uuid.New().String()
    88  
    89  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    90  		if r.Method != http.MethodPost {
    91  			w.WriteHeader(http.StatusMethodNotAllowed)
    92  			return
    93  		}
    94  		if r.URL.EscapedPath() != common.ApiDeviceProfileUploadFileRoute {
    95  			w.WriteHeader(http.StatusBadRequest)
    96  			return
    97  		}
    98  
    99  		w.WriteHeader(http.StatusCreated)
   100  		br := dtoCommon.NewBaseWithIdResponse(requestId, "", http.StatusCreated, uuid.New().String())
   101  		res, _ := json.Marshal(br)
   102  		_, _ = w.Write(res)
   103  	}))
   104  	defer ts.Close()
   105  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   106  	_, b, _, _ := runtime.Caller(0)
   107  
   108  	tests := []struct {
   109  		name          string
   110  		filePath      string
   111  		errorExpected bool
   112  	}{
   113  		{name: "Add device profile by yaml file", filePath: filepath.Dir(b) + "/data/sample-profile.yaml", errorExpected: false},
   114  		{name: "file not found error", filePath: filepath.Dir(b) + "/data/file-not-found.yaml", errorExpected: true},
   115  	}
   116  	for _, testCase := range tests {
   117  		t.Run(testCase.name, func(t *testing.T) {
   118  			res, err := client.AddByYaml(context.Background(), testCase.filePath)
   119  			if testCase.errorExpected {
   120  				require.True(t, errors.Is(err, os.ErrNotExist))
   121  				assert.Equal(t, edgexErrors.KindIOError, edgexErrors.Kind(err))
   122  			} else {
   123  				require.NoError(t, err)
   124  				assert.Equal(t, requestId, res.RequestId)
   125  				assert.Equal(t, http.StatusCreated, res.StatusCode)
   126  			}
   127  		})
   128  	}
   129  }
   130  
   131  func TestUpdateDeviceProfileByYaml(t *testing.T) {
   132  	requestId := uuid.New().String()
   133  
   134  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   135  		if r.Method != http.MethodPut {
   136  			w.WriteHeader(http.StatusMethodNotAllowed)
   137  			return
   138  		}
   139  		if r.URL.EscapedPath() != common.ApiDeviceProfileUploadFileRoute {
   140  			w.WriteHeader(http.StatusBadRequest)
   141  			return
   142  		}
   143  
   144  		w.WriteHeader(http.StatusOK)
   145  		br := dtoCommon.NewBaseResponse(requestId, "", http.StatusOK)
   146  		res, _ := json.Marshal(br)
   147  		_, _ = w.Write(res)
   148  	}))
   149  	defer ts.Close()
   150  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   151  	_, b, _, _ := runtime.Caller(0)
   152  
   153  	tests := []struct {
   154  		name          string
   155  		filePath      string
   156  		errorExpected bool
   157  	}{
   158  		{name: "Update device profile by yaml file", filePath: filepath.Dir(b) + "/data/sample-profile.yaml", errorExpected: false},
   159  		{name: "file not found error", filePath: filepath.Dir(b) + "/data/file-not-found.yaml", errorExpected: true},
   160  	}
   161  	for _, testCase := range tests {
   162  		t.Run(testCase.name, func(t *testing.T) {
   163  			res, err := client.UpdateByYaml(context.Background(), testCase.filePath)
   164  			if testCase.errorExpected {
   165  				require.True(t, errors.Is(err, os.ErrNotExist))
   166  				assert.Equal(t, edgexErrors.KindIOError, edgexErrors.Kind(err))
   167  			} else {
   168  				require.NoError(t, err)
   169  				assert.Equal(t, requestId, res.RequestId)
   170  				assert.Equal(t, http.StatusOK, res.StatusCode)
   171  			}
   172  		})
   173  	}
   174  }
   175  
   176  func TestDeleteDeviceProfileByName(t *testing.T) {
   177  	testName := "testName"
   178  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Name, testName)
   179  	ts := newTestServer(http.MethodDelete, urlPath, dtoCommon.BaseResponse{})
   180  	defer ts.Close()
   181  
   182  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   183  	res, err := client.DeleteByName(context.Background(), testName)
   184  	require.NoError(t, err)
   185  	require.NotNil(t, res)
   186  }
   187  
   188  func TestQueryDeviceProfileByName(t *testing.T) {
   189  	testName := "testName"
   190  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Name, testName)
   191  	ts := newTestServer(http.MethodGet, urlPath, responses.DeviceProfileResponse{})
   192  	defer ts.Close()
   193  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   194  	_, err := client.DeviceProfileByName(context.Background(), testName)
   195  	require.NoError(t, err)
   196  }
   197  
   198  func TestQueryAllDeviceProfiles(t *testing.T) {
   199  	ts := newTestServer(http.MethodGet, common.ApiAllDeviceProfileRoute, responses.MultiDeviceProfilesResponse{})
   200  	defer ts.Close()
   201  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   202  	_, err := client.AllDeviceProfiles(context.Background(), []string{"testLabel1", "testLabel2"}, 1, 10)
   203  	require.NoError(t, err)
   204  }
   205  
   206  func TestQueryDeviceProfilesByModel(t *testing.T) {
   207  	testModel := "testModel"
   208  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Model, testModel)
   209  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiDeviceProfilesResponse{})
   210  	defer ts.Close()
   211  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   212  	_, err := client.DeviceProfilesByModel(context.Background(), testModel, 1, 10)
   213  	require.NoError(t, err)
   214  }
   215  
   216  func TestQueryDeviceProfilesByManufacturer(t *testing.T) {
   217  	testManufacturer := "testManufacturer"
   218  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Manufacturer, testManufacturer)
   219  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiDeviceProfilesResponse{})
   220  	defer ts.Close()
   221  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   222  	_, err := client.DeviceProfilesByManufacturer(context.Background(), testManufacturer, 1, 10)
   223  	require.NoError(t, err)
   224  }
   225  
   226  func TestQueryDeviceProfilesByManufacturerAndModel(t *testing.T) {
   227  	testManufacturer := "testManufacturer"
   228  	testModel := "testModel"
   229  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Manufacturer, testManufacturer, common.Model, testModel)
   230  	ts := newTestServer(http.MethodGet, urlPath, responses.MultiDeviceProfilesResponse{})
   231  	defer ts.Close()
   232  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   233  	_, err := client.DeviceProfilesByManufacturerAndModel(context.Background(), testManufacturer, testModel, 1, 10)
   234  	require.NoError(t, err)
   235  }
   236  
   237  func TestDeviceResourceByProfileNameAndResourceName(t *testing.T) {
   238  	profileName := "testProfile"
   239  	resourceName := "testResource"
   240  	urlPath := path.Join(common.ApiDeviceResourceRoute, common.Profile, profileName, common.Resource, resourceName)
   241  	ts := newTestServer(http.MethodGet, urlPath, responses.DeviceResourceResponse{})
   242  	defer ts.Close()
   243  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   244  
   245  	res, err := client.DeviceResourceByProfileNameAndResourceName(context.Background(), profileName, resourceName)
   246  
   247  	require.NoError(t, err)
   248  	assert.IsType(t, responses.DeviceResourceResponse{}, res)
   249  }
   250  
   251  func TestUpdateDeviceProfileBasicInfo(t *testing.T) {
   252  	ts := newTestServer(http.MethodPatch, common.ApiDeviceProfileBasicInfoRoute, []dtoCommon.BaseResponse{})
   253  	defer ts.Close()
   254  
   255  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   256  	res, err := client.UpdateDeviceProfileBasicInfo(context.Background(), []requests.DeviceProfileBasicInfoRequest{})
   257  	require.NoError(t, err)
   258  	require.IsType(t, []dtoCommon.BaseResponse{}, res)
   259  }
   260  
   261  func TestAddDeviceProfileResource(t *testing.T) {
   262  	ts := newTestServer(http.MethodPost, common.ApiDeviceProfileResourceRoute, []dtoCommon.BaseResponse{})
   263  	defer ts.Close()
   264  
   265  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   266  	res, err := client.AddDeviceProfileResource(context.Background(), []requests.AddDeviceResourceRequest{})
   267  
   268  	require.NoError(t, err)
   269  	assert.IsType(t, []dtoCommon.BaseResponse{}, res)
   270  }
   271  
   272  func TestUpdateDeviceProfileResource(t *testing.T) {
   273  	ts := newTestServer(http.MethodPatch, common.ApiDeviceProfileResourceRoute, []dtoCommon.BaseResponse{})
   274  	defer ts.Close()
   275  
   276  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   277  	res, err := client.UpdateDeviceProfileResource(context.Background(), []requests.UpdateDeviceResourceRequest{})
   278  	require.NoError(t, err)
   279  	require.IsType(t, []dtoCommon.BaseResponse{}, res)
   280  }
   281  
   282  func TestDeleteDeviceResourceByName(t *testing.T) {
   283  	profileName := "testProfile"
   284  	resourceName := "testResource"
   285  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Name, profileName, common.Resource, resourceName)
   286  	ts := newTestServer(http.MethodDelete, urlPath, dtoCommon.BaseResponse{})
   287  	defer ts.Close()
   288  
   289  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   290  	res, err := client.DeleteDeviceResourceByName(context.Background(), profileName, resourceName)
   291  	require.NoError(t, err)
   292  	require.NotNil(t, res)
   293  }
   294  
   295  func TestAddDeviceProfileDeviceCommand(t *testing.T) {
   296  	ts := newTestServer(http.MethodPost, common.ApiDeviceProfileDeviceCommandRoute, []dtoCommon.BaseResponse{})
   297  	defer ts.Close()
   298  
   299  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   300  	res, err := client.AddDeviceProfileDeviceCommand(context.Background(), []requests.AddDeviceCommandRequest{})
   301  
   302  	require.NoError(t, err)
   303  	assert.IsType(t, []dtoCommon.BaseResponse{}, res)
   304  }
   305  
   306  func TestUpdateDeviceProfileDeviceCommand(t *testing.T) {
   307  	ts := newTestServer(http.MethodPatch, common.ApiDeviceProfileDeviceCommandRoute, []dtoCommon.BaseResponse{})
   308  	defer ts.Close()
   309  
   310  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   311  	res, err := client.UpdateDeviceProfileDeviceCommand(context.Background(), []requests.UpdateDeviceCommandRequest{})
   312  	require.NoError(t, err)
   313  	require.IsType(t, []dtoCommon.BaseResponse{}, res)
   314  }
   315  
   316  func TestDeleteDeviceCommandByName(t *testing.T) {
   317  	profileName := "testProfile"
   318  	commandName := "testCommand"
   319  	urlPath := path.Join(common.ApiDeviceProfileRoute, common.Name, profileName, common.DeviceCommand, commandName)
   320  	ts := newTestServer(http.MethodDelete, urlPath, dtoCommon.BaseResponse{})
   321  	defer ts.Close()
   322  
   323  	client := NewDeviceProfileClient(ts.URL, NewNullAuthenticationInjector(), false)
   324  	res, err := client.DeleteDeviceCommandByName(context.Background(), profileName, commandName)
   325  	require.NoError(t, err)
   326  	require.NotNil(t, res)
   327  }