MusicaX79
MusicaX79

Reputation: 11

assert List<Map> contains String

I am trying to make an assertion the only issue is the order could be random so I can't do a normal compare so I need to use something along the lines of .contains the only problem it the below does not work.

List<Map> aliasQueryResults
Map<String, String> newTokenValuesMap
assert aliasQueryResults.contains(newTokenValuesMap.get("{BASEALIAS}"))

newTokenValuesMap.get("{BASEALIAS}") is fine it returns the string so it's not the issue of this attempt.

Upvotes: 1

Views: 80

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96394

Assuming the List is really a List<Map<String, String>>, and what you want is to see if a value is represented in any of the values from any of the maps in the list, then you can pull out the values from the maps:

aliasQueryResults*.values().flatten().contains(newTokenValuesMap.get("{BASEALIAS}"))

example:

groovy:000> mylist = []
===> []
groovy:000> mylist << [a:'asdf', b:'zxcv', c:'qwer']
===> [[a:asdf, b:zxcv, c:qwer]]
groovy:000> mylist << [d:'xcvb',e:'wert', f:'sdfg']
===> [[a:asdf, b:zxcv, c:qwer], [d:xcvb, e:wert, f:sdfg]]
groovy:000> mylist*.values()
===> [[asdf, zxcv, qwer], [xcvb, wert, sdfg]]
groovy:000> mylist*.values().flatten()
===> [asdf, zxcv, qwer, xcvb, wert, sdfg]
groovy:000> mylist*.values().flatten().contains('asdf')
===> true

Upvotes: 1

Related Questions