Skip to content

Commit

Permalink
Testing API with running micro
Browse files Browse the repository at this point in the history
  • Loading branch information
pondzix committed Feb 15, 2024
1 parent d6f241f commit bdc56cd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/resources/collector-micro.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ collector {
accept = ${?ACCEPT_LIMITED_USE_LICENSE}
}
interface = "0.0.0.0"
port = 8080
port = 9090

streams {
good = "good"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.snowplowanalytics.snowplow.micro

import cats.effect.IO
import cats.effect.kernel.Resource
import cats.effect.testing.specs2.CatsEffect
import io.circe.Json
import org.http4s.Method.GET
import org.http4s.Request
import org.http4s.blaze.client.BlazeClientBuilder
import org.http4s.circe.CirceEntityCodec.circeEntityDecoder
import org.http4s.client.Client
import org.http4s.implicits.http4sLiteralsSyntax
import org.specs2.mutable.Specification
import org.specs2.specification.AfterEach

import scala.concurrent.duration.{Duration, DurationInt}

class MicroApiSpec extends Specification with CatsEffect with AfterEach {
sequential

override protected val Timeout: Duration = 100.seconds

override protected def after: Unit = {
ValidationCache.reset()
}

"Micro should accept good data" in {
setup().use { client =>
for {
_ <- client.run(Request(GET, uri"http://localhost:9090/i?e=pp&p=web&tv=lol")).use_
all <- client.run(Request(GET, uri"http://localhost:9090/micro/all")).use(_.as[Json])
good <- client.run(Request(GET, uri"http://localhost:9090/micro/good")).use(_.as[Json])
} yield {

all.noSpaces must beEqualTo("""{"total":1,"good":1,"bad":0}""")
good.isArray must beTrue
good.asArray.map(_.length).get must beEqualTo(1)

val firstEvent = good.hcursor.downN(0)
firstEvent.downField("eventType").as[String] must beRight("page_ping")
firstEvent.downField("schema").as[String] must beRight("iglu:com.snowplowanalytics.snowplow/page_ping/jsonschema/1-0-0")
firstEvent.downField("event").downField("platform").as[String] must beRight("web")
firstEvent.downField("event").downField("v_tracker").as[String] must beRight("lol")
}
}
}

"Micro should handle bad data" in {
setup().use { client =>
for {
_ <- client.run(Request(GET, uri"http://localhost:9090/i?e=pp&p=web&tv=lol&eid=invalidEventId")).use_
all <- client.run(Request(GET, uri"http://localhost:9090/micro/all")).use(_.as[Json])
bad <- client.run(Request(GET, uri"http://localhost:9090/micro/bad")).use(_.as[Json])
} yield {

bad.isArray must beTrue
bad.asArray.map(_.length).get must beEqualTo(1)
all.noSpaces must beEqualTo("""{"total":1,"good":0,"bad":1}""")
}
}
}

"Micro should reset stored data" in {
setup().use { client =>
for {
_ <- client.run(Request(GET, uri"http://localhost:9090/i?e=pp&p=web&tv=lol")).use_
_ <- client.run(Request(GET, uri"http://localhost:9090/i?e=pp&p=web&tv=lol&eid=invalidEventId")).use_
beforeReset <- client.run(Request(GET, uri"http://localhost:9090/micro/all")).use(_.as[Json])
afterReset <- client.run(Request(GET, uri"http://localhost:9090/micro/reset")).use(_.as[Json])
} yield {

beforeReset.noSpaces must beEqualTo("""{"total":2,"good":1,"bad":1}""")
afterReset.noSpaces must beEqualTo("""{"total":0,"good":0,"bad":0}""")
}
}
}


private def setup(): Resource[IO, Client[IO]] = {
for {
_ <- Main.run(List.empty).background
client <- BlazeClientBuilder.apply[IO].resource
_ <- Resource.sleep[IO](1.seconds)
} yield client

}
}

0 comments on commit bdc56cd

Please sign in to comment.