github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/switch_deprecated_test.go (about)

     1  package processor
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/Jeffail/benthos/v3/lib/condition"
     9  	"github.com/Jeffail/benthos/v3/lib/log"
    10  	"github.com/Jeffail/benthos/v3/lib/message"
    11  	"github.com/Jeffail/benthos/v3/lib/metrics"
    12  )
    13  
    14  func TestSwitchDeprecatedCases(t *testing.T) {
    15  	conf := NewConfig()
    16  	conf.Type = "switch"
    17  
    18  	condConf := condition.NewConfig()
    19  	condConf.Type = condition.TypeText
    20  	condConf.Text.Operator = "contains_cs"
    21  	condConf.Text.Arg = "A"
    22  
    23  	procConf := NewConfig()
    24  	procConf.Type = TypeText
    25  	procConf.Text.Operator = "prepend"
    26  	procConf.Text.Value = "Hit case 0: "
    27  
    28  	conf.Switch = append(conf.Switch, SwitchCaseConfig{
    29  		Condition:   condConf,
    30  		Processors:  []Config{procConf},
    31  		Fallthrough: false,
    32  	})
    33  
    34  	condConf = condition.NewConfig()
    35  	condConf.Type = condition.TypeText
    36  	condConf.Text.Operator = "contains_cs"
    37  	condConf.Text.Arg = "B"
    38  
    39  	procConf = NewConfig()
    40  	procConf.Type = TypeText
    41  	procConf.Text.Operator = "prepend"
    42  	procConf.Text.Value = "Hit case 1: "
    43  
    44  	conf.Switch = append(conf.Switch, SwitchCaseConfig{
    45  		Condition:   condConf,
    46  		Processors:  []Config{procConf},
    47  		Fallthrough: true,
    48  	})
    49  
    50  	condConf = condition.NewConfig()
    51  	condConf.Type = condition.TypeText
    52  	condConf.Text.Operator = "contains_cs"
    53  	condConf.Text.Arg = "C"
    54  
    55  	procConf = NewConfig()
    56  	procConf.Type = TypeText
    57  	procConf.Text.Operator = "prepend"
    58  	procConf.Text.Value = "Hit case 2: "
    59  
    60  	conf.Switch = append(conf.Switch, SwitchCaseConfig{
    61  		Condition:   condConf,
    62  		Processors:  []Config{procConf},
    63  		Fallthrough: false,
    64  	})
    65  
    66  	condConf = condition.NewConfig()
    67  	condConf.Type = condition.TypeText
    68  	condConf.Text.Operator = "contains_cs"
    69  	condConf.Text.Arg = "D"
    70  
    71  	procConf = NewConfig()
    72  	procConf.Type = TypeText
    73  	procConf.Text.Operator = "prepend"
    74  	procConf.Text.Value = "Hit case 3: "
    75  
    76  	conf.Switch = append(conf.Switch, SwitchCaseConfig{
    77  		Condition:   condConf,
    78  		Processors:  []Config{procConf},
    79  		Fallthrough: false,
    80  	})
    81  
    82  	c, err := New(conf, nil, log.Noop(), metrics.Noop())
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	defer func() {
    87  		c.CloseAsync()
    88  		if err = c.WaitForClose(time.Second); err != nil {
    89  			t.Fatal(err)
    90  		}
    91  	}()
    92  
    93  	type testCase struct {
    94  		name     string
    95  		input    [][]byte
    96  		expected [][]byte
    97  	}
    98  	tests := []testCase{
    99  		{
   100  			name: "switch test 1",
   101  			input: [][]byte{
   102  				[]byte("A"),
   103  				[]byte("AB"),
   104  			},
   105  			expected: [][]byte{
   106  				[]byte("Hit case 0: A"),
   107  				[]byte("Hit case 0: AB"),
   108  			},
   109  		},
   110  		{
   111  			name: "switch test 2",
   112  			input: [][]byte{
   113  				[]byte("B"),
   114  				[]byte("BD"),
   115  			},
   116  			expected: [][]byte{
   117  				[]byte("Hit case 2: Hit case 1: B"),
   118  				[]byte("Hit case 2: Hit case 1: BD"),
   119  			},
   120  		},
   121  		{
   122  			name: "switch test 3",
   123  			input: [][]byte{
   124  				[]byte("C"),
   125  				[]byte("CD"),
   126  			},
   127  			expected: [][]byte{
   128  				[]byte("Hit case 2: C"),
   129  				[]byte("Hit case 2: CD"),
   130  			},
   131  		},
   132  		{
   133  			name: "switch test 4",
   134  			input: [][]byte{
   135  				[]byte("D"),
   136  			},
   137  			expected: [][]byte{
   138  				[]byte("Hit case 3: D"),
   139  			},
   140  		},
   141  	}
   142  
   143  	for _, test := range tests {
   144  		msg, res := c.ProcessMessage(message.New(test.input))
   145  		if res != nil {
   146  			t.Error(res.Error())
   147  			continue
   148  		}
   149  		if act, exp := message.GetAllBytes(msg[0]), test.expected; !reflect.DeepEqual(act, exp) {
   150  			t.Errorf("Wrong result for test '%s': %s != %s", test.name, act, exp)
   151  		}
   152  	}
   153  }