Skip to content

Commit

Permalink
chore(pact-jvm-server): Converted PactSessionResults to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Oct 30, 2024
1 parent 21c9978 commit d154527
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package au.com.dius.pact.server

import au.com.dius.pact.core.matchers.PartialRequestMatch
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.Request
import scala.collection.JavaConverters.asJavaIterable

data class PactSessionResults(
val matched: List<Interaction>,
val almostMatched: List<PartialRequestMatch>,
val missing: List<Interaction>,
val unexpected: List<Request>
) {
fun addMatched(inter: Interaction) = copy(matched = listOf(inter) + matched)
fun addUnexpected(request: Request) = copy(unexpected = listOf(request) + unexpected)
fun addMissing(inters: scala.collection.Iterable<Interaction>) = copy(missing = asJavaIterable(inters).toList() + missing)
fun addAlmostMatched(partial: PartialRequestMatch) = copy(almostMatched = listOf(partial) + almostMatched)

fun allMatched(): Boolean = missing.isEmpty() && unexpected.isEmpty()

companion object {
@JvmStatic
val empty = PactSessionResults(emptyList(), emptyList(), emptyList(), emptyList())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,10 @@ import au.com.dius.pact.core.matchers.{FullRequestMatch, PartialRequestMatch, Re
import au.com.dius.pact.core.model.{Interaction, OptionalBody, Request, RequestResponseInteraction, Response, Pact => PactModel}
import org.apache.commons.lang3.StringEscapeUtils

object PactSessionResults {
val empty = PactSessionResults(Nil, Nil, Nil, Nil)
}

case class PactSessionResults(
matched: List[Interaction],
almostMatched: List[PartialRequestMatch],
missing: List[Interaction],
unexpected: List[Request]) {

def addMatched(inter: Interaction) = copy(matched = inter :: matched)
def addUnexpected(request: Request) = copy(unexpected = request :: unexpected)
def addMissing(inters: Iterable[Interaction]) = copy(missing = inters ++: missing)
def addAlmostMatched(partial: PartialRequestMatch) = copy(almostMatched = partial :: almostMatched)

def allMatched: Boolean = missing.isEmpty && unexpected.isEmpty
}

object PactSession {
val empty = PactSession(None, PactSessionResults.empty)
val empty = PactSession(None, PactSessionResults.getEmpty)

def forPact(pact: PactModel) = PactSession(Some(pact), PactSessionResults.empty)
def forPact(pact: PactModel) = PactSession(Some(pact), PactSessionResults.getEmpty)
}

case class PactSession(expected: Option[PactModel], results: PactSessionResults) {
Expand Down Expand Up @@ -67,5 +49,5 @@ case class PactSession(expected: Option[PactModel], results: PactSessionResults)

def withTheRestMissing: PactSession = PactSession(None, remainingResults)

def remainingResults: PactSessionResults = results.addMissing(expected.get.getInteractions.asScala diff results.matched)
def remainingResults: PactSessionResults = results.addMissing(expected.get.getInteractions.asScala diff results.getMatched.asScala)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package au.com.dius.pact.server

import au.com.dius.pact.core.model.RequestResponseInteraction

import scala.collection.JavaConverters._
import scala.util.Failure
import scala.util.Success
import scala.util.Try
Expand All @@ -19,9 +20,9 @@ sealed trait VerificationResult {
override def toString() = this match {
case PactVerified => "Pact verified."
case PactMismatch(results, error) => s"""
|Missing: ${results.missing.map(_.asInstanceOf[RequestResponseInteraction].getRequest)}\n
|AlmostMatched: ${results.almostMatched}\n
|Unexpected: ${results.unexpected}\n"""
|Missing: ${results.getMissing.asScala.map(_.asInstanceOf[RequestResponseInteraction].getRequest)}\n
|AlmostMatched: ${results.getAlmostMatched.asScala}\n
|Unexpected: ${results.getUnexpected.asScala}\n"""
case PactError(error) => s"${error.getClass.getName} ${error.getMessage}"
case UserCodeFailed(error) => s"${error.getClass.getName} $error"
}
Expand All @@ -32,18 +33,18 @@ object PactVerified extends VerificationResult
case class PactMismatch(results: PactSessionResults, userError: Option[Throwable] = None) extends VerificationResult {
override def toString() = {
var s = "Pact verification failed for the following reasons:\n"
for (mismatch <- results.almostMatched) {
for (mismatch <- results.getAlmostMatched.asScala) {
s += mismatch.description()
}
if (results.unexpected.nonEmpty) {
if (results.getUnexpected.asScala.nonEmpty) {
s += "\nThe following unexpected results were received:\n"
for (unexpectedResult <- results.unexpected) {
for (unexpectedResult <- results.getUnexpected.asScala) {
s += unexpectedResult.toString()
}
}
if (results.missing.nonEmpty) {
if (results.getMissing.asScala.nonEmpty) {
s += "\nThe following requests were not received:\n"
for (unexpectedResult <- results.missing) {
for (unexpectedResult <- results.getMissing.asScala) {
s += unexpectedResult.toString()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package au.com.dius.pact.server

import au.com.dius.pact.core.matchers.PartialRequestMatch
import au.com.dius.pact.core.model.Request
import au.com.dius.pact.core.model.RequestResponseInteraction
import spock.lang.Specification

import static scala.collection.JavaConverters.iterableAsScalaIterable

class PactSessionResultsSpec extends Specification {
def 'empty state'() {
given:
def state = PactSessionResults.empty

expect:
state.allMatched()
state.almostMatched.empty
state.matched.empty
state.unexpected.empty
state.missing.empty
}

def 'add matched interaction'() {
given:
def state = PactSessionResults.empty

when:
state = state.addMatched(new RequestResponseInteraction('test'))

then:
state.allMatched()
state.almostMatched.empty
state.matched*.description == ['test']
state.unexpected.empty
state.missing.empty
}

def 'add two matched interactions'() {
given:
def state = PactSessionResults.empty

when:
state = state.addMatched(new RequestResponseInteraction('test'))
state = state.addMatched(new RequestResponseInteraction('test2'))

then:
state.allMatched()
state.almostMatched.empty
state.matched*.description == ['test2', 'test']
state.unexpected.empty
state.missing.empty
}

def 'add unexpected request'() {
given:
def state = PactSessionResults.empty
def request = new Request('GET', '/test')

when:
state = state.addUnexpected(request)

then:
!state.allMatched()
state.almostMatched.empty
state.matched.empty
state.unexpected*.path == ['/test']
state.missing.empty
}

def 'add missing interactions'() {
given:
def state = PactSessionResults.empty
def interaction1 = new RequestResponseInteraction('test')
def interaction2 = new RequestResponseInteraction('test2')
def interaction3 = new RequestResponseInteraction('test3')

when:
state = state.addMissing(iterableAsScalaIterable([interaction1]))
state = state.addMissing(iterableAsScalaIterable([interaction2, interaction3]))

then:
!state.allMatched()
state.almostMatched.empty
state.matched.empty
state.unexpected.empty
state.missing*.description == ['test2', 'test3', 'test']
}

def 'add almost matched'() {
given:
def state = PactSessionResults.empty

when:
state = state.addAlmostMatched(new PartialRequestMatch([:]))

then:
state.allMatched()
!state.almostMatched.empty
state.matched.empty
state.unexpected.empty
state.missing.empty
}
}

0 comments on commit d154527

Please sign in to comment.