Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
language: minimal
dist: focal

branches:
only:
- master
- /^v\d+\.\d+(\.\d+)?$/

services:
- docker

Expand Down
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ val catsEffectVersion = "2.5.1"
val graalvmVersion = "21.1.0"
val http4sVersion = "0.21.22"
val jsoupVersion = "1.3.1"
val specs2Version = "4.12.0"

libraryDependencies ++= Seq(
"org.http4s" %% "http4s-blaze-client" % http4sVersion,
"org.http4s" %% "http4s-circe" % http4sVersion,
"org.http4s" %% "http4s-dsl" % http4sVersion,
"org.jsoup" % "jsoup" % jsoupVersion,
"org.typelevel" %% "cats-effect" % catsEffectVersion,
"org.graalvm.nativeimage" % "svm" % graalvmVersion % Provided
"org.graalvm.nativeimage" % "svm" % graalvmVersion % Provided,
"org.specs2" %% "specs2-core" % specs2Version % Test
)

scalacOptions ++= Seq(
Expand Down Expand Up @@ -89,7 +91,7 @@ graalVMNativeImageOptions ++= Seq(
"-J-Xms3072m", // Pass `-Xms3072m` directly to the JVM running the image generator
"-J-Xmx6144m",
"-H:+AddAllCharsets", // Make all hosted charsets available at run time.
"-H:+ReportExceptionStackTraces", // Show exception stack traces for exceptions during image building.).
"-H:+ReportExceptionStackTraces" // Show exception stack traces for exceptions during image building.).
// Find out all options at https://chriswhocodes.com/graalvm_native_image_ce_jdk11_options.html
)

Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/info/genghis/notifier/Main.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package info.genghis.notifier

import cats.effect._
import cats.implicits._
import info.genghis.notifier.config.AppConfig
import info.genghis.notifier.error.AppError
import info.genghis.notifier.monitor.GiftMonitor
import info.genghis.notifier.notifier.PagerDutyNotifier
import org.http4s.client.Client
import org.http4s.client.blaze.BlazeClientBuilder

Expand All @@ -23,7 +26,7 @@ object Main extends IOApp {
} yield ExitCode.Success

def handleError(x: IO[ExitCode]): IO[ExitCode] = x.handleErrorWith {
case err: AppError => IO(println(err.getMessage)).map(_ => ExitCode.Error)
case err: AppError => IO(println(err.show)).map(_ => ExitCode.Error)
case other => IO(other.printStackTrace()).map(_ => ExitCode.Error)
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/scala/info/genghis/notifier/error/AppError.scala
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package info.genghis.notifier.error

import cats.Show
import io.circe.Json
import org.http4s.Status

sealed trait AppError extends Throwable

object AppError {
implicit class AppErrorOps(appError: AppError) {
def getMessage(): String = appError.toString
override def toString: String = appError match {
case PagerDutyTriggerError(request, body) => s"Cannot trigger PagerDuty $request with body: $body"
case WatchingError(errStatus, errPayload) => s"Cannot watch destination web page, got $errStatus, $errPayload"
case MissingApplicationConfigError => "Missing required environment variables"
case NoDestinationGoodFoundError => "Cannot find the destination good"
}
implicit val showAppError: Show[AppError] = Show.show {
case PagerDutyTriggerError(request, body) => s"Cannot trigger PagerDuty $request with body: $body"
case WatchingError(errStatus, errPayload) => s"Cannot watch destination web page, got $errStatus, $errPayload"
case MissingApplicationConfigError => "Missing required environment variables"
case NoDestinationGoodFoundError => "Cannot find the destination good"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package info.genghis.notifier
package info.genghis.notifier.monitor

import cats.effect.Sync
import cats.implicits._
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package info.genghis.notifier
package info.genghis.notifier.notifier

import io.circe.syntax._
import io.circe.{Encoder, Json}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package info.genghis.notifier
package info.genghis.notifier.notifier

import cats.effect.Sync
import cats.implicits._
Expand Down
32 changes: 32 additions & 0 deletions src/test/scala/info/genghis/notifier/config/AppConfigSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package info.genghis.notifier.config

import cats.effect.IO
import info.genghis.notifier.error.MissingApplicationConfigError
import org.specs2.mutable.Specification

class AppConfigSpec extends Specification {

"AppConfigSpec" >> {
"loadConfigFrom" should {
"generate AppConfig if all environment variables are valid" in {
val env = Map(
"WATCH_URL" -> "http://watch.url",
"PAGER_DUTY_URL" -> "https://pagerduty.utl",
"PAGER_DUTY_API_KEY" -> "PAGERDUTY_API_KEY"
)
AppConfig.loadConfigFrom[IO](env).unsafeRunSync() must_== AppConfig(
"http://watch.url",
"https://pagerduty.utl",
"PAGERDUTY_API_KEY"
)
}

"raise error if some environment variables are missing" in {
val env = Map(
"WATCH_URL" -> "http://watch.url"
)
AppConfig.loadConfigFrom[IO](env).unsafeRunSync() must throwA(MissingApplicationConfigError)
}
}
}
}
26 changes: 26 additions & 0 deletions src/test/scala/info/genghis/notifier/error/AppErrorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package info.genghis.notifier.error

import cats.implicits._
import io.circe.Json
import org.http4s.Status
import org.specs2.mutable.Specification

class AppErrorSpec extends Specification {
"AppErrorSpec" >> {
"AppError" should {
"show error message" in {
val pagerDutyTriggerError: AppError = PagerDutyTriggerError("Test", Json.obj())
pagerDutyTriggerError.show must_== "Cannot trigger PagerDuty Test with body: {\n \n}"

val watchingError: AppError = WatchingError(Status.NotFound, "")
watchingError.show must_== "Cannot watch destination web page, got 404 Not Found, "

val missingApplicationConfigError: AppError = MissingApplicationConfigError
missingApplicationConfigError.show must_== "Missing required environment variables"

val noDestinationGoodFoundError: AppError = NoDestinationGoodFoundError
noDestinationGoodFoundError.show must_== "Cannot find the destination good"
}
}
}
}
Loading