This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/master by this push:
     new 30e1d9f  Fix YAML rest-dsl inspections
30e1d9f is described below

commit 30e1d9f8313494e349c9b1a8c2c3ba1ed84c4328
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Sat Apr 4 15:47:23 2020 +0200

    Fix YAML rest-dsl inspections
---
 pkg/util/source/inspector_yaml.go      |  54 +++++++++--------
 pkg/util/source/inspector_yaml_test.go | 106 +++++++++++++++++++++++++++++++++
 2 files changed, 135 insertions(+), 25 deletions(-)

diff --git a/pkg/util/source/inspector_yaml.go 
b/pkg/util/source/inspector_yaml.go
index 8ddf8c3..d05b2e8 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 != "" {
diff --git a/pkg/util/source/inspector_yaml_test.go 
b/pkg/util/source/inspector_yaml_test.go
new file mode 100644
index 0000000..2d5db00
--- /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)
+               })
+       }
+}

Reply via email to