diff --git a/src/main/scala/api/Endpoints.scala b/src/main/scala/api/Endpoints.scala index 51cb4d4..a6efb68 100644 --- a/src/main/scala/api/Endpoints.scala +++ b/src/main/scala/api/Endpoints.scala @@ -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 => diff --git a/src/test/scala/app/MainSpec.scala b/src/test/scala/app/MainSpec.scala index e209182..e5a689e 100644 --- a/src/test/scala/app/MainSpec.scala +++ b/src/test/scala/app/MainSpec.scala @@ -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()