By doing some Test in Java I find two problems when trying the function to obtain a file from a folder that is in the properties of the application:
The first problem is that when doing the @Test
it does not recognize the value extracted from ${routing.folder}
, something that does not happen to me executing and debugging the code, that's why I left it commented and directly copied its value. Next I share the Test and the Class:
SepaRoutingFromXMLTest.java:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {SepaRoutingUtils.class})
public class SepaRoutingFromXMLTest {
@Autowired
SepaRoutingUtils sepa;
@Test
public void existValidOneFullXMLFileInFolder() throws Exception {
SepaRoutingUtils sepa = new SepaRoutingUtils();
assertThat(sepa.readSepaXMLFile(), containsString(".xml"));
assertThat(sepa.readSepaXMLFile(), containsString("SEPAROUTING_V3_FULL_"));
}
}
SepaRoutingUtils.java:
@Component
public class SepaRoutingUtils {
// @Value("${routing.folder}")
// private String SEPA_FOLDER
private final String SEPA_FOLDER = "../../separouting";
private File readSepaFolder() {
File folder = new File(SEPA_FOLDER);
if(!folder.isDirectory()) {
throw new FolderAccessDeniedException();
}
return folder;
}
public String readSepaXMLFile() {
try {
return Utils.prepareXMLFile(readSepaFolder(), SepaRoutingFileType.FULL).getName();
}
catch (ParseException e) {
e.printStackTrace();
throw new NotValidFileException();
}
}
}
The Utils.prepareXMLFile
function returns the file I need to process it, validates among other things the file name and only takes the most updated one.
The second drawback is that if I wanted something like @Test(expected = FolderAccessDeniedException.class)
I could not test it because it is assumed that the value is taken from the properties and this value simply can not be modified from @Test
. And it would always be right or wrong.
Then I do not know how to make it fail when, for example, a route does not exist or is not accessible. I have thought about changing the function so that the path of the folder is passed to the readSepaFolder()
function, however I would not like to leave it that way.