diff --git a/clients/http/interval.go b/clients/http/interval.go deleted file mode 100644 index 2fea8b75..00000000 --- a/clients/http/interval.go +++ /dev/null @@ -1,93 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// Copyright (C) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 - -package http - -import ( - "context" - "net/url" - "strconv" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" - "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" -) - -type IntervalClient struct { - baseUrl string - authInjector interfaces.AuthenticationInjector - enableNameFieldEscape bool -} - -// NewIntervalClient creates an instance of IntervalClient -func NewIntervalClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.IntervalClient { - return &IntervalClient{ - baseUrl: baseUrl, - authInjector: authInjector, - enableNameFieldEscape: enableNameFieldEscape, - } -} - -// Add adds new intervals -func (client IntervalClient) Add(ctx context.Context, reqs []requests.AddIntervalRequest) ( - res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiIntervalRoute, nil, reqs, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// Update updates intervals -func (client IntervalClient) Update(ctx context.Context, reqs []requests.UpdateIntervalRequest) ( - res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, client.baseUrl, common.ApiIntervalRoute, nil, reqs, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// AllIntervals query the intervals with offset, limit -func (client IntervalClient) AllIntervals(ctx context.Context, offset int, limit int) ( - res responses.MultiIntervalsResponse, err errors.EdgeX) { - requestParams := url.Values{} - requestParams.Set(common.Offset, strconv.Itoa(offset)) - requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllIntervalRoute, requestParams, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// IntervalByName query the interval by name -func (client IntervalClient) IntervalByName(ctx context.Context, name string) ( - res responses.IntervalResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). - SetPath(common.ApiIntervalRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// DeleteIntervalByName delete the interval by name -func (client IntervalClient) DeleteIntervalByName(ctx context.Context, name string) ( - res dtoCommon.BaseResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). - SetPath(common.ApiIntervalRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} diff --git a/clients/http/interval_test.go b/clients/http/interval_test.go deleted file mode 100644 index bc39021d..00000000 --- a/clients/http/interval_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// Copyright (C) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 - -package http - -import ( - "context" - "net/http" - "path" - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestAddIntervals(t *testing.T) { - ts := newTestServer(http.MethodPost, common.ApiIntervalRoute, []dtoCommon.BaseWithIdResponse{}) - defer ts.Close() - dto := dtos.NewInterval(TestIntervalName, TestFrequency) - request := []requests.AddIntervalRequest{requests.NewAddIntervalRequest(dto)} - client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.Add(context.Background(), request) - - require.NoError(t, err) - assert.IsType(t, []dtoCommon.BaseWithIdResponse{}, res) -} - -func TestPatchIntervals(t *testing.T) { - ts := newTestServer(http.MethodPatch, common.ApiIntervalRoute, []dtoCommon.BaseResponse{}) - defer ts.Close() - dto := dtos.NewUpdateInterval(TestIntervalName) - request := []requests.UpdateIntervalRequest{requests.NewUpdateIntervalRequest(dto)} - client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.Update(context.Background(), request) - - require.NoError(t, err) - assert.IsType(t, []dtoCommon.BaseResponse{}, res) -} - -func TestQueryAllIntervals(t *testing.T) { - ts := newTestServer(http.MethodGet, common.ApiAllIntervalRoute, responses.MultiIntervalsResponse{}) - defer ts.Close() - client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.AllIntervals(context.Background(), 0, 10) - - require.NoError(t, err) - assert.IsType(t, responses.MultiIntervalsResponse{}, res) -} - -func TestQueryIntervalByName(t *testing.T) { - path := path.Join(common.ApiIntervalRoute, common.Name, TestIntervalName) - ts := newTestServer(http.MethodGet, path, responses.DeviceResponse{}) - defer ts.Close() - - client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.IntervalByName(context.Background(), TestIntervalName) - require.NoError(t, err) - assert.IsType(t, responses.IntervalResponse{}, res) -} - -func TestDeleteIntervalByName(t *testing.T) { - path := path.Join(common.ApiIntervalRoute, common.Name, TestIntervalName) - ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{}) - defer ts.Close() - client := NewIntervalClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.DeleteIntervalByName(context.Background(), TestIntervalName) - - require.NoError(t, err) - assert.IsType(t, dtoCommon.BaseResponse{}, res) -} diff --git a/clients/http/intervalaction.go b/clients/http/intervalaction.go deleted file mode 100644 index dbcbfc18..00000000 --- a/clients/http/intervalaction.go +++ /dev/null @@ -1,93 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// Copyright (C) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 - -package http - -import ( - "context" - "net/url" - "strconv" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/http/utils" - "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" -) - -type IntervalActionClient struct { - baseUrl string - authInjector interfaces.AuthenticationInjector - enableNameFieldEscape bool -} - -// NewIntervalActionClient creates an instance of IntervalActionClient -func NewIntervalActionClient(baseUrl string, authInjector interfaces.AuthenticationInjector, enableNameFieldEscape bool) interfaces.IntervalActionClient { - return &IntervalActionClient{ - baseUrl: baseUrl, - authInjector: authInjector, - enableNameFieldEscape: enableNameFieldEscape, - } -} - -// Add adds new intervalActions -func (client IntervalActionClient) Add(ctx context.Context, reqs []requests.AddIntervalActionRequest) ( - res []dtoCommon.BaseWithIdResponse, err errors.EdgeX) { - err = utils.PostRequestWithRawData(ctx, &res, client.baseUrl, common.ApiIntervalActionRoute, nil, reqs, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// Update updates intervalActions -func (client IntervalActionClient) Update(ctx context.Context, reqs []requests.UpdateIntervalActionRequest) ( - res []dtoCommon.BaseResponse, err errors.EdgeX) { - err = utils.PatchRequest(ctx, &res, client.baseUrl, common.ApiIntervalActionRoute, nil, reqs, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// AllIntervalActions query the intervalActions with offset, limit -func (client IntervalActionClient) AllIntervalActions(ctx context.Context, offset int, limit int) ( - res responses.MultiIntervalActionsResponse, err errors.EdgeX) { - requestParams := url.Values{} - requestParams.Set(common.Offset, strconv.Itoa(offset)) - requestParams.Set(common.Limit, strconv.Itoa(limit)) - err = utils.GetRequest(ctx, &res, client.baseUrl, common.ApiAllIntervalActionRoute, requestParams, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// IntervalActionByName query the intervalAction by name -func (client IntervalActionClient) IntervalActionByName(ctx context.Context, name string) ( - res responses.IntervalActionResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). - SetPath(common.ApiIntervalActionRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.GetRequest(ctx, &res, client.baseUrl, path, nil, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} - -// DeleteIntervalActionByName delete the intervalAction by name -func (client IntervalActionClient) DeleteIntervalActionByName(ctx context.Context, name string) ( - res dtoCommon.BaseResponse, err errors.EdgeX) { - path := common.NewPathBuilder().EnableNameFieldEscape(client.enableNameFieldEscape). - SetPath(common.ApiIntervalActionRoute).SetPath(common.Name).SetNameFieldPath(name).BuildPath() - err = utils.DeleteRequest(ctx, &res, client.baseUrl, path, client.authInjector) - if err != nil { - return res, errors.NewCommonEdgeXWrapper(err) - } - return res, nil -} diff --git a/clients/http/intervalaction_test.go b/clients/http/intervalaction_test.go deleted file mode 100644 index 1a55feff..00000000 --- a/clients/http/intervalaction_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// Copyright (C) 2023 Intel Corporation -// -// SPDX-License-Identifier: Apache-2.0 - -package http - -import ( - "context" - "net/http" - "path" - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestAddIntervalActions(t *testing.T) { - ts := newTestServer(http.MethodPost, common.ApiIntervalActionRoute, []dtoCommon.BaseWithIdResponse{}) - defer ts.Close() - dto := dtos.NewIntervalAction(TestIntervalActionName, TestIntervalName, dtos.NewRESTAddress(TestHost, TestPort, TestHTTPMethod)) - request := []requests.AddIntervalActionRequest{requests.NewAddIntervalActionRequest(dto)} - client := NewIntervalActionClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.Add(context.Background(), request) - - require.NoError(t, err) - assert.IsType(t, []dtoCommon.BaseWithIdResponse{}, res) -} - -func TestPatchIntervalActions(t *testing.T) { - ts := newTestServer(http.MethodPatch, common.ApiIntervalActionRoute, []dtoCommon.BaseResponse{}) - defer ts.Close() - dto := dtos.NewUpdateIntervalAction(TestIntervalActionName) - request := []requests.UpdateIntervalActionRequest{requests.NewUpdateIntervalActionRequest(dto)} - client := NewIntervalActionClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.Update(context.Background(), request) - - require.NoError(t, err) - assert.IsType(t, []dtoCommon.BaseResponse{}, res) -} - -func TestQueryAllIntervalActions(t *testing.T) { - ts := newTestServer(http.MethodGet, common.ApiAllIntervalActionRoute, responses.MultiIntervalActionsResponse{}) - defer ts.Close() - client := NewIntervalActionClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.AllIntervalActions(context.Background(), 0, 10) - - require.NoError(t, err) - assert.IsType(t, responses.MultiIntervalActionsResponse{}, res) -} - -func TestQueryIntervalActionByName(t *testing.T) { - path := path.Join(common.ApiIntervalActionRoute, common.Name, TestIntervalActionName) - ts := newTestServer(http.MethodGet, path, responses.DeviceResponse{}) - defer ts.Close() - client := NewIntervalActionClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.IntervalActionByName(context.Background(), TestIntervalActionName) - - require.NoError(t, err) - assert.IsType(t, responses.IntervalActionResponse{}, res) -} - -func TestDeleteIntervalActionByName(t *testing.T) { - path := path.Join(common.ApiIntervalActionRoute, common.Name, TestIntervalActionName) - ts := newTestServer(http.MethodDelete, path, dtoCommon.BaseResponse{}) - defer ts.Close() - client := NewIntervalActionClient(ts.URL, NewNullAuthenticationInjector(), false) - - res, err := client.DeleteIntervalActionByName(context.Background(), TestIntervalActionName) - - require.NoError(t, err) - assert.IsType(t, dtoCommon.BaseResponse{}, res) -} diff --git a/clients/interfaces/interval.go b/clients/interfaces/interval.go deleted file mode 100644 index 9a2ffb1e..00000000 --- a/clients/interfaces/interval.go +++ /dev/null @@ -1,32 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package interfaces - -import ( - "context" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" -) - -// IntervalClient defines the interface for interactions with the Interval endpoint on the EdgeX Foundry support-scheduler service. -type IntervalClient interface { - // Add adds new intervals. - Add(ctx context.Context, reqs []requests.AddIntervalRequest) ([]common.BaseWithIdResponse, errors.EdgeX) - // Update updates intervals. - Update(ctx context.Context, reqs []requests.UpdateIntervalRequest) ([]common.BaseResponse, errors.EdgeX) - // AllIntervals returns all intervals. - // The result can be limited in a certain range by specifying the offset and limit parameters. - // offset: The number of items to skip before starting to collect the result set. Default is 0. - // limit: The number of items to return. Specify -1 will return all remaining items after offset. The maximum will be the MaxResultCount as defined in the configuration of service. Default is 20. - AllIntervals(ctx context.Context, offset int, limit int) (responses.MultiIntervalsResponse, errors.EdgeX) - // IntervalByName returns a interval by name. - IntervalByName(ctx context.Context, name string) (responses.IntervalResponse, errors.EdgeX) - // DeleteIntervalByName deletes a interval by name. - DeleteIntervalByName(ctx context.Context, name string) (common.BaseResponse, errors.EdgeX) -} diff --git a/clients/interfaces/intervalaction.go b/clients/interfaces/intervalaction.go deleted file mode 100644 index d934f4bc..00000000 --- a/clients/interfaces/intervalaction.go +++ /dev/null @@ -1,32 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package interfaces - -import ( - "context" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" -) - -// IntervalActionClient defines the interface for interactions with the IntervalAction endpoint on the EdgeX Foundry support-scheduler service. -type IntervalActionClient interface { - // Add adds new intervalActions. - Add(ctx context.Context, reqs []requests.AddIntervalActionRequest) ([]common.BaseWithIdResponse, errors.EdgeX) - // Update updates intervalActions. - Update(ctx context.Context, reqs []requests.UpdateIntervalActionRequest) ([]common.BaseResponse, errors.EdgeX) - // AllIntervalActions returns all intervalActions. - // The result can be limited in a certain range by specifying the offset and limit parameters. - // offset: The number of items to skip before starting to collect the result set. Default is 0. - // limit: The number of items to return. Specify -1 will return all remaining items after offset. The maximum will be the MaxResultCount as defined in the configuration of service. Default is 20. - AllIntervalActions(ctx context.Context, offset int, limit int) (responses.MultiIntervalActionsResponse, errors.EdgeX) - // IntervalActionByName returns a intervalAction by name. - IntervalActionByName(ctx context.Context, name string) (responses.IntervalActionResponse, errors.EdgeX) - // DeleteIntervalActionByName deletes a intervalAction by name. - DeleteIntervalActionByName(ctx context.Context, name string) (common.BaseResponse, errors.EdgeX) -} diff --git a/clients/interfaces/mocks/IntervalActionClient.go b/clients/interfaces/mocks/IntervalActionClient.go deleted file mode 100644 index bc45b897..00000000 --- a/clients/interfaces/mocks/IntervalActionClient.go +++ /dev/null @@ -1,156 +0,0 @@ -// Code generated by mockery v2.15.0. DO NOT EDIT. - -package mocks - -import ( - context "context" - - common "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - - errors "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" - - mock "github.com/stretchr/testify/mock" - - requests "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - - responses "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" -) - -// IntervalActionClient is an autogenerated mock type for the IntervalActionClient type -type IntervalActionClient struct { - mock.Mock -} - -// Add provides a mock function with given fields: ctx, reqs -func (_m *IntervalActionClient) Add(ctx context.Context, reqs []requests.AddIntervalActionRequest) ([]common.BaseWithIdResponse, errors.EdgeX) { - ret := _m.Called(ctx, reqs) - - var r0 []common.BaseWithIdResponse - if rf, ok := ret.Get(0).(func(context.Context, []requests.AddIntervalActionRequest) []common.BaseWithIdResponse); ok { - r0 = rf(ctx, reqs) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]common.BaseWithIdResponse) - } - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, []requests.AddIntervalActionRequest) errors.EdgeX); ok { - r1 = rf(ctx, reqs) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// AllIntervalActions provides a mock function with given fields: ctx, offset, limit -func (_m *IntervalActionClient) AllIntervalActions(ctx context.Context, offset int, limit int) (responses.MultiIntervalActionsResponse, errors.EdgeX) { - ret := _m.Called(ctx, offset, limit) - - var r0 responses.MultiIntervalActionsResponse - if rf, ok := ret.Get(0).(func(context.Context, int, int) responses.MultiIntervalActionsResponse); ok { - r0 = rf(ctx, offset, limit) - } else { - r0 = ret.Get(0).(responses.MultiIntervalActionsResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, int, int) errors.EdgeX); ok { - r1 = rf(ctx, offset, limit) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// DeleteIntervalActionByName provides a mock function with given fields: ctx, name -func (_m *IntervalActionClient) DeleteIntervalActionByName(ctx context.Context, name string) (common.BaseResponse, errors.EdgeX) { - ret := _m.Called(ctx, name) - - var r0 common.BaseResponse - if rf, ok := ret.Get(0).(func(context.Context, string) common.BaseResponse); ok { - r0 = rf(ctx, name) - } else { - r0 = ret.Get(0).(common.BaseResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, string) errors.EdgeX); ok { - r1 = rf(ctx, name) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// IntervalActionByName provides a mock function with given fields: ctx, name -func (_m *IntervalActionClient) IntervalActionByName(ctx context.Context, name string) (responses.IntervalActionResponse, errors.EdgeX) { - ret := _m.Called(ctx, name) - - var r0 responses.IntervalActionResponse - if rf, ok := ret.Get(0).(func(context.Context, string) responses.IntervalActionResponse); ok { - r0 = rf(ctx, name) - } else { - r0 = ret.Get(0).(responses.IntervalActionResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, string) errors.EdgeX); ok { - r1 = rf(ctx, name) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// Update provides a mock function with given fields: ctx, reqs -func (_m *IntervalActionClient) Update(ctx context.Context, reqs []requests.UpdateIntervalActionRequest) ([]common.BaseResponse, errors.EdgeX) { - ret := _m.Called(ctx, reqs) - - var r0 []common.BaseResponse - if rf, ok := ret.Get(0).(func(context.Context, []requests.UpdateIntervalActionRequest) []common.BaseResponse); ok { - r0 = rf(ctx, reqs) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]common.BaseResponse) - } - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, []requests.UpdateIntervalActionRequest) errors.EdgeX); ok { - r1 = rf(ctx, reqs) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -type mockConstructorTestingTNewIntervalActionClient interface { - mock.TestingT - Cleanup(func()) -} - -// NewIntervalActionClient creates a new instance of IntervalActionClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewIntervalActionClient(t mockConstructorTestingTNewIntervalActionClient) *IntervalActionClient { - mock := &IntervalActionClient{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/clients/interfaces/mocks/IntervalClient.go b/clients/interfaces/mocks/IntervalClient.go deleted file mode 100644 index b53ffc4e..00000000 --- a/clients/interfaces/mocks/IntervalClient.go +++ /dev/null @@ -1,156 +0,0 @@ -// Code generated by mockery v2.15.0. DO NOT EDIT. - -package mocks - -import ( - context "context" - - common "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - - errors "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" - - mock "github.com/stretchr/testify/mock" - - requests "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/requests" - - responses "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/responses" -) - -// IntervalClient is an autogenerated mock type for the IntervalClient type -type IntervalClient struct { - mock.Mock -} - -// Add provides a mock function with given fields: ctx, reqs -func (_m *IntervalClient) Add(ctx context.Context, reqs []requests.AddIntervalRequest) ([]common.BaseWithIdResponse, errors.EdgeX) { - ret := _m.Called(ctx, reqs) - - var r0 []common.BaseWithIdResponse - if rf, ok := ret.Get(0).(func(context.Context, []requests.AddIntervalRequest) []common.BaseWithIdResponse); ok { - r0 = rf(ctx, reqs) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]common.BaseWithIdResponse) - } - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, []requests.AddIntervalRequest) errors.EdgeX); ok { - r1 = rf(ctx, reqs) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// AllIntervals provides a mock function with given fields: ctx, offset, limit -func (_m *IntervalClient) AllIntervals(ctx context.Context, offset int, limit int) (responses.MultiIntervalsResponse, errors.EdgeX) { - ret := _m.Called(ctx, offset, limit) - - var r0 responses.MultiIntervalsResponse - if rf, ok := ret.Get(0).(func(context.Context, int, int) responses.MultiIntervalsResponse); ok { - r0 = rf(ctx, offset, limit) - } else { - r0 = ret.Get(0).(responses.MultiIntervalsResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, int, int) errors.EdgeX); ok { - r1 = rf(ctx, offset, limit) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// DeleteIntervalByName provides a mock function with given fields: ctx, name -func (_m *IntervalClient) DeleteIntervalByName(ctx context.Context, name string) (common.BaseResponse, errors.EdgeX) { - ret := _m.Called(ctx, name) - - var r0 common.BaseResponse - if rf, ok := ret.Get(0).(func(context.Context, string) common.BaseResponse); ok { - r0 = rf(ctx, name) - } else { - r0 = ret.Get(0).(common.BaseResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, string) errors.EdgeX); ok { - r1 = rf(ctx, name) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// IntervalByName provides a mock function with given fields: ctx, name -func (_m *IntervalClient) IntervalByName(ctx context.Context, name string) (responses.IntervalResponse, errors.EdgeX) { - ret := _m.Called(ctx, name) - - var r0 responses.IntervalResponse - if rf, ok := ret.Get(0).(func(context.Context, string) responses.IntervalResponse); ok { - r0 = rf(ctx, name) - } else { - r0 = ret.Get(0).(responses.IntervalResponse) - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, string) errors.EdgeX); ok { - r1 = rf(ctx, name) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -// Update provides a mock function with given fields: ctx, reqs -func (_m *IntervalClient) Update(ctx context.Context, reqs []requests.UpdateIntervalRequest) ([]common.BaseResponse, errors.EdgeX) { - ret := _m.Called(ctx, reqs) - - var r0 []common.BaseResponse - if rf, ok := ret.Get(0).(func(context.Context, []requests.UpdateIntervalRequest) []common.BaseResponse); ok { - r0 = rf(ctx, reqs) - } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).([]common.BaseResponse) - } - } - - var r1 errors.EdgeX - if rf, ok := ret.Get(1).(func(context.Context, []requests.UpdateIntervalRequest) errors.EdgeX); ok { - r1 = rf(ctx, reqs) - } else { - if ret.Get(1) != nil { - r1 = ret.Get(1).(errors.EdgeX) - } - } - - return r0, r1 -} - -type mockConstructorTestingTNewIntervalClient interface { - mock.TestingT - Cleanup(func()) -} - -// NewIntervalClient creates a new instance of IntervalClient. It also registers a testing interface on the mock and a cleanup function to assert the mocks expectations. -func NewIntervalClient(t mockConstructorTestingTNewIntervalClient) *IntervalClient { - mock := &IntervalClient{} - mock.Mock.Test(t) - - t.Cleanup(func() { mock.AssertExpectations(t) }) - - return mock -} diff --git a/clients/interfaces/scheduleactionrecord.go b/clients/interfaces/scheduleactionrecord.go index 24fc076b..52f2dc00 100644 --- a/clients/interfaces/scheduleactionrecord.go +++ b/clients/interfaces/scheduleactionrecord.go @@ -12,7 +12,7 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" ) -// ScheduleActionRecordClient defines the interface for interactions with the ScheduleActionRecord endpoint on the EdgeX Foundry support-cron-scheduler service. +// ScheduleActionRecordClient defines the interface for interactions with the ScheduleActionRecord endpoint on the EdgeX Foundry support-scheduler service. type ScheduleActionRecordClient interface { // AllScheduleActionRecords query schedule action records with start, end, offset, and limit AllScheduleActionRecords(ctx context.Context, start, end int64, offset, limit int) (responses.MultiScheduleActionRecordsResponse, errors.EdgeX) diff --git a/clients/interfaces/schedulejob.go b/clients/interfaces/schedulejob.go index 948eb5fb..c871eb2e 100644 --- a/clients/interfaces/schedulejob.go +++ b/clients/interfaces/schedulejob.go @@ -14,7 +14,7 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" ) -// ScheduleJobClient defines the interface for interactions with the ScheduleJob endpoint on the EdgeX Foundry support-cron-scheduler service. +// ScheduleJobClient defines the interface for interactions with the ScheduleJob endpoint on the EdgeX Foundry support-scheduler service. type ScheduleJobClient interface { // Add adds new schedule jobs. Add(ctx context.Context, reqs []requests.AddScheduleJobRequest) ([]common.BaseWithIdResponse, errors.EdgeX) diff --git a/common/constants.go b/common/constants.go index f57ed42b..b88307d4 100644 --- a/common/constants.go +++ b/common/constants.go @@ -134,14 +134,6 @@ const ( ApiDiscoveryByIdRoute = ApiDiscoveryRoute + "/" + RequestId + "/{" + RequestId + "}" ApiProfileScanByDeviceNameRoute = ApiProfileScanRoute + "/" + Device + "/" + Name + "/{" + Name + "}" - ApiIntervalRoute = ApiBase + "/interval" - ApiAllIntervalRoute = ApiIntervalRoute + "/" + All - ApiIntervalByNameRoute = ApiIntervalRoute + "/" + Name + "/{" + Name + "}" - ApiIntervalActionRoute = ApiBase + "/intervalaction" - ApiAllIntervalActionRoute = ApiIntervalActionRoute + "/" + All - ApiIntervalActionByNameRoute = ApiIntervalActionRoute + "/" + Name + "/{" + Name + "}" - ApiIntervalActionByTargetRoute = ApiIntervalActionRoute + "/" + Target + "/{" + Target + "}" - ApiSystemRoute = ApiBase + "/system" ApiOperationRoute = ApiSystemRoute + "/operation" ApiHealthRoute = ApiSystemRoute + "/health" @@ -320,7 +312,6 @@ const ( SupportNotificationsServiceKey = "support-notifications" SystemManagementAgentServiceKey = "sys-mgmt-agent" SupportSchedulerServiceKey = "support-scheduler" - SupportCronSchedulerServiceKey = "support-cron-scheduler" SecuritySecretStoreSetupServiceKey = "security-secretstore-setup" SecurityProxyAuthServiceKey = "security-proxy-auth" SecurityProxySetupServiceKey = "security-proxy-setup" diff --git a/common/echo_api_constants.go b/common/echo_api_constants.go index e7e66c68..4a37cd69 100644 --- a/common/echo_api_constants.go +++ b/common/echo_api_constants.go @@ -79,10 +79,6 @@ const ( ApiProfileCallbackNameEchoRoute = ApiBase + "/callback/profile/name/:name" ApiWatcherCallbackNameEchoRoute = ApiBase + "/callback/watcher/name/:name" - ApiIntervalByNameEchoRoute = ApiIntervalRoute + "/" + Name + "/:" + Name - ApiIntervalActionByNameEchoRoute = ApiIntervalActionRoute + "/" + Name + "/:" + Name - ApiIntervalActionByTargetEchoRoute = ApiIntervalActionRoute + "/" + Target + "/:" + Target - ApiKVSByKeyEchoRoute = ApiKVSRoute + "/" + Key + "/:" + Key ApiRegistrationByServiceIdEchoRoute = ApiRegisterRoute + "/" + ServiceId + "/:" + ServiceId diff --git a/dtos/interval.go b/dtos/interval.go deleted file mode 100644 index 96c0c10e..00000000 --- a/dtos/interval.go +++ /dev/null @@ -1,60 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package dtos - -import ( - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" -) - -type Interval struct { - DBTimestamp `json:",inline"` - Id string `json:"id,omitempty" validate:"omitempty,uuid"` - Name string `json:"name" validate:"edgex-dto-none-empty-string"` - Start string `json:"start,omitempty" validate:"omitempty,edgex-dto-interval-datetime"` - End string `json:"end,omitempty" validate:"omitempty,edgex-dto-interval-datetime"` - Interval string `json:"interval" validate:"required,edgex-dto-duration"` -} - -// NewInterval creates interval DTO with required fields -func NewInterval(name, interval string) Interval { - return Interval{Name: name, Interval: interval} -} - -type UpdateInterval struct { - Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"` - Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string"` - Start *string `json:"start" validate:"omitempty,edgex-dto-interval-datetime"` - End *string `json:"end" validate:"omitempty,edgex-dto-interval-datetime"` - Interval *string `json:"interval" validate:"omitempty,edgex-dto-duration"` -} - -// NewUpdateInterval creates updateInterval DTO with required field -func NewUpdateInterval(name string) UpdateInterval { - return UpdateInterval{Name: &name} -} - -// ToIntervalModel transforms the Interval DTO to the Interval Model -func ToIntervalModel(dto Interval) models.Interval { - var model models.Interval - model.Id = dto.Id - model.Name = dto.Name - model.Start = dto.Start - model.End = dto.End - model.Interval = dto.Interval - return model -} - -// FromIntervalModelToDTO transforms the Interval Model to the Interval DTO -func FromIntervalModelToDTO(model models.Interval) Interval { - var dto Interval - dto.DBTimestamp = DBTimestamp(model.DBTimestamp) - dto.Id = model.Id - dto.Name = model.Name - dto.Start = model.Start - dto.End = model.End - dto.Interval = model.Interval - return dto -} diff --git a/dtos/intervalaction.go b/dtos/intervalaction.go deleted file mode 100644 index fa9da13d..00000000 --- a/dtos/intervalaction.go +++ /dev/null @@ -1,77 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package dtos - -import ( - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" -) - -type IntervalAction struct { - DBTimestamp `json:",inline"` - Id string `json:"id,omitempty" validate:"omitempty,uuid"` - Name string `json:"name" validate:"edgex-dto-none-empty-string"` - IntervalName string `json:"intervalName" validate:"edgex-dto-none-empty-string"` - Address Address `json:"address" validate:"required"` - Content string `json:"content,omitempty"` - ContentType string `json:"contentType,omitempty"` - AdminState string `json:"adminState" validate:"oneof='LOCKED' 'UNLOCKED'"` - AuthMethod string `json:"authMethod" validate:"oneof='' 'NONE' 'JWT'"` -} - -// NewIntervalAction creates intervalAction DTO with required fields -func NewIntervalAction(name string, intervalName string, address Address) IntervalAction { - return IntervalAction{ - Name: name, - IntervalName: intervalName, - Address: address, - AdminState: models.Unlocked, - } -} - -type UpdateIntervalAction struct { - Id *string `json:"id" validate:"required_without=Name,edgex-dto-uuid"` - Name *string `json:"name" validate:"required_without=Id,edgex-dto-none-empty-string"` - IntervalName *string `json:"intervalName" validate:"omitempty,edgex-dto-none-empty-string"` - Content *string `json:"content"` - ContentType *string `json:"contentType"` - Address *Address `json:"address"` - AdminState *string `json:"adminState" validate:"omitempty,oneof='LOCKED' 'UNLOCKED'"` - AuthMethod *string `json:"authMethod" validate:"omitempty,oneof='' 'NONE' 'JWT'"` -} - -// NewUpdateIntervalAction creates updateIntervalAction DTO with required field -func NewUpdateIntervalAction(name string) UpdateIntervalAction { - return UpdateIntervalAction{Name: &name} -} - -// ToIntervalActionModel transforms the IntervalAction DTO to the IntervalAction Model -func ToIntervalActionModel(dto IntervalAction) models.IntervalAction { - var model models.IntervalAction - model.Id = dto.Id - model.Name = dto.Name - model.IntervalName = dto.IntervalName - model.Content = dto.Content - model.ContentType = dto.ContentType - model.Address = ToAddressModel(dto.Address) - model.AdminState = models.AdminState(dto.AdminState) - model.AuthMethod = models.AuthMethod(dto.AuthMethod) - return model -} - -// FromIntervalActionModelToDTO transforms the IntervalAction Model to the IntervalAction DTO -func FromIntervalActionModelToDTO(model models.IntervalAction) IntervalAction { - var dto IntervalAction - dto.DBTimestamp = DBTimestamp(model.DBTimestamp) - dto.Id = model.Id - dto.Name = model.Name - dto.IntervalName = model.IntervalName - dto.Content = model.Content - dto.ContentType = model.ContentType - dto.Address = FromAddressModelToDTO(model.Address) - dto.AdminState = string(model.AdminState) - dto.AuthMethod = string(model.AuthMethod) - return dto -} diff --git a/dtos/requests/interval.go b/dtos/requests/interval.go deleted file mode 100644 index ef9070ee..00000000 --- a/dtos/requests/interval.go +++ /dev/null @@ -1,114 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package requests - -import ( - "encoding/json" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" -) - -// AddIntervalRequest defines the Request Content for POST Interval DTO. -type AddIntervalRequest struct { - dtoCommon.BaseRequest `json:",inline"` - Interval dtos.Interval `json:"interval"` -} - -// Validate satisfies the Validator interface -func (request AddIntervalRequest) Validate() error { - err := common.Validate(request) - return err -} - -// UnmarshalJSON implements the Unmarshaler interface for the AddIntervalRequest type -func (request *AddIntervalRequest) UnmarshalJSON(b []byte) error { - var alias struct { - dtoCommon.BaseRequest - Interval dtos.Interval - } - if err := json.Unmarshal(b, &alias); err != nil { - return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) - } - - *request = AddIntervalRequest(alias) - - // validate AddIntervalRequest DTO - if err := request.Validate(); err != nil { - return err - } - return nil -} - -// AddIntervalReqToIntervalModels transforms the AddIntervalRequest DTO array to the Interval model array -func AddIntervalReqToIntervalModels(addRequests []AddIntervalRequest) (intervals []models.Interval) { - for _, req := range addRequests { - d := dtos.ToIntervalModel(req.Interval) - intervals = append(intervals, d) - } - return intervals -} - -// UpdateIntervalRequest defines the Request Content for PUT event as pushed DTO. -type UpdateIntervalRequest struct { - dtoCommon.BaseRequest `json:",inline"` - Interval dtos.UpdateInterval `json:"interval"` -} - -// Validate satisfies the Validator interface -func (request UpdateIntervalRequest) Validate() error { - err := common.Validate(request) - return err -} - -// UnmarshalJSON implements the Unmarshaler interface for the UpdateIntervalRequest type -func (request *UpdateIntervalRequest) UnmarshalJSON(b []byte) error { - var alias struct { - dtoCommon.BaseRequest - Interval dtos.UpdateInterval - } - if err := json.Unmarshal(b, &alias); err != nil { - return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) - } - - *request = UpdateIntervalRequest(alias) - - // validate UpdateIntervalRequest DTO - if err := request.Validate(); err != nil { - return err - } - return nil -} - -// ReplaceIntervalModelFieldsWithDTO replace existing Interval's fields with DTO patch -func ReplaceIntervalModelFieldsWithDTO(interval *models.Interval, patch dtos.UpdateInterval) { - if patch.Start != nil { - interval.Start = *patch.Start - } - if patch.End != nil { - interval.End = *patch.End - } - if patch.Interval != nil { - interval.Interval = *patch.Interval - } -} - -func NewAddIntervalRequest(dto dtos.Interval) AddIntervalRequest { - return AddIntervalRequest{ - BaseRequest: dtoCommon.NewBaseRequest(), - Interval: dto, - } -} - -func NewUpdateIntervalRequest(dto dtos.UpdateInterval) UpdateIntervalRequest { - return UpdateIntervalRequest{ - BaseRequest: dtoCommon.NewBaseRequest(), - Interval: dto, - } -} diff --git a/dtos/requests/interval_test.go b/dtos/requests/interval_test.go deleted file mode 100644 index 107ac96b..00000000 --- a/dtos/requests/interval_test.go +++ /dev/null @@ -1,261 +0,0 @@ -// -// Copyright (C) 2021-2023 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package requests - -import ( - "encoding/json" - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func addIntervalRequestData() AddIntervalRequest { - return AddIntervalRequest{ - BaseRequest: common.BaseRequest{ - RequestId: ExampleUUID, - Versionable: common.NewVersionable(), - }, - Interval: dtos.Interval{ - Name: TestIntervalName, - Start: TestIntervalStart, - End: TestIntervalEnd, - Interval: TestIntervalInterval, - }, - } -} - -func updateIntervalRequestData() UpdateIntervalRequest { - return UpdateIntervalRequest{ - BaseRequest: common.BaseRequest{ - RequestId: ExampleUUID, - Versionable: common.NewVersionable(), - }, - Interval: updateIntervalData(), - } -} - -func updateIntervalData() dtos.UpdateInterval { - testId := ExampleUUID - testName := TestIntervalName - testStart := TestIntervalStart - testEnd := TestIntervalEnd - testFrequency := TestIntervalInterval - dto := dtos.UpdateInterval{} - dto.Id = &testId - dto.Name = &testName - dto.Start = &testStart - dto.End = &testEnd - dto.Interval = &testFrequency - return dto -} - -func TestAddIntervalRequest_Validate(t *testing.T) { - emptyString := " " - valid := addIntervalRequestData() - noReqId := addIntervalRequestData() - noReqId.RequestId = "" - invalidReqId := addIntervalRequestData() - invalidReqId.RequestId = "abc" - - noIntervalName := addIntervalRequestData() - noIntervalName.Interval.Name = emptyString - intervalNameWithUnreservedChars := addIntervalRequestData() - intervalNameWithUnreservedChars.Interval.Name = nameWithUnreservedChars - intervalNameWithReservedChars := addIntervalRequestData() - intervalNameWithReservedChars.Interval.Name = "name!.~_001" - - invalidFrequency := addIntervalRequestData() - invalidFrequency.Interval.Interval = "300" - invalidStartDatetime := addIntervalRequestData() - invalidStartDatetime.Interval.Start = "20190802150405" - invalidEndDatetime := addIntervalRequestData() - invalidEndDatetime.Interval.End = "20190802150405" - - tests := []struct { - name string - Interval AddIntervalRequest - expectError bool - }{ - {"valid AddIntervalRequest", valid, false}, - {"valid AddIntervalRequest, no Request Id", noReqId, false}, - {"valid AddIntervalRequest, interval name containing unreserved chars", intervalNameWithUnreservedChars, false}, - {"valid AddIntervalRequest, interval name containing reserved chars", intervalNameWithReservedChars, false}, - {"invalid AddIntervalRequest, Request Id is not an uuid", invalidReqId, true}, - {"invalid AddIntervalRequest, no IntervalName", noIntervalName, true}, - {"invalid AddIntervalRequest, invalid frequency", invalidFrequency, true}, - {"invalid AddIntervalRequest, invalid start datetime", invalidStartDatetime, true}, - {"invalid AddIntervalRequest, invalid end datetime", invalidEndDatetime, true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := tt.Interval.Validate() - assert.Equal(t, tt.expectError, err != nil, "Unexpected addIntervalRequest validation result.", err) - }) - } - -} - -func TestAddInterval_UnmarshalJSON(t *testing.T) { - valid := addIntervalRequestData() - jsonData, _ := json.Marshal(addIntervalRequestData()) - tests := []struct { - name string - expected AddIntervalRequest - data []byte - wantErr bool - }{ - {"unmarshal AddIntervalRequest with success", valid, jsonData, false}, - {"unmarshal invalid AddIntervalRequest, empty data", AddIntervalRequest{}, []byte{}, true}, - {"unmarshal invalid AddIntervalRequest, string data", AddIntervalRequest{}, []byte("Invalid AddIntervalRequest"), true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var result AddIntervalRequest - err := result.UnmarshalJSON(tt.data) - if tt.wantErr { - require.Error(t, err) - } else { - require.NoError(t, err) - assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected AddIntervalRequest.") - } - }) - } -} - -func TestAddIntervalReqToIntervalModels(t *testing.T) { - requests := []AddIntervalRequest{addIntervalRequestData()} - expectedIntervalModel := []models.Interval{ - { - Name: TestIntervalName, - Start: TestIntervalStart, - End: TestIntervalEnd, - Interval: TestIntervalInterval, - }, - } - resultModels := AddIntervalReqToIntervalModels(requests) - assert.Equal(t, expectedIntervalModel, resultModels, "AddIntervalReqToIntervalModels did not result in expected Interval model.") -} - -func TestUpdateIntervalRequest_UnmarshalJSON(t *testing.T) { - valid := updateIntervalRequestData() - jsonData, _ := json.Marshal(updateIntervalRequestData()) - tests := []struct { - name string - expected UpdateIntervalRequest - data []byte - wantErr bool - }{ - {"unmarshal UpdateIntervalRequest with success", valid, jsonData, false}, - {"unmarshal invalid UpdateIntervalRequest, empty data", UpdateIntervalRequest{}, []byte{}, true}, - {"unmarshal invalid UpdateIntervalRequest, string data", UpdateIntervalRequest{}, []byte("Invalid UpdateIntervalRequest"), true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var result UpdateIntervalRequest - err := result.UnmarshalJSON(tt.data) - if tt.wantErr { - require.Error(t, err) - } else { - require.NoError(t, err) - assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected UpdateIntervalRequest.", err) - } - }) - } -} - -func TestUpdateIntervalRequest_Validate(t *testing.T) { - emptyString := " " - invalidUUID := "invalidUUID" - invalidDatetime := "20190802150405" - - valid := updateIntervalRequestData() - noReqId := valid - noReqId.RequestId = "" - invalidReqId := valid - invalidReqId.RequestId = invalidUUID - - validOnlyId := valid - validOnlyId.Interval.Name = nil - invalidId := valid - invalidId.Interval.Id = &invalidUUID - - validOnlyName := valid - validOnlyName.Interval.Id = nil - invalidEmptyName := valid - invalidEmptyName.Interval.Name = &emptyString - nameAndEmptyId := valid - nameAndEmptyId.Interval.Id = &emptyString - - invalidFrequency := valid - invalidFrequency.Interval.Interval = &emptyString - invalidStartDatetime := valid - invalidStartDatetime.Interval.Start = &invalidDatetime - invalidEndDatetime := valid - invalidEndDatetime.Interval.End = &invalidDatetime - - tests := []struct { - name string - req UpdateIntervalRequest - expectError bool - }{ - {"valid", valid, false}, - {"valid, no Request Id", noReqId, false}, - {"invalid, Request Id is not an uuid", invalidReqId, true}, - - {"valid, only id", validOnlyId, false}, - {"invalid, invalid Id", invalidId, true}, - {"valid, only name", validOnlyName, false}, - {"valid, name and empty Id", nameAndEmptyId, false}, - {"invalid, empty name", invalidEmptyName, true}, - - {"invalid AddIntervalRequest, invalid frequency", invalidFrequency, true}, - {"invalid AddIntervalRequest, invalid start datetime", invalidStartDatetime, true}, - {"invalid AddIntervalRequest, invalid end datetime", invalidEndDatetime, true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := tt.req.Validate() - assert.Equal(t, tt.expectError, err != nil, "Unexpected updateIntervalRequest validation result.", err) - }) - } -} - -func TestUpdateIntervalRequest_UnmarshalJSON_NilField(t *testing.T) { - reqJson := `{ - "apiVersion" : "v3", - "requestId":"7a1707f0-166f-4c4b-bc9d-1d54c74e0137", - "interval":{"apiVersion":"v3", "name":"TestInterval"} - }` - var req UpdateIntervalRequest - - err := req.UnmarshalJSON([]byte(reqJson)) - - require.NoError(t, err) - // Nil field checking is used to update with patch - assert.Nil(t, req.Interval.Start) - assert.Nil(t, req.Interval.End) - assert.Nil(t, req.Interval.Interval) -} - -func TestReplaceIntervalModelFieldsWithDTO(t *testing.T) { - interval := models.Interval{ - Id: "7a1707f0-166f-4c4b-bc9d-1d54c74e0137", - Name: TestIntervalName, - } - patch := updateIntervalData() - - ReplaceIntervalModelFieldsWithDTO(&interval, patch) - - assert.Equal(t, TestIntervalName, interval.Name) - assert.Equal(t, TestIntervalStart, interval.Start) - assert.Equal(t, TestIntervalEnd, interval.End) - assert.Equal(t, TestIntervalInterval, interval.Interval) -} diff --git a/dtos/requests/intervalaction.go b/dtos/requests/intervalaction.go deleted file mode 100644 index 571b9bac..00000000 --- a/dtos/requests/intervalaction.go +++ /dev/null @@ -1,136 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package requests - -import ( - "encoding/json" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" -) - -// AddIntervalActionRequest defines the Request Content for POST Interval DTO. -type AddIntervalActionRequest struct { - dtoCommon.BaseRequest `json:",inline"` - Action dtos.IntervalAction `json:"action"` -} - -// Validate satisfies the Validator interface -func (request AddIntervalActionRequest) Validate() error { - err := common.Validate(request) - if err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - err = request.Action.Address.Validate() - if err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - return nil -} - -// UnmarshalJSON implements the Unmarshaler interface for the AddIntervalActionRequest type -func (request *AddIntervalActionRequest) UnmarshalJSON(b []byte) error { - var alias struct { - dtoCommon.BaseRequest - Action dtos.IntervalAction - } - if err := json.Unmarshal(b, &alias); err != nil { - return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) - } - - *request = AddIntervalActionRequest(alias) - - // validate AddIntervalActionRequest DTO - if err := request.Validate(); err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - return nil -} - -// AddIntervalActionReqToIntervalActionModels transforms the AddIntervalActionRequest DTO array to the IntervalAction model array -func AddIntervalActionReqToIntervalActionModels(addRequests []AddIntervalActionRequest) (actions []models.IntervalAction) { - for _, req := range addRequests { - d := dtos.ToIntervalActionModel(req.Action) - actions = append(actions, d) - } - return actions -} - -// UpdateIntervalActionRequest defines the Request Content for PUT event as pushed DTO. -type UpdateIntervalActionRequest struct { - dtoCommon.BaseRequest `json:",inline"` - Action dtos.UpdateIntervalAction `json:"action"` -} - -// Validate satisfies the Validator interface -func (request UpdateIntervalActionRequest) Validate() error { - err := common.Validate(request) - if err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - if request.Action.Address != nil { - err = request.Action.Address.Validate() - if err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - } - return nil -} - -// UnmarshalJSON implements the Unmarshaler interface for the UpdateIntervalActionRequest type -func (request *UpdateIntervalActionRequest) UnmarshalJSON(b []byte) error { - var alias struct { - dtoCommon.BaseRequest - Action dtos.UpdateIntervalAction - } - if err := json.Unmarshal(b, &alias); err != nil { - return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) - } - - *request = UpdateIntervalActionRequest(alias) - - // validate UpdateIntervalActionRequest DTO - if err := request.Validate(); err != nil { - return err - } - return nil -} - -// ReplaceIntervalActionModelFieldsWithDTO replace existing IntervalAction's fields with DTO patch -func ReplaceIntervalActionModelFieldsWithDTO(action *models.IntervalAction, patch dtos.UpdateIntervalAction) { - if patch.IntervalName != nil { - action.IntervalName = *patch.IntervalName - } - if patch.Address != nil { - action.Address = dtos.ToAddressModel(*patch.Address) - } - if patch.Content != nil { - action.Content = *patch.Content - } - if patch.ContentType != nil { - action.ContentType = *patch.ContentType - } - if patch.AdminState != nil { - action.AdminState = models.AdminState(*patch.AdminState) - } -} - -func NewAddIntervalActionRequest(dto dtos.IntervalAction) AddIntervalActionRequest { - return AddIntervalActionRequest{ - BaseRequest: dtoCommon.NewBaseRequest(), - Action: dto, - } -} - -func NewUpdateIntervalActionRequest(dto dtos.UpdateIntervalAction) UpdateIntervalActionRequest { - return UpdateIntervalActionRequest{ - BaseRequest: dtoCommon.NewBaseRequest(), - Action: dto, - } -} diff --git a/dtos/requests/intervalaction_test.go b/dtos/requests/intervalaction_test.go deleted file mode 100644 index 006ec9e4..00000000 --- a/dtos/requests/intervalaction_test.go +++ /dev/null @@ -1,276 +0,0 @@ -// -// Copyright (C) 2021-2023 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package requests - -import ( - "encoding/json" - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" - "github.com/edgexfoundry/go-mod-core-contracts/v4/models" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func addIntervalActionRequestData() AddIntervalActionRequest { - address := dtos.NewRESTAddress(TestHost, TestPort, TestHTTPMethod) - dto := dtos.NewIntervalAction(TestIntervalActionName, TestIntervalName, address) - return NewAddIntervalActionRequest(dto) -} - -func updateIntervalActionRequestData() UpdateIntervalActionRequest { - return UpdateIntervalActionRequest{ - BaseRequest: dtoCommon.BaseRequest{ - RequestId: ExampleUUID, - Versionable: dtoCommon.NewVersionable(), - }, - Action: updateIntervalActionData(), - } -} - -func updateIntervalActionData() dtos.UpdateIntervalAction { - testId := ExampleUUID - testName := TestIntervalActionName - testIntervalName := TestIntervalName - testContent := TestContent - testContentType := common.ContentTypeText - - dto := dtos.UpdateIntervalAction{} - dto.Id = &testId - dto.Name = &testName - dto.IntervalName = &testIntervalName - dto.Content = &testContent - dto.ContentType = &testContentType - address := dtos.NewRESTAddress(TestHost, TestPort, TestHTTPMethod) - dto.Address = &address - return dto -} - -func TestAddIntervalActionRequest_Validate(t *testing.T) { - emptyString := " " - valid := addIntervalActionRequestData() - noReqId := addIntervalActionRequestData() - noReqId.RequestId = "" - invalidReqId := addIntervalActionRequestData() - invalidReqId.RequestId = "abc" - - noIntervalActionName := addIntervalActionRequestData() - noIntervalActionName.Action.Name = emptyString - noIntervalName := addIntervalActionRequestData() - noIntervalName.Action.IntervalName = emptyString - intervalNameWithUnreservedChars := addIntervalActionRequestData() - intervalNameWithUnreservedChars.Action.Name = nameWithUnreservedChars - intervalNameWithReservedChars := addIntervalActionRequestData() - intervalNameWithReservedChars.Action.Name = "name!.~_001" - - invalidNoAddressType := addIntervalActionRequestData() - invalidNoAddressType.Action.Address.Type = "" - invalidNoAddressHTTPMethod := addIntervalActionRequestData() - invalidNoAddressHTTPMethod.Action.Address.HTTPMethod = "" - invalidNoAddressMQTTPublisher := addIntervalActionRequestData() - invalidNoAddressMQTTPublisher.Action.Address.Type = common.MQTT - invalidNoAddressMQTTPublisher.Action.Address.Topic = TestTopic - invalidNoAddressMQTTPublisher.Action.Address.Publisher = "" - invalidNoAddressMQTTTopic := addIntervalActionRequestData() - invalidNoAddressMQTTTopic.Action.Address.Type = common.MQTT - invalidNoAddressMQTTTopic.Action.Address.Publisher = TestPublisher - invalidNoAddressMQTTTopic.Action.Address.Topic = "" - - tests := []struct { - name string - IntervalAction AddIntervalActionRequest - expectError bool - }{ - {"valid AddIntervalActionRequest", valid, false}, - {"valid AddIntervalActionRequest, no Request Id", noReqId, false}, - {"valid AddIntervalActionRequest, interval name containing unreserved chars", intervalNameWithUnreservedChars, false}, - {"valid AddIntervalActionRequest, interval name containing reserved chars", intervalNameWithReservedChars, false}, - {"invalid AddIntervalActionRequest, Request Id is not an uuid", invalidReqId, true}, - {"invalid AddIntervalActionRequest, no IntervalActionName", noIntervalActionName, true}, - {"invalid AddIntervalActionRequest, no IntervalActionName", noIntervalName, true}, - {"invalid AddIntervalActionRequest, no address type", invalidNoAddressType, true}, - {"invalid AddIntervalActionRequest, no address http method", invalidNoAddressHTTPMethod, true}, - {"invalid AddIntervalActionRequest, no address MQTT publisher", invalidNoAddressMQTTPublisher, true}, - {"invalid AddIntervalActionRequest, no address MQTT topic", invalidNoAddressMQTTTopic, true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := tt.IntervalAction.Validate() - assert.Equal(t, tt.expectError, err != nil, "Unexpected addIntervalActionRequest validation result.", err) - }) - } - -} - -func TestAddIntervalAction_UnmarshalJSON(t *testing.T) { - valid := addIntervalActionRequestData() - jsonData, _ := json.Marshal(valid) - tests := []struct { - name string - expected AddIntervalActionRequest - data []byte - wantErr bool - }{ - {"unmarshal AddIntervalActionRequest with success", valid, jsonData, false}, - {"unmarshal invalid AddIntervalActionRequest, empty data", AddIntervalActionRequest{}, []byte{}, true}, - {"unmarshal invalid AddIntervalActionRequest, string data", AddIntervalActionRequest{}, []byte("Invalid AddIntervalActionRequest"), true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var result AddIntervalActionRequest - err := result.UnmarshalJSON(tt.data) - if tt.wantErr { - require.Error(t, err) - } else { - require.NoError(t, err) - assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected AddIntervalActionRequest.") - } - }) - } -} - -func TestAddIntervalActionReqToIntervalActionModels(t *testing.T) { - requests := []AddIntervalActionRequest{addIntervalActionRequestData()} - expectedIntervalActionModel := []models.IntervalAction{ - { - Id: requests[0].Action.Id, - Name: TestIntervalActionName, - IntervalName: TestIntervalName, - Address: dtos.ToAddressModel(requests[0].Action.Address), - AdminState: models.Unlocked, - }, - } - resultModels := AddIntervalActionReqToIntervalActionModels(requests) - assert.Equal(t, expectedIntervalActionModel, resultModels, "AddIntervalActionReqToIntervalActionModels did not result in expected IntervalAction model.") -} - -func TestUpdateIntervalActionRequest_UnmarshalJSON(t *testing.T) { - valid := updateIntervalActionRequestData() - jsonData, _ := json.Marshal(updateIntervalActionRequestData()) - tests := []struct { - name string - expected UpdateIntervalActionRequest - data []byte - wantErr bool - }{ - {"unmarshal UpdateIntervalActionRequest with success", valid, jsonData, false}, - {"unmarshal invalid UpdateIntervalActionRequest, empty data", UpdateIntervalActionRequest{}, []byte{}, true}, - {"unmarshal invalid UpdateIntervalActionRequest, string data", UpdateIntervalActionRequest{}, []byte("Invalid UpdateIntervalActionRequest"), true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var result UpdateIntervalActionRequest - err := result.UnmarshalJSON(tt.data) - if tt.wantErr { - require.Error(t, err) - } else { - require.NoError(t, err) - assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected UpdateIntervalActionRequest.", err) - } - }) - } -} - -func TestUpdateIntervalActionRequest_Validate(t *testing.T) { - emptyString := " " - invalidUUID := "invalidUUID" - - valid := updateIntervalActionRequestData() - noReqId := valid - noReqId.RequestId = "" - invalidReqId := valid - invalidReqId.RequestId = invalidUUID - - validOnlyId := valid - validOnlyId.Action.Name = nil - invalidId := valid - invalidId.Action.Id = &invalidUUID - - validOnlyName := valid - validOnlyName.Action.Id = nil - nameAndEmptyId := valid - nameAndEmptyId.Action.Id = &emptyString - invalidEmptyName := valid - invalidEmptyName.Action.Name = &emptyString - invalidEmptyIntervalName := valid - invalidEmptyIntervalName.Action.IntervalName = &emptyString - - invalidNoAddressType := updateIntervalActionRequestData() - invalidNoAddressType.Action.Address.Type = "" - invalidNoAddressHttpMethod := updateIntervalActionRequestData() - invalidNoAddressHttpMethod.Action.Address.HTTPMethod = "" - invalidNoAddressMQTTPublisher := updateIntervalActionRequestData() - invalidNoAddressMQTTPublisher.Action.Address.Type = common.MQTT - invalidNoAddressMQTTPublisher.Action.Address.Topic = TestTopic - invalidNoAddressMQTTPublisher.Action.Address.Publisher = "" - invalidNoAddressMQTTTopic := updateIntervalActionRequestData() - invalidNoAddressMQTTTopic.Action.Address.Type = common.MQTT - invalidNoAddressMQTTTopic.Action.Address.Publisher = TestPublisher - invalidNoAddressMQTTTopic.Action.Address.Topic = "" - - tests := []struct { - name string - req UpdateIntervalActionRequest - expectError bool - }{ - {"valid", valid, false}, - {"valid, no Request Id", noReqId, false}, - {"invalid, Request Id is not an uuid", invalidReqId, true}, - - {"valid, only id", validOnlyId, false}, - {"invalid, invalid Id", invalidId, true}, - {"valid, only name", validOnlyName, false}, - {"valid, name and empty Id", nameAndEmptyId, false}, - {"invalid, empty name", invalidEmptyName, true}, - {"invalid, empty interval name", invalidEmptyIntervalName, true}, - - {"invalid, no address type", invalidNoAddressType, true}, - {"invalid, no address HTTP method", invalidNoAddressHttpMethod, true}, - {"invalid, no address MQTT publisher", invalidNoAddressMQTTPublisher, true}, - {"invalid, no address MQTT Topic", invalidNoAddressMQTTPublisher, true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - err := tt.req.Validate() - assert.Equal(t, tt.expectError, err != nil, "Unexpected updateIntervalActionRequest validation result.", err) - }) - } -} - -func TestUpdateIntervalActionRequest_UnmarshalJSON_NilField(t *testing.T) { - reqJson := `{ - "apiVersion" : "v3", - "requestId":"7a1707f0-166f-4c4b-bc9d-1d54c74e0137", - "action":{"apiVersion":"v3", "name":"TestIntervalAction", "intervalName": "afternoon"} - }` - var req UpdateIntervalActionRequest - - err := req.UnmarshalJSON([]byte(reqJson)) - - require.NoError(t, err) - // Nil field checking is used to update with patch - assert.Nil(t, req.Action.Address) -} - -func TestReplaceIntervalActionModelFieldsWithDTO(t *testing.T) { - interval := models.IntervalAction{ - Id: "7a1707f0-166f-4c4b-bc9d-1d54c74e0137", - Name: TestIntervalActionName, - } - patch := updateIntervalActionData() - - ReplaceIntervalActionModelFieldsWithDTO(&interval, patch) - - expectedAddress := dtos.ToAddressModel(*patch.Address) - assert.Equal(t, TestIntervalActionName, interval.Name) - assert.Equal(t, TestIntervalName, interval.IntervalName) - assert.Equal(t, TestContent, interval.Content) - assert.Equal(t, common.ContentTypeText, interval.ContentType) - assert.Equal(t, expectedAddress, interval.Address) -} diff --git a/dtos/responses/interval.go b/dtos/responses/interval.go deleted file mode 100644 index 023a811c..00000000 --- a/dtos/responses/interval.go +++ /dev/null @@ -1,37 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package responses - -import ( - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" -) - -// IntervalResponse defines the Response Content for GET Interval DTOs. -type IntervalResponse struct { - common.BaseResponse `json:",inline"` - Interval dtos.Interval `json:"interval"` -} - -func NewIntervalResponse(requestId string, message string, statusCode int, interval dtos.Interval) IntervalResponse { - return IntervalResponse{ - BaseResponse: common.NewBaseResponse(requestId, message, statusCode), - Interval: interval, - } -} - -// MultiIntervalsResponse defines the Response Content for GET multiple Interval DTOs. -type MultiIntervalsResponse struct { - common.BaseWithTotalCountResponse `json:",inline"` - Intervals []dtos.Interval `json:"intervals"` -} - -func NewMultiIntervalsResponse(requestId string, message string, statusCode int, totalCount uint32, intervals []dtos.Interval) MultiIntervalsResponse { - return MultiIntervalsResponse{ - BaseWithTotalCountResponse: common.NewBaseWithTotalCountResponse(requestId, message, statusCode, totalCount), - Intervals: intervals, - } -} diff --git a/dtos/responses/interval_test.go b/dtos/responses/interval_test.go deleted file mode 100644 index 7f80cd9b..00000000 --- a/dtos/responses/interval_test.go +++ /dev/null @@ -1,44 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package responses - -import ( - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - "github.com/stretchr/testify/assert" -) - -func TestNewIntervalResponse(t *testing.T) { - expectedRequestId := "123456" - expectedStatusCode := 200 - expectedMessage := "unit test message" - expectedInterval := dtos.Interval{Name: "test interval"} - actual := NewIntervalResponse(expectedRequestId, expectedMessage, expectedStatusCode, expectedInterval) - - assert.Equal(t, expectedRequestId, actual.RequestId) - assert.Equal(t, expectedStatusCode, actual.StatusCode) - assert.Equal(t, expectedMessage, actual.Message) - assert.Equal(t, expectedInterval, actual.Interval) -} - -func TestNewMultiIntervalsResponse(t *testing.T) { - expectedRequestId := "123456" - expectedStatusCode := 200 - expectedMessage := "unit test message" - expectedIntervals := []dtos.Interval{ - {Name: "test interval1"}, - {Name: "test interval2"}, - } - expectedTotalCount := uint32(len(expectedIntervals)) - actual := NewMultiIntervalsResponse(expectedRequestId, expectedMessage, expectedStatusCode, expectedTotalCount, expectedIntervals) - - assert.Equal(t, expectedRequestId, actual.RequestId) - assert.Equal(t, expectedStatusCode, actual.StatusCode) - assert.Equal(t, expectedMessage, actual.Message) - assert.Equal(t, expectedTotalCount, actual.TotalCount) - assert.Equal(t, expectedIntervals, actual.Intervals) -} diff --git a/dtos/responses/intervalaction.go b/dtos/responses/intervalaction.go deleted file mode 100644 index c9d60106..00000000 --- a/dtos/responses/intervalaction.go +++ /dev/null @@ -1,37 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package responses - -import ( - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" -) - -// IntervalActionResponse defines the Response Content for GET IntervalAction DTOs. -type IntervalActionResponse struct { - common.BaseResponse `json:",inline"` - Action dtos.IntervalAction `json:"action"` -} - -func NewIntervalActionResponse(requestId string, message string, statusCode int, action dtos.IntervalAction) IntervalActionResponse { - return IntervalActionResponse{ - BaseResponse: common.NewBaseResponse(requestId, message, statusCode), - Action: action, - } -} - -// MultiIntervalActionsResponse defines the Response Content for GET multiple IntervalAction DTOs. -type MultiIntervalActionsResponse struct { - common.BaseWithTotalCountResponse `json:",inline"` - Actions []dtos.IntervalAction `json:"actions"` -} - -func NewMultiIntervalActionsResponse(requestId string, message string, statusCode int, totalCount uint32, actions []dtos.IntervalAction) MultiIntervalActionsResponse { - return MultiIntervalActionsResponse{ - BaseWithTotalCountResponse: common.NewBaseWithTotalCountResponse(requestId, message, statusCode, totalCount), - Actions: actions, - } -} diff --git a/dtos/responses/intervalaction_test.go b/dtos/responses/intervalaction_test.go deleted file mode 100644 index a4a6121a..00000000 --- a/dtos/responses/intervalaction_test.go +++ /dev/null @@ -1,45 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package responses - -import ( - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos" - - "github.com/stretchr/testify/assert" -) - -func TestNewIntervalActionResponse(t *testing.T) { - expectedRequestId := "123456" - expectedStatusCode := 200 - expectedMessage := "unit test message" - expectedAction := dtos.IntervalAction{Name: "test action"} - actual := NewIntervalActionResponse(expectedRequestId, expectedMessage, expectedStatusCode, expectedAction) - - assert.Equal(t, expectedRequestId, actual.RequestId) - assert.Equal(t, expectedStatusCode, actual.StatusCode) - assert.Equal(t, expectedMessage, actual.Message) - assert.Equal(t, expectedAction, actual.Action) -} - -func TestNewMultiIntervalActionsResponse(t *testing.T) { - expectedRequestId := "123456" - expectedStatusCode := 200 - expectedMessage := "unit test message" - expectedActions := []dtos.IntervalAction{ - {Name: "test action1"}, - {Name: "test action2"}, - } - expectedTotalCount := uint32(len(expectedActions)) - actual := NewMultiIntervalActionsResponse(expectedRequestId, expectedMessage, expectedStatusCode, expectedTotalCount, expectedActions) - - assert.Equal(t, expectedRequestId, actual.RequestId) - assert.Equal(t, expectedStatusCode, actual.StatusCode) - assert.Equal(t, expectedMessage, actual.Message) - assert.Equal(t, expectedTotalCount, actual.TotalCount) - assert.Equal(t, expectedActions, actual.Actions) -} diff --git a/models/interval.go b/models/interval.go deleted file mode 100644 index ea4e906b..00000000 --- a/models/interval.go +++ /dev/null @@ -1,15 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package models - -type Interval struct { - DBTimestamp - Id string - Name string - Start string - End string - Interval string -} diff --git a/models/intervalaction.go b/models/intervalaction.go deleted file mode 100644 index 2af74282..00000000 --- a/models/intervalaction.go +++ /dev/null @@ -1,61 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package models - -import ( - "encoding/json" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" -) - -// AuthMethod controls the authentication method to be applied to outbound http requests for interval actions -type AuthMethod string - -type IntervalAction struct { - DBTimestamp - Id string - Name string - IntervalName string - Content string - ContentType string - Address Address - AdminState AdminState - AuthMethod AuthMethod -} - -func (intervalAction *IntervalAction) UnmarshalJSON(b []byte) error { - var alias struct { - DBTimestamp - Id string - Name string - IntervalName string - Content string - ContentType string - Address interface{} - AdminState AdminState - AuthMethod AuthMethod - } - if err := json.Unmarshal(b, &alias); err != nil { - return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal intervalAction.", err) - } - address, err := instantiateAddress(alias.Address) - if err != nil { - return errors.NewCommonEdgeXWrapper(err) - } - - *intervalAction = IntervalAction{ - DBTimestamp: alias.DBTimestamp, - Id: alias.Id, - Name: alias.Name, - IntervalName: alias.IntervalName, - Content: alias.Content, - ContentType: alias.ContentType, - Address: address, - AdminState: alias.AdminState, - AuthMethod: alias.AuthMethod, - } - return nil -} diff --git a/models/intervalaction_test.go b/models/intervalaction_test.go deleted file mode 100644 index d3ff562e..00000000 --- a/models/intervalaction_test.go +++ /dev/null @@ -1,83 +0,0 @@ -// -// Copyright (C) 2021 IOTech Ltd -// -// SPDX-License-Identifier: Apache-2.0 - -package models - -import ( - "encoding/json" - "testing" - - "github.com/edgexfoundry/go-mod-core-contracts/v4/common" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func actionWithRESTAddressData() IntervalAction { - return IntervalAction{ - DBTimestamp: DBTimestamp{}, - Id: ExampleUUID, - Name: TestIntervalActionName, - IntervalName: TestIntervalName, - Address: RESTAddress{ - BaseAddress: BaseAddress{ - Type: common.REST, - Host: TestHost, - Port: TestPort, - }, - HTTPMethod: TestHTTPMethod, - }, - } -} -func actionWithMQTTPubAddressData() IntervalAction { - return IntervalAction{ - DBTimestamp: DBTimestamp{}, - Id: ExampleUUID, - Name: TestIntervalActionName, - IntervalName: TestIntervalName, - Address: MQTTPubAddress{ - BaseAddress: BaseAddress{ - Type: common.MQTT, - Host: TestHost, - Port: TestPort, - }, - Publisher: TestPublisher, - Topic: TestTopic, - }, - } -} - -func TestIntervalAction_UnmarshalJSON(t *testing.T) { - actionWithRestAddress := actionWithRESTAddressData() - actionWithRestAddressJsonData, err := json.Marshal(actionWithRestAddress) - require.NoError(t, err) - actionWithMQTTPubAddress := actionWithMQTTPubAddressData() - actionWithMQTTPubAddressJsonData, err := json.Marshal(actionWithMQTTPubAddress) - require.NoError(t, err) - - tests := []struct { - name string - expected IntervalAction - data []byte - wantErr bool - }{ - {"valid, unmarshal intervalAction with REST address", actionWithRestAddress, actionWithRestAddressJsonData, false}, - {"valid, unmarshal intervalAction with MQTT address", actionWithMQTTPubAddress, actionWithMQTTPubAddressJsonData, false}, - {"unmarshal invalid AddIntervalActionRequest, empty data", IntervalAction{}, []byte{}, true}, - {"unmarshal invalid AddIntervalActionRequest, string data", IntervalAction{}, []byte("Invalid IntervalAction"), true}, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var result IntervalAction - err := json.Unmarshal(tt.data, &result) - if tt.wantErr { - require.Error(t, err) - } else { - require.NoError(t, err) - assert.Equal(t, tt.expected, result, "Unmarshal did not result in expected AddIntervalActionRequest.") - } - }) - } -}