Skip to content

Commit

Permalink
Fixes bug with sub-emotions.
Browse files Browse the repository at this point in the history
Fixes gatling and test sources.
  • Loading branch information
vega113 committed Nov 19, 2023
1 parent d44a2c2 commit c2c6d68
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
37 changes: 21 additions & 16 deletions app/service/EmotionDataService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 5 additions & 5 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ javaOptions ++= Seq(
"-Xss1M",
"-XX:+CMSClassUnloadingEnabled"
)
Gatling / scalaSource := sourceDirectory.value / "gatling" / "scala"

lazy val root = (project in file("."))
.enablePlugins(PlayScala, GatlingPlugin)
Expand All @@ -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/"
Expand Down
3 changes: 0 additions & 3 deletions gatling/UserSimulation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
)
Expand All @@ -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
}
}
}
Expand Down

0 comments on commit c2c6d68

Please sign in to comment.