Skip to content

Commit

Permalink
finos#1052 handling when static resource is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
naleeha authored and chrisjstevo committed Jan 4, 2024
1 parent 166d52a commit 8f1d873
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class BasketLoader(resourcePath: Option[String] = None) extends StrictLogging {

def loadConstituents(basketId: String): Array[Map[String, Any]] = {
try {
val csvFiles = FileLoader.getFiles(getPath(resourcePath), getBasketFileName(basketId))
val filePath = getPath(resourcePath)
val csvFiles = FileLoader.getFiles(filePath, getBasketFileName(basketId))

if (csvFiles.isEmpty) {
logger.error(s"Failed to find constituents file for $basketId")
logger.error(s"Failed to find constituents file for $basketId in $filePath")
Array.empty
}
else {
Expand All @@ -25,16 +26,16 @@ class BasketLoader(resourcePath: Option[String] = None) extends StrictLogging {

logger.info(s"Found ${csvContent.dataRows.length} constituents for basket $basketId")

csvContent.dataRows.map(row => toConstituent(csvContent, row))
csvContent.dataRows.map(row => toConstituentMap(csvContent, row))
}
}
catch {
case NonFatal(t) => logger.error(s"Failed to parse constituents for $basketId", t)
case NonFatal(t) => logger.error(s"Failed to get and parse constituents for $basketId", t)
Array.empty
}
}

private def toConstituent(csvContent: CsvContent, row: Array[String]) = {
private def toConstituentMap(csvContent: CsvContent, row: Array[String]) = {
Map[String, Any](
"Symbol" -> csvContent.getValue("Symbol", row),
"Last Trade" -> csvContent.getValue("Last Trade", row),
Expand All @@ -51,6 +52,12 @@ class BasketLoader(resourcePath: Option[String] = None) extends StrictLogging {

private def getPath(resourcePath: Option[String] = None): String = {
if (resourcePath.isDefined) resourcePath.get
else getClass.getResource("/static").getPath
else {
val url = getStaticResourcePath
if (url == null) "" else url.getPath
}
}

private def getStaticResourcePath =
getClass.getResource("/static")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class BasketLoaderTests extends AnyFeatureSpec {
assert(basketIds.contains(".FTSEWITHERROR"))
}

Scenario("when no file found return empty") {
Scenario("When no file found return empty") {
val testResourcePath = this.getClass.getResource("/constituents").getPath + "/doesNotExist"
val basketLoader = new BasketLoader(Some(testResourcePath))
val basketIds = basketLoader.loadBasketIds()
Expand Down

0 comments on commit 8f1d873

Please sign in to comment.