Skip to content

Commit

Permalink
chore: Use camel case for Pipe error handler ref
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Mar 20, 2024
1 parent d891a83 commit 0b64ac1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pkg/apis/camel/v1/integration_types_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ func (in *IntegrationSpec) AddDependency(dependency string) {
in.Dependencies = append(in.Dependencies, dependency)
}

// AddConfigurationProperty adds a new configuration property.
func (in *IntegrationSpec) AddConfigurationProperty(confValue string) {
in.AddConfiguration("property", confValue)
}

// GetConfigurationProperty returns a configuration property.
func (in *IntegrationSpec) GetConfigurationProperty(property string) string {
for _, confSpec := range in.Configuration {
Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/pipe/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,7 @@ func configureBinding(integration *v1.Integration, bindings ...*bindings.Binding
return err
}

integration.Spec.Configuration = append(integration.Spec.Configuration, v1.ConfigurationSpec{
Type: "property",
Value: entry,
})
integration.Spec.AddConfigurationProperty(entry)
}

}
Expand Down
50 changes: 50 additions & 0 deletions pkg/controller/pipe/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,56 @@ func TestCreateIntegrationForPipe(t *testing.T) {
assert.Equal(t, expectedNominalRoute(), string(dsl))
}

func TestCreateIntegrationForPipeWithSinkErrorHandler(t *testing.T) {
client, err := test.NewFakeClient()
require.NoError(t, err)

pipe := nominalPipe("my-error-handler-pipe")
pipe.Spec.ErrorHandler = &v1.ErrorHandlerSpec{
RawMessage: []byte(`{"sink": {"endpoint": {"uri": "someUri"}}}`),
}

it, err := CreateIntegrationFor(context.TODO(), client, &pipe)
require.NoError(t, err)
assert.Equal(t, "my-error-handler-pipe", it.Name)
assert.Equal(t, "default", it.Namespace)
assert.Equal(t, "camel.apache.org/v1", it.OwnerReferences[0].APIVersion)
assert.Equal(t, "Pipe", it.OwnerReferences[0].Kind)
assert.Equal(t, "my-error-handler-pipe", it.OwnerReferences[0].Name)
assert.Len(t, it.Spec.Configuration, 3)
assert.Equal(t, "#class:org.apache.camel.builder.DeadLetterChannelBuilder", it.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler"))
assert.Equal(t, "someUri", it.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler.deadLetterUri"))
assert.Equal(t, "defaultErrorHandler", it.Spec.GetConfigurationProperty(v1.ErrorHandlerRefName))
dsl, err := dsl.ToYamlDSL(it.Spec.Flows)
require.NoError(t, err)
assert.Equal(t, expectedNominalRoute(), string(dsl))
}

func TestCreateIntegrationForPipeWithLogErrorHandler(t *testing.T) {
client, err := test.NewFakeClient()
require.NoError(t, err)

pipe := nominalPipe("my-error-handler-pipe")
pipe.Spec.ErrorHandler = &v1.ErrorHandlerSpec{
RawMessage: []byte(`{"log": {"parameters": {"showHeaders": "true"}}}`),
}

it, err := CreateIntegrationFor(context.TODO(), client, &pipe)
require.NoError(t, err)
assert.Equal(t, "my-error-handler-pipe", it.Name)
assert.Equal(t, "default", it.Namespace)
assert.Equal(t, "camel.apache.org/v1", it.OwnerReferences[0].APIVersion)
assert.Equal(t, "Pipe", it.OwnerReferences[0].Kind)
assert.Equal(t, "my-error-handler-pipe", it.OwnerReferences[0].Name)
assert.Len(t, it.Spec.Configuration, 3)
assert.Equal(t, "#class:org.apache.camel.builder.DefaultErrorHandlerBuilder", it.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler"))
assert.Equal(t, "true", it.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler.showHeaders"))
assert.Equal(t, "defaultErrorHandler", it.Spec.GetConfigurationProperty(v1.ErrorHandlerRefName))
dsl, err := dsl.ToYamlDSL(it.Spec.Flows)
require.NoError(t, err)
assert.Equal(t, expectedNominalRoute(), string(dsl))
}

func TestCreateIntegrationForPipeDataType(t *testing.T) {
client, err := test.NewFakeClient()
require.NoError(t, err)
Expand Down
5 changes: 2 additions & 3 deletions pkg/trait/error_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"

"github.com/apache/camel-k/v2/pkg/util"
)

Expand Down Expand Up @@ -86,8 +85,8 @@ func (t *errorHandlerTrait) addErrorHandlerDependencies(e *Environment, uri stri

func (t *errorHandlerTrait) addGlobalErrorHandlerAsSource(e *Environment) error {
flowErrorHandler := map[string]interface{}{
"error-handler": map[string]string{
"ref-error-handler": t.ErrorHandlerRef,
"errorHandler": map[string]string{
"refErrorHandler": t.ErrorHandlerRef,
},
}
encodedFlowErrorHandler, err := yaml.Marshal([]map[string]interface{}{flowErrorHandler})
Expand Down
17 changes: 10 additions & 7 deletions pkg/trait/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/stretchr/testify/require"

v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"

"github.com/apache/camel-k/v2/pkg/util/camel"
)

Expand All @@ -34,7 +33,7 @@ func TestErrorHandlerConfigureFromIntegrationProperty(t *testing.T) {
Catalog: NewEnvironmentTestCatalog(),
Integration: &v1.Integration{},
}
e.Integration.Spec.AddConfiguration("property", fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))
e.Integration.Spec.AddConfigurationProperty(fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))

trait := newErrorHandlerTrait()
enabled, condition, err := trait.Configure(e)
Expand Down Expand Up @@ -72,8 +71,8 @@ func TestErrorHandlerApplySource(t *testing.T) {

err = trait.Apply(e)
require.NoError(t, err)
assert.Equal(t, `- error-handler:
ref-error-handler: defaultErrorHandler
assert.Equal(t, `- errorHandler:
refErrorHandler: defaultErrorHandler
`, e.Integration.Status.GeneratedSources[0].Content)
}

Expand All @@ -85,9 +84,9 @@ func TestErrorHandlerApplyDependency(t *testing.T) {
CamelCatalog: c,
Integration: &v1.Integration{},
}
e.Integration.Spec.AddConfiguration("property", "camel.beans.defaultErrorHandler = #class:org.apache.camel.builder.DeadLetterChannelBuilder")
e.Integration.Spec.AddConfiguration("property", "camel.beans.defaultErrorHandler.deadLetterUri = log:info")
e.Integration.Spec.AddConfiguration("property", fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))
e.Integration.Spec.AddConfigurationProperty("camel.beans.defaultErrorHandler = #class:org.apache.camel.builder.DeadLetterChannelBuilder")
e.Integration.Spec.AddConfigurationProperty("camel.beans.defaultErrorHandler.deadLetterUri = log:info")
e.Integration.Spec.AddConfigurationProperty(fmt.Sprintf("%v = %s", v1.ErrorHandlerRefName, "defaultErrorHandler"))
e.Integration.Status.Phase = v1.IntegrationPhaseInitialization

trait := newErrorHandlerTrait()
Expand All @@ -98,5 +97,9 @@ func TestErrorHandlerApplyDependency(t *testing.T) {

err = trait.Apply(e)
require.NoError(t, err)
assert.Len(t, e.Integration.Spec.Configuration, 3)
assert.Equal(t, "#class:org.apache.camel.builder.DeadLetterChannelBuilder", e.Integration.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler"))
assert.Equal(t, "log:info", e.Integration.Spec.GetConfigurationProperty("camel.beans.defaultErrorHandler.deadLetterUri"))
assert.Equal(t, "defaultErrorHandler", e.Integration.Spec.GetConfigurationProperty(v1.ErrorHandlerRefName))
assert.Equal(t, "camel:log", e.Integration.Status.Dependencies[0])
}

0 comments on commit 0b64ac1

Please sign in to comment.