Repository: apache/camel-k
Pull request #1392 (2020-04-04 13:48:02)

Fix YAML rest-dsl inspections
<!-- Description -->




<!--
Enter your extended release note in the below block. If the PR requires
additional action from users switching to the new release, include the string
"action required". If no release note is required, write "NONE". 

You can (optionally) mark this PR with labels "kind/bug" or "kind/feature" to make sure
the text is added to the right section of the release notes. 
-->

**Release Note**
```release-note
NONE
```


Patch:
diff --git a/pkg/builder/builder_steps.go b/pkg/builder/builder_steps.go
index cef2dd6dd..e33597a81 100644
--- a/pkg/builder/builder_steps.go
+++ b/pkg/builder/builder_steps.go
@@ -289,15 +289,14 @@ func incrementalImageContext(ctx *Context) error {
 
 		bestImage, commonLibs := findBestImage(images, ctx.Artifacts)
 		if bestImage.Image != "" {
-			selectedArtifacts := make([]v1.Artifact, 0)
+			ctx.BaseImage = bestImage.Image
+			ctx.SelectedArtifacts = make([]v1.Artifact, 0)
+
 			for _, entry := range ctx.Artifacts {
 				if _, isCommon := commonLibs[entry.ID]; !isCommon {
-					selectedArtifacts = append(selectedArtifacts, entry)
+					ctx.SelectedArtifacts = append(ctx.SelectedArtifacts, entry)
 				}
 			}
-
-			ctx.BaseImage = bestImage.Image
-			ctx.SelectedArtifacts = selectedArtifacts
 		}
 
 		return nil
@@ -346,7 +345,7 @@ func imageContext(ctx *Context, selector artifactsSelector) error {
 	return nil
 }
 
-func listPublishedImages(context *Context) ([]publishedImage, error) {
+func listPublishedImages(context *Context) ([]v1.IntegrationKitStatus, error) {
 	options := []k8sclient.ListOption{
 		k8sclient.InNamespace(context.Namespace),
 		k8sclient.MatchingLabels{
@@ -364,7 +363,7 @@ func listPublishedImages(context *Context) ([]publishedImage, error) {
 		return nil, err
 	}
 
-	images := make([]publishedImage, 0)
+	images := make([]v1.IntegrationKitStatus, 0)
 	for _, item := range list.Items {
 		kit := item
 
@@ -372,17 +371,13 @@ func listPublishedImages(context *Context) ([]publishedImage, error) {
 			continue
 		}
 
-		images = append(images, publishedImage{
-			Image:        kit.Status.Image,
-			Artifacts:    kit.Status.Artifacts,
-			Dependencies: kit.Spec.Dependencies,
-		})
+		images = append(images, kit.Status)
 	}
 	return images, nil
 }
 
-func findBestImage(images []publishedImage, artifacts []v1.Artifact) (publishedImage, map[string]bool) {
-	var bestImage publishedImage
+func findBestImage(images []v1.IntegrationKitStatus, artifacts []v1.Artifact) (v1.IntegrationKitStatus, map[string]bool) {
+	var bestImage v1.IntegrationKitStatus
 
 	if len(images) == 0 {
 		return bestImage, nil
diff --git a/pkg/builder/builder_types.go b/pkg/builder/builder_types.go
index d7bdda90d..529a78a1b 100644
--- a/pkg/builder/builder_types.go
+++ b/pkg/builder/builder_types.go
@@ -122,9 +122,3 @@ type Context struct {
 func (c *Context) HasRequiredImage() bool {
 	return c.Build.Image != ""
 }
-
-type publishedImage struct {
-	Image        string
-	Artifacts    []v1.Artifact
-	Dependencies []string
-}
diff --git a/pkg/util/source/inspector_yaml.go b/pkg/util/source/inspector_yaml.go
index 8ddf8c3b2..d05b2e823 100644
--- a/pkg/util/source/inspector_yaml.go
+++ b/pkg/util/source/inspector_yaml.go
@@ -70,31 +70,11 @@ func (inspector YAMLInspector) parseStep(key string, content interface{}, meta *
 	case string:
 		maybeURI = t
 	case map[interface{}]interface{}:
-		if u, ok := t["uri"]; ok {
-			maybeURI = u.(string)
-		}
-
-		if _, ok := t["language"]; ok {
-			if s, ok := t["language"].(string); ok {
-				if dependency, ok := inspector.catalog.GetLanguageDependency(s); ok {
-					inspector.addDependency(dependency, meta)
-				}
-			} else if m, ok := t["language"].(map[interface{}]interface{}); ok {
-				if err := inspector.parseStep("language", m, meta); err != nil {
-					return err
-				}
-			}
-		}
-
-		for k := range t {
-			if s, ok := k.(string); ok {
-				if dependency, ok := inspector.catalog.GetLanguageDependency(s); ok {
-					inspector.addDependency(dependency, meta)
-				}
-			}
-		}
-
-		if u, ok := t["steps"]; ok {
+		if u, ok := t["rest"]; ok {
+			return inspector.parseStep("rest", u, meta)
+		} else if u, ok := t["from"]; ok {
+			return inspector.parseStep("from", u, meta)
+		} else if u, ok := t["steps"]; ok {
 			steps := u.([]interface{})
 
 			for i := range steps {
@@ -120,6 +100,30 @@ func (inspector YAMLInspector) parseStep(key string, content interface{}, meta *
 				}
 			}
 		}
+
+		if u, ok := t["uri"]; ok {
+			maybeURI = u.(string)
+		}
+
+		if _, ok := t["language"]; ok {
+			if s, ok := t["language"].(string); ok {
+				if dependency, ok := inspector.catalog.GetLanguageDependency(s); ok {
+					inspector.addDependency(dependency, meta)
+				}
+			} else if m, ok := t["language"].(map[interface{}]interface{}); ok {
+				if err := inspector.parseStep("language", m, meta); err != nil {
+					return err
+				}
+			}
+		}
+
+		for k := range t {
+			if s, ok := k.(string); ok {
+				if dependency, ok := inspector.catalog.GetLanguageDependency(s); ok {
+					inspector.addDependency(dependency, meta)
+				}
+			}
+		}
 	}
 
 	if maybeURI != "" {


Test patch:
diff --git a/pkg/util/source/inspector_yaml_test.go b/pkg/util/source/inspector_yaml_test.go
new file mode 100644
index 000000000..2d5db00ea
--- /dev/null
+++ b/pkg/util/source/inspector_yaml_test.go
@@ -0,0 +1,106 @@
+/*
+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 source
+
+import (
+	"testing"
+
+	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/camel"
+	"github.com/stretchr/testify/assert"
+)
+
+func NewtestYAMLInspector(t *testing.T) YAMLInspector {
+	catalog, err := camel.DefaultCatalog()
+	assert.Nil(t, err)
+
+	return YAMLInspector{
+		baseInspector: baseInspector{
+			catalog: catalog,
+		},
+	}
+}
+
+const YAMLRestDSL = `
+- rest:
+    verb: "post"
+    uri: "/api/flow"
+    accepts: "text/plain"
+    binding-mode: "off"
+    steps:
+      - convert-body:
+          type: "java.lang.String"
+      - to:
+          uri: "log:in"
+      - filter:
+          simple: "${body.contains(\",\")}"
+      - split:
+          tokenize: ";"
+      - set-body:
+          simple: "${body.toLowerCase()}"
+      - to:
+          uri: "log:out"
+`
+
+const YAMLRestDSLWithRoute = `
+- route:
+    id: "flow"
+    group: "routes"
+    rest:
+      verb: "post"
+      uri: "/api/flow"
+      accepts: "text/plain"
+      binding-mode: "off"
+      steps:
+        - convert-body:
+            type: "java.lang.String"
+        - to:
+            uri: "log:in"
+        - filter:
+            simple: "${body.contains(\",\")}"
+        - split:
+            tokenize: ";"
+        - set-body:
+            simple: "${body.toLowerCase()}"
+        - to:
+            uri: "log:out"
+`
+
+func TestYAMLRestDSL(t *testing.T) {
+	for name, content := range map[string]string{"YAMLRestDSL": YAMLRestDSL, "YAMLRestDSLWithRoute": YAMLRestDSLWithRoute} {
+		sourceContent := content
+		t.Run(name, func(t *testing.T) {
+			code := v1.SourceSpec{
+				DataSpec: v1.DataSpec{
+					Name:    "route.yaml",
+					Content: sourceContent,
+				},
+				Language: v1.LanguageYaml,
+			}
+
+			meta := NewMetadata()
+			inspector := NewtestYAMLInspector(t)
+
+			err := inspector.Extract(code, &meta)
+			assert.Nil(t, err)
+			assert.True(t, meta.RequiredCapabilities.Has(v1.CapabilityRest))
+			assert.True(t, meta.Dependencies.Has("camel:log"))
+			assert.True(t, meta.ExposesHTTPServices)
+		})
+	}
+}