AssociationColumnRemapper { public static void apply(QueryResults queryResults) { Map<String, List<Object>> resultsMap = queryResults.getResultsMap(); Map<String, List<Object>> associationsMap = new TreeMap<>(); if (resultsMap.containsKey("Associations")) { List associationResults = queryResults.getResults("Associations"); Set<String> linkNames = new HashSet<>(); for (Object a : associationResults) { if (a != null) { String s = (String) a; String[] parts = s.split(ILink.DELIMITER); linkNames.add(parts[0].trim()); } } for (String linkName : linkNames) { List<Object> list = new ArrayList<>(queryResults.rowCount()); for (int i = 0; i < queryResults.rowCount(); i++) { list.add(null); } associationsMap.put(linkName, list); } for (int i = 0; i < queryResults.rowCount(); i++) { String association = (String) associationResults.get(i); if (association != null) { String[] parts = association.split(ILink.DELIMITER); String linkName = parts[0]; List<Object> list = associationsMap.get(linkName); list.set(i, association); } } resultsMap.remove("Associations"); resultsMap.putAll(associationsMap); } }  static void apply(QueryResults queryResults);  }
@Test public void testWithNoAssociationColumn() { Map<String, List<Object>> map = new TreeMap<>(); List list = Lists.newArrayList("a", "b", "c"); map.put("Foo", list); QueryResults queryResults = new QueryResults(map); AssociationColumnRemapper.apply(queryResults); assertTrue("Expected one column", queryResults.columnCount() == 1); }
@Test public void testWithOneAssociation() { List list = Lists.newArrayList("assoc1 | foo | bar", null, "assoc1 | not foo | not bar"); QueryResults queryResults = buildQueryResults(list); AssociationColumnRemapper.apply(queryResults); assertFalse("Found Association Column", queryResults.containsColumnName(ASSOC)); assertTrue("Did not find new assoc1 column", queryResults.containsColumnName("assoc1")); assertTrue("Did not find correct # of associations", queryResults.getResults("assoc1").size() == 3); }
@Test public void testWithMultipleAssociations() { List list = Lists.newArrayList("assoc1 | foo | bar", null, "assoc2 | not foo | not bar", "assoc2 | foo foo | bar bar"); QueryResults queryResults = buildQueryResults(list); AssociationColumnRemapper.apply(queryResults); assertFalse("Found Association Column", queryResults.containsColumnName(ASSOC)); assertTrue("Did not find new assoc1 column", queryResults.containsColumnName("assoc1")); assertTrue("Did not find new assoc2 column", queryResults.containsColumnName("assoc2")); assertTrue("Did not find correct # of associations", queryResults.getResults("assoc1").size() == 4); }