Skip to content

Commit

Permalink
Merge pull request #529 from weichou1229/issue-528
Browse files Browse the repository at this point in the history
fix: Check empty value when converting Model to UpdateDTO
  • Loading branch information
lenny-goodell authored Mar 2, 2021
2 parents f9dfc94 + 47da0a4 commit 860d36c
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 40 deletions.
9 changes: 2 additions & 7 deletions v2/clients/http/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/edgexfoundry/go-mod-core-contracts/v2/clients"
"github.com/edgexfoundry/go-mod-core-contracts/v2/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/dtos/common"

"github.com/google/uuid"
)
Expand Down Expand Up @@ -150,11 +149,7 @@ func sendRequest(ctx context.Context, req *http.Request) ([]byte, errors.EdgeX)
}

// Handle error response
var res common.BaseResponse
if err := json.Unmarshal(bodyBytes, &res); err != nil {
return nil, errors.NewCommonEdgeXWrapper(err)
}
msg := fmt.Sprintf("request failed, status code: %d, err: %s", res.StatusCode, res.Message)
errKind := errors.KindMapping(res.StatusCode)
msg := fmt.Sprintf("request failed, status code: %d, err: %s", resp.StatusCode, string(bodyBytes))
errKind := errors.KindMapping(resp.StatusCode)
return nil, errors.NewCommonEdgeX(errKind, msg, nil)
}
61 changes: 43 additions & 18 deletions v2/dtos/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,48 @@ func FromDeviceModelToDTO(d models.Device) Device {

// FromDeviceModelToUpdateDTO transforms the Device Model to the UpdateDevice DTO
func FromDeviceModelToUpdateDTO(d models.Device) UpdateDevice {
adminState := string(d.AdminState)
operatingState := string(d.OperatingState)
return UpdateDevice{
Versionable: common.NewVersionable(),
Id: &d.Id,
Name: &d.Name,
Description: &d.Description,
AdminState: &adminState,
OperatingState: &operatingState,
LastConnected: &d.LastConnected,
LastReported: &d.LastReported,
ServiceName: &d.ServiceName,
ProfileName: &d.ProfileName,
Labels: d.Labels,
Location: d.Location,
AutoEvents: FromAutoEventModelsToDTOs(d.AutoEvents),
Protocols: FromProtocolModelsToDTOs(d.Protocols),
Notify: &d.Notify,
dto := UpdateDevice{
Versionable: common.NewVersionable(),
Labels: d.Labels,
Notify: &d.Notify,
}
if d.Id != "" {
dto.Id = &d.Id
}
if d.Name != "" {
dto.Name = &d.Name
}
if d.Description != "" {
dto.Description = &d.Description
}
if d.AdminState != "" {
adminState := string(d.AdminState)
dto.AdminState = &adminState
}
if d.OperatingState != "" {
operatingState := string(d.OperatingState)
dto.OperatingState = &operatingState
}
if d.LastConnected != 0 {
dto.LastConnected = &d.LastConnected
}
if d.LastReported != 0 {
dto.LastReported = &d.LastReported
}
if d.ServiceName != "" {
dto.ServiceName = &d.ServiceName
}
if d.ProfileName != "" {
dto.ProfileName = &d.ProfileName
}
if d.Location != nil {
dto.Location = d.Location
}
if d.AutoEvents != nil {
dto.AutoEvents = FromAutoEventModelsToDTOs(d.AutoEvents)
}
if d.Protocols != nil {
dto.Protocols = FromProtocolModelsToDTOs(d.Protocols)
}
return dto
}
33 changes: 33 additions & 0 deletions v2/dtos/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package dtos

import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
)

func TestFromDeviceModelToUpdateDTO(t *testing.T) {
model := models.Device{}
dto := FromDeviceModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Description)
assert.Nil(t, dto.AdminState)
assert.Nil(t, dto.OperatingState)
assert.Nil(t, dto.LastConnected)
assert.Nil(t, dto.LastReported)
assert.Nil(t, dto.ServiceName)
assert.Nil(t, dto.ProfileName)
assert.Nil(t, dto.Location)
assert.Nil(t, dto.AutoEvents)
assert.Nil(t, dto.Protocols)
}
21 changes: 15 additions & 6 deletions v2/dtos/deviceservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,22 @@ func FromDeviceServiceModelToDTO(ds models.DeviceService) DeviceService {

// FromDeviceServiceModelToUpdateDTO transforms the DeviceService Model to the UpdateDeviceService DTO
func FromDeviceServiceModelToUpdateDTO(ds models.DeviceService) UpdateDeviceService {
adminState := string(ds.AdminState)
return UpdateDeviceService{
dto := UpdateDeviceService{
Versionable: common.NewVersionable(),
Id: &ds.Id,
Name: &ds.Name,
BaseAddress: &ds.BaseAddress,
Labels: ds.Labels,
AdminState: &adminState,
}
if ds.Id != "" {
dto.Id = &ds.Id
}
if ds.Name != "" {
dto.Name = &ds.Name
}
if ds.BaseAddress != "" {
dto.BaseAddress = &ds.BaseAddress
}
if ds.AdminState != "" {
adminState := string(ds.AdminState)
dto.AdminState = &adminState
}
return dto
}
26 changes: 26 additions & 0 deletions v2/dtos/deviceservice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package dtos

import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
)

func TestFromDeviceServiceModelToUpdateDTO(t *testing.T) {
model := models.DeviceService{}
dto := FromDeviceServiceModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Labels)
assert.Nil(t, dto.BaseAddress)
assert.Nil(t, dto.AdminState)
}
29 changes: 21 additions & 8 deletions v2/dtos/provisionwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,30 @@ func FromProvisionWatcherModelToDTO(pw models.ProvisionWatcher) ProvisionWatcher

