### Question:
FixFixtures { Cdr getGroupWithFilter(Cdr message, String groupName) { if (!groupName.contains("[") && !groupName.contains("]")) { return message.getItem(groupName).getCdrs().get(0); } int filterStart = groupName.indexOf("["); int filterEnd = groupName.indexOf("]"); assertNotEquals("Could not find start of filter '['", -1, filterStart); assertNotEquals("Could not find end of filter ']'", -1, filterEnd); String name = groupName.substring(0, filterStart); String filter = groupName.substring(filterStart + 1, filterEnd); String[] split = filter.split("="); String field = split[0]; assertFalse("No field specified in group filter " + groupName, StringUtils.isNullOrEmpty(field)); assertEquals("No value specified in group filter " + groupName, 2, split.length); String value = split[1]; CdrItem childItem = message.getItem(name); assertEquals("Field " + name + " was not a group", CdrItem.CdrItemType.CDR_ARRAY, childItem.getType()); Cdr child = childItem.getCdrs() .stream() .filter(cdr -> cdr.containsKey(field) && cdr.getAsString(field).equals(value)) .findFirst() .orElse(null); assertNotNull("Could not find matching group " + groupName, child); return child; } FixFixtures(EtiqetHandlers handlers); @Then("create a repeating group \"([^\"]*)\" in message \"([^\"]*)\"") void createRepeatingGroup(String groupType, String messageName); @Then("create a repeating group \"([^\"]*)\" with \"([^\"]*)\" in message \"([^\"]*)\"") void createRepeatingGroupWithFields(String groupType, String params, String messageName); @Then("create a repeating group \"([^\"]*)\" in group \"([^\"]*)\" in message \"([^\"]*)\"") void createRepeatingGroupInGroup(String groupType, String existingGroup, String messageName); @Then("create a repeating group \"([^\"]*)\" with \"([^\"]*)\" in group \"([^\"]*)\" in message \"([^\"]*)\"") void createRepeatingGroupWithFieldsInGroup(String groupType, String params, String existingGroup, String messageName); @Then("add fields \"([^\"]*)\" to \"([^\"]*)\" group in message \"([^\"]*)\"") void addFieldsToRepeatingGroup(String params, String groupType, String messageName);  }### Answer:
@Test public void testGetGroupWithFilter_NoFilterSpecified() { Cdr nested = new Cdr("nested"); CdrItem children = new CdrItem(CdrItem.CdrItemType.CDR_ARRAY); children.addCdrToList(nested); Cdr message = new Cdr("Test"); message.setItem("field", children); Cdr field = fixtures.getGroupWithFilter(message, "field"); assertEquals(nested, field); }
@Test public void testGetGroupWithFilter_FilterApplied() { CdrItem field = new CdrItem(CdrItem.CdrItemType.CDR_ARRAY); Cdr child1 = new Cdr("Test"); child1.set("child", "value1"); Cdr child2 = new Cdr("Test"); child2.set("child", "value2"); field.setCdrs(Arrays.asList(child1, child2)); Cdr parent = new Cdr("Parent"); parent.setItem("Test", field); Cdr filteredGroup = fixtures.getGroupWithFilter(parent, "Test[child=value1]"); assertEquals(child1, filteredGroup); filteredGroup = fixtures.getGroupWithFilter(parent, "Test[child=value2]"); assertEquals(child2, filteredGroup); }