From 2b14f0bcc5d890a190aac98871e907a95dd42104 Mon Sep 17 00:00:00 2001 From: handlessmidas Date: Mon, 2 Mar 2020 18:37:11 +0300 Subject: [PATCH 1/7] Added random cat generation --- build.sbt | 6 ++++- src/main/scala/bot/BotStarter.scala | 40 +++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/build.sbt b/build.sbt index 2350a09..548dca0 100644 --- a/build.sbt +++ b/build.sbt @@ -4,4 +4,8 @@ version := "0.1" scalaVersion := "2.12.9" -libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2" \ No newline at end of file +libraryDependencies ++= Seq( + "com.bot4s" %% "telegram-core" % "4.4.0-RC2", + "com.softwaremill.sttp" %% "json4s" % "1.7.2", + "org.json4s" %% "json4s-native" % "3.6.0" +) \ No newline at end of file diff --git a/src/main/scala/bot/BotStarter.scala b/src/main/scala/bot/BotStarter.scala index 565a9a9..45802b5 100644 --- a/src/main/scala/bot/BotStarter.scala +++ b/src/main/scala/bot/BotStarter.scala @@ -1,5 +1,6 @@ package bot +import bot.BotStarter.Service import cats.instances.future._ import cats.syntax.functor._ import com.bot4s.telegram.api.RequestHandler @@ -7,7 +8,8 @@ import com.bot4s.telegram.api.declarative.Commands import com.bot4s.telegram.clients.{FutureSttpClient, ScalajHttpClient} import com.bot4s.telegram.future.{Polling, TelegramBot} import com.bot4s.telegram.models.{Message, User} -import com.softwaremill.sttp.SttpBackendOptions +import com.softwaremill.sttp.{SttpBackendOptions, sttp} +import com.softwaremill.sttp.json4s.asJson import com.softwaremill.sttp.okhttp.{OkHttpBackend, OkHttpFutureBackend} import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory} @@ -17,8 +19,12 @@ import scala.collection.mutable.Queue import scala.concurrent.duration.Duration import scala.concurrent.{Await, ExecutionContext, Future} import scala.io.Source +import scala.util.Random +import scala.io.Source +import com.softwaremill.sttp._ +import com.softwaremill.sttp.json4s._ -class BotStarter(override val client: RequestHandler[Future]) extends TelegramBot +class BotStarter(override val client: RequestHandler[Future], val service: Service) extends TelegramBot with Polling with Commands[Future] { @@ -29,7 +35,7 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo case None => reply("Register error").void case Some(user) => { registeredUsers += user - reply(s"You're registered.\n Your id is ${user.id}").void + reply(s"You're registered.\nYour id is ${user.id}").void } } } @@ -41,19 +47,43 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo } reply(usersString).void } + + onCommand("/cat") { implicit msg => + service.getCat().flatMap(reply(_)).void + } } object BotStarter { + + implicit val serialization = org.json4s.native.Serialization + + case class Response(data: List[Data]) + case class Data(link: String) + + class Service(implicit val backend: SttpBackend[Future, Nothing]) { + implicit val ec: ExecutionContext = ExecutionContext.global + val request: RequestT[Id, Response, Nothing] = sttp + .header("Authorization", "Client-ID 2a47c24862afdf7") + .get(uri"https://api.imgur.com/3/gallery/search?q=cats") + .response(asJson[Response]) + + def getCat() = backend.send(request).map { response => + scala.util.Random.shuffle(response.unsafeBody.data).head.link + } + } + def main(args: Array[String]): Unit = { implicit val ec: ExecutionContext = ExecutionContext.global - implicit val backend = OkHttpFutureBackend( + implicit val backend: SttpBackend[Future, Nothing] = OkHttpFutureBackend( SttpBackendOptions.Default.socksProxy("ps8yglk.ddns.net", 11999) ) val fileSource = Source.fromFile("token.txt") val token = fileSource.mkString fileSource.close() - val bot = new BotStarter(new FutureSttpClient(token)) + + val service: Service = new Service() + val bot = new BotStarter(new FutureSttpClient(token), service) Await.result(bot.run(), Duration.Inf) } } \ No newline at end of file From 52cdb74268a544905b3c122598d91afeedafa428 Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Thu, 12 Mar 2020 15:31:45 +0300 Subject: [PATCH 2/7] Old but gold --- src/main/scala/bot/BotStarter.scala | 39 ++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/src/main/scala/bot/BotStarter.scala b/src/main/scala/bot/BotStarter.scala index 565a9a9..7390f1f 100644 --- a/src/main/scala/bot/BotStarter.scala +++ b/src/main/scala/bot/BotStarter.scala @@ -23,14 +23,14 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo with Commands[Future] { val registeredUsers: mutable.Set[User] = mutable.Set[User]() + var messagesUsers: mutable.Map[String, mutable.ListBuffer[(String, String)]] = mutable.Map[String, mutable.ListBuffer[(String, String)]]().withDefaultValue(mutable.ListBuffer()) onCommand("/start") { implicit msg => msg.from match { case None => reply("Register error").void - case Some(user) => { - registeredUsers += user - reply(s"You're registered.\n Your id is ${user.id}").void - } + case Some(user) => + registeredUsers += user + reply(s"You're registered.\nYour id is ${user.id}").void } } @@ -41,6 +41,37 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo } reply(usersString).void } + + onCommand("/check") { implicit msg => + msg.from match { + case None => reply("Error.").void + case Some(user) => + messagesUsers(user.id.toString).foreach { p => + reply(s"From: ${p._1}\n${p._2}\n\n") + } + reply("").void + } + } + + onCommand("/send") { implicit msg => + var from_id : Int = 0 + msg.from match { + case None => from_id = 0 + case Some(user) => from_id = user.id + } + + msg.text match { + case None => reply("Enter non-empty message").void + case Some(s) => + val id: String = s.slice(6, 15) + val text: String = s.slice(16, s.length) + if (!messagesUsers.contains(id)) { + messagesUsers(id) = mutable.ListBuffer() + } + messagesUsers(id) += Tuple2(from_id.toString, text) + reply(s"Message was sent to ${id}").void + } + } } object BotStarter { From d19ea49925a549906699468f5e8ac43fe22b56d3 Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Wed, 29 Apr 2020 20:49:12 +0300 Subject: [PATCH 3/7] Some changes from february --- build.sbt | 4 +++- src/main/scala/bot/BotStarter.scala | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index 2350a09..81d87e5 100644 --- a/build.sbt +++ b/build.sbt @@ -4,4 +4,6 @@ version := "0.1" scalaVersion := "2.12.9" -libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2" \ No newline at end of file +libraryDependencies += "com.bot4s" %% "telegram-core" % "4.4.0-RC2" +libraryDependencies += "org.scalamock" %% "scalamock" % "4.4.0" % Test +libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.0" % Test \ No newline at end of file diff --git a/src/main/scala/bot/BotStarter.scala b/src/main/scala/bot/BotStarter.scala index 7390f1f..a990f60 100644 --- a/src/main/scala/bot/BotStarter.scala +++ b/src/main/scala/bot/BotStarter.scala @@ -7,7 +7,7 @@ import com.bot4s.telegram.api.declarative.Commands import com.bot4s.telegram.clients.{FutureSttpClient, ScalajHttpClient} import com.bot4s.telegram.future.{Polling, TelegramBot} import com.bot4s.telegram.models.{Message, User} -import com.softwaremill.sttp.SttpBackendOptions +import com.softwaremill.sttp.{SttpBackend, SttpBackendOptions} import com.softwaremill.sttp.okhttp.{OkHttpBackend, OkHttpFutureBackend} import slogging.{LogLevel, LoggerConfig, PrintLoggerFactory} @@ -49,6 +49,7 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo messagesUsers(user.id.toString).foreach { p => reply(s"From: ${p._1}\n${p._2}\n\n") } + messagesUsers(user.id.toString).clear() reply("").void } } @@ -69,7 +70,7 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo messagesUsers(id) = mutable.ListBuffer() } messagesUsers(id) += Tuple2(from_id.toString, text) - reply(s"Message was sent to ${id}").void + reply(s"Message was sent to $id").void } } } @@ -77,7 +78,7 @@ class BotStarter(override val client: RequestHandler[Future]) extends TelegramBo object BotStarter { def main(args: Array[String]): Unit = { implicit val ec: ExecutionContext = ExecutionContext.global - implicit val backend = OkHttpFutureBackend( + implicit val backend: SttpBackend[Future, Nothing] = OkHttpFutureBackend( SttpBackendOptions.Default.socksProxy("ps8yglk.ddns.net", 11999) ) From ab5b7ba07232b736cb84f9430a4cf778cfaf325c Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Fri, 8 May 2020 19:04:26 +0300 Subject: [PATCH 4/7] refactored code --- src/main/scala/bot/BotStarter.scala | 60 +++++++------------------ src/main/scala/bot/MessageHandler.scala | 21 +++++++++ src/main/scala/bot/Service.scala | 24 ++++++++++ src/main/scala/bot/UserHandler.scala | 15 +++++++ 4 files changed, 77 insertions(+), 43 deletions(-) create mode 100644 src/main/scala/bot/MessageHandler.scala create mode 100644 src/main/scala/bot/Service.scala create mode 100644 src/main/scala/bot/UserHandler.scala diff --git a/src/main/scala/bot/BotStarter.scala b/src/main/scala/bot/BotStarter.scala index 65640d3..ac10140 100644 --- a/src/main/scala/bot/BotStarter.scala +++ b/src/main/scala/bot/BotStarter.scala @@ -1,6 +1,5 @@ package bot -import bot.BotStarter.Service import cats.instances.future._ import cats.syntax.functor._ import com.bot4s.telegram.api.RequestHandler @@ -23,44 +22,34 @@ import scala.util.Random import scala.io.Source import com.softwaremill.sttp._ import com.softwaremill.sttp.json4s._ +import org.json4s.native.Serialization -class BotStarter(override val client: RequestHandler[Future], val service: Service) extends TelegramBot +class BotStarter(override val client: RequestHandler[Future], val service: Service, + val userHandler: UserHandler, val messageHandler: MessageHandler) extends TelegramBot with Polling with Commands[Future] { - val registeredUsers: mutable.Set[User] = mutable.Set[User]() - var messagesUsers: mutable.Map[String, mutable.ListBuffer[(String, String)]] = mutable.Map[String, mutable.ListBuffer[(String, String)]]().withDefaultValue(mutable.ListBuffer()) - onCommand("/start") { implicit msg => msg.from match { case None => reply("Register error").void case Some(user) => - registeredUsers += user + userHandler.register(user) reply(s"You're registered.\nYour id is ${user.id}").void } } onCommand("/users") { implicit msg => - var usersString = "" - registeredUsers.foreach { - it => usersString += s"${it.firstName} ${it.lastName}\n" - } - reply(usersString).void - } - - onCommand("/cat") { implicit msg => - service.getCat().flatMap(reply(_)).void + reply(userHandler.show()).void } onCommand("/check") { implicit msg => msg.from match { case None => reply("Error.").void case Some(user) => - messagesUsers(user.id.toString()).foreach { p => - reply(s"From: ${p._1}\n${p._2}\n\n") - } - messagesUsers(user.id.toString).clear() - reply("").void + val messages = messageHandler.show(user.id.toString) + messageHandler.clear(user.id.toString) + if (messages.nonEmpty) reply(messages).void + else reply("You haven't received any new messages yet.").void } } @@ -76,46 +65,31 @@ class BotStarter(override val client: RequestHandler[Future], val service: Servi case Some(s) => val id: String = s.slice(6, 15) val text: String = s.slice(16, s.length) - if (!messagesUsers.contains(id)) { - messagesUsers(id) = mutable.ListBuffer() - } - messagesUsers(id) += Tuple2(from_id.toString, text) + messageHandler.send(from_id.toString, id, text) reply(s"Message was sent to $id").void } } -} - -object BotStarter { - implicit val serialization = org.json4s.native.Serialization - - case class Response(data: List[Data]) - case class Data(link: String) - - class Service(implicit val backend: SttpBackend[Future, Nothing]) { - implicit val ec: ExecutionContext = ExecutionContext.global - val request: RequestT[Id, Response, Nothing] = sttp - .header("Authorization", "Client-ID 2a47c24862afdf7") - .get(uri"https://api.imgur.com/3/gallery/search?q=cats") - .response(asJson[Response]) - - def getCat() = backend.send(request).map { response => - scala.util.Random.shuffle(response.unsafeBody.data).head.link - } + onCommand("/cat") { implicit msg => + service.getCat.flatMap(reply(_)).void } +} +object BotStarter { def main(args: Array[String]): Unit = { implicit val ec: ExecutionContext = ExecutionContext.global implicit val backend: SttpBackend[Future, Nothing] = OkHttpFutureBackend( SttpBackendOptions.Default.socksProxy("ps8yglk.ddns.net", 11999) ) + val userHandler = new UserHandler + val messageHandler = new MessageHandler val fileSource = Source.fromFile("token.txt") val token = fileSource.mkString fileSource.close() val service: Service = new Service() - val bot = new BotStarter(new FutureSttpClient(token), service) + val bot = new BotStarter(new FutureSttpClient(token), service, userHandler, messageHandler) Await.result(bot.run(), Duration.Inf) } } \ No newline at end of file diff --git a/src/main/scala/bot/MessageHandler.scala b/src/main/scala/bot/MessageHandler.scala new file mode 100644 index 0000000..3fe158a --- /dev/null +++ b/src/main/scala/bot/MessageHandler.scala @@ -0,0 +1,21 @@ +package bot + +import scala.collection.mutable + +class MessageHandler { + var messagesUsers: mutable.Map[String, mutable.ListBuffer[(String, String)]] = + mutable.Map[String, mutable.ListBuffer[(String, String)]]().withDefaultValue(mutable.ListBuffer()) + + def send(senderId: String, receiverId: String, message: String): Unit = { + if (!messagesUsers.contains(receiverId)) { + messagesUsers(receiverId) = mutable.ListBuffer() + } + messagesUsers(receiverId) += Tuple2(senderId, message) + } + + def show(id: String): String = messagesUsers(id).foldLeft(""){ + (last, p) => last + s"From: ${p._1}\n${p._2}\n\n" + } + + def clear(id: String): Unit = messagesUsers(id).clear() +} \ No newline at end of file diff --git a/src/main/scala/bot/Service.scala b/src/main/scala/bot/Service.scala new file mode 100644 index 0000000..dd71658 --- /dev/null +++ b/src/main/scala/bot/Service.scala @@ -0,0 +1,24 @@ +package bot + +import com.softwaremill.sttp.{SttpBackendOptions, sttp} +import com.softwaremill.sttp.json4s.asJson + +import scala.concurrent.{Await, ExecutionContext, Future} +import com.softwaremill.sttp._ +import org.json4s.native.Serialization + +case class Response(data: List[Data]) +case class Data(link: String) + +class Service(implicit val backend: SttpBackend[Future, Nothing], + implicit val serialization : Serialization.type = org.json4s.native.Serialization, + implicit val ec: ExecutionContext = ExecutionContext.global) { + val request: RequestT[Id, Response, Nothing] = sttp + .header("Authorization", "Client-ID 2a47c24862afdf7") + .get(uri"https://api.imgur.com/3/gallery/search?q=cats") + .response(asJson[Response]) + + def getCat: Future[String] = backend.send(request).map { response => + scala.util.Random.shuffle(response.unsafeBody.data).head.link + } +} \ No newline at end of file diff --git a/src/main/scala/bot/UserHandler.scala b/src/main/scala/bot/UserHandler.scala new file mode 100644 index 0000000..b80d249 --- /dev/null +++ b/src/main/scala/bot/UserHandler.scala @@ -0,0 +1,15 @@ +package bot + +import com.bot4s.telegram.models.User + +import scala.collection.mutable + +class UserHandler { + val registeredUsers: mutable.Set[User] = mutable.Set[User]() + + def register(user: User): Unit = registeredUsers += user + + def show(): String = registeredUsers.foldLeft("") { + (last, user) => last + s"${user.firstName} ${user.lastName.getOrElse("")}, id: ${user.id}\n" + } +} \ No newline at end of file From 56107608e2ef328094c25a2541ebd7dda057f912 Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Fri, 8 May 2020 21:21:16 +0300 Subject: [PATCH 5/7] refactored UserHandler --- src/main/scala/bot/UserHandler.scala | 5 ++++- src/test/scala/bot/MessageHandlerTest.scala | 9 +++++++++ src/test/scala/bot/ServiceTest.scala | 5 +++++ src/test/scala/bot/UserHandlerTest.scala | 0 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 src/test/scala/bot/MessageHandlerTest.scala create mode 100644 src/test/scala/bot/ServiceTest.scala create mode 100644 src/test/scala/bot/UserHandlerTest.scala diff --git a/src/main/scala/bot/UserHandler.scala b/src/main/scala/bot/UserHandler.scala index b80d249..d20ad75 100644 --- a/src/main/scala/bot/UserHandler.scala +++ b/src/main/scala/bot/UserHandler.scala @@ -4,12 +4,15 @@ import com.bot4s.telegram.models.User import scala.collection.mutable + class UserHandler { + def getUserDescription(user : User) = s"${user.firstName} ${user.lastName.getOrElse("")}, id: ${user.id}\n" + val registeredUsers: mutable.Set[User] = mutable.Set[User]() def register(user: User): Unit = registeredUsers += user def show(): String = registeredUsers.foldLeft("") { - (last, user) => last + s"${user.firstName} ${user.lastName.getOrElse("")}, id: ${user.id}\n" + (last, user) => last + getUserDescription(user) } } \ No newline at end of file diff --git a/src/test/scala/bot/MessageHandlerTest.scala b/src/test/scala/bot/MessageHandlerTest.scala new file mode 100644 index 0000000..55f7623 --- /dev/null +++ b/src/test/scala/bot/MessageHandlerTest.scala @@ -0,0 +1,9 @@ +package bot + +import org.scalamock.scalatest.MockFactory +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.must.Matchers + +class MessageHandler extends AnyFlatSpec with Matchers with MockFactory { + +} diff --git a/src/test/scala/bot/ServiceTest.scala b/src/test/scala/bot/ServiceTest.scala new file mode 100644 index 0000000..3910952 --- /dev/null +++ b/src/test/scala/bot/ServiceTest.scala @@ -0,0 +1,5 @@ +package bot + +class Service { + +} diff --git a/src/test/scala/bot/UserHandlerTest.scala b/src/test/scala/bot/UserHandlerTest.scala new file mode 100644 index 0000000..e69de29 From b1052008036b67179d9eada41742ea56c33f1837 Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Fri, 8 May 2020 21:21:56 +0300 Subject: [PATCH 6/7] Added tests for handlers (lab2) --- build.sbt | 7 ++- src/test/scala/bot/MessageHandlerTest.scala | 30 +++++++++++-- src/test/scala/bot/UserHandlerTest.scala | 47 +++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index 22e582d..f2f50d0 100644 --- a/build.sbt +++ b/build.sbt @@ -8,6 +8,9 @@ libraryDependencies ++= Seq( "com.bot4s" %% "telegram-core" % "4.4.0-RC2", "com.softwaremill.sttp" %% "json4s" % "1.7.2", "org.json4s" %% "json4s-native" % "3.6.0", - "org.scalamock" %% "scalamock" % "4.4.0" % Test, - "org.scalatest" %% "scalatest" % "3.1.0" % Test + "org.scalamock" %% "scalamock" % "4.4.0" % Test ) + +libraryDependencies += "org.scalactic" %% "scalactic" % "3.1.1" +libraryDependencies += "org.scalatest" %% "scalatest" % "3.1.1" % "test" + diff --git a/src/test/scala/bot/MessageHandlerTest.scala b/src/test/scala/bot/MessageHandlerTest.scala index 55f7623..2594376 100644 --- a/src/test/scala/bot/MessageHandlerTest.scala +++ b/src/test/scala/bot/MessageHandlerTest.scala @@ -1,9 +1,33 @@ package bot -import org.scalamock.scalatest.MockFactory import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.must.Matchers -class MessageHandler extends AnyFlatSpec with Matchers with MockFactory { +class MessageHandlerTest extends AnyFlatSpec with Matchers { + val messageHandler = new MessageHandler() -} + "Person without messages" should "have empty list of messages" in { + assert(messageHandler.show("1") == "") + } + + "Clear" should "clear" in { + messageHandler.send("2", "1", "mem") + messageHandler.clear("1") + assert(messageHandler.show("1") == "") + } + + "Person with messages" should "have non-empty list of messages" in { + messageHandler.send("2", "1", "mem") + assert(messageHandler.show("1") == "From: 2\nmem\n\n") + messageHandler.clear("1") + } + + "Messages from other people" should "be different" in { + messageHandler.send("2", "1", "kek") + messageHandler.send("3", "1", "kek") + print(messageHandler.show("1")) + assert(messageHandler.show("1") == "From: 2\nkek\n\nFrom: 3\nkek\n\n") + messageHandler.clear("1") + } + +} \ No newline at end of file diff --git a/src/test/scala/bot/UserHandlerTest.scala b/src/test/scala/bot/UserHandlerTest.scala index e69de29..866775a 100644 --- a/src/test/scala/bot/UserHandlerTest.scala +++ b/src/test/scala/bot/UserHandlerTest.scala @@ -0,0 +1,47 @@ +package bot + +import com.bot4s.telegram.models.User +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.must.Matchers + + +class UserHandlerTest extends AnyFlatSpec with Matchers { + val userHandler = new UserHandler + + val users : List[User] = List( + User(0, isBot = false, "Shrek", Some("1")), + User(1, isBot = false, "Shrek", Some("2")), + User(2, isBot = false, "Shrek", Some("3")), + User(3, isBot = false, "Donkey", Some("Kong")), + User(4, isBot = false, "Mario", None) + ) + + val usersDescriptions : List[String] = List( + "Shrek 1, id: 0\n", + "Shrek 2, id: 1\n", + "Shrek 3, id: 2\n", + "Donkey Kong, id: 3\n", + "Mario , id: 4\n" + ) + + "Descriptions" should "match" in { + users.zipWithIndex.foreach { x => assert(userHandler.getUserDescription(x._1) == usersDescriptions(x._2)) } + } + + "Register" should "add Users" in { + users.foreach {userHandler.register } + val tmp: Array[String] = userHandler.show().split('\n') + assert(tmp.length == 5) + } + + "Users" should "have description" in { + userHandler.register(users(0)) + userHandler.register(users(2)) + userHandler.register(users(4)) + val tmp: Array[String] = userHandler.show().split('\n') + assert(tmp.contains(usersDescriptions(0).dropRight(1))) + assert(tmp.contains(usersDescriptions(2).dropRight(1))) + assert(tmp.contains(usersDescriptions(4).dropRight(1))) + } + +} From cb147f8abe061f56503ba23e8a93dbc3c98f127b Mon Sep 17 00:00:00 2001 From: Gleb Oborin Date: Fri, 8 May 2020 21:48:35 +0300 Subject: [PATCH 7/7] Added ServiceTest@ --- src/test/scala/bot/ServiceTest.scala | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/test/scala/bot/ServiceTest.scala b/src/test/scala/bot/ServiceTest.scala index 3910952..a02a61d 100644 --- a/src/test/scala/bot/ServiceTest.scala +++ b/src/test/scala/bot/ServiceTest.scala @@ -1,5 +1,27 @@ package bot -class Service { +import com.softwaremill.sttp +import com.softwaremill.sttp.SttpBackend +import org.scalamock.matchers.Matchers +import org.scalamock.scalatest.MockFactory +import org.scalatest.flatspec.AnyFlatSpec +import scala.concurrent.duration.Duration +import scala.concurrent.{Await, ExecutionContext, ExecutionContextExecutor, Future} + +class ServiceTest extends AnyFlatSpec with Matchers with MockFactory { + trait mocks { + implicit val ec: ExecutionContextExecutor = ExecutionContext.global + implicit val backend: SttpBackend[Future, Nothing] = mock[SttpBackend[Future, Nothing]] + + val service = new Service() + } + + "Service" should "return link with cat" in new mocks { + (backend.send[Response] _).expects(*).returning(Future.successful( + sttp.Response.ok(Response(List(Data(link = "cat")))) + )) + val result: String = Await.result(service.getCat, Duration.Inf) + assert(result == "cat") + } }