github.com/TIBCOSoftware/flogo-lib@v0.5.9/core/mapper/exprmapper/arraymapperr_test.go (about) 1 package exprmapper 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 //7. array mapping with leaf field 12 func TestArrayMapping(t *testing.T) { 13 mappingValue := `{ 14 "fields": [ 15 { 16 "from": "$.street", 17 "to": "$.street", 18 "type": "primitive" 19 }, 20 { 21 "from": "$.zipcode", 22 "to": "$.zipcode", 23 "type": "primitive" 24 }, 25 { 26 "from": "$.state", 27 "to": "$.state", 28 "type": "primitive" 29 } 30 ], 31 "from": "$activity[a1].field.addresses", 32 "to": "field.addresses", 33 "type": "foreach" 34 }` 35 36 arrayData := `{ 37 "person": "name", 38 "addresses": [ 39 { 40 "street": "street", 41 "zipcode": 77479, 42 "state": "tx" 43 } 44 ] 45 }` 46 47 array, err := ParseArrayMapping(mappingValue) 48 assert.Nil(t, array.Validate()) 49 50 inputScope := GetObjectFieldScope("_A.a1.field", arrayData) 51 outputScope := GetObjectFieldScope("field", "") 52 err = array.DoArrayMapping(inputScope, outputScope, GetTestResolver()) 53 assert.Nil(t, err) 54 55 arr, ok := outputScope.GetAttr("field") 56 assert.True(t, ok) 57 assert.Equal(t, "street", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["street"]) 58 assert.Equal(t, float64(77479), arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["zipcode"]) 59 assert.Equal(t, "tx", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["state"]) 60 61 } 62 63 //8. array mappping with static function and leaf field 64 func TestArrayMappingWithFunction(t *testing.T) { 65 mappingValue := `{ 66 "fields": [ 67 { 68 "from": "string.concat(\"this stree name: \", $.street)", 69 "to": "$.street", 70 "type": "primitive" 71 }, 72 { 73 "from": "string.concat(\"The zipcode is: \",$.zipcode)", 74 "to": "$.zipcode", 75 "type": "primitive" 76 }, 77 { 78 "from": "$.state", 79 "to": "$.state", 80 "type": "primitive" 81 } 82 ], 83 "from": "$activity[a1].field.addresses", 84 "to": "field.addresses", 85 "type": "foreach" 86 }` 87 88 arrayData := `{ 89 "person": "name", 90 "addresses": [ 91 { 92 "street": "street", 93 "zipcode": 77479, 94 "state": "tx" 95 } 96 ] 97 }` 98 99 array, err := ParseArrayMapping(mappingValue) 100 assert.Nil(t, err) 101 assert.Nil(t, array.Validate()) 102 103 inputScope := GetObjectFieldScope("_A.a1.field", arrayData) 104 outputScope := GetObjectFieldScope("field", "") 105 err = array.DoArrayMapping(inputScope, outputScope, GetTestResolver()) 106 assert.Nil(t, err) 107 108 arr, ok := outputScope.GetAttr("field") 109 assert.True(t, ok) 110 assert.Equal(t, "this stree name: street", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["street"]) 111 assert.Equal(t, "The zipcode is: 77479", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["zipcode"]) 112 assert.Equal(t, "tx", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["state"]) 113 114 } 115 116 //9. array mapping with other activity output 117 func TestArrayMappingWithUpstreamingOutput(t *testing.T) { 118 mappingValue := `{ 119 "fields": [ 120 { 121 "from": "string.concat(\"this stree name: \", $activity[a1].field.person)", 122 "to": "$.street", 123 "type": "primitive" 124 }, 125 { 126 "from": "string.concat(\"The zipcode is: \",$.zipcode)", 127 "to": "$.zipcode", 128 "type": "primitive" 129 }, 130 { 131 "from": "$.state", 132 "to": "$.state", 133 "type": "primitive" 134 } 135 ], 136 "from": "$activity[a1].field.addresses", 137 "to": "field.addresses", 138 "type": "foreach" 139 }` 140 141 arrayData := `{ 142 "person": "name", 143 "addresses": [ 144 { 145 "street": "street", 146 "zipcode": 77479, 147 "state": "tx" 148 } 149 ] 150 }` 151 152 array, err := ParseArrayMapping(mappingValue) 153 assert.Nil(t, err) 154 assert.Nil(t, array.Validate()) 155 156 inputScope := GetObjectFieldScope("_A.a1.field", arrayData) 157 outputScope := GetObjectFieldScope("field", "") 158 err = array.DoArrayMapping(inputScope, outputScope, GetTestResolver()) 159 assert.Nil(t, err) 160 161 arr, ok := outputScope.GetAttr("field") 162 assert.True(t, ok) 163 assert.Equal(t, "this stree name: name", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["street"]) 164 assert.Equal(t, "The zipcode is: 77479", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["zipcode"]) 165 assert.Equal(t, "tx", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["state"]) 166 167 } 168 169 //9. array mapping with other activity output 170 func TestArrayMappingWithNest(t *testing.T) { 171 mappingValue := `{ 172 "fields": [ 173 { 174 "from": "string.concat(\"this stree name: \", $activity[a1].field.person)", 175 "to": "$.street", 176 "type": "primitive" 177 }, 178 { 179 "from": "string.concat(\"The zipcode is: \",$.zipcode)", 180 "to": "$.zipcode", 181 "type": "primitive" 182 }, 183 { 184 "from": "$.state", 185 "to": "$.state", 186 "type": "primitive" 187 }, 188 { 189 "from": "$.array", 190 "to": "$.array", 191 "type": "foreach", 192 "fields":[ 193 { 194 "from": "$.field1", 195 "to": "$.tofield1", 196 "type": "assign" 197 }, 198 { 199 "from": "$.field2", 200 "to": "$.tofield2", 201 "type": "assign" 202 }, 203 { 204 "from": "wangzai", 205 "to": "$.tofield3", 206 "type": "assign" 207 } 208 ] 209 210 } 211 ], 212 "from": "$activity[a1].field.addresses", 213 "to": "field.addresses", 214 "type": "foreach" 215 }` 216 217 arrayData := `{ 218 "person": "name", 219 "addresses": [ 220 { 221 "street": "street", 222 "zipcode": 77479, 223 "state": "tx", 224 "array":[ 225 { 226 "field1":"field1value", 227 "field2":"field2value", 228 "field3":"field3value" 229 } 230 ] 231 } 232 ] 233 }` 234 235 array, err := ParseArrayMapping(mappingValue) 236 assert.Nil(t, err) 237 assert.Nil(t, array.Validate()) 238 239 inputScope := GetObjectFieldScope("_A.a1.field", arrayData) 240 outputScope := GetObjectFieldScope("field", "") 241 err = array.DoArrayMapping(inputScope, outputScope, GetTestResolver()) 242 assert.Nil(t, err) 243 244 arr, ok := outputScope.GetAttr("field") 245 assert.True(t, ok) 246 v, _ := json.Marshal(arr.Value()) 247 fmt.Println(string(v)) 248 assert.Equal(t, "this stree name: name", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["street"]) 249 assert.Equal(t, "The zipcode is: 77479", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["zipcode"]) 250 assert.Equal(t, "tx", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["state"]) 251 252 assert.Equal(t, "field1value", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["array"].([]interface{})[0].(map[string]interface{})["tofield1"]) 253 assert.Equal(t, "field2value", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["array"].([]interface{})[0].(map[string]interface{})["tofield2"]) 254 assert.Equal(t, "wangzai", arr.Value().(map[string]interface{})["addresses"].([]interface{})[0].(map[string]interface{})["array"].([]interface{})[0].(map[string]interface{})["tofield3"]) 255 256 }