You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/05/17 10:32:35 UTC
[plc4x] 03/03: fix(plc4go/spi): gracefully handle tag names not found on ReadResponse.
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 7de8439f1d098a0e84024d560752698f83bf0416
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Wed May 17 12:32:25 2023 +0200
fix(plc4go/spi): gracefully handle tag names not found on ReadResponse.
---
plc4go/spi/model/DefaultPlcReadResponse.go | 19 +-
plc4go/spi/model/DefaultPlcReadResponse_test.go | 226 ++++++++++++++++++++++++
plc4go/spi/model/render_test.go | 10 +-
3 files changed, 246 insertions(+), 9 deletions(-)
diff --git a/plc4go/spi/model/DefaultPlcReadResponse.go b/plc4go/spi/model/DefaultPlcReadResponse.go
index badb6c75b5..43f0f1b6a5 100644
--- a/plc4go/spi/model/DefaultPlcReadResponse.go
+++ b/plc4go/spi/model/DefaultPlcReadResponse.go
@@ -21,7 +21,8 @@ package model
import (
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
- "github.com/apache/plc4x/plc4go/pkg/api/values"
+ apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
+ spiValues "github.com/apache/plc4x/plc4go/spi/values"
)
//go:generate go run ../../tools/plc4xgenerator/gen.go -type=DefaultPlcReadResponse
@@ -30,7 +31,7 @@ type DefaultPlcReadResponse struct {
values map[string]*ResponseItem
}
-func NewDefaultPlcReadResponse(request apiModel.PlcReadRequest, responseCodes map[string]apiModel.PlcResponseCode, values map[string]values.PlcValue) apiModel.PlcReadResponse {
+func NewDefaultPlcReadResponse(request apiModel.PlcReadRequest, responseCodes map[string]apiModel.PlcResponseCode, values map[string]apiValues.PlcValue) apiModel.PlcReadResponse {
valueMap := map[string]*ResponseItem{}
for name, code := range responseCodes {
value := values[name]
@@ -66,9 +67,17 @@ func (d *DefaultPlcReadResponse) GetRequest() apiModel.PlcReadRequest {
}
func (d *DefaultPlcReadResponse) GetResponseCode(name string) apiModel.PlcResponseCode {
- return d.values[name].GetCode()
+ item, ok := d.values[name]
+ if !ok {
+ return apiModel.PlcResponseCode_NOT_FOUND
+ }
+ return item.GetCode()
}
-func (d *DefaultPlcReadResponse) GetValue(name string) values.PlcValue {
- return d.values[name].GetValue()
+func (d *DefaultPlcReadResponse) GetValue(name string) apiValues.PlcValue {
+ item, ok := d.values[name]
+ if !ok {
+ return spiValues.PlcNull{}
+ }
+ return item.GetValue()
}
diff --git a/plc4go/spi/model/DefaultPlcReadResponse_test.go b/plc4go/spi/model/DefaultPlcReadResponse_test.go
new file mode 100644
index 0000000000..33fd07fe4d
--- /dev/null
+++ b/plc4go/spi/model/DefaultPlcReadResponse_test.go
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package model
+
+import (
+ "testing"
+
+ apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
+ apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
+ spiValues "github.com/apache/plc4x/plc4go/spi/values"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestDefaultPlcReadResponse_GetRequest(t *testing.T) {
+ type fields struct {
+ request apiModel.PlcReadRequest
+ values map[string]*ResponseItem
+ }
+ tests := []struct {
+ name string
+ fields fields
+ want apiModel.PlcReadRequest
+ }{
+ {
+ name: "get it",
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ d := &DefaultPlcReadResponse{
+ request: tt.fields.request,
+ values: tt.fields.values,
+ }
+ assert.Equalf(t, tt.want, d.GetRequest(), "GetRequest()")
+ })
+ }
+}
+
+func TestDefaultPlcReadResponse_GetResponseCode(t *testing.T) {
+ type fields struct {
+ request apiModel.PlcReadRequest
+ values map[string]*ResponseItem
+ }
+ type args struct {
+ name string
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ want apiModel.PlcResponseCode
+ }{
+ {
+ name: "get it (not found)",
+ want: apiModel.PlcResponseCode_NOT_FOUND,
+ },
+ {
+ name: "get it",
+ fields: fields{
+ values: map[string]*ResponseItem{
+ "something": {
+ code: apiModel.PlcResponseCode_OK,
+ },
+ },
+ },
+ args: args{
+ name: "something",
+ },
+ want: apiModel.PlcResponseCode_OK,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ d := &DefaultPlcReadResponse{
+ request: tt.fields.request,
+ values: tt.fields.values,
+ }
+ assert.Equalf(t, tt.want, d.GetResponseCode(tt.args.name), "GetResponseCode(%v)", tt.args.name)
+ })
+ }
+}
+
+func TestDefaultPlcReadResponse_GetTagNames(t *testing.T) {
+ type fields struct {
+ request apiModel.PlcReadRequest
+ values map[string]*ResponseItem
+ }
+ tests := []struct {
+ name string
+ fields fields
+ want []string
+ }{
+ {
+ name: "get it",
+ fields: fields{
+ request: NewDefaultPlcReadRequest(nil, []string{"tag1", "tag2"}, nil, nil),
+ values: map[string]*ResponseItem{
+ "tag1": nil,
+ "tag2": nil,
+ },
+ },
+ want: []string{"tag1", "tag2"},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ d := &DefaultPlcReadResponse{
+ request: tt.fields.request,
+ values: tt.fields.values,
+ }
+ assert.Equalf(t, tt.want, d.GetTagNames(), "GetTagNames()")
+ })
+ }
+}
+
+func TestDefaultPlcReadResponse_GetValue(t *testing.T) {
+ type fields struct {
+ request apiModel.PlcReadRequest
+ values map[string]*ResponseItem
+ }
+ type args struct {
+ name string
+ }
+ tests := []struct {
+ name string
+ fields fields
+ args args
+ want apiValues.PlcValue
+ }{
+ {
+ name: "get it (not found)",
+ want: spiValues.PlcNull{},
+ },
+ {
+ name: "get it",
+ fields: fields{
+ values: map[string]*ResponseItem{
+ "something": {
+ code: apiModel.PlcResponseCode_OK,
+ value: spiValues.NewPlcSTRING("yes"),
+ },
+ },
+ },
+ args: args{
+ name: "something",
+ },
+ want: spiValues.NewPlcSTRING("yes"),
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ d := &DefaultPlcReadResponse{
+ request: tt.fields.request,
+ values: tt.fields.values,
+ }
+ assert.Equalf(t, tt.want, d.GetValue(tt.args.name), "GetValue(%v)", tt.args.name)
+ })
+ }
+}
+
+func TestDefaultPlcReadResponse_IsAPlcMessage(t *testing.T) {
+ type fields struct {
+ request apiModel.PlcReadRequest
+ values map[string]*ResponseItem
+ }
+ tests := []struct {
+ name string
+ fields fields
+ want bool
+ }{
+ {
+ name: "it is",
+ want: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ d := &DefaultPlcReadResponse{
+ request: tt.fields.request,
+ values: tt.fields.values,
+ }
+ assert.Equalf(t, tt.want, d.IsAPlcMessage(), "IsAPlcMessage()")
+ })
+ }
+}
+
+func TestNewDefaultPlcReadResponse(t *testing.T) {
+ type args struct {
+ request apiModel.PlcReadRequest
+ responseCodes map[string]apiModel.PlcResponseCode
+ values map[string]apiValues.PlcValue
+ }
+ tests := []struct {
+ name string
+ args args
+ want apiModel.PlcReadResponse
+ }{
+ {
+ name: "create it",
+ want: &DefaultPlcReadResponse{values: map[string]*ResponseItem{}},
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equalf(t, tt.want, NewDefaultPlcReadResponse(tt.args.request, tt.args.responseCodes, tt.args.values), "NewDefaultPlcReadResponse(%v, %v, %v)", tt.args.request, tt.args.responseCodes, tt.args.values)
+ })
+ }
+}
diff --git a/plc4go/spi/model/render_test.go b/plc4go/spi/model/render_test.go
index 1608cac319..4d6eb82895 100644
--- a/plc4go/spi/model/render_test.go
+++ b/plc4go/spi/model/render_test.go
@@ -30,7 +30,7 @@ import (
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
"github.com/apache/plc4x/plc4go/spi/utils"
- spiValue "github.com/apache/plc4x/plc4go/spi/values"
+ spiValues "github.com/apache/plc4x/plc4go/spi/values"
"github.com/stretchr/testify/assert"
)
@@ -120,8 +120,8 @@ func TestRenderTestCustom(t *testing.T) {
"tagid2": NewMockPlcBrowseItem(t),
},
map[string]apiValues.PlcValue{
- "tagid1": spiValue.PlcNull{},
- "tagid2": spiValue.PlcNull{},
+ "tagid1": spiValues.PlcNull{},
+ "tagid2": spiValues.PlcNull{},
"tagid3": nil,
},
).(interface { // TODO: workaround
@@ -182,7 +182,9 @@ func TestRenderTestCustom(t *testing.T) {
"something",
"something",
url.URL{},
- nil,
+ map[string][]string{
+ "something": {"else"},
+ },
"something",
nil,
).(interface { // TODO: workaround