Firstly, I'm new to Groovy so apologies in advance for any school boy errors in the below.
I've created a script which I'm using to simulate the behaviour of a SOAP service in SOAP UI (as a mock service) for sprint testing purposes but am having problems when trying to iterate over a List I've created. The List is made up of a number of Maps, and each Map contains a List. It should look something like this:
[[Rec: 1, Items: ["AB123","BC234","CD345"]],[Rec: 2, Items: ["AB123","BC234","CD345","DE456"]]]
And this is the code I have to build up the List:
def offerMap = [:] def outputList = [] def offerItemList = [] def outputMap = [:] def outList = [] def item = "" def rec = "" offerItemList.add("AB123") offerItemList.add("BC234") offerItemList.add("CD345") offerMap.put("Rec",1) offerMap.put("Items",offerItemList) outputList.add(offerMap) log.info "OUT: outputList.size ${outputList.size()}" log.info "OUT: offerItemList.size ${offerItemList.size()}" offerMap.clear() offerItemList.clear() offerItemList.add("AB123") offerItemList.add("BC234") offerItemList.add("CD345") offerItemList.add("DE456") offerMap.put("Rec",2) offerMap.put("Items",offerItemList) outputList.add(offerMap) log.info "OUT: outputList.size ${outputList.size()}" log.info "OUT: offerItemList.size ${offerItemList.size()}"
And this is the the code I have to iterate over the list:
outputList.each { log.info "OUT: outputList.size ${outputList.size()}" outputMap.clear() outputMap = it row = outputMap.get("Rec") log.info "OUT: REC ${rec}" outList.clear() outList = outputMap.get("Items") outList.each { item = it log.info "OUT: Item ${item}" } }
But this is not giving me the results I expect, first problem is that the .each loop for outputList appears to immediately be jumping to the second entry in the list, as witnessed from the output:
Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 1 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 3 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: offerItemList.size 4 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC 2 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item AB123 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item BC234 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item CD345 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: Item DE456 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: outputList.size 2 Fri Nov 03 17:54:32 GMT 2017:INFO:OUT: REC null
I'm running out of ideas and fear I may be missing something fundamental due to my lack of experience with Groovy.
If you can put me right, I'd be most grateful.
TIA