From 8f1d8730cfc3d2a9734f971d460fda73423644fb Mon Sep 17 00:00:00 2001 From: Na Lee Ha Date: Mon, 18 Dec 2023 16:32:25 +0000 Subject: [PATCH] #1052 handling when static resource is not found --- .../core/module/basket/csv/BasketLoader.scala | 19 +++++++++++++------ .../org/finos/vuu/csv/BasketLoaderTests.scala | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/example/basket/src/main/scala/org/finos/vuu/core/module/basket/csv/BasketLoader.scala b/example/basket/src/main/scala/org/finos/vuu/core/module/basket/csv/BasketLoader.scala index 5b1984c76..0708e6e09 100644 --- a/example/basket/src/main/scala/org/finos/vuu/core/module/basket/csv/BasketLoader.scala +++ b/example/basket/src/main/scala/org/finos/vuu/core/module/basket/csv/BasketLoader.scala @@ -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 { @@ -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), @@ -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") } diff --git a/example/basket/src/test/scala/org/finos/vuu/csv/BasketLoaderTests.scala b/example/basket/src/test/scala/org/finos/vuu/csv/BasketLoaderTests.scala index 41a0dd780..2832e7c7b 100644 --- a/example/basket/src/test/scala/org/finos/vuu/csv/BasketLoaderTests.scala +++ b/example/basket/src/test/scala/org/finos/vuu/csv/BasketLoaderTests.scala @@ -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()