<commit_before>package au.com.dius.pact.model

import au.com.dius.pact.consumer.dsl.DslPart
import au.com.dius.pact.consumer.{ConsumerTestVerification, VerificationResult}
import au.com.dius.pact.model.HttpMethod._
import org.json.JSONObject

object PactFragmentBuilder {
  def apply(consumer: Consumer) = {
    WithConsumer(consumer)
  }

  case class WithConsumer(consumer: Consumer) {
    def hasPactWith(provider: String) = {
      WithProvider(new Provider(provider))
    }

    case class WithProvider(provider: Provider) {
      def given(state: String) = {
        InState(Some(state))
      }

      def uponReceiving(description: String) = {
        InState(None).uponReceiving(description)
      }

      case class InState(state: Option[String]) {
        def uponReceiving(description: String) = {
          DescribingRequest(consumer, provider, state, description)
        }
      }
    }
  }

  case class DescribingRequest(consumer: Consumer, provider: Provider, state: Option[String], description: String,
                               builder: CanBuildPactFragment.Builder = CanBuildPactFragment.firstBuild) extends Optionals {
    import scala.collection.JavaConversions._

    /**
     * supports java DSL
     */
    def matching(path: String, method: String, query: String, headers: java.util.Map[String, String], body: String,
                 matchers: java.util.Map[String, Any]): DescribingResponse = {
      import collection.JavaConversions._
      matching(path, method, query, headers.toMap, body, matchers.toMap.asInstanceOf[Map[String, Map[String, String]]])
    }

    def matching(path: String,
                 method: String = Get,
                 query: String = "",
                 headers: Map[String, String] = Map(),
                 body: String = "",
                 matchers: Map[String, Map[String, String]] = Map()): DescribingResponse = {
      DescribingResponse(new Request(method, path, PactReader.queryStringToMap(query), headers, OptionalBody.body(body),
        CollectionUtils.scalaMMapToJavaMMap(matchers)))
    }

    case class DescribingResponse(request: Request) {
      /**
       * supports java DSL
       */
      def willRespondWith(status: Int, headers: java.util.Map[String, String], body: String, matchers: JSONObject): PactWithAtLeastOneRequest = {
        import collection.JavaConversions._
        willRespondWith(status, headers.toMap, body, matchers)
      }

      def willRespondWith(status: Int = 200,
                          headers: Map[String, String] = Map(),
                          body: String = "",
                          matchers: Map[String, Map[String, String]] = Map()): PactWithAtLeastOneRequest = {
        builder(
          consumer,
          provider,
          state,
          Seq(new RequestResponseInteraction(
            description,
            state.orNull,
            request,
            new Response(status, headers, OptionalBody.body(body), CollectionUtils.scalaMMapToJavaMMap(matchers)))))
      }

      def willRespondWith(status: Int,
                          headers: Map[String, String],
                          bodyAndMatchers: DslPart): PactWithAtLeastOneRequest = {
        val matchers = bodyAndMatchers.getMatchers.mapValues(_.mapValues(_.toString).toMap).toMap
        willRespondWith(status, headers, bodyAndMatchers.getBody.toString, matchers)
      }
    }
  }<commit_msg>try not to convert matchers to scala collections, as they then need to be converted back<commit_after>package au.com.dius.pact.model

import au.com.dius.pact.consumer.dsl.DslPart
import au.com.dius.pact.consumer.{ConsumerTestVerification, VerificationResult}
import au.com.dius.pact.model.HttpMethod._
import org.json.JSONObject

object PactFragmentBuilder {
  def apply(consumer: Consumer) = {
    WithConsumer(consumer)
  }

  case class WithConsumer(consumer: Consumer) {
    def hasPactWith(provider: String) = {
      WithProvider(new Provider(provider))
    }

    case class WithProvider(provider: Provider) {
      def given(state: String) = {
        InState(Some(state))
      }

      def uponReceiving(description: String) = {
        InState(None).uponReceiving(description)
      }

      case class InState(state: Option[String]) {
        def uponReceiving(description: String) = {
          DescribingRequest(consumer, provider, state, description)
        }
      }
    }
  }

  case class DescribingRequest(consumer: Consumer, provider: Provider, state: Option[String], description: String,
                               builder: CanBuildPactFragment.Builder = CanBuildPactFragment.firstBuild) extends Optionals {
    import scala.collection.JavaConversions._

    /**
     * supports java DSL
     */
    def matching(path: String, method: String, query: String, headers: java.util.Map[String, String], body: String,
                 matchers: java.util.Map[String, Any]): DescribingResponse = {
      import collection.JavaConversions._
      matching(path, method, query, headers.toMap, body, matchers.toMap.asInstanceOf[Map[String, Map[String, String]]])
    }

    def matching(path: String,
                 method: String = Get,
                 query: String = "",
                 headers: Map[String, String] = Map(),
                 body: String = "",
                 matchers: Map[String, Map[String, String]] = Map()): DescribingResponse = {
      DescribingResponse(new Request(method, path, PactReader.queryStringToMap(query), headers, OptionalBody.body(body),
        CollectionUtils.scalaMMapToJavaMMap(matchers)))
    }

    case class DescribingResponse(request: Request) {
      /**
       * supports java DSL
       */
      def willRespondWith(status: Int, headers: java.util.Map[String, String], body: String, matchers: JSONObject): PactWithAtLeastOneRequest = {
        import collection.JavaConversions._
        willRespondWith(status, headers.toMap, body, matchers)
      }

      def willRespondWith(status: Int = 200,
                          headers: Map[String, String] = Map(),
                          body: String = "",
                          matchers: Map[String, Map[String, String]] = Map()): PactWithAtLeastOneRequest = {
        builder(
          consumer,
          provider,
          state,
          Seq(new RequestResponseInteraction(
            description,
            state.orNull,
            request,
            new Response(status, headers, OptionalBody.body(body), CollectionUtils.scalaMMapToJavaMMap(matchers)))))
      }

      def willRespondWith(status: Int,
                          headers: Map[String, String],
                          bodyAndMatchers: DslPart): PactWithAtLeastOneRequest = {
        builder(
          consumer,
          provider,
          state,
          Seq(new RequestResponseInteraction(
            description,
            state.orNull,
            request,
            new Response(status, headers, OptionalBody.body(bodyAndMatchers.toString), bodyAndMatchers.getMatchers))))
      }
    }
  }
{'fraction_non_alphanumeric': 0.10143895141758086, 'fraction_numerical': 0.0008548226243054566, 'mean_word_length': 2.4925373134328357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262479', 'n_tokens_mistral': 1967, 'n_tokens_neox': 1871, 'n_words': 406}