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

     1  package processor
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  
     7  	"github.com/Jeffail/benthos/v3/lib/log"
     8  	"github.com/Jeffail/benthos/v3/lib/message"
     9  	"github.com/Jeffail/benthos/v3/lib/metrics"
    10  )
    11  
    12  func TestMergeJSON(t *testing.T) {
    13  	tLog := log.Noop()
    14  	tStats := metrics.Noop()
    15  
    16  	type jTest struct {
    17  		name   string
    18  		first  string
    19  		second string
    20  		output string
    21  	}
    22  
    23  	tests := []jTest{
    24  		{
    25  			name:   "object 1",
    26  			first:  `{"baz":{"foo":1}}`,
    27  			second: `{"baz":{"bar":5}}`,
    28  			output: `{"baz":{"bar":5,"foo":1}}`,
    29  		},
    30  		{
    31  			name:   "val to array 1",
    32  			first:  `{"baz":{"foo":3}}`,
    33  			second: `{"baz":{"foo":5}}`,
    34  			output: `{"baz":{"foo":[3,5]}}`,
    35  		},
    36  		{
    37  			name:   "array 1",
    38  			first:  `{"baz":{"foo":[1,2,3]}}`,
    39  			second: `{"baz":{"foo":5}}`,
    40  			output: `{"baz":{"foo":[1,2,3,5]}}`,
    41  		},
    42  	}
    43  
    44  	for _, test := range tests {
    45  		conf := NewConfig()
    46  		conf.MergeJSON.Parts = []int{}
    47  		conf.MergeJSON.RetainParts = false
    48  
    49  		jMrg, err := NewMergeJSON(conf, nil, tLog, tStats)
    50  		if err != nil {
    51  			t.Fatalf("Error for test '%v': %v", test.name, err)
    52  		}
    53  
    54  		inMsg := message.New(
    55  			[][]byte{
    56  				[]byte(test.first),
    57  				[]byte(test.second),
    58  			},
    59  		)
    60  		msgs, _ := jMrg.ProcessMessage(inMsg)
    61  		if len(msgs) != 1 {
    62  			t.Fatalf("Test '%v' did not succeed", test.name)
    63  		}
    64  
    65  		if exp, act := test.output, string(message.GetAllBytes(msgs[0])[0]); exp != act {
    66  			t.Errorf("Wrong result '%v': %v != %v", test.name, act, exp)
    67  		}
    68  	}
    69  }
    70  
    71  func TestMergeJSONRetention(t *testing.T) {
    72  	tLog := log.Noop()
    73  	tStats := metrics.Noop()
    74  
    75  	conf := NewConfig()
    76  	conf.MergeJSON.Parts = []int{}
    77  	conf.MergeJSON.RetainParts = true
    78  
    79  	jMrg, err := NewMergeJSON(conf, nil, tLog, tStats)
    80  	if err != nil {
    81  		t.Fatal(err)
    82  	}
    83  
    84  	input := message.New(
    85  		[][]byte{
    86  			[]byte(`{"foo":1}`),
    87  			[]byte(`{"foo":2}`),
    88  		},
    89  	)
    90  	expOutput := input.Copy()
    91  	expOutput.Append(message.NewPart([]byte(`{"foo":[1,2]}`)))
    92  	exp := message.GetAllBytes(expOutput)
    93  
    94  	msgs, _ := jMrg.ProcessMessage(input)
    95  	if len(msgs) != 1 {
    96  		t.Errorf("Wrong output count")
    97  	}
    98  	act := message.GetAllBytes(msgs[0])
    99  	if !reflect.DeepEqual(exp, act) {
   100  		t.Errorf("Wrong result: %s != %s", act, exp)
   101  	}
   102  
   103  	input = message.New(
   104  		[][]byte{
   105  			[]byte(`{"foo":1}`),
   106  		},
   107  	)
   108  	expOutput = input.Copy()
   109  	expOutput.Append(message.NewPart([]byte(`{"foo":1}`)))
   110  	exp = message.GetAllBytes(expOutput)
   111  
   112  	msgs, _ = jMrg.ProcessMessage(input)
   113  	if len(msgs) != 1 {
   114  		t.Errorf("Wrong output count")
   115  	}
   116  	act = message.GetAllBytes(msgs[0])
   117  	if !reflect.DeepEqual(exp, act) {
   118  		t.Errorf("Wrong result: %s != %s", act, exp)
   119  	}
   120  
   121  	conf = NewConfig()
   122  	conf.MergeJSON.Parts = []int{0, -1}
   123  	conf.MergeJSON.RetainParts = true
   124  
   125  	jMrg, err = NewMergeJSON(conf, nil, tLog, tStats)
   126  	if err != nil {
   127  		t.Fatal(err)
   128  	}
   129  
   130  	input = message.New(
   131  		[][]byte{
   132  			[]byte(`{"foo":1}`),
   133  			[]byte(`not related`),
   134  			[]byte(`{"foo":2}`),
   135  		},
   136  	)
   137  	expOutput = input.Copy()
   138  	expOutput.Append(message.NewPart([]byte(`{"foo":[1,2]}`)))
   139  	exp = message.GetAllBytes(expOutput)
   140  
   141  	msgs, _ = jMrg.ProcessMessage(input)
   142  	if len(msgs) != 1 {
   143  		t.Errorf("Wrong output count")
   144  	}
   145  	act = message.GetAllBytes(msgs[0])
   146  	if !reflect.DeepEqual(exp, act) {
   147  		t.Errorf("Wrong result: %s != %s", act, exp)
   148  	}
   149  
   150  	input = message.New(
   151  		[][]byte{
   152  			[]byte(`{"foo":1}`),
   153  		},
   154  	)
   155  	expOutput = input.Copy()
   156  	expOutput.Append(message.NewPart([]byte(`{"foo":1}`)))
   157  	exp = message.GetAllBytes(expOutput)
   158  
   159  	msgs, _ = jMrg.ProcessMessage(input)
   160  	if len(msgs) != 1 {
   161  		t.Errorf("Wrong output count")
   162  	}
   163  	act = message.GetAllBytes(msgs[0])
   164  	if !reflect.DeepEqual(exp, act) {
   165  		t.Errorf("Wrong result: %s != %s", act, exp)
   166  	}
   167  }
   168  
   169  func TestMergeJSONNoRetention(t *testing.T) {
   170  	tLog := log.Noop()
   171  	tStats := metrics.Noop()
   172  
   173  	conf := NewConfig()
   174  	conf.MergeJSON.Parts = []int{0, -1}
   175  	conf.MergeJSON.RetainParts = false
   176  
   177  	jMrg, err := NewMergeJSON(conf, nil, tLog, tStats)
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  
   182  	input := message.New(
   183  		[][]byte{
   184  			[]byte(`{"foo":1}`),
   185  			[]byte(`not related`),
   186  			[]byte(`{"foo":2}`),
   187  		},
   188  	)
   189  	input.Get(0).Metadata().Set("foo", "1")
   190  	input.Get(1).Metadata().Set("foo", "2")
   191  	input.Get(2).Metadata().Set("foo", "3")
   192  
   193  	expParts := [][]byte{
   194  		[]byte(`not related`),
   195  		[]byte(`{"foo":[1,2]}`),
   196  	}
   197  
   198  	msgs, _ := jMrg.ProcessMessage(input)
   199  	if len(msgs) != 1 {
   200  		t.Errorf("Wrong output count")
   201  	}
   202  	if act, exp := message.GetAllBytes(msgs[0]), expParts; !reflect.DeepEqual(exp, act) {
   203  		t.Errorf("Wrong result: %s != %s", act, exp)
   204  	}
   205  	if exp, act := "2", msgs[0].Get(0).Metadata().Get("foo"); exp != act {
   206  		t.Errorf("Wrong metadata: %v != %v", act, exp)
   207  	}
   208  	if exp, act := "1", msgs[0].Get(1).Metadata().Get("foo"); exp != act {
   209  		t.Errorf("Wrong metadata: %v != %v", act, exp)
   210  	}
   211  }