From ce5f9ed209358d86304cc1993bef34986a132c73 Mon Sep 17 00:00:00 2001 From: realMartinez Date: Mon, 15 Apr 2024 12:46:26 +0200 Subject: [PATCH] feat/expanded route parsing capabilities --- pkg/util/camel/camel_runtime_catalog.go | 22 ++++++++++++++--- pkg/util/camel/camel_runtime_catalog_test.go | 26 +++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/pkg/util/camel/camel_runtime_catalog.go b/pkg/util/camel/camel_runtime_catalog.go index 00ed0101eb..37255f9717 100644 --- a/pkg/util/camel/camel_runtime_catalog.go +++ b/pkg/util/camel/camel_runtime_catalog.go @@ -183,10 +183,26 @@ func (c *RuntimeCatalog) VisitSchemes(visitor func(string, v1.CamelScheme) bool) // DecodeComponent parses the given URI and return a camel artifact and a scheme. func (c *RuntimeCatalog) DecodeComponent(uri string) (*v1.CamelArtifact, *v1.CamelScheme) { - uriSplit := strings.SplitN(uri, ":", 2) - if len(uriSplit) < 2 { - return nil, nil + + var uriSplit []string + + // Decode URI using formats http://my-site/test?param=value or log:info + if strings.Contains(uri, ":") { + uriSplit = strings.SplitN(uri, ":", 2) + if len(uriSplit) < 2 { + return nil, nil + } + } else { + if strings.Contains(uri, "?") { + uriSplit = strings.SplitN(uri, "?", 2) + if len(uriSplit) < 2 { + return nil, nil + } + } else { + uriSplit = append(uriSplit, uri) + } } + uriStart := uriSplit[0] var schemeRef *v1.CamelScheme if scheme, ok := c.GetScheme(uriStart); ok { diff --git a/pkg/util/camel/camel_runtime_catalog_test.go b/pkg/util/camel/camel_runtime_catalog_test.go index 02639a293b..fef51fd0db 100644 --- a/pkg/util/camel/camel_runtime_catalog_test.go +++ b/pkg/util/camel/camel_runtime_catalog_test.go @@ -57,11 +57,12 @@ func TestIsResolvable(t *testing.T) { expected bool }{ // static dependencies + {desc: "Basic static dependency single component", uri: "log", expected: true}, {desc: "Basic static dependency", uri: "log:info", expected: true}, {desc: "Basic static dependency with path and param", uri: "http://my-site/test?param=value", expected: true}, {desc: "Basic static dependency with path and param placeholder", uri: "http://my-site/test?{{params}}", expected: true}, {desc: "Basic static dependency with path placeholder and param", uri: "http://my-site/{{path}}?key=val", expected: true}, - + {desc: "Basic static dependency with path placeholder and name", uri: "direct?name=val", expected: true}, // placeholders {desc: "Basic", uri: "{{url}}", expected: false}, {desc: "With query param placeholder", uri: "{{url}}?authMethod={{authMethod}}", expected: false}, @@ -81,3 +82,26 @@ func TestIsResolvable(t *testing.T) { }) } } + +func TestDecodeComponent(t *testing.T) { + catalog, err := DefaultCatalog() + require.NoError(t, err) + + testCases := []struct { + desc string + uri string + expectedID string + }{ + {desc: "Basic static dependency", uri: "direct", expectedID: "direct"}, + {desc: "Basic static dependency", uri: "log:info", expectedID: "log"}, + {desc: "Basic static dependency witch path and name", uri: "direct?name=route", expectedID: "direct"}, + {desc: "Basic static dependency with path and param placeholder", uri: "http://my-site/test?{{params}}", expectedID: "http"}, + } + for _, testCase := range testCases { + t.Run(testCase.desc, func(t *testing.T) { + if _, gotScheme := catalog.DecodeComponent(testCase.uri); gotScheme.ID != testCase.expectedID { + t.Errorf("DecodeComponent(%v) = %v, want %v", testCase.uri, gotScheme.ID, testCase.expectedID) + } + }) + } +}