Skip to content

Commit

Permalink
feat(tests): fix tests using service integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sobakavosne committed Nov 12, 2024
1 parent 4e9f3e2 commit 421a5ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/api/Endpoints.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import core.errors.http.ReactionError
class Endpoints(
reactionService: ReactionService[IO],
mechanismService: MechanismService[IO]
){
) {

private val getReactionRoute: HttpRoutes[IO] = HttpRoutes.of[IO] {
case GET -> Root / "reaction" / id =>
Expand Down
48 changes: 36 additions & 12 deletions src/test/scala/app/MainSpec.scala
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
package app

import api.Endpoints
import api.ServerBuilder
import cats.effect.IO
import api.{Endpoints, ServerBuilder}
import cats.effect.{IO, Resource}
import cats.effect.unsafe.implicits.global
import com.comcast.ip4s.Host
import com.comcast.ip4s.Port
import com.comcast.ip4s.{Host, Port}
import core.services.{CacheService, MechanismService, ReactionService}
import org.http4s.ember.client.EmberClientBuilder
import org.http4s.Uri
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec
import org.typelevel.log4cats.slf4j.Slf4jLogger
import org.typelevel.log4cats.Logger

class MainSpec extends AsyncWordSpec with Matchers with BeforeAndAfterAll {

implicit val logger: Logger[IO] = Slf4jLogger.getLogger[IO]

"Main" should {
"start the http4s server as a Resource" in {
implicit val endpoints = new Endpoints
val serverBuilder = new ServerBuilder
val maybeHost = Host.fromString("0.0.0.0")
val maybePort = Port.fromInt(8081)
val bindingResource = serverBuilder.startServer(maybeHost.get, maybePort.get)

bindingResource
val maybeHost = Host.fromString("0.0.0.0").get
val maybePort = Port.fromInt(8081).get
val baseUri = Uri.unsafeFromString("http://localhost:8081")

val serverResource = for {
client <- EmberClientBuilder.default[IO].build
cacheService <- Resource.make(
IO(new CacheService[IO])
)(_ => IO.unit)
mechanismService <- Resource.make(
IO(new MechanismService[IO](client, cacheService, baseUri / "mechanism"))
)(_ => IO.unit)
reactionService <- Resource.make(
IO(new ReactionService[IO](client, cacheService, baseUri / "reaction"))
)(_ => IO.unit)
endpoints <- Resource.make(
IO(new Endpoints(reactionService, mechanismService))
)(_ => IO.unit)
serverBuilder <- Resource.make(
IO(new ServerBuilder(endpoints))
)(_ => IO.unit)
server <- serverBuilder.startServer(maybeHost, maybePort)
} yield server

serverResource
.use { server =>
IO {
server.address.getPort shouldEqual 8081
// More tests here to verify specific endpoint behaviour
// Additional tests can be added here to verify endpoint behaviour
}
}
.unsafeToFuture()
Expand Down

0 comments on commit 421a5ac

Please sign in to comment.