-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Add AddElementXML Converter
- Loading branch information
1 parent
5fc4370
commit 2fe7e84
Showing
7 changed files
with
219 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: 'enhancement' | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add AddElementXML Converter | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35436] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_AddElementXML(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
document string | ||
xPath string | ||
elementName string | ||
want string | ||
expectErr string | ||
}{ | ||
{ | ||
name: "add single element", | ||
document: `<a></a>`, | ||
xPath: "/a", | ||
elementName: "b", | ||
want: `<a><b></b></a>`, | ||
}, | ||
{ | ||
name: "add to multiple elements", | ||
document: `<a></a><a></a>`, | ||
xPath: "/a", | ||
elementName: "b", | ||
want: `<a><b></b></a><a><b></b></a>`, | ||
}, | ||
{ | ||
name: "add at multiple levels", | ||
document: `<a></a><b><a></a></b>`, | ||
xPath: "//a", | ||
elementName: "c", | ||
want: `<a><c></c></a><b><a><c></c></a></b>`, | ||
}, | ||
{ | ||
name: "add root element to empty document", | ||
document: ``, | ||
xPath: "/", | ||
elementName: "a", | ||
want: `<a></a>`, | ||
}, | ||
{ | ||
name: "add root element to non-empty document", | ||
document: `<a></a>`, | ||
xPath: "/", | ||
elementName: "a", | ||
want: `<a></a><a></a>`, | ||
}, | ||
{ | ||
name: "err on attribute", | ||
document: `<a foo="bar"></a>`, | ||
xPath: "/a/@foo", | ||
elementName: "b", | ||
want: `<a foo="bar"></a>`, | ||
expectErr: `AddElementXML XPath selected non-element: "foo"`, | ||
}, | ||
{ | ||
name: "err on text content", | ||
document: `<a>foo</a>`, | ||
xPath: "/a/text()", | ||
elementName: "b", | ||
want: `<a>foo</a>`, | ||
expectErr: `AddElementXML XPath selected non-element: "foo"`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
f := NewAddElementXMLFactory[any]() | ||
exprFunc, err := f.CreateFunction( | ||
ottl.FunctionContext{}, | ||
&AddElementXMLArguments[any]{ | ||
Target: ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return tt.document, nil | ||
}, | ||
}, | ||
XPath: tt.xPath, | ||
Name: tt.elementName, | ||
}) | ||
assert.NoError(t, err) | ||
|
||
result, err := exprFunc(context.Background(), nil) | ||
if tt.expectErr == "" { | ||
assert.NoError(t, err) | ||
} else { | ||
assert.EqualError(t, err, tt.expectErr) | ||
} | ||
assert.Equal(t, tt.want, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateAddElementXMLFunc(t *testing.T) { | ||
factory := NewAddElementXMLFactory[any]() | ||
fCtx := ottl.FunctionContext{} | ||
|
||
// Invalid arg type | ||
exprFunc, err := factory.CreateFunction(fCtx, nil) | ||
assert.Error(t, err) | ||
assert.Nil(t, exprFunc) | ||
|
||
// Invalid XPath should error on function creation | ||
exprFunc, err = factory.CreateFunction( | ||
fCtx, &AddElementXMLArguments[any]{ | ||
XPath: "!", | ||
Name: "foo", | ||
}) | ||
assert.Error(t, err) | ||
assert.Nil(t, exprFunc) | ||
|
||
// Empty Name should error on function creation | ||
exprFunc, err = factory.CreateFunction( | ||
fCtx, &AddElementXMLArguments[any]{ | ||
XPath: "/", | ||
}) | ||
assert.Error(t, err) | ||
assert.Nil(t, exprFunc) | ||
|
||
// Invalid XML should error on function execution | ||
exprFunc, err = factory.CreateFunction( | ||
fCtx, &AddElementXMLArguments[any]{ | ||
Target: invalidXMLGetter(), | ||
XPath: "/", | ||
Name: "foo", | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exprFunc) | ||
_, err = exprFunc(context.Background(), nil) | ||
assert.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters