diff --git a/sparkplug_plugin/sparkplug_b_input_bytes_test.go b/sparkplug_plugin/sparkplug_b_input_bytes_test.go new file mode 100644 index 0000000..f22c316 --- /dev/null +++ b/sparkplug_plugin/sparkplug_b_input_bytes_test.go @@ -0,0 +1,59 @@ +// Copyright 2026 UMH Systems GmbH +// +// Licensed 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 +// +// http://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 sparkplug_plugin + +import ( + "encoding/json" + "testing" + + sparkplugb "github.com/united-manufacturing-hub/benthos-umh/sparkplug_plugin/sparkplugb" +) + +func TestCreateMessageFromMetricProcessesByteArray(t *testing.T) { + input := NewSparkplugInputForTesting().input + byteValue := []byte{0x00, 0x01, 0xff} + metricName := "payload" + datatype := uint32(17) + metric := &sparkplugb.Payload_Metric{ + Name: &metricName, + Datatype: &datatype, + Value: &sparkplugb.Payload_Metric_BytesValue{BytesValue: byteValue}, + } + topicInfo := &TopicInfo{Group: "Factory", EdgeNode: "Line1"} + + msg := input.createMessageFromMetric(metric, &sparkplugb.Payload{}, MessageTypeNDATA, topicInfo, "spBv1.0/Factory/NDATA/Line1", 0, 1) + if msg == nil { + t.Fatal("expected byte-array metric to produce a message") + } + + bodyBytes, err := msg.AsBytes() + if err != nil { + t.Fatalf("read message body: %v", err) + } + var body map[string]any + if err := json.Unmarshal(bodyBytes, &body); err != nil { + t.Fatalf("decode message body: %v", err) + } + if got, want := body["value"], "AAH/"; got != want { + t.Errorf("byte-array value = %v, want base64 %q", got, want) + } + + if got, exists := msg.MetaGet("spb_datatype"); !exists || got != "Bytes" { + t.Errorf("spb_datatype = %q (exists %t), want Bytes", got, exists) + } + if got, exists := msg.MetaGet("umh_conversion_status"); !exists || got != "success" { + t.Errorf("umh_conversion_status = %q (exists %t), want success", got, exists) + } +} \ No newline at end of file