// FromProvisionWatcherModelToUpdateDTO transforms the ProvisionWatcher Model to the UpdateProvisionWatcher DTO
func FromProvisionWatcherModelToUpdateDTO(pw models.ProvisionWatcher) UpdateProvisionWatcher {
adminState := string(pw.AdminState)
return UpdateProvisionWatcher{
dto := UpdateProvisionWatcher{
Versionable: common.NewVersionable(),
Id: &pw.Id,
Name: &pw.Name,
Labels: pw.Labels,
Identifiers: pw.Identifiers,
BlockingIdentifiers: pw.BlockingIdentifiers,
ProfileName: &pw.ProfileName,
ServiceName: &pw.ServiceName,
AdminState: &adminState,
AutoEvents: FromAutoEventModelsToDTOs(pw.AutoEvents),
}
if pw.Id != "" {
dto.Id = &pw.Id
}
if pw.Name != "" {
dto.Name = &pw.Name
}
if pw.ProfileName != "" {
dto.ProfileName = &pw.ProfileName
}
if pw.ServiceName != "" {
dto.ServiceName = &pw.ServiceName
}
if pw.AdminState != "" {
adminState := string(pw.AdminState)
dto.AdminState = &adminState
}
if pw.AutoEvents != nil {
dto.AutoEvents = FromAutoEventModelsToDTOs(pw.AutoEvents)
}
return dto
}
30 changes: 30 additions & 0 deletions v2/dtos/provisionwatcher_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0

package dtos

import (
"testing"

"github.com/edgexfoundry/go-mod-core-contracts/v2/v2"
"github.com/edgexfoundry/go-mod-core-contracts/v2/v2/models"

"github.com/stretchr/testify/assert"
)

func TestFromProvisionWatcherModelToUpdateDTO(t *testing.T) {
model := models.ProvisionWatcher{}
dto := FromProvisionWatcherModelToUpdateDTO(model)
assert.Equal(t, v2.ApiVersion, dto.ApiVersion)
assert.Nil(t, dto.Id)
assert.Nil(t, dto.Name)
assert.Nil(t, dto.Labels)
assert.Nil(t, dto.Identifiers)
assert.Nil(t, dto.BlockingIdentifiers)
assert.Nil(t, dto.ProfileName)
assert.Nil(t, dto.ServiceName)
assert.Nil(t, dto.AdminState)
assert.Nil(t, dto.AutoEvents)
}
2 changes: 1 addition & 1 deletion v2/dtos/reading.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type SimpleReading struct {
// BinaryReading and its properties are defined in the APIv2 specification:
// https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-data/2.x#/BinaryReading
type BinaryReading struct {
BinaryValue []byte `json:"binaryValue" validate:"gt=0,dive,required"`
BinaryValue []byte `json:"binaryValue" validate:"gt=0,required"`
MediaType string `json:"mediaType" validate:"required"`
}

Expand Down

0 comments on commit 860d36c

Please sign in to comment.