github.com/Jeffail/benthos/v3@v3.65.0/lib/processor/process_map_test.go (about) 1 package processor 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/Jeffail/benthos/v3/lib/condition" 8 "github.com/Jeffail/benthos/v3/lib/log" 9 "github.com/Jeffail/benthos/v3/lib/message" 10 "github.com/Jeffail/benthos/v3/lib/metrics" 11 ) 12 13 func TestProcessMapParts(t *testing.T) { 14 conf := NewConfig() 15 conf.Type = "process_map" 16 conf.ProcessMap.Premap["."] = "foo" 17 conf.ProcessMap.Postmap["foo.baz"] = "baz" 18 conf.ProcessMap.Parts = []int{1} 19 20 procConf := NewConfig() 21 procConf.Type = "json" 22 procConf.JSON.Operator = "select" 23 procConf.JSON.Path = "bar" 24 25 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 26 27 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 28 if err != nil { 29 t.Error(err) 30 return 31 } 32 33 exp := [][]byte{ 34 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 35 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 36 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 37 } 38 39 msg, res := c.ProcessMessage(message.New([][]byte{ 40 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 41 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 42 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 43 })) 44 if res != nil { 45 t.Error(res.Error()) 46 } 47 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 48 t.Errorf("Wrong result: %s != %s", act, exp) 49 } 50 } 51 52 func TestProcessMapConditions(t *testing.T) { 53 conf := NewConfig() 54 conf.Type = "process_map" 55 conf.ProcessMap.Premap["."] = "foo" 56 conf.ProcessMap.Postmap["foo.baz"] = "baz" 57 58 condConf := condition.NewConfig() 59 condConf.Type = "text" 60 condConf.Text.Operator = "contains" 61 condConf.Text.Arg = "select" 62 63 conf.ProcessMap.Conditions = append(conf.ProcessMap.Conditions, condConf) 64 65 procConf := NewConfig() 66 procConf.Type = "json" 67 procConf.JSON.Operator = "select" 68 procConf.JSON.Path = "bar" 69 70 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 71 72 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 73 if err != nil { 74 t.Error(err) 75 return 76 } 77 78 exp := [][]byte{ 79 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 80 []byte(`{"a":"select","foo":{"bar":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 81 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 82 } 83 84 msg, res := c.ProcessMessage(message.New([][]byte{ 85 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 86 []byte(`{"a":"select","foo":{"bar":{"baz":"put me at the root"}}}`), 87 []byte(`{"foo":{"bar":{"baz":"original"}}}`), 88 })) 89 if res != nil { 90 t.Error(res.Error()) 91 } 92 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 93 t.Errorf("Wrong result: %s != %s", act, exp) 94 } 95 } 96 97 func TestProcessMapAllParts(t *testing.T) { 98 conf := NewConfig() 99 conf.Type = "process_map" 100 conf.ProcessMap.Premap["."] = "foo" 101 conf.ProcessMap.Postmap["foo.baz"] = "baz" 102 conf.ProcessMap.Parts = []int{} 103 104 procConf := NewConfig() 105 procConf.Type = "json" 106 procConf.JSON.Operator = "select" 107 procConf.JSON.Path = "bar" 108 109 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 110 111 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 112 if err != nil { 113 t.Error(err) 114 return 115 } 116 117 exp := [][]byte{ 118 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 119 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 120 } 121 122 msg, res := c.ProcessMessage(message.New([][]byte{ 123 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 124 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 125 })) 126 if res != nil { 127 t.Error(res.Error()) 128 } 129 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 130 t.Errorf("Wrong result: %s != %s", act, exp) 131 } 132 } 133 134 func TestProcessMapStrict(t *testing.T) { 135 conf := NewConfig() 136 conf.Type = "process_map" 137 conf.ProcessMap.Premap["bar"] = "foo.bar" 138 conf.ProcessMap.Premap["bar2"] = "foo.bar2" 139 conf.ProcessMap.PostmapOptional["foo.baz"] = "baz" 140 conf.ProcessMap.PostmapOptional["foo.baz2"] = "baz2" 141 conf.ProcessMap.Parts = []int{} 142 143 procConf := NewConfig() 144 procConf.Type = "json" 145 procConf.JSON.Operator = "select" 146 procConf.JSON.Path = "bar" 147 148 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 149 150 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 151 if err != nil { 152 t.Error(err) 153 return 154 } 155 156 exp := [][]byte{ 157 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"bar2":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 158 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 159 } 160 161 msg, res := c.ProcessMessage(message.New([][]byte{ 162 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"bar2":{"baz":"put me at the root"}}}`), 163 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 164 })) 165 if res != nil { 166 t.Error(res.Error()) 167 } 168 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 169 t.Errorf("Wrong result: %s != %s", act, exp) 170 } 171 } 172 173 func TestProcessMapOptional(t *testing.T) { 174 conf := NewConfig() 175 conf.Type = "process_map" 176 conf.ProcessMap.Premap["bar"] = "foo.bar" 177 conf.ProcessMap.PremapOptional["bar2"] = "foo.bar2" 178 conf.ProcessMap.PostmapOptional["foo.baz"] = "baz" 179 conf.ProcessMap.PostmapOptional["foo.baz2"] = "baz2" 180 conf.ProcessMap.Parts = []int{} 181 182 procConf := NewConfig() 183 procConf.Type = "json" 184 procConf.JSON.Operator = "select" 185 procConf.JSON.Path = "bar" 186 187 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 188 189 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 190 if err != nil { 191 t.Error(err) 192 return 193 } 194 195 exp := [][]byte{ 196 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"bar2":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 197 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"baz":"put me at the root"}}`), 198 } 199 200 msg, res := c.ProcessMessage(message.New([][]byte{ 201 []byte(`{"foo":{"bar":{"baz":"put me at the root"},"bar2":{"baz":"put me at the root"}}}`), 202 []byte(`{"foo":{"bar":{"baz":"put me at the root"}}}`), 203 })) 204 if res != nil { 205 t.Error(res.Error()) 206 } 207 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 208 t.Errorf("Wrong result: %s != %s", act, exp) 209 } 210 } 211 212 func TestProcessMapBadProc(t *testing.T) { 213 conf := NewConfig() 214 conf.Type = "process_map" 215 conf.ProcessMap.Premap["."] = "foo.bar" 216 conf.ProcessMap.Postmap["foo.baz"] = "." 217 conf.ProcessMap.Parts = []int{} 218 219 procConf := NewConfig() 220 procConf.Type = "archive" 221 222 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 223 224 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 225 if err != nil { 226 t.Error(err) 227 return 228 } 229 230 exp := [][]byte{ 231 []byte(`{"foo":{"bar":"encode me"}}`), 232 []byte(`{"foo":{"bar":"encode me too"}}`), 233 } 234 235 msg, res := c.ProcessMessage(message.New(exp)) 236 if res != nil { 237 t.Error(res.Error()) 238 } 239 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 240 t.Errorf("Wrong result: %s != %s", act, exp) 241 } 242 } 243 244 func TestProcessMapBadProcTwo(t *testing.T) { 245 conf := NewConfig() 246 conf.Type = "process_map" 247 conf.ProcessMap.Premap["."] = "foo.bar" 248 conf.ProcessMap.Postmap["foo.baz"] = "." 249 conf.ProcessMap.Parts = []int{} 250 251 procConf := NewConfig() 252 procConf.Type = "filter" 253 procConf.Filter.Type = "static" 254 procConf.Filter.Static = false 255 256 conf.ProcessMap.Processors = append(conf.ProcessMap.Processors, procConf) 257 258 c, err := New(conf, nil, log.Noop(), metrics.Noop()) 259 if err != nil { 260 t.Error(err) 261 return 262 } 263 264 exp := [][]byte{ 265 []byte(`{"foo":{"bar":"encode me"}}`), 266 []byte(`{"foo":{"bar":"encode me too"}}`), 267 } 268 269 msg, res := c.ProcessMessage(message.New(exp)) 270 if res != nil { 271 t.Error(res.Error()) 272 } 273 if act := message.GetAllBytes(msg[0]); !reflect.DeepEqual(act, exp) { 274 t.Errorf("Wrong result: %s != %s", act, exp) 275 } 276 }