Skip to content

Commit

Permalink
Make file type formatter handle more string cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bseeger committed Feb 9, 2024
1 parent 852a643 commit 608485e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/main/java/org/mdbenefits/app/utils/UserFileUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,27 @@ public class UserFileUtilities {
* Formats the application.yaml `accepted-file-type` string to be more human-readable.
*
* @param fileTypeStr a string containing the file types this system can support
* @return A formatted string of the file types
* @return A formatted string of the file types
*/
public static String formatAcceptedFileTypeString(String fileTypeStr) {
if (fileTypeStr == null || fileTypeStr.isBlank()) {
return "";
}

List<String> types = List.of(fileTypeStr.split(","));

String fileTypes = types.stream()
.map(String::trim)
.collect(Collectors.joining(", "));
.map(String::trim)
.collect(Collectors.joining(", "));

int lastComma = fileTypes.lastIndexOf(",");
return fileTypes.replace(fileTypes.substring(lastComma), ", and" + fileTypes.substring(lastComma+1));

if (lastComma != -1) {
String replacement = types.size() == 2 ? " and" : ", and";
return fileTypes.replace(fileTypes.substring(lastComma), replacement + fileTypes.substring(lastComma + 1));
}

return fileTypes;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import org.junit.jupiter.params.provider.CsvSource;

public class TestUserFileUtilities {
@CsvSource(delimiter=';', value = {
// passed in value; expected value
".jpg, .jpeg, .csv,.pdf;.jpg, .jpeg, .csv, and .pdf",
".jpg,.jpeg,.csv,.pdf,.doc;.jpg, .jpeg, .csv, .pdf, and .doc"

@CsvSource(delimiter = ';', value = {
// passed in value; expected value
".jpg, .jpeg, .csv,.pdf;.jpg, .jpeg, .csv, and .pdf",
".jpg,.jpeg,.csv,.pdf,.doc;.jpg, .jpeg, .csv, .pdf, and .doc",
" .jpg ;.jpg",
".jpg, .gif;.jpg and .gif",
"'';''"
})
@ParameterizedTest
public void shouldParseFileTypesSuccessfully(String acceptedFileTypeString, String expectedString) {
Expand Down

0 comments on commit 608485e

Please sign in to comment.