This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
230 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
package com.example.integrationtest | ||
|
||
import io.github.jjba23.zzspec.ZZSpec | ||
import io.github.jjba23.zzspec.ZZSpec.{ containerLogger, networkLayer } | ||
import io.github.jjba23.zzspec.mockserver._ | ||
import io.github.jjba23.zzspec.postgresql._ | ||
import zio._ | ||
import zio.test._ | ||
import java.util.UUID | ||
import com.example.domain.SomeRepository | ||
import com.example.persistence.PostgreSQLSomeRepository | ||
import slick.jdbc.JdbcBackend | ||
import com.example.domain.Some | ||
import com.example.persistence.Flyway | ||
import org.mockserver.mock.Expectation | ||
|
||
object BlackBoxSpec extends ZIOSpecDefault { | ||
def spec: Spec[Any, Throwable] = | ||
suite("Some Job - BlackBox specs")( | ||
happyFlowEmptyListSpec, | ||
happyFlowListOf2Spec, | ||
happyFlowListOf1OneAlreadyExistsSpec, | ||
) @@ TestAspect.withLiveConsole @@ TestAspect.sequential // use parallel if enough horsepower | ||
|
||
private val keycloakReturnsTokenRestBehaviour: ZIO[MockServer.Client with MockServer.Client, Throwable, Array[Expectation]] = | ||
ZIO.serviceWithZIO[MockServer.Client](mockServer => | ||
ZIO.attempt( | ||
mockServer.value.when(org.mockserver.model.HttpRequest.request().withPath("/realms/some/protocol/openid-connect/token")).respond( | ||
org.mockserver.model.HttpResponse.response() | ||
.withBody(""" {"access_token": "some-super-secret-token"} """) | ||
.withStatusCode(200), | ||
), | ||
), | ||
) | ||
|
||
private val prometheusPushingReturnsOkRestBehaviour: ZIO[MockServer.Client with MockServer.Client, Throwable, Array[Expectation]] = | ||
ZIO.serviceWithZIO[MockServer.Client](mockServer => | ||
ZIO.attempt( | ||
mockServer.value.when( | ||
org.mockserver.model.HttpRequest.request().withPath("/metrics/job/someJob"), | ||
).respond(org.mockserver.model.HttpResponse.response().withStatusCode(200)), | ||
), | ||
) | ||
|
||
private def happyFlowEmptyListSpec: Spec[Any, Throwable] = cssofjBlackBoxTest( | ||
specName = "Happy flow empty list of onboarding failures returned", | ||
mockServerBehaviour = ZIO.serviceWithZIO[MockServer.Client](mockServer => | ||
keycloakReturnsTokenRestBehaviour *> prometheusPushingReturnsOkRestBehaviour | ||
*> ZIO.attempt( | ||
mockServer.value.when(org.mockserver.model.HttpRequest.request().withPath("/api/v1/some-endpoint")) | ||
.respond( | ||
org.mockserver.model.HttpResponse.response().withStatusCode(200) | ||
.withBody(""" | ||
{ | ||
"httpStatusCode": 200, | ||
"timestamp": "1970-01-01T00:00:00Z", | ||
"data": { | ||
"someData":[] | ||
} | ||
} | ||
"""), | ||
), | ||
), | ||
), | ||
) | ||
|
||
private def happyFlowListOf2Spec: Spec[Any, Throwable] = cssofjBlackBoxTest( | ||
specName = "Happy flow list of 2 onboarding failures returned", | ||
mockServerBehaviour = | ||
keycloakReturnsTokenRestBehaviour *> prometheusPushingReturnsOkRestBehaviour *> | ||
ZIO.serviceWithZIO[MockServer.Client](mockServer => | ||
ZIO.attempt( | ||
mockServer.value.when(org.mockserver.model.HttpRequest.request().withPath("/api/v1/some-endpoint")) | ||
.respond(org.mockserver.model.HttpResponse.response().withStatusCode(200).withBody(""" | ||
{ | ||
"httpStatusCode": 200, | ||
"timestamp": "1970-01-01T00:00:00Z", | ||
"data": { | ||
"someData": [ | ||
{ "id": "7104b44c-c7d3-499d-aed3-7df074f37ed8", | ||
"creationTime": "1970-01-01T00:00:00Z" | ||
}, | ||
{ "id": "a8163ec8-3b07-44a5-8390-51267622c879", | ||
"creationTime": "1970-01-01T00:00:00Z" | ||
} | ||
] | ||
} | ||
} | ||
""")), | ||
), | ||
), | ||
postgreSQLChecks = | ||
for { | ||
repo <- ZIO.service[SomeRepository] | ||
maybeFirstRow <- repo.getByIds(Set(UUID.fromString("7104b44c-c7d3-499d-aed3-7df074f37ed8"))) | ||
maybeSecondRow <- repo.getByIds(Set(UUID.fromString("a8163ec8-3b07-44a5-8390-51267622c879"))) | ||
nonAlerted <- repo.getNonAlerted.map(_.map(_.map(_.id))) | ||
|
||
} yield assertTrue( | ||
maybeFirstRow.map(_.headOption.map(_.id)) == | ||
Right(Some(UUID.fromString("7104b44c-c7d3-499d-aed3-7df074f37ed8"))), | ||
maybeSecondRow.map(_.headOption.map(_.id)) == | ||
Right(Some(UUID.fromString("a8163ec8-3b07-44a5-8390-51267622c879"))), | ||
), | ||
) | ||
|
||
private def happyFlowListOf1OneAlreadyExistsSpec: Spec[Any, Throwable] = cssofjBlackBoxTest( | ||
specName = "Happy flow list of 1 onboarding failures returned, one non-alerted was already present in DB", | ||
mockServerBehaviour = ZIO.serviceWithZIO[MockServer.Client](mockServer => | ||
keycloakReturnsTokenRestBehaviour *> prometheusPushingReturnsOkRestBehaviour *> ZIO.attempt( | ||
mockServer.value.when(org.mockserver.model.HttpRequest.request().withPath("/api/v1/onboarding-failures")) | ||
.respond(org.mockserver.model.HttpResponse.response().withStatusCode(200).withBody(""" | ||
{ | ||
"httpStatusCode": 200, | ||
"timestamp": "1970-01-01T00:00:00Z", | ||
"data": { | ||
"someData": [ | ||
{ "id": "7104b44c-c7d3-499d-aed3-7df074f37ed8", | ||
"creationTime": "1970-01-01T00:00:00Z" | ||
}, | ||
{ "id": "a8163ec8-3b07-44a5-8390-51267622c879", | ||
"creationTime": "1970-01-01T00:00:00Z" | ||
} | ||
] | ||
} | ||
} | ||
""")), | ||
), | ||
), | ||
postgreSQLChecks = | ||
for { | ||
repo <- ZIO.service[SomeRepository] | ||
maybeFirstRow <- repo.getByIds(Set(UUID.fromString("7104b44c-c7d3-499d-aed3-7df074f37ed8"))) | ||
maybeSecondRow <- repo.getByIds(Set(UUID.fromString("a8163ec8-3b07-44a5-8390-51267622c879"))) | ||
nonAlerted <- repo.getNonAlerted.map(_.map(_.map(_.id))) | ||
} yield assertTrue( | ||
maybeFirstRow.map(_.headOption.map(_.id)) == | ||
Right(Some(UUID.fromString("7104b44c-c7d3-499d-aed3-7df074f37ed8"))), | ||
maybeSecondRow.map(_.headOption.map(_.id)) == | ||
Right(Some(UUID.fromString("a8163ec8-3b07-44a5-8390-51267622c879"))), | ||
), | ||
postgreSQLState = for { | ||
_ <- Flyway.migrate() | ||
repo <- ZIO.service[SomeRepository] | ||
now <- Clock.instant | ||
insertRes <- repo.save(Seq(Some( | ||
id = UUID.fromString("7104b44c-c7d3-499d-aed3-7df074f37ed8"), | ||
creationTime = java.time.Instant.EPOCH | ||
))) | ||
_ <- ZIO.logInfo(insertRes.toString()) | ||
} yield (), | ||
) | ||
|
||
private def cssofjBlackBoxTest( | ||
specName: String, | ||
mockServerBehaviour: ZIO[MockServer.Client, Throwable, Unit], | ||
postgreSQLChecks: ZIO[JdbcBackend.Database with SomeRepository, Throwable, TestResult] = | ||
assertTrue(true), | ||
postgreSQLState: ZIO[JdbcBackend.Database with SomeRepository, Throwable, Unit] = ZIO.unit, | ||
): Spec[Any, Throwable] = | ||
test(specName) { | ||
for { | ||
postgreSQL <- ZIO.service[PostgreSQLContainer.Container] | ||
mockServer <- ZIO.service[MockServer.Client] | ||
|
||
_ <- ZIO.logInfo("preparing PostgreSQL state") | ||
_ <- postgreSQLState | ||
|
||
_ <- ZIO.logInfo("preparing MockServer behaviour") | ||
_ <- mockServerBehaviour | ||
|
||
_ <- ZIO.logInfo("starting an sbt module run") | ||
// effectively doing: sbt "someJob/run" | ||
_ <- ZZSpec.runSbtModule(ZZSpec.SbtModuleRun( | ||
sbtTask = "run", | ||
moduleName = Some("someJob"), | ||
env = Map( | ||
( | ||
"POSTGRES_JDBC_URL", | ||
s"jdbc:postgresql://${postgreSQL.value.getHost()}:${postgreSQL.value.getMappedPort(5432)}/${postgreSQL.value.getDatabaseName()}?search_path=some_job", | ||
), | ||
("POSTGRES_USER", postgreSQL.value.getUsername()), | ||
("POSTGRES_PASSWORD", postgreSQL.value.getPassword()), | ||
("PUSHGATEWAY_HOST", mockServer.value.remoteAddress().getHostString()), | ||
("PUSHGATEWAY_PORT", mockServer.value.getPort().toString()), | ||
( | ||
"SOME_SERVICE_URL", | ||
s"http://${mockServer.value.remoteAddress().getHostString()}:${mockServer.value.getPort()}", | ||
), | ||
("KEYCLOAK_AUTH_SERVER_URL", s"http://${mockServer.value.remoteAddress().getHostString()}:${mockServer.value.getPort()}"), | ||
), | ||
testLocation = "./", | ||
)) | ||
|
||
_ <- ZIO.logInfo("performing PostgreSQL checks") | ||
persistenceChecks <- postgreSQLChecks | ||
} yield assertTrue(true) && persistenceChecks | ||
}.provide( | ||
networkLayer, | ||
containerLogger(), | ||
PostgreSQLContainer.Settings.layer(), | ||
PostgreSQLContainer.layer(), | ||
MockServerContainer.layer(), | ||
MockServer.layer, | ||
PostgreSQLPool.layer(schema = "some_job"), | ||
PostgreSQLSomeRepository.layer, | ||
) @@ TestAspect.tag("blackbox") | ||
} |