|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + |
| 12 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 13 | +) |
| 14 | + |
| 15 | +func Test_MD5(t *testing.T) { |
| 16 | + tests := []struct { |
| 17 | + name string |
| 18 | + value any |
| 19 | + expected any |
| 20 | + err bool |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "string", |
| 24 | + value: "hello world", |
| 25 | + expected: "5eb63bbbe01eeed093cb22bb8f5acdc3", |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "empty string", |
| 29 | + value: "", |
| 30 | + expected: "d41d8cd98f00b204e9800998ecf8427e", |
| 31 | + }, |
| 32 | + } |
| 33 | + for _, tt := range tests { |
| 34 | + t.Run(tt.name, func(t *testing.T) { |
| 35 | + exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{ |
| 36 | + Getter: func(context.Context, any) (any, error) { |
| 37 | + return tt.value, nil |
| 38 | + }, |
| 39 | + }) |
| 40 | + assert.NoError(t, err) |
| 41 | + result, err := exprFunc(nil, nil) |
| 42 | + if tt.err { |
| 43 | + assert.Error(t, err) |
| 44 | + } else { |
| 45 | + assert.NoError(t, err) |
| 46 | + } |
| 47 | + assert.Equal(t, tt.expected, result) |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func Test_MD5Error(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + value any |
| 56 | + err bool |
| 57 | + expectedError string |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "non-string", |
| 61 | + value: 10, |
| 62 | + expectedError: "expected string but got int", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "nil", |
| 66 | + value: nil, |
| 67 | + expectedError: "expected string but got nil", |
| 68 | + }, |
| 69 | + } |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + exprFunc, err := MD5HashString[any](&ottl.StandardStringGetter[any]{ |
| 73 | + Getter: func(context.Context, any) (any, error) { |
| 74 | + return tt.value, nil |
| 75 | + }, |
| 76 | + }) |
| 77 | + assert.NoError(t, err) |
| 78 | + _, err = exprFunc(nil, nil) |
| 79 | + assert.ErrorContains(t, err, tt.expectedError) |
| 80 | + }) |
| 81 | + } |
| 82 | +} |
0 commit comments