|
| 1 | +// Copyright 2023 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package builder |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/serverlessworkflow/sdk-go/v2/model" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func prepareBuilder() *model.WorkflowBuilder { |
| 25 | + builder := New().Key("key test").ID("id test") |
| 26 | + |
| 27 | + builder.AddFunctions().Name("function name").Operation("http://test") |
| 28 | + builder.AddFunctions().Name("function name2").Operation("http://test") |
| 29 | + |
| 30 | + function3 := builder.AddFunctions().Name("function name2").Operation("http://test") |
| 31 | + builder.RemoveFunctions(function3) |
| 32 | + |
| 33 | + state1 := builder.AddStates(). |
| 34 | + Name("state"). |
| 35 | + Type(model.StateTypeInject) |
| 36 | + state1.End().Terminate(true) |
| 37 | + |
| 38 | + inject := state1.InjectState() |
| 39 | + inject.Data(map[string]model.Object{ |
| 40 | + "test": model.FromMap(map[string]any{}), |
| 41 | + }) |
| 42 | + |
| 43 | + return builder |
| 44 | +} |
| 45 | + |
| 46 | +func TestObject(t *testing.T) { |
| 47 | + workflow, err := Object(prepareBuilder()) |
| 48 | + if assert.NoError(t, err) { |
| 49 | + assert.Equal(t, "key test", workflow.Key) |
| 50 | + assert.Equal(t, "id test", workflow.ID) |
| 51 | + assert.Equal(t, "0.8", workflow.SpecVersion) |
| 52 | + assert.Equal(t, "jq", workflow.ExpressionLang.String()) |
| 53 | + assert.Equal(t, 2, len(workflow.Functions)) |
| 54 | + |
| 55 | + assert.Equal(t, "function name", workflow.Functions[0].Name) |
| 56 | + assert.Equal(t, "function name2", workflow.Functions[1].Name) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestJson(t *testing.T) { |
| 61 | + data, err := Json(prepareBuilder()) |
| 62 | + if assert.NoError(t, err) { |
| 63 | + d := `{"id":"id test","key":"key test","version":"","specVersion":"0.8","expressionLang":"jq","states":[{"name":"state","type":"inject","end":{"terminate":true},"data":{"test":{}}}],"functions":[{"name":"function name","operation":"http://test","type":"rest"},{"name":"function name2","operation":"http://test","type":"rest"}]}` |
| 64 | + assert.Equal(t, d, string(data)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestYaml(t *testing.T) { |
| 69 | + data, err := Yaml(prepareBuilder()) |
| 70 | + if assert.NoError(t, err) { |
| 71 | + d := `expressionLang: jq |
| 72 | +functions: |
| 73 | +- name: function name |
| 74 | + operation: http://test |
| 75 | + type: rest |
| 76 | +- name: function name2 |
| 77 | + operation: http://test |
| 78 | + type: rest |
| 79 | +id: id test |
| 80 | +key: key test |
| 81 | +specVersion: "0.8" |
| 82 | +states: |
| 83 | +- data: |
| 84 | + test: {} |
| 85 | + end: |
| 86 | + terminate: true |
| 87 | + name: state |
| 88 | + type: inject |
| 89 | +version: "" |
| 90 | +` |
| 91 | + |
| 92 | + assert.Equal(t, d, string(data)) |
| 93 | + } |
| 94 | +} |
0 commit comments