| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package xpdata |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
|
| 12 |
"go.opentelemetry.io/collector/pdata/pcommon" |
| 13 |
) |
| 14 |
|
| 15 |
func TestMarshalAndUnmarshalValue(t *testing.T) { |
| 16 |
for name, src := range genTestEncodingValues() { |
| 17 |
t.Run(name, func(t *testing.T) { |
| 18 |
m := &JSONMarshaler{} |
| 19 |
b, err := m.MarshalValue(src) |
| 20 |
require.NoError(t, err) |
| 21 |
|
| 22 |
u := &JSONUnmarshaler{} |
| 23 |
dest, err := u.UnmarshalValue(b) |
| 24 |
require.NoError(t, err) |
| 25 |
|
| 26 |
require.Equal(t, src, dest) |
| 27 |
}) |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
func TestUnmarshalValueUnknown(t *testing.T) { |
| 32 |
m := &JSONUnmarshaler{} |
| 33 |
|
| 34 |
b, err := m.UnmarshalValue([]byte(`{"unknown": "string"}`)) |
| 35 |
require.NoError(t, err) |
| 36 |
assert.Equal(t, pcommon.NewValueEmpty(), b) |
| 37 |
} |
| 38 |
|
| 39 |
func genTestEncodingValues() map[string]pcommon.Value { |
| 40 |
return map[string]pcommon.Value{ |
| 41 |
"empty": pcommon.NewValueEmpty(), |
| 42 |
"StringValue/default": pcommon.NewValueStr(""), |
| 43 |
"StringValue/test": pcommon.NewValueStr("test_stringvalue"), |
| 44 |
"BoolValue/default": pcommon.NewValueBool(false), |
| 45 |
"BoolValue/test": pcommon.NewValueBool(true), |
| 46 |
"IntValue/default": pcommon.NewValueInt(0), |
| 47 |
"IntValue/test": pcommon.NewValueInt(13), |
| 48 |
"DoubleValue/default": pcommon.NewValueDouble(0), |
| 49 |
"DoubleValue/test": pcommon.NewValueDouble(3.1415926), |
| 50 |
"ArrayValue/default": pcommon.NewValueSlice(), |
| 51 |
"ArrayValue/test": func() pcommon.Value { |
| 52 |
s := pcommon.NewValueSlice() |
| 53 |
s.Slice().AppendEmpty().SetStr("test1") |
| 54 |
s.Slice().AppendEmpty().SetInt(13) |
| 55 |
s.Slice().AppendEmpty().SetBool(true) |
| 56 |
s.Slice().AppendEmpty().SetDouble(3.1415926) |
| 57 |
|
| 58 |
s1 := s.Slice().AppendEmpty().SetEmptySlice() |
| 59 |
s1.AppendEmpty().SetStr("nested") |
| 60 |
|
| 61 |
m := s.Slice().AppendEmpty().SetEmptyMap() |
| 62 |
m.PutStr("key1", "value1") |
| 63 |
|
| 64 |
return s |
| 65 |
}(), |
| 66 |
"KvlistValue/default": pcommon.NewValueMap(), |
| 67 |
"KvlistValue/test": func() pcommon.Value { |
| 68 |
m := pcommon.NewValueMap() |
| 69 |
m.Map().PutStr("key1", "value1") |
| 70 |
m.Map().PutInt("key2", 13) |
| 71 |
m.Map().PutBool("key3", true) |
| 72 |
m.Map().PutDouble("key4", 3.1415926) |
| 73 |
m.Map().PutEmpty("key5").SetStr("value5") |
| 74 |
|
| 75 |
s := m.Map().PutEmptySlice("key6") |
| 76 |
s.AppendEmpty().SetStr("nested1") |
| 77 |
|
| 78 |
m1 := m.Map().PutEmptyMap("key6") |
| 79 |
m1.PutStr("nestedKey1", "nestedValue1") |
| 80 |
|
| 81 |
return m |
| 82 |
}(), |
| 83 |
"BytesValue/test": func() pcommon.Value { |
| 84 |
v := pcommon.NewValueBytes() |
| 85 |
v.Bytes().FromRaw([]byte{1, 2, 3}) |
| 86 |
return v |
| 87 |
}(), |
| 88 |
} |
| 89 |
} |