All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.com.dius.pact.model.v3.messaging.MessagePact.groovy Maven / Gradle / Ivy

Go to download

Pact model ========== The model project is responsible for providing: * a model to represent pacts * serialization and deserialization * comparison between two parts of the pact model * conversion between the pact model and whatever third party libraries used by the pact-consumer and pact-provider requires You should never need to include this project directly

There is a newer version: 2.4.15
Show newest version
package au.com.dius.pact.model.v3.messaging

import au.com.dius.pact.model.Consumer
import au.com.dius.pact.model.Interaction
import au.com.dius.pact.model.InvalidPactException
import au.com.dius.pact.model.Pact
import au.com.dius.pact.model.PactSpecVersion
import au.com.dius.pact.model.Provider
import au.com.dius.pact.model.v3.V3Pact
import groovy.transform.CompileStatic
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import groovy.util.logging.Slf4j

/**
 * Pact for a sequences of messages
 */
@Slf4j
@ToString(includeSuperProperties = true)
@EqualsAndHashCode(callSuper = true)
@CompileStatic
class MessagePact extends V3Pact {
  List messages = []

  MessagePact(Provider provider, Consumer consumer, List messages) {
    this(provider, consumer, messages, DEFAULT_METADATA)
  }

  MessagePact(Provider provider, Consumer consumer, List messages, Map metadata) {
    super(provider, consumer, metadata)
    this.messages = messages
  }

  static MessagePact fromMap(Map map) {
    def consumer = Consumer.fromMap(map.consumer as Map)
    def provider = Provider.fromMap(map.provider as Map)
    def messages = map.messages.collect { new Message().fromMap((Map) it) }
    def metadata = map.metadata as Map
    new MessagePact(provider, consumer, messages, metadata)
  }

  @Override
  Map toMap(PactSpecVersion pactSpecVersion) {
    if (pactSpecVersion < PactSpecVersion.V3) {
      throw new InvalidPactException('Message pacts only support version 3+, cannot write pact specification ' +
        "version ${pactSpecVersion}")
    }
    [
      consumer: [name: consumer.name],
      provider: [name: provider.name],
      messages: messages*.toMap(),
      metadata: metadata
    ]
  }

  List getInteractions() {
    messages as List
  }

  @Override
  Pact sortInteractions() {
    messages.sort { it.providerState + it.description }
    this
  }

  MessagePact mergePact(Pact other) {
    if (!(other instanceof MessagePact)) {
      throw new InvalidPactException("Unable to merge pact $other as it is not a MessagePact")
    }
    messages = (messages + (other.interactions as List)).unique { it.description }
    this
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy