Skip to content

Commit 70c0615

Browse files
Don't process body when update/get return code valid (#413)
* Don't process body when update/get return code valid --------- Co-authored-by: cah <lubomir.capucha@kistler.com>
1 parent 3200d43 commit 70c0615

File tree

3 files changed

+65
-33
lines changed

3 files changed

+65
-33
lines changed

client/getResource_test.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestClientGetResource(t *testing.T) {
5050
tests := []struct {
5151
name string
5252
args args
53-
want map[string]interface{}
53+
want coap.DetailedResponse[interface{}]
5454
wantErr bool
5555
}{
5656
{
@@ -60,8 +60,11 @@ func TestClientGetResource(t *testing.T) {
6060
href: configuration.ResourceURI,
6161
opts: []client.GetOption{client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration())},
6262
},
63-
want: map[string]interface{}{
64-
"n": test.DevsimName,
63+
want: coap.DetailedResponse[interface{}]{
64+
Code: codes.Content,
65+
Body: map[interface{}]interface{}{
66+
"n": test.DevsimName,
67+
},
6568
},
6669
},
6770
{
@@ -72,10 +75,13 @@ func TestClientGetResource(t *testing.T) {
7275
opts: []client.GetOption{client.WithInterface(interfaces.OC_IF_BASELINE)},
7376
},
7477
wantErr: false,
75-
want: map[string]interface{}{
76-
"if": []interface{}{interfaces.OC_IF_RW, interfaces.OC_IF_BASELINE},
77-
"n": test.DevsimName,
78-
"rt": []interface{}{configuration.ResourceType},
78+
want: coap.DetailedResponse[interface{}]{
79+
Code: codes.Content,
80+
Body: map[interface{}]interface{}{
81+
"if": []interface{}{interfaces.OC_IF_RW, interfaces.OC_IF_BASELINE},
82+
"n": test.DevsimName,
83+
"rt": []interface{}{configuration.ResourceType},
84+
},
7985
},
8086
},
8187
{
@@ -109,13 +115,14 @@ func TestClientGetResource(t *testing.T) {
109115
defer disown(t, c, deviceID)
110116
for _, tt := range tests {
111117
t.Run(tt.name, func(t *testing.T) {
112-
var got map[string]interface{}
118+
var got coap.DetailedResponse[interface{}]
113119
err := c.GetResource(ctx, tt.args.deviceID, tt.args.href, &got, tt.args.opts...)
114120
if tt.wantErr {
115121
require.Error(t, err)
116122
return
117123
}
118124
require.NoError(t, err)
125+
got.ETag = nil
119126
require.Equal(t, tt.want, got)
120127
})
121128
}

client/updateResource_test.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ import (
2222

2323
"github.com/plgd-dev/device/v2/client"
2424
"github.com/plgd-dev/device/v2/client/core"
25+
"github.com/plgd-dev/device/v2/pkg/net/coap"
2526
"github.com/plgd-dev/device/v2/schema/configuration"
2627
"github.com/plgd-dev/device/v2/schema/device"
2728
"github.com/plgd-dev/device/v2/schema/interfaces"
2829
"github.com/plgd-dev/device/v2/test"
30+
"github.com/plgd-dev/go-coap/v3/message/codes"
2931
"github.com/stretchr/testify/require"
3032
)
3133

@@ -40,7 +42,7 @@ func TestClientUpdateResource(t *testing.T) {
4042
tests := []struct {
4143
name string
4244
args args
43-
want interface{}
45+
want coap.DetailedResponse[interface{}]
4446
wantErr bool
4547
}{
4648
{
@@ -53,8 +55,11 @@ func TestClientUpdateResource(t *testing.T) {
5355
},
5456
opts: []client.UpdateOption{client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration())},
5557
},
56-
want: map[interface{}]interface{}{
57-
"n": t.Name() + "-valid",
58+
want: coap.DetailedResponse[interface{}]{
59+
Code: codes.Changed,
60+
Body: map[interface{}]interface{}{
61+
"n": t.Name() + "-valid",
62+
},
5863
},
5964
},
6065
{
@@ -67,8 +72,11 @@ func TestClientUpdateResource(t *testing.T) {
6772
},
6873
opts: []client.UpdateOption{client.WithInterface(interfaces.OC_IF_BASELINE)},
6974
},
70-
want: map[interface{}]interface{}{
71-
"n": t.Name() + "-valid with interface",
75+
want: coap.DetailedResponse[interface{}]{
76+
Code: codes.Changed,
77+
Body: map[interface{}]interface{}{
78+
"n": t.Name() + "-valid with interface",
79+
},
7280
},
7381
},
7482
{
@@ -80,8 +88,11 @@ func TestClientUpdateResource(t *testing.T) {
8088
"n": test.DevsimName,
8189
},
8290
},
83-
want: map[interface{}]interface{}{
84-
"n": test.DevsimName,
91+
want: coap.DetailedResponse[interface{}]{
92+
Code: codes.Changed,
93+
Body: map[interface{}]interface{}{
94+
"n": test.DevsimName,
95+
},
8596
},
8697
},
8798
{
@@ -120,7 +131,7 @@ func TestClientUpdateResource(t *testing.T) {
120131

121132
for _, tt := range tests {
122133
t.Run(tt.name, func(t *testing.T) {
123-
var got interface{}
134+
var got coap.DetailedResponse[interface{}]
124135
err = c.UpdateResource(ctx, tt.args.deviceID, tt.args.href, tt.args.data, &got, tt.args.opts...)
125136
if tt.wantErr {
126137
require.Error(t, err)
@@ -143,7 +154,7 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
143154
tests := []struct {
144155
name string
145156
args args
146-
want interface{}
157+
want coap.DetailedResponse[interface{}]
147158
wantErr bool
148159
}{
149160
{
@@ -156,10 +167,13 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
156167
},
157168
opts: []client.UpdateOption{client.WithDiscoveryConfiguration(core.DefaultDiscoveryConfiguration())},
158169
},
159-
want: map[interface{}]interface{}{
160-
"name": "Light",
161-
"power": uint64(0),
162-
"state": true,
170+
want: coap.DetailedResponse[interface{}]{
171+
Code: codes.Content,
172+
Body: map[interface{}]interface{}{
173+
"name": "Light",
174+
"power": uint64(0),
175+
"state": true,
176+
},
163177
},
164178
},
165179
{
@@ -172,10 +186,13 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
172186
},
173187
opts: []client.UpdateOption{client.WithInterface(interfaces.OC_IF_BASELINE)},
174188
},
175-
want: map[interface{}]interface{}{
176-
"name": "Light",
177-
"power": uint64(1),
178-
"state": true,
189+
want: coap.DetailedResponse[interface{}]{
190+
Code: codes.Content,
191+
Body: map[interface{}]interface{}{
192+
"name": "Light",
193+
"power": uint64(1),
194+
"state": true,
195+
},
179196
},
180197
},
181198
{
@@ -188,10 +205,13 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
188205
"power": uint64(0),
189206
},
190207
},
191-
want: map[interface{}]interface{}{
192-
"name": "Light",
193-
"power": uint64(0),
194-
"state": false,
208+
want: coap.DetailedResponse[interface{}]{
209+
Code: codes.Content,
210+
Body: map[interface{}]interface{}{
211+
"name": "Light",
212+
"power": uint64(0),
213+
"state": false,
214+
},
195215
},
196216
},
197217
{
@@ -235,9 +255,10 @@ func TestClientUpdateResourceInRFOTM(t *testing.T) {
235255
return
236256
}
237257
require.NoError(t, err)
238-
var got interface{}
258+
var got coap.DetailedResponse[interface{}]
239259
err = c.GetResource(ctx, tt.args.deviceID, tt.args.href, &got)
240260
require.NoError(t, err)
261+
got.ETag = nil
241262
require.Equal(t, tt.want, got)
242263
})
243264
}

pkg/net/coap/client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func requestError(m *pool.Message) error {
190190
}
191191

192192
func decodeError(href string, err error) error {
193-
return fmt.Errorf("could not decode the query %s: %w", href, err)
193+
return fmt.Errorf("could not decode the response %s: %w", href, err)
194194
}
195195

196196
func (c *Client) UpdateResourceWithCodec(
@@ -203,7 +203,7 @@ func (c *Client) UpdateResourceWithCodec(
203203
) error {
204204
body, err := codec.Encode(request)
205205
if err != nil {
206-
return fmt.Errorf("could not encode the query %s: %w", href, err)
206+
return fmt.Errorf("could not encode the request %s: %w", href, err)
207207
}
208208
opts := make(message.Options, 0, 4)
209209
for _, o := range options {
@@ -214,12 +214,16 @@ func (c *Client) UpdateResourceWithCodec(
214214
if err != nil {
215215
return fmt.Errorf("could update request %s: %w", href, err)
216216
}
217+
response, err = TrySetDetailedReponse(resp, response)
217218
if err != nil {
218-
return fmt.Errorf("could not query %s: %w", href, err)
219+
return err
219220
}
220221
if resp.Code() != codes.Changed && resp.Code() != codes.Valid && resp.Code() != codes.Created {
221222
return status.Error(resp, requestError(resp))
222223
}
224+
if resp.Code() == codes.Valid {
225+
return nil
226+
}
223227
if err := codec.Decode(resp, response); err != nil {
224228
return status.Error(resp, decodeError(href, err))
225229
}

0 commit comments

Comments
 (0)