:wave: Do we have any examples for pact provider v...
# general
d
đź‘‹ Do we have any examples for pact provider verifications for scala?
y
Assuming you are using pact4s, there are examples in the repo https://github.com/jbwheatley/pact4s/tree/main/example
d
Will try and let you know thanks
@Yousaf Nabi (pactflow.io) looking into the dependencies needed for the provider verification here, what dependencies exactly i needed, can you please confirm?
y
I’m not the maintainer of the repo, nor have used it. Have you read the readme? It shows 4 libraries I believe based on the unit testing framework you are using. hopefully it is one of those. these examples in that repo, have demos for each type it seems https://github.com/jbwheatley/pact4s?tab=readme-ov-file#getting-started You can file an issue against the repo, if you are stuck or ask in the #C02ACMH759Q channel, but I’m not sure the maintainer is active logging into Slack
d
I'm looking for the consumerVersionSelectors to use the pactUrl in CI instead using local json file. Do we have any imports for this? If yes can you give us an example? Right now I dont see anything in the link i shared above here CC @Jack Wheatley & @Yousaf Nabi (pactflow.io)
y
It doesn’t look to be implemented https://github.com/jbwheatley/pact4s/blob/main/models/src/main/scala/pact4s/provider/PactSource.scala It’s mentioned here
PactSource
is an ADT that provides various different configurations for fetching pacts, either from the local filesystem or from a Pact Broker.
d
So there is no way right now to verify in CI from the consumer pactURL?
And also we dont have the import from scala for this
pact4s.provider.Selectors.ConsumerVersionSelector
a
Hi Dilip Punna, if you are using a webhook you can transmit information like the branch of the consumer you want to verify from the broker to the CI job
Copy code
ConsumerVersionSelectors.branch(sys.env.getOrElse("CONSUMER_VERSION_BRANCH"))
you can import it from “import pact4s.provider.*”
FYI: We are using gitlab-ci
d
is this looks good to you?
Copy code
val provider: ProviderInfoBuilder = ProviderInfoBuilder(
    "responses-api-service",
    PactSource.PactBrokerWithSelectors(
      brokerUrl = PACT_BROKER_BASE_URL,
      consumerVersionSelectors = List(
        ConsumerVersionSelector(mainBranch = Some(true)),
        ConsumerVersionSelector(latest = true)
      ),
      brokerToken = Some(getPactBrokerToken)
    )
  ).withHost("localhost").withPort(1234).withStateManagementFunction(StateManagementFunction {
    case ProviderState("A request to get sessions", params) =>
      val name = params.get("name")
      name.foreach { n => <http://logger.info|logger.info>(s"Setting up state with name: $n") }
  }).withProviderVersion(providerVersion).withProviderBranch(providerVersionBranch)
    .withPendingEnabled(true).withFailIfNoPactsFound(false)
@Adrien DEVILLERS, tried the above by using the
"import pact4s.provider._
however, i still get this error
Copy code
ses/contract/provider/SessionProviderVerficiationTest.scala:81:9: not found: value ConsumerVersionSelector
[error]         ConsumerVersionSelector(mainBranch = Some(true)),
[error]         ^
[error] /Users/dpunna/Documents/Projects/analytics-responses-service/responsesApi/test/com/usertesting/analytics/responses/contract/provider/SessionProviderVerficiationTest.scala:81:33: not found: value mainBranch
[error]         ConsumerVersionSelector(mainBranch = Some(true)),
[error]                                 ^
[error] /Users/dpunna/Documents/Projects/analytics-responses-service/responsesApi/test/com/usertesting/analytics/responses/contract/provider/SessionProviderVerficiationTest.scala:82:9: not found: value ConsumerVersionSelector
[error]         ConsumerVersionSelector(latest = true)
[error]         ^
[error] /Users/dpunna/Documents/Projects/analytics-responses-service/responsesApi/test/com/usertesting/analytics/responses/contract/provider/SessionProviderVerficiationTest.scala:82:33: not found: value latest
[error]         ConsumerVersionSelector(latest = true)
a
which version of pact4s are you using?
d
Copy code
"io.github.jbwheatley" %% "pact4s-core" % "0.13.0" % Test,
      "io.github.jbwheatley" %% "pact4s-play-json" % "0.13.2" % Test,
      "io.github.jbwheatley" %% "pact4s-scalatest" % "0.13.2" % Test,
Is there any new version @Adrien DEVILLERS?
a
there is 0.15.0 yes, but I think it was working fine with 0.13.2 and scala 2.13.15 it the issue persists there might be an issue with your sbt
d
I tried the latest version 0.15.2 with the scala 2.13.15, here is my sbt
Copy code
.settings(dockerSettings("responses-api-service")).settings(logPlaySettings()).settings(
    name := "responsesApi",
    Test / parallelExecution := true,
    scalacOptions += "-deprecation",
    libraryDependencies ++= Seq(
      "com.fasterxml.jackson.module" % "jackson-module-scala_2.13" % "2.17.2",
      "com.nimbusds" % "nimbus-jose-jwt" % "9.40",
      "com.nimbusds" % "oauth2-oidc-sdk" % "11.12.1",
      "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion %
        "protobuf",
      "com.typesafe" % "config" % "1.4.3",
      "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5",
      "net.logstash.logback" % "logstash-logback-encoder" % "8.0",

      //Test dependencies
      "org.typelevel" %% "cats-effect" % "3.5.6" % Test,
      "org.http4s" %% "http4s-core" % "0.23.29" % Test,
      "org.http4s" %% "http4s-ember-server" % "0.23.29" % Test,
      "org.http4s" %% "http4s-dsl" % "0.23.29" % Test,
      "io.github.jbwheatley" %% "pact4s-core" % "0.15.2" % Test,
      "io.github.jbwheatley" %% "pact4s-scalatest" % "0.15.2" % Test,
    ),
     ContractProviderTest / testOptions := Seq(Tests.Filter(providerContractTestsFilter)),
  ).dependsOn(responsesCommon % "test->test;compile->compile")
  .enablePlugins(PlayScala, SwaggerPlugin)
complaining about not exist
Copy code
error] /Users/dpunna/Documents/Projects/analytics-responses-service/responsesApi/test/com/usertesting/analytics/responses/contract/provider/SessionProviderVerficiationTest.scala:80:42: not found: value consumerVersionSelectors
[error]       .withConsumerVersionSelectors(List(consumerVersionSelectors(mainBranch = Some(true))))
a
Ok no your setup is fine, the error is with the way you declare your ConsumerVersionSelector, try this :
Copy code
ConsumerVersionSelectors().mainBranch
d
is this the right way to do it? Now its complaining about the withProviderVersion and other stuff, please confirm
Copy code
scala-2.13/test-classes ...
[error] /Users/dpunna/Documents/Projects/analytics-responses-service/responsesApi/test/com/usertesting/analytics/responses/contract/provider/SessionProviderVerficiationTest.scala:85:6: value withProviderVersion is not a member of pact4s.provider.ProviderInfoBuilder
[error] possible cause: maybe a semicolon is missing before `value withProviderVersion`?
[error]   }).withProviderVersion(providerVersion).withProviderBranch(providerVersionBranch)
[error]      ^
[error] one error found
a
given the code you posted , I would do as follows:
Copy code
ProviderInfoBuilder(
  "responses-api-service",
  PactSource.PactBrokerWithSelectors(brokerUrl = "PACT_BROKER_BASE_URL")
    .withConsumerVersionSelectors(
      ConsumerVersionSelectors().mainBranch
    )
  )
d
what about having the providerVersion and withFailIfNopacsFound?
I would like to pass pactBrokerToken and also providerVersion and providerBranch. But it looks like there is no support for this?
And also how can i publish verification results from here, right now it says
None
only
Copy code
verifyPacts(
      publishVerificationResults = None,
      providerVerificationOptions = Nil,
      verificationTimeout = Some(10.seconds)
I need to pass
brokerToken
, right now it is not valid if i pass that value. Can you help here please @Adrien DEVILLERS
a
I don’t know about all these options and what you’re trying to do with the ProviderInfoBuilder. For the verify methods I have something like this :
Copy code
verifyPacts(
  providerBranch = Some(Branch(sys.env.getOrElse("CI_COMMIT_REF_NAME", "local"))),
  publishVerificationResults = Some(PublishVerificationResults(buildInfoVersion)),
  providerVerificationOptions = List.empty,
  verificationTimeout = Some(10.seconds)
)