【问题标题】:how to create a non empty map consumer in spring cloud contract(with groovy)?如何在 Spring Cloud 合约(使用 groovy)中创建非空地图使用者?
【发布时间】:2017-03-27 17:41:49
【问题描述】:

我正在为 http 请求编写 spring cloud contract,其中一个 json 正文(我们称之为 myMap)应该是 non空 映射(字符串到字符串)。所以请求者(又名 consumer)必须有这样的东西: "myMap": {"key": "val"}

可以在合同中强制执行吗?

这里是我为确定上下文而编写的现有合同的示例:

  package contracts


org.springframework.cloud.contract.spec.Contract.make {
    description("""
        Represents a successful scenario of registering new host 

        given:
            hostProperties are valid
        then:
            we'll register the host
    """)
    request {
        method 'POST'
        urlPath value(consumer(~/\/api\/hosts\/[a-zA-Z0-90-9]+/), producer('/api/hosts/icsl7875'))
        body([
            timeStamp     : $(consumer(anyNumber()), producer(334)),
            hyperThreaded : $(consumer(regex('^(true|false)$')), producer(false)),
            virtualMachine: $(consumer(regex('^(true|false)$')), producer(false)),
            poolName      : $(consumer(regex('(.+)')), producer("dev_regression")),
            osImage       : $(consumer(regex('(.+)')), producer("osImage1")),
            cores         : $(consumer(anyNumber()), producer(2)),
            memory        : $(consumer(anyNumber()), producer(256)),
            osRelease     : $(consumer(regex('(.+)')), producer("osRelease1")),
            wsmVeriosn    : $(consumer(regex('(.+)')), producer("8.2.16")),
            cpuCount      : $(consumer(anyNumber()), producer(2445L)),
            cpuMhz        : $(consumer(anyNumber()), producer(22354L)),
            cpuMips       : $(consumer(anyNumber()), producer(256F))
        ])
        headers {
            contentType(applicationJsonUtf8())
        }
    }
    response {
        status 201
        body([
            groupId: $('067e6162-3b6f-4ae2-a171-2470b63dff00')
        ])
        headers {
            contentType(applicationJson())
        }
    }
}

【问题讨论】:

    标签: groovy spring-cloud-contract


    【解决方案1】:

    请求 我认为您可以使用stubMatcher 部分。只有存在条目时,才应解析 WireMock 中的 JSON 路径。所以如果你传入stubMatcher一个正确的json路径并验证它byEquality那么应该没问题。

    响应: 您可以使用 testMatcher 部分并与 byCommand (https://cloud.spring.io/spring-cloud-contract/1.0.x/#_dynamic_properties_in_matchers_sections) 一起委托。在该方法中,您必须验证该条目是否为非空。在文档中有这样的东西 jsonPath('$.duck', byCommand('assertThatValueIsANumber($it)')) 。您需要做类似的事情,但断言该条目是非空的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 1970-01-01
      相关资源
      最近更新 更多