SurveyIdGenerator { @NonNull public String getSurveyIdFromFilePath(@NonNull String entryName) { int fileSeparatorPosition = entryName.lastIndexOf("/"); String folderPath = fileSeparatorPosition <= 0 ? "" : entryName.substring(0, fileSeparatorPosition); String folders[] = folderPath == null || folderPath.isEmpty()? null : folderPath.split("/"); if (folders == null || folders.length == 0) { return ""; } else { int lastItemIndex = folders.length - 1; for (int i = lastItemIndex; i >= 0; i--) { String folderName = folders[i]; if (TextUtils.isDigitsOnly(folderName)) { return folderName; } } return folders[lastItemIndex]; } } SurveyIdGenerator(); @NonNull String getSurveyIdFromFilePath(@NonNull String entryName);  }
@Test public void getSurveyIdFromFilePath_shouldReturnEmptyIfFolderMissing() { SurveyIdGenerator surveyIdGenerator = new SurveyIdGenerator(); String surveyId = surveyIdGenerator.getSurveyIdFromFilePath("file.xml"); assertEquals("", surveyId); }
@Test public void getSurveyIdFromFilePath_shouldReturnCorrectIdFromFolder() { SurveyIdGenerator surveyIdGenerator = new SurveyIdGenerator(); String surveyId = surveyIdGenerator .getSurveyIdFromFilePath("123/file.xml"); assertEquals("123", surveyId); surveyId = surveyIdGenerator .getSurveyIdFromFilePath("folder/123/file.xml"); assertEquals("123", surveyId); surveyId = surveyIdGenerator .getSurveyIdFromFilePath("123/folder/file.xml"); assertEquals("123", surveyId); surveyId = surveyIdGenerator .getSurveyIdFromFilePath("folder1/123/folder/file.xml"); assertEquals("123", surveyId); surveyId = surveyIdGenerator .getSurveyIdFromFilePath("/"); assertEquals("", surveyId); }
@Test public void getSurveyIdFromFilePath_shouldReturnUseClosestFolderNameIfNoId() { SurveyIdGenerator surveyIdGenerator = new SurveyIdGenerator(); String surveyId = surveyIdGenerator .getSurveyIdFromFilePath("abc/folder/survey.xml"); assertEquals("folder", surveyId); }