Skip to content

Commit

Permalink
Refactor and add test cases for key size limiting function
Browse files Browse the repository at this point in the history
Refactors existing code by simplifying key size limiting logic in VisualizationUtil and added several new unit tests to ensure its correct behavior with different input scenarios including long keys, empty maps, null maps, and uniqueness near middle.
  • Loading branch information
Gcolon021 committed Feb 12, 2024
1 parent 3e62cbb commit 9fa4347
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,81 @@

public class VisualizationUtilTests {

@Test
@DisplayName("Test limitKeySize")
public void testLimitKeySizeUniqueness() {
Map<String, Integer> axisMap = new HashMap<>(Map.of(
"Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation)", 1
));

Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);

Map<String, Integer> expected = Map.of(
"Disease-Specific (Asthma, Allergy an..., PUB)", 1,
"Disease-Specific (Asthma, Allergy an...ation)", 1,
"Disease-Specific (Asthma, Allergy an..., NPU)", 1,
"Disease-Specific (Asthma, Allergy a...B, NPU)", 1
);
assertEquals(expected, actual);
}
@Test
@DisplayName("Test limitKeySize")
public void testLimitKeySizeUniqueness() {
Map<String, Integer> axisMap = new HashMap<>(Map.of(
"Disease-Specific (Asthma, Allergy and Inflammation, PUB)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation, PUB, NPU)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation, NPU)", 1,
"Disease-Specific (Asthma, Allergy and Inflammation)", 1
));

Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);

Map<String, Integer> expected = Map.of(
"Disease-Specific (Asthma, Allergy an..., PUB)", 1,
"Disease-Specific (Asthma, Allergy an...ation)", 1,
"Disease-Specific (Asthma, Allergy an..., NPU)", 1,
"Disease-Specific (Asthma, Allergy a...B, NPU)", 1
);
assertEquals(expected, actual);
}

@Test
@DisplayName("Test Empty Map limitKeySize")
public void testEmptyMapLimitKeySize() {
Map<String, Integer> axisMap = new HashMap<>();
Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);
Map<String, Integer> expected = new HashMap<>();
assertEquals(expected, actual);
}

@Test
@DisplayName("Test null Map limitKeySize")
public void testNullMapLimitKeySize() {
Map<String, Integer> axisMap = null;
Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);
Map<String, Integer> expected = new HashMap<>();
assertEquals(expected, actual);
}

@Test
@DisplayName("Test with no long keys limitKeySize")
public void testNoLongKeysLimitKeySize() {
// Test with no long keys
Map<String, Integer> axisMap = new HashMap<>();
for (int i = 0; i < 10; i++) {
axisMap.put("key" + i, 1);
}
Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);
Map<String, Integer> expected = new HashMap<>(axisMap);
assertEquals(expected, actual);
}

@Test
@DisplayName("Test with keys of greater than 45 characters and uniqueness is near middle limitKeySize")
public void testKeysOfGreaterLengthAndUniquenessNearMiddleLimitKeySize() {
Map<String, Integer> axisMap = new HashMap<>();
axisMap.put("Hello, this is a long key that is STRING1 greater than 45 characters and is unique", 1);
axisMap.put("Hello, this is a long key that is STRING2 greater than 45 characters and is unique", 1);
axisMap.put("Hello, this is a long key that is STRING3 greater than 45 characters and is unique", 1);

Map<String, Integer> actual = VisualizationUtil.limitKeySize(axisMap);

// loop through the keys and check if they are less than 45 characters
for (String key : actual.keySet()) {
assertEquals(45, key.length());
}

Map<String, Integer> expected = Map.of(
"Hello, this is a long key that is ST...unique", 1,
"Hello, this is a long key that is S... unique", 1,
"Hello, this is a long key that is ...s unique", 1
);

assertEquals(expected, actual);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -71,60 +71,48 @@ public static Map<String, Integer> doProcessResults(Map<String, Integer> axisMap
/**
* This method is used to limit the size of the keys in the axisMap to a maximum of 45 characters. If the key is longer
* than 45 characters, it will be shortened to 45 characters and the last 3 characters will be replaced with "...".
* If the shortened key is not unique, we will create a unique one
* If the shortened key is not unique, we will create a unique one
* <p>
*
* @param axisMap - Map of the categories and their counts
* @return Map<String, Integer> - Map of the categories and their counts with the keys limited to 45 characters
*/
public static Map<String, Integer> limitKeySize(Map<String, Integer> axisMap) {
if (axisMap == null) {
return new HashMap<>();
}

Map<String, Integer> newAxisMap = new HashMap<>();
HashSet<String> keys = new HashSet<>();

axisMap.forEach((key, value) -> {
if (key.length() < MAX_X_LABEL_LINE_LENGTH) {
newAxisMap.put(key, value);
keys.add(key);
} else {
// proposed key
StringBuilder proposedKey;

// Check if the key exists in the keys set.
boolean hasEqual = false;
// Let first check if the proposed key is a leading substring of another key
for (String innerKey : axisMap.keySet()) {// Check if this key will be equal to any future keys.
if (innerKey.length() >= MAX_X_LABEL_LINE_LENGTH) {
hasEqual = key.substring(0, MAX_X_LABEL_LINE_LENGTH).equals(innerKey.substring(0, MAX_X_LABEL_LINE_LENGTH));
if (hasEqual) {
break;
}
}
}

if (hasEqual) { // if we have an equal we want to create a more unique key
int countFromEnd = 6;
String uniqueness;
do {
// get the original key substring max x - 3 (for the dots) - countFromEnd.
proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd));

// Get the last "countFromEnd" characters
uniqueness = key.substring(key.length() - countFromEnd);
String adjustedKey = key.length() < MAX_X_LABEL_LINE_LENGTH ? key : createAdjustedKey(axisMap, keys, key);
newAxisMap.put(adjustedKey, value);
keys.add(adjustedKey);
});
return newAxisMap;
}

proposedKey.append("...").append(uniqueness);
countFromEnd++; // increase the number of characters from the end of the string we will use
} while (keys.contains(proposedKey.toString()));
private static String createAdjustedKey(Map<String, Integer> axisMap, HashSet<String> keys, String key) {
String keyPrefix = key.substring(0, MAX_X_LABEL_LINE_LENGTH);
return isKeyPrefixInAxisMap(axisMap, keyPrefix) ? generateUniqueKey(keys, key) : appendEllipsis(keyPrefix);
}

} else {
proposedKey = new StringBuilder(key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3) + "...");
}
private static boolean isKeyPrefixInAxisMap(Map<String, Integer> axisMap, String keyPrefix) {
return axisMap.keySet().stream().anyMatch(k -> k.startsWith(keyPrefix));
}

newAxisMap.put(proposedKey.toString(), value);
keys.add(proposedKey.toString());
}
});
private static String generateUniqueKey(HashSet<String> keys, String key) {
int countFromEnd = 6;
String proposedKey;
do {
proposedKey = String.format("%s...%s", key.substring(0, MAX_X_LABEL_LINE_LENGTH - 3 - countFromEnd), key.substring(key.length() - countFromEnd));
countFromEnd++;
} while (keys.contains(proposedKey));
return proposedKey;
}

return newAxisMap;
private static String appendEllipsis(String keyPrefixAdjusted) {
return String.format("%s...", keyPrefixAdjusted);
}

}

0 comments on commit 9fa4347

Please sign in to comment.