Skip to content

Commit

Permalink
fix: Update handler funcs to use Echo signatures
Browse files Browse the repository at this point in the history
- Update handler funcs and AddCustomRoute method to use Echo signatures.
- Modify failed unit tests.

Signed-off-by: Lindsey Cheng <beckysocute@gmail.com>
  • Loading branch information
lindseysimple committed Aug 2, 2023
1 parent 72d350f commit 93fdda3
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 195 deletions.
5 changes: 2 additions & 3 deletions example/cmd/device-simple/Attribution.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
The following open source projects are referenced by Device Service SDK Go:

gorilla/mux 1.6.2 (BSD-3) https://github.com/gorilla/mux
https://github.com/gorilla/mux/blob/master/LICENSE

hashicorp/consul 1.1.0 (Mozilla Public License 2.0) https://github.com/hashicorp/consul
https://github.com/hashicorp/consul/blob/master/LICENSE

Expand Down Expand Up @@ -225,3 +222,5 @@ https://github.com/klauspost/compress/blob/master/LICENSE
github.com/gabriel-vasile/mimetype (MIT) https://github.com/gabriel-vasile/mimetype
https://github.com/gabriel-vasile/mimetype/blob/master/LICENSE

github.com/labstack/echo/v4 (MIT) https://github.com/labstack/echo
https://github.com/labstack/echo/blob/master/LICENSE
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ go 1.20
require (
github.com/OneOfOne/xxhash v1.2.8
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.1.0-dev.14
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.2
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.3
github.com/edgexfoundry/go-mod-messaging/v3 v3.1.0-dev.12
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.1
github.com/labstack/echo/v4 v4.11.1
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.1.0-dev.14 h1:yDDt0qwMDjhEDbV7rEX
github.com/edgexfoundry/go-mod-bootstrap/v3 v3.1.0-dev.14/go.mod h1:Qe0cE8xdcddeTKVhKt079SnA6obquNSiIFLfAfBKdDE=
github.com/edgexfoundry/go-mod-configuration/v3 v3.1.0-dev.4 h1:RmZVvR9sa3CdRNIiBNtuYG1gkP3Y98jOf69kL2SabGI=
github.com/edgexfoundry/go-mod-configuration/v3 v3.1.0-dev.4/go.mod h1:hVlzVoVpbgOZKppX+vwYsp7Kf0eBfRmBZP2FsyMwe9I=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.2 h1:H3ls1vyxCv6pigZ/RZlRhj9lUTQq7CiT5/dnZRsgVmQ=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.2/go.mod h1:dadH49hOQlIKkbOfRj5gYEez8l9kUQ3aj9cc8uA5hv4=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.3 h1:NJa87GfnUZw/GvX2aeeZ4ZQoSzwkRotuLQej2ay6XUc=
github.com/edgexfoundry/go-mod-core-contracts/v3 v3.1.0-dev.3/go.mod h1:dadH49hOQlIKkbOfRj5gYEez8l9kUQ3aj9cc8uA5hv4=
github.com/edgexfoundry/go-mod-messaging/v3 v3.1.0-dev.12 h1:b0ZNEkF0Xnsjlvdd4RjAvdqiq/bGLsVe72yysg92tQQ=
github.com/edgexfoundry/go-mod-messaging/v3 v3.1.0-dev.12/go.mod h1:1f0XKGDQifMd/2D11aHvAOpVJ19BmaPzr1f0T4nVpdw=
github.com/edgexfoundry/go-mod-registry/v3 v3.1.0-dev.4 h1:qYWqqsXzJie8HBj6103RCuFRi8Ks4VbDT45rdZjJ8Z8=
Expand Down Expand Up @@ -89,8 +89,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/consul/api v1.23.0 h1:L6e4v1AfoumqAHq/Rrsmuulev+nd7vltM3k8H329tyI=
Expand Down
43 changes: 20 additions & 23 deletions internal/controller/http/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ import (
commonDTO "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
"github.com/gorilla/mux"

"github.com/edgexfoundry/device-sdk-go/v3/internal/application"
sdkCommon "github.com/edgexfoundry/device-sdk-go/v3/internal/common"
)

func (c *RestController) GetCommand(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
deviceName := vars[common.Name]
commandName := vars[common.Command]
"github.com/labstack/echo/v4"
)

func (c *RestController) GetCommand(e echo.Context) error {
deviceName := e.Param(common.Name)
commandName := e.Param(common.Command)
r := e.Request()
w := e.Response()
ctx := r.Context()
correlationId := utils.FromContext(ctx, common.CorrelationHeader)

// parse query parameter
queryParams, reserved, err := filterQueryParams(r.URL.RawQuery)
if err != nil {
c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
return
return c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
}

regexCmd := true
Expand All @@ -46,8 +46,7 @@ func (c *RestController) GetCommand(w http.ResponseWriter, r *http.Request) {

event, err := application.GetCommand(ctx, deviceName, commandName, queryParams, regexCmd, c.dic)
if err != nil {
c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
return
return c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
}

// push event to CoreData if specified (default false)
Expand All @@ -58,40 +57,38 @@ func (c *RestController) GetCommand(w http.ResponseWriter, r *http.Request) {
// return event in http response if specified (default true)
if returnEvent := reserved.Get(common.ReturnEvent); returnEvent == "" || returnEvent == common.ValueTrue {
res := responses.NewEventResponse("", "", http.StatusOK, *event)
c.sendEventResponse(w, r, res, http.StatusOK)
return
return c.sendEventResponse(w, r, res, http.StatusOK)
}

w.WriteHeader(http.StatusOK)
return nil
}

func (c *RestController) SetCommand(w http.ResponseWriter, r *http.Request) {
func (c *RestController) SetCommand(e echo.Context) error {
r := e.Request()
w := e.Response()
if r.Body != nil {
defer func() { _ = r.Body.Close() }()
}

ctx := r.Context()
vars := mux.Vars(r)
deviceName := vars[common.Name]
commandName := vars[common.Command]
deviceName := e.Param(common.Name)
commandName := e.Param(common.Command)

// parse query parameter
queryParams, _, err := filterQueryParams(r.URL.RawQuery)
if err != nil {
c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
return
return c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
}

requestParamsMap, err := parseRequestBody(r)
if err != nil {
c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
return
return c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
}

event, err := application.SetCommand(ctx, deviceName, commandName, queryParams, requestParamsMap, c.dic)
if err != nil {
c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
return
return c.sendEdgexError(w, r, err, common.ApiDeviceNameCommandNameRoute)
}

if event != nil {
Expand All @@ -100,7 +97,7 @@ func (c *RestController) SetCommand(w http.ResponseWriter, r *http.Request) {
}

res := commonDTO.NewBaseResponse("", "", http.StatusOK)
c.sendResponse(w, r, common.ApiDeviceNameCommandNameRoute, res, http.StatusOK)
return c.sendResponse(w, r, common.ApiDeviceNameCommandNameRoute, res, http.StatusOK)
}

func parseRequestBody(req *http.Request) (map[string]interface{}, errors.EdgeX) {
Expand Down
51 changes: 23 additions & 28 deletions internal/controller/http/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/responses"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
messagingMocks "github.com/edgexfoundry/go-mod-messaging/v3/messaging/mocks"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -209,8 +208,8 @@ func TestRestController_GetCommand(t *testing.T) {
e := echo.New()
dic := mockDic()

err := cache.InitCache(testService, testService, dic)
require.NoError(t, err)
edgexErr := cache.InitCache(testService, testService, dic)
require.NoError(t, edgexErr)

controller := NewRestController(e, dic, testService)
assert.NotNil(t, controller)
Expand All @@ -237,15 +236,15 @@ func TestRestController_GetCommand(t *testing.T) {
}
for _, testCase := range tests {
t.Run(testCase.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)
req = mux.SetURLVars(req, map[string]string{common.Name: testCase.deviceName, common.Command: testCase.commandName})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)

// Act
recorder := httptest.NewRecorder()
handler := WrapHandler(controller.GetCommand)
c := e.NewContext(req, recorder)
err = handler(c)
c.SetParamNames(common.Name, common.Command)
c.SetParamValues(testCase.deviceName, testCase.commandName)

err := controller.GetCommand(c)
assert.NoError(t, err)

var res responses.EventResponse
Expand Down Expand Up @@ -283,15 +282,14 @@ func TestRestController_GetCommand_ServiceLocked(t *testing.T) {
controller := NewRestController(e, dic, testService)
assert.NotNil(t, controller)

req, err := http.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)
req = mux.SetURLVars(req, map[string]string{common.Name: testDevice, common.Command: testResource})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)

// Act
recorder := httptest.NewRecorder()
handler := WrapHandler(controller.GetCommand)
c := e.NewContext(req, recorder)
err = handler(c)
c.SetParamNames(common.Name, common.Command)
c.SetParamValues(testDevice, testResource)
err := controller.GetCommand(c)
assert.NoError(t, err)

var res responses.EventResponse
Expand All @@ -315,18 +313,17 @@ func TestRestController_GetCommand_ReturnEvent(t *testing.T) {
controller := NewRestController(e, dic, testService)
assert.NotNil(t, controller)

req, err := http.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)
req = mux.SetURLVars(req, map[string]string{common.Name: testDevice, common.Command: testResource})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodGet, common.ApiDeviceNameCommandNameRoute, http.NoBody)

query := req.URL.Query()
query.Add("ds-returnevent", common.ValueFalse)
req.URL.RawQuery = query.Encode()
// Act
recorder := httptest.NewRecorder()
handler := WrapHandler(controller.GetCommand)
c := e.NewContext(req, recorder)
err = handler(c)
c.SetParamNames(common.Name, common.Command)
c.SetParamValues(testDevice, testResource)
err := controller.GetCommand(c)
assert.NoError(t, err)

// Assert
Expand Down Expand Up @@ -380,9 +377,7 @@ func TestRestController_SetCommand(t *testing.T) {
require.NoError(t, err)

reader := strings.NewReader(string(jsonData))
req, err := http.NewRequest(http.MethodPut, common.ApiDeviceNameCommandNameRoute, reader)
req = mux.SetURLVars(req, map[string]string{common.Name: testCase.deviceName, common.Command: testCase.commandName})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPut, common.ApiDeviceNameCommandNameRoute, reader)

var wg sync.WaitGroup
if testCase.commandName != writeOnlyCommand && testCase.commandName != writeOnlyResource {
Expand All @@ -402,9 +397,10 @@ func TestRestController_SetCommand(t *testing.T) {

// Act
recorder := httptest.NewRecorder()
handler := WrapHandler(controller.SetCommand)
c := e.NewContext(req, recorder)
err = handler(c)
c.SetParamNames(common.Name, common.Command)
c.SetParamValues(testCase.deviceName, testCase.commandName)
err = controller.SetCommand(c)
assert.NoError(t, err)

var res commonDTO.BaseResponse
Expand Down Expand Up @@ -452,15 +448,14 @@ func TestRestController_SetCommand_ServiceLocked(t *testing.T) {
require.NoError(t, err)

reader := strings.NewReader(string(jsonData))
req, err := http.NewRequest(http.MethodPut, common.ApiDeviceNameCommandNameRoute, reader)
req = mux.SetURLVars(req, map[string]string{common.Name: testDevice, common.Command: testResource})
require.NoError(t, err)
req := httptest.NewRequest(http.MethodPut, common.ApiDeviceNameCommandNameRoute, reader)

// Act
recorder := httptest.NewRecorder()
handler := WrapHandler(controller.SetCommand)
c := e.NewContext(req, recorder)
err = handler(c)
c.SetParamNames(common.Name, common.Command)
c.SetParamValues(testDevice, testResource)
err = controller.SetCommand(c)
assert.NoError(t, err)

var res commonDTO.BaseResponse
Expand Down
67 changes: 0 additions & 67 deletions internal/controller/http/correlation/middleware.go

This file was deleted.

14 changes: 8 additions & 6 deletions internal/controller/http/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ import (

"github.com/edgexfoundry/device-sdk-go/v3/internal/autodiscovery"
"github.com/edgexfoundry/device-sdk-go/v3/internal/container"

"github.com/labstack/echo/v4"
)

func (c *RestController) Discovery(writer http.ResponseWriter, request *http.Request) {
func (c *RestController) Discovery(e echo.Context) error {
request := e.Request()
writer := e.Response()
ds := container.DeviceServiceFrom(c.dic.Get)
if ds.AdminState == models.Locked {
err := errors.NewCommonEdgeX(errors.KindServiceLocked, "service locked", nil)
c.sendEdgexError(writer, request, err, common.ApiDiscoveryRoute)
return
return c.sendEdgexError(writer, request, err, common.ApiDiscoveryRoute)
}

configuration := container.ConfigurationFrom(c.dic.Get)
if !configuration.Device.Discovery.Enabled {
err := errors.NewCommonEdgeX(errors.KindServiceUnavailable, "device discovery disabled", nil)
c.sendEdgexError(writer, request, err, common.ApiDiscoveryRoute)
return
return c.sendEdgexError(writer, request, err, common.ApiDiscoveryRoute)
}

driver := container.ProtocolDriverFrom(c.dic.Get)

go autodiscovery.DiscoveryWrapper(driver, c.lc)
c.sendResponse(writer, request, common.ApiDiscoveryRoute, nil, http.StatusAccepted)
return c.sendResponse(writer, request, common.ApiDiscoveryRoute, nil, http.StatusAccepted)
}
Loading

0 comments on commit 93fdda3

Please sign in to comment.