pact4s.scalatest.InlineRequestResponsePactForging.scala Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2021 io.github.jbwheatley
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pact4s
package scalatest
import au.com.dius.pact.consumer.{BaseMockServer, PactTestExecutionContext}
import au.com.dius.pact.core.model.RequestResponsePact
import org.scalatest.{Suite, SuiteMixin}
import pact4s.Pact4sLogger.{notWritingPactMessage, pact4sLogger}
trait InlineRequestResponsePactForging extends InlineRequestResponsePactResources with SuiteMixin { self: Suite =>
sealed abstract class ForgerImpl extends InlineRequestResponsePactForger {
def apply[A](test: BaseMockServer => A): A
}
override private[pact4s] type Forger = ForgerImpl
override def withPact(aPact: RequestResponsePact): Forger =
new ForgerImpl {
override val pact: RequestResponsePact = aPact
override val pactTestExecutionContext: PactTestExecutionContext = self.pactTestExecutionContext
def apply[A](test: BaseMockServer => A): A = {
val server = createServer
validatePactVersion(mockProviderConfig.getPactVersion).left.foreach(throw _)
server.start()
server.waitForServer()
try {
val result = test(server)
self.beforeWritePacts().flatMap { _ =>
verifyResultAndWritePactFiles(server)
} match {
case Left(e) => throw e
case Right(_) => ()
}
result
} catch {
case e: Throwable =>
server.stop()
pact4sLogger.error(e)(
notWritingPactMessage(pact)
)
throw e
} finally server.stop()
}
}
override private[pact4s] type Effect[A] = Either[Throwable, A]
def beforeWritePacts(): Either[Throwable, Unit] = Right(())
}