diff --git a/app/service/EmotionDataService.scala b/app/service/EmotionDataService.scala index e91ce8f..3867f9a 100644 --- a/app/service/EmotionDataService.scala +++ b/app/service/EmotionDataService.scala @@ -20,22 +20,27 @@ class EmotionDataServiceImpl @Inject()(emotionDao: EmotionDao, subEmotionDao: SubEmotionDao, ) extends EmotionDataService { override def fetchEmotionData(): Future[EmotionData] = { // TODO: add caching - Future { - databaseExecutionContext.withConnection { implicit connection => - val emotions: List[Emotion] = emotionDao.findAll() - val triggers = triggerDao.findAll() - - val subEmotions: List[SubEmotion] = subEmotionDao.findAll() - - val emotionSubEmotions: List[EmotionWithSubEmotions] = emotions.map(emotion => - controllers.model.EmotionWithSubEmotions(emotion, subEmotions.map(subEmotion => - controllers.model.SubEmotionWrapper(subEmotion, List.empty)))) - val emotionTypes = emotionSubEmotions.groupBy(_.emotion.emotionType).map { - case (Some(emotionType), emotionWithSubEmotions) => EmotionTypesWithEmotions(emotionType, - emotionWithSubEmotions) - }.toList - EmotionData(emotionTypes, triggers) - } + for { + emotions <- Future(databaseExecutionContext.withConnection(implicit connection => emotionDao.findAll())) + triggers <- Future(databaseExecutionContext.withConnection(implicit connection => triggerDao.findAll())) + subEmotions <- Future(databaseExecutionContext.withConnection(implicit connection => subEmotionDao.findAll())) + } yield { + val emotionSubEmotions: List[EmotionWithSubEmotions] = emotions.map(emotion => + EmotionWithSubEmotions(emotion, + subEmotions.filter(subEmotion => equals(subEmotion.parentEmotionId, emotion.id)). + map(subEmotion => SubEmotionWrapper(subEmotion, List.empty)) + ) + ) + val emotionTypes = emotionSubEmotions.groupBy(_.emotion.emotionType).map { + case (Some(emotionType), emotionWithSubEmotions) => EmotionTypesWithEmotions(emotionType, + emotionWithSubEmotions) + }.toList + EmotionData(emotionTypes, triggers) } } + + private def equals[T](a: Option[T], b: Option[T]): Boolean = (a, b) match { + case (Some(a), Some(b)) => a == b + case _ => false + } } \ No newline at end of file diff --git a/build.sbt b/build.sbt index 8edf2d4..64a5c88 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,6 @@ javaOptions ++= Seq( "-Xss1M", "-XX:+CMSClassUnloadingEnabled" ) -Gatling / scalaSource := sourceDirectory.value / "gatling" / "scala" lazy val root = (project in file(".")) .enablePlugins(PlayScala, GatlingPlugin) @@ -24,12 +23,13 @@ lazy val root = (project in file(".")) "org.scalatestplus.play" %% "scalatestplus-play" % "5.1.0" % Test ), watchSources ++= (baseDirectory.value / "ui/emo-app/src" ** "*").get, - inConfig(Gatling)(Defaults.testSettings), + inConfig(GatlingIt)(Defaults.testSettings), ) -Gatling / resourceDirectory := baseDirectory.value / "gatling/resources" -Gatling / scalaSource := baseDirectory.value / "gatling" -Gatling / javaOptions := overrideDefaultJavaOptions("-Xms1024m", "-Xmx2048m") +GatlingIt / resourceDirectory := baseDirectory.value / "gatling/resources" +GatlingIt / scalaSource := baseDirectory.value / "gatling" +GatlingIt / javaOptions := overrideDefaultJavaOptions("-Xms1024m", "-Xmx2048m") + resolvers += "Typesafe Releases" at "https://repo.typesafe.com/typesafe/releases/" resolvers += "Typesafe Simple Repository" at "https://repo.typesafe.com/typesafe/simple/maven-releases/" diff --git a/gatling/UserSimulation.scala b/gatling/UserSimulation.scala index 79646ee..78d1c36 100644 --- a/gatling/UserSimulation.scala +++ b/gatling/UserSimulation.scala @@ -6,9 +6,6 @@ import io.gatling.http.protocol.HttpProtocolBuilder import java.util.Base64 import scala.concurrent.duration._ - - - class UserSimulation extends Simulation { val userFeeder: Feeder[String] = Iterator.continually(Map( diff --git a/test/service/EmotionTypeServiceImplSpec.scala b/test/service/EmotionDataServiceImplSpec.scala similarity index 86% rename from test/service/EmotionTypeServiceImplSpec.scala rename to test/service/EmotionDataServiceImplSpec.scala index 05e33cf..2d21ac6 100644 --- a/test/service/EmotionTypeServiceImplSpec.scala +++ b/test/service/EmotionDataServiceImplSpec.scala @@ -10,7 +10,7 @@ import org.mockito.Mockito.when import org.scalatest.time.SpanSugar.convertIntToGrainOfTime import play.api.libs.json.Json -class EmotionTypeServiceImplSpec extends PlaySpec with MockitoSugar { +class EmotionDataServiceImplSpec extends PlaySpec with MockitoSugar { "EmotionServiceImpl" should { "fetch EmotionData successfully" in { val mockEmotionDao = mock[EmotionDao] @@ -29,16 +29,14 @@ class EmotionTypeServiceImplSpec extends PlaySpec with MockitoSugar { val emotions = List(Emotion(Some("Joy"), Option("Joy"), Some("Positive")), Emotion(Some("Sadness"), Option("Sadness"), Some("Negative"))) val subEmotions = List(SubEmotion(Some("Content"), Some("Content"), Some("description"), Some("Joy"))) - val suggestedActions = List(SuggestedAction(Some("Content"), "Watch a comedy")) val triggers = List(Trigger(Some(1), Some("Bad weather"), None, None, Some("Bad weather"))) when(mockEmotionDao.findAll()(connection)).thenReturn(emotions) when(mockSubEmotionDao.findAll()(connection)).thenReturn(subEmotions) - when(mockSuggestedActionDao.findAllBySubEmotionId("Content")(connection)).thenReturn(suggestedActions) when(mockTriggerDao.findAll()(connection)).thenReturn(triggers) val subEmotionWithActions: SubEmotionWrapper = SubEmotionWrapper( - subEmotions.head, suggestedActions) + subEmotions.head, List()) val emotionWithSubEmotions1: EmotionWithSubEmotions = EmotionWithSubEmotions( emotions.head,List(subEmotionWithActions) ) @@ -50,10 +48,11 @@ class EmotionTypeServiceImplSpec extends PlaySpec with MockitoSugar { val expectedEmotionData = EmotionData(List(emotionType2, emotionType1),triggers) val actual: EmotionData = Await.result(emotionServiceImpl.fetchEmotionData(), 10000.seconds) - println("actual: " + Json.toJson(actual)) + val sortedActual = EmotionData(actual.emotionTypes.sortBy(_.emotionType), actual.triggers) + println("actual: " + Json.toJson(sortedActual)) println("expected: " + Json.toJson(expectedEmotionData)) - actual mustEqual expectedEmotionData + sortedActual mustEqual expectedEmotionData } } }