-
Notifications
You must be signed in to change notification settings - Fork 360
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
1 parent
b568fac
commit 0abcaff
Showing
7 changed files
with
447 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
c# pact4s [Under construction] | ||
|
||
pact4s is used for contract testing. | ||
|
||
# Dependencies | ||
|
||
```scala | ||
val pact4sDependencies = Seq( | ||
pact4sScalaTest, | ||
pact4sCirce, | ||
http4sEmberClient, | ||
http4sDsl, | ||
http4sEmberServer, | ||
http4sCirce, | ||
circeCore, | ||
typelevelCat, | ||
scalaTest | ||
) | ||
|
||
lazy val pact4s = project.in(file("pact4s")) | ||
.settings(pact4sSettings) | ||
.dependsOn(http % "test->test;compile->compile") | ||
``` | ||
|
||
## Building and running contract tests | ||
Clone the repo. | ||
``` | ||
$ git clone https://github.com/DataBiosphere/cromwell.git | ||
$ cd cromwell | ||
``` | ||
|
||
If you are already using OpenJDK 17, run the following command. | ||
``` | ||
$ sbt "project pact4s" clean test | ||
``` | ||
|
||
Otherwise, you can run the command inside a docker container with OpenJDK 17 installed. | ||
This is especially useful when automating contract tests in a GitHub Action runner which does not guarantee the correct OpenJDK version. | ||
``` | ||
docker run --rm -v $PWD:/working \ | ||
-v jar-cache:/root/.ivy \ | ||
-v jar-cache:/root/.ivy2 \ | ||
-w /working \ | ||
sbtscala/scala-sbt:openjdk-17.0.2_1.7.2_2.13.10 \ | ||
sbt "project pact4s" clean test | ||
``` | ||
|
||
The generated contracts can be found in the `./target/pacts` folder | ||
- `cromwell-consumer-drshub-provider.json` | ||
- `cromwell-consumer-fake-provider.json` | ||
|
38 changes: 38 additions & 0 deletions
38
pact4s/src/main/scala/org/broadinstitute/dede/workbench/leonardo/consumer/DrsHubClient.scala
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,38 @@ | ||
package org.broadinstitute.dede.workbench.leonardo.consumer | ||
|
||
import cats.effect.kernel.Concurrent | ||
import cats.syntax.all._ | ||
import io.circe.Decoder | ||
import org.broadinstitute.dsde.workbench.leonardo.DrsHubResourceType | ||
import org.broadinstitute.dsde.workbench.leonardo.dao.HttpDrsHubDAO._ | ||
import org.broadinstitute.dsde.workbench.leonardo.dao.ListResourceResponse | ||
import org.broadinstitute.dsde.workbench.util.health.StatusCheckResponse | ||
import org.http4s.Credentials.Token | ||
import org.http4s._ | ||
import org.http4s.circe.CirceEntityCodec.circeEntityDecoder | ||
import org.http4s.client.Client | ||
import org.http4s.headers.Authorization | ||
|
||
trait DrsHubClient[F[_]] { | ||
def fetchSystemStatus(): F[StatusCheckResponse] | ||
} | ||
|
||
/* | ||
This class represents the consumer (Cromwell) view of the DrsHub provider that implements the following endpoints: | ||
- GET /status | ||
*/ | ||
class DrsHubClientImpl[F[_]: Concurrent](client: Client[F], baseUrl: Uri, bearer: Token) extends DrsHubClient[F] { | ||
|
||
override def fetchSystemStatus(): F[StatusCheckResponse] = { | ||
val request = Request[F](uri = baseUrl / "status").withHeaders( | ||
org.http4s.headers.Accept(MediaType.application.json) | ||
) | ||
client.run(request).use { resp => | ||
resp.status match { | ||
case Status.Ok => resp.as[StatusCheckResponse] | ||
case Status.InternalServerError => resp.as[StatusCheckResponse] | ||
case _ => UnknownError.raiseError | ||
} | ||
} | ||
} | ||
} |
200 changes: 200 additions & 0 deletions
200
pact4s/src/main/scala/org/broadinstitute/dede/workbench/leonardo/consumer/Helper.scala
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,200 @@ | ||
package org.broadinstitute.dede.workbench.leonardo.consumer | ||
|
||
import au.com.dius.pact.consumer.dsl.{DslPart, PactDslResponse, PactDslWithProvider} | ||
import org.broadinstitute.dsde.workbench.model.WorkbenchEmail | ||
import org.http4s.Credentials.Token | ||
import org.http4s.{AuthScheme, Credentials} | ||
import pact4s.algebras.PactBodyJsonEncoder | ||
|
||
case object InvalidCredentials extends Exception | ||
|
||
case object UserAlreadyExists extends Exception | ||
|
||
case object UnknownError extends Exception | ||
|
||
object AuthHelper { | ||
def mockBearerHeader(workbenchEmail: WorkbenchEmail) = s"Bearer TokenFor$workbenchEmail" | ||
def mockAuthToken(workbenchEmail: WorkbenchEmail): Token = | ||
Credentials.Token(AuthScheme.Bearer, s"TokenFor$workbenchEmail") | ||
} | ||
|
||
object PactHelper { | ||
def buildInteraction(builder: PactDslResponse, | ||
state: String, | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int, | ||
responseHeaders: Seq[(String, String)], | ||
body: DslPart | ||
): PactDslResponse = | ||
builder | ||
.`given`(state) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) | ||
.body(body) | ||
|
||
def buildInteraction(builder: PactDslResponse, | ||
state: String, | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int | ||
): PactDslResponse = | ||
builder | ||
.`given`(state) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
|
||
def buildInteraction(builder: PactDslResponse, | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int | ||
): PactDslResponse = | ||
builder | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
|
||
def buildInteraction(builder: PactDslWithProvider, | ||
state: String, | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int, | ||
responseHeaders: Seq[(String, String)], | ||
body: DslPart | ||
): PactDslResponse = | ||
builder | ||
.`given`(state) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) | ||
.body(body) | ||
|
||
def buildInteraction(builder: PactDslWithProvider, | ||
state: String, | ||
stateParams: Map[String, Any], | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int, | ||
responseHeaders: Seq[(String, String)], | ||
body: DslPart | ||
): PactDslResponse = | ||
builder | ||
.`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) | ||
.body(body) | ||
|
||
def buildInteraction(builder: PactDslResponse, | ||
state: String, | ||
stateParams: Map[String, Any], | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int, | ||
responseHeaders: Seq[(String, String)], | ||
body: DslPart | ||
): PactDslResponse = | ||
builder | ||
.`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) | ||
.body(body) | ||
|
||
def buildInteraction[A](builder: PactDslWithProvider, | ||
state: String, | ||
stateParams: Map[String, Any], | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
status: Int, | ||
responseHeaders: Seq[(String, String)], | ||
body: A | ||
)(implicit ev: PactBodyJsonEncoder[A]): PactDslResponse = | ||
builder | ||
.`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.willRespondWith() | ||
.status(status) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(responseHeaders.toMap).asJava) | ||
.body(ev.toJsonString(body)) | ||
|
||
def buildInteraction[A](builder: PactDslResponse, | ||
state: String, | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
requestBody: A, | ||
status: Int | ||
)(implicit ev: PactBodyJsonEncoder[A]): PactDslResponse = | ||
builder | ||
.`given`(state) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.body(ev.toJsonString(requestBody)) | ||
.willRespondWith() | ||
.status(status) | ||
|
||
def buildInteraction[A](builder: PactDslResponse, | ||
state: String, | ||
stateParams: Map[String, Any], | ||
uponReceiving: String, | ||
method: String, | ||
path: String, | ||
requestHeaders: Seq[(String, String)], | ||
requestBody: A, | ||
status: Int | ||
)(implicit ev: PactBodyJsonEncoder[A]): PactDslResponse = | ||
builder | ||
.`given`(state, scala.jdk.CollectionConverters.MapHasAsJava(stateParams).asJava) | ||
.uponReceiving(uponReceiving) | ||
.method(method) | ||
.path(path) | ||
.headers(scala.jdk.CollectionConverters.MapHasAsJava(requestHeaders.toMap).asJava) | ||
.body(ev.toJsonString(requestBody)) | ||
.willRespondWith() | ||
.status(status) | ||
} |
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,14 @@ | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d [%thread] %-5level %logger{35} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
<logger name="io.github.jbwheatley.pact4s.Pact4sLogger" level="INFO" /> | ||
<logger name="au.com.dius.pact.consumer" level="DEBUG"/> | ||
<logger name="au.com.dius.pact.provider" level="DEBUG"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |
Oops, something went wrong.