Repository: apache/camel-k (go)

Issue:
Dependencies & rest-dsl traits are never enabled
Follow up investigation to my [observation](https://github.com/apache/camel-k/pull/805#discussion_r300453032) in #805.

The cause of the problem is that the dependencies trait is never enabled. It checks for `e.IntegrationInPhase("")`, but since daf9c0fa003a80f69bbae3724fee8e642f2a20e4, the initial phase seen by the trait is `Initialization`.

The same issue is true for the rest-dsl trait.

Pull request description:
fix: Dependencies & rest-dsl traits are never enabled
fixes #807

Patch:
diff --git a/pkg/trait/dependencies.go b/pkg/trait/dependencies.go
index 5a0ad2588..d12d777d2 100644
--- a/pkg/trait/dependencies.go
+++ b/pkg/trait/dependencies.go
@@ -40,7 +40,7 @@ func (t *dependenciesTrait) Configure(e *Environment) (bool, error) {
 		return false, nil
 	}
 
-	return e.IntegrationInPhase(""), nil
+	return e.IntegrationInPhase(v1alpha1.IntegrationPhaseInitialization), nil
 }
 
 func (t *dependenciesTrait) Apply(e *Environment) error {
diff --git a/pkg/trait/rest-dsl.go b/pkg/trait/rest-dsl.go
index ab1789d27..ef6b22bf9 100644
--- a/pkg/trait/rest-dsl.go
+++ b/pkg/trait/rest-dsl.go
@@ -60,7 +60,7 @@ func (t *restDslTrait) Configure(e *Environment) (bool, error) {
 
 	for _, resource := range e.Integration.Spec.Resources {
 		if resource.Type == v1alpha1.ResourceTypeOpenAPI {
-			return e.IntegrationInPhase(""), nil
+			return e.IntegrationInPhase(v1alpha1.IntegrationPhaseInitialization), nil
 		}
 	}
 
diff --git a/pkg/util/kubernetes/util.go b/pkg/util/kubernetes/util.go
index b55b56d34..7f1df15af 100644
--- a/pkg/util/kubernetes/util.go
+++ b/pkg/util/kubernetes/util.go
@@ -286,7 +286,7 @@ func GetConfigMapRefValue(ctx context.Context, client client.Client, namespace s
 // ResolveValueSource --
 func ResolveValueSource(ctx context.Context, client client.Client, namespace string, valueSource *v1alpha1.ValueSource) (string, error) {
 	if valueSource.ConfigMapKeyRef != nil && valueSource.SecretKeyRef != nil {
-		return "", fmt.Errorf("value source has bot config map and secret configuired")
+		return "", fmt.Errorf("value source has bot config map and secret configured")
 	}
 	if valueSource.ConfigMapKeyRef != nil {
 		return GetConfigMapRefValue(ctx, client, namespace, valueSource.ConfigMapKeyRef)


Test patch:
diff --git a/pkg/trait/dependencies_test.go b/pkg/trait/dependencies_test.go
new file mode 100644
index 000000000..774c17b6e
--- /dev/null
+++ b/pkg/trait/dependencies_test.go
@@ -0,0 +1,121 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"testing"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+	"github.com/apache/camel-k/pkg/util/test"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestDependenciesTraitApplicability(t *testing.T) {
+	e := &Environment{
+		Integration: &v1alpha1.Integration{},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseNone
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseInitialization
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+}
+
+func TestIntegrationDefaultDeps(t *testing.T) {
+	catalog, err := test.DefaultCatalog()
+	assert.Nil(t, err)
+
+	e := &Environment{
+		CamelCatalog: catalog,
+		Integration: &v1alpha1.Integration{
+			Spec: v1alpha1.IntegrationSpec{
+				Sources: []v1alpha1.SourceSpec{
+					{
+						DataSpec: v1alpha1.DataSpec{
+							Name:    "Request.java",
+							Content: `from("direct:foo").to("log:bar");`,
+						},
+						Language: v1alpha1.LanguageJavaSource,
+					},
+				},
+			},
+			Status: v1alpha1.IntegrationStatus{
+				Phase: v1alpha1.IntegrationPhaseInitialization,
+			},
+		},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+
+	err = trait.Apply(e)
+	assert.Nil(t, err)
+	assert.ElementsMatch(t, []string{"camel:core", "camel:direct", "camel:log", "runtime:jvm"}, e.Integration.Status.Dependencies)
+}
+
+func TestIntegrationCustomDeps(t *testing.T) {
+	catalog, err := test.DefaultCatalog()
+	assert.Nil(t, err)
+
+	e := &Environment{
+		CamelCatalog: catalog,
+		Integration: &v1alpha1.Integration{
+			Spec: v1alpha1.IntegrationSpec{
+				Dependencies: []string{
+					"camel:undertow",
+					"org.foo:bar",
+				},
+				Sources: []v1alpha1.SourceSpec{
+					{
+						DataSpec: v1alpha1.DataSpec{
+							Name:    "Request.java",
+							Content: `from("direct:foo").to("log:bar");`,
+						},
+						Language: v1alpha1.LanguageJavaSource,
+					},
+				},
+			},
+			Status: v1alpha1.IntegrationStatus{
+				Phase: v1alpha1.IntegrationPhaseInitialization,
+			},
+		},
+	}
+
+	trait := newDependenciesTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+
+	err = trait.Apply(e)
+	assert.Nil(t, err)
+	assert.ElementsMatch(t, []string{"camel:core", "camel:direct", "camel:log",
+		"camel:undertow", "org.foo:bar", "runtime:jvm"}, e.Integration.Status.Dependencies)
+}
diff --git a/pkg/trait/rest-dsl_test.go b/pkg/trait/rest-dsl_test.go
new file mode 100644
index 000000000..e63cd94e8
--- /dev/null
+++ b/pkg/trait/rest-dsl_test.go
@@ -0,0 +1,57 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements.  See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License.  You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package trait
+
+import (
+	"testing"
+
+	"github.com/apache/camel-k/pkg/apis/camel/v1alpha1"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestRestDslTraitApplicability(t *testing.T) {
+	e := &Environment{}
+
+	trait := newRestDslTrait()
+	enabled, err := trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration = &v1alpha1.Integration{
+		Status: v1alpha1.IntegrationStatus{
+			Phase: v1alpha1.IntegrationPhaseNone,
+		},
+	}
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	resource := v1alpha1.ResourceSpec{
+		Type: v1alpha1.ResourceTypeOpenAPI,
+	}
+	e.Integration.Spec.Resources = append(e.Integration.Spec.Resources, resource)
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.False(t, enabled)
+
+	e.Integration.Status.Phase = v1alpha1.IntegrationPhaseInitialization
+	enabled, err = trait.Configure(e)
+	assert.Nil(t, err)
+	assert.True(t, enabled)
+}