Skip to content

Commit

Permalink
fix(plc4go/spi): gracefully handle tag names not found on WriteResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed May 17, 2023
1 parent fdce5b9 commit 0a14655
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
7 changes: 5 additions & 2 deletions plc4go/spi/model/DefaultPlcWriteResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func (d *DefaultPlcWriteResponse) GetRequest() apiModel.PlcWriteRequest {
}

func (d *DefaultPlcWriteResponse) GetResponseCode(name string) apiModel.PlcResponseCode {
// TODO: guard
return d.responseCodes[name]
code, ok := d.responseCodes[name]
if !ok {
return apiModel.PlcResponseCode_NOT_FOUND
}
return code
}
51 changes: 45 additions & 6 deletions plc4go/spi/model/DefaultPlcWriteResponse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
package model

import (
"testing"

apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"

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

func TestDefaultPlcWriteResponse_GetRequest(t *testing.T) {
Expand All @@ -35,7 +37,9 @@ func TestDefaultPlcWriteResponse_GetRequest(t *testing.T) {
fields fields
want apiModel.PlcWriteRequest
}{
// TODO: Add test cases.
{
name: "get it",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -62,7 +66,22 @@ func TestDefaultPlcWriteResponse_GetResponseCode(t *testing.T) {
args args
want apiModel.PlcResponseCode
}{
// TODO: Add test cases.
{
name: "get it (not found)",
want: apiModel.PlcResponseCode_NOT_FOUND,
},
{
name: "get it",
fields: fields{
responseCodes: map[string]apiModel.PlcResponseCode{
"something": apiModel.PlcResponseCode_OK,
},
},
args: args{
name: "something",
},
want: apiModel.PlcResponseCode_OK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -85,7 +104,21 @@ func TestDefaultPlcWriteResponse_GetTagNames(t *testing.T) {
fields fields
want []string
}{
// TODO: Add test cases.
{
name: "get em (none)",
},
{
name: "get em",
fields: fields{
request: NewDefaultPlcWriteRequest(nil, []string{"a", "b", "c"}, nil, nil, nil),
responseCodes: map[string]apiModel.PlcResponseCode{
"a": apiModel.PlcResponseCode_OK,
"b": apiModel.PlcResponseCode_OK,
"c": apiModel.PlcResponseCode_OK,
},
},
want: []string{"a", "b", "c"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -108,7 +141,10 @@ func TestDefaultPlcWriteResponse_IsAPlcMessage(t *testing.T) {
fields fields
want bool
}{
// TODO: Add test cases.
{
name: "is it",
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -131,7 +167,10 @@ func TestNewDefaultPlcWriteResponse(t *testing.T) {
args args
want apiModel.PlcWriteResponse
}{
// TODO: Add test cases.
{
name: "create it",
want: &DefaultPlcWriteResponse{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 0a14655

Please sign in to comment.