Search in sources :

Example 1 with SingleContractMetadata

use of org.springframework.cloud.contract.verifier.file.SingleContractMetadata in project spring-cloud-contract by spring-cloud.

the class BodyParser method requestBodyAsString.

@SuppressWarnings("unchecked")
default String requestBodyAsString(SingleContractMetadata metadata) {
    ContentType contentType = metadata.getInputTestContentType();
    DslProperty body = requestBody(metadata);
    Object bodyValue = extractServerValueFromBody(contentType, body.getServerValue());
    if (contentType == ContentType.FORM) {
        if (bodyValue instanceof Map) {
            // [a:3, b:4] == "a=3&b=4"
            return ((Map) bodyValue).entrySet().stream().map(o -> {
                Map.Entry entry = (Map.Entry) o;
                return convertUnicodeEscapesIfRequired(entry.getKey().toString() + "=" + MapConverter.getTestSideValuesForText(entry.getValue()));
            }).collect(Collectors.joining("&")).toString();
        } else if (bodyValue instanceof List) {
            // ["a=3", "b=4"] == "a=3&b=4"
            return ((List) bodyValue).stream().map(o -> convertUnicodeEscapesIfRequired(MapConverter.getTestSideValuesForText(o).toString())).collect(Collectors.joining("&")).toString();
        }
    } else {
        return convertToJsonString(bodyValue);
    }
    return "";
}
Also used : DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) TEXT(org.springframework.cloud.contract.verifier.util.ContentType.TEXT) JsonOutput(groovy.json.JsonOutput) ContentUtils(org.springframework.cloud.contract.verifier.util.ContentUtils) JSON(org.springframework.cloud.contract.verifier.util.ContentType.JSON) ContentUtils.extractValue(org.springframework.cloud.contract.verifier.util.ContentUtils.extractValue) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) SingleContractMetadata(org.springframework.cloud.contract.verifier.file.SingleContractMetadata) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) GString(groovy.lang.GString) FromFileProperty(org.springframework.cloud.contract.spec.internal.FromFileProperty) StringEscapeUtils.escapeJava(org.apache.commons.text.StringEscapeUtils.escapeJava) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) List(java.util.List) DEFINED(org.springframework.cloud.contract.verifier.util.ContentType.DEFINED) Map(java.util.Map) FORM(org.springframework.cloud.contract.verifier.util.ContentType.FORM) MapConverter(org.springframework.cloud.contract.verifier.util.MapConverter) ContentType(org.springframework.cloud.contract.verifier.util.ContentType) DslProperty(org.springframework.cloud.contract.spec.internal.DslProperty) List(java.util.List) Map(java.util.Map)

Example 2 with SingleContractMetadata

use of org.springframework.cloud.contract.verifier.file.SingleContractMetadata in project spring-cloud-contract by spring-cloud.

the class NameProvider method generateMethodName.

private String generateMethodName(SingleContractMetadata singleContractMetadata) {
    ContractMetadata contractMetadata = singleContractMetadata.getContractMetadata();
    File stubsFile = contractMetadata.getPath().toFile();
    Contract stubContent = singleContractMetadata.getContract();
    if (StringUtils.hasText(stubContent.getName())) {
        String name = NamesUtil.camelCase(NamesUtil.convertIllegalPackageChars(stubContent.getName()));
        if (log.isDebugEnabled()) {
            log.debug("Overriding the default test name with [" + name + "]");
        }
        return name;
    } else if (contractMetadata.getConvertedContract().size() > 1) {
        int index = findIndexOf(contractMetadata.getConvertedContract(), stubContent);
        String name = camelCasedMethodFromFileName(stubsFile) + "_" + index;
        if (log.isTraceEnabled()) {
            log.trace("Scenario found. The method name will be [" + name + "]");
        }
        return name;
    }
    String name = camelCasedMethodFromFileName(stubsFile);
    if (StringUtils.hasText(name) && log.isTraceEnabled()) {
        log.trace("The method name will be [" + name + "]");
    }
    return name;
}
Also used : ContractMetadata(org.springframework.cloud.contract.verifier.file.ContractMetadata) SingleContractMetadata(org.springframework.cloud.contract.verifier.file.SingleContractMetadata) File(java.io.File) Contract(org.springframework.cloud.contract.spec.Contract)

Aggregations

SingleContractMetadata (org.springframework.cloud.contract.verifier.file.SingleContractMetadata)2 JsonOutput (groovy.json.JsonOutput)1 GString (groovy.lang.GString)1 File (java.io.File)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 StringEscapeUtils (org.apache.commons.text.StringEscapeUtils)1 StringEscapeUtils.escapeJava (org.apache.commons.text.StringEscapeUtils.escapeJava)1 Contract (org.springframework.cloud.contract.spec.Contract)1 DslProperty (org.springframework.cloud.contract.spec.internal.DslProperty)1 FromFileProperty (org.springframework.cloud.contract.spec.internal.FromFileProperty)1 ContractMetadata (org.springframework.cloud.contract.verifier.file.ContractMetadata)1 ContentType (org.springframework.cloud.contract.verifier.util.ContentType)1 DEFINED (org.springframework.cloud.contract.verifier.util.ContentType.DEFINED)1 FORM (org.springframework.cloud.contract.verifier.util.ContentType.FORM)1 JSON (org.springframework.cloud.contract.verifier.util.ContentType.JSON)1 TEXT (org.springframework.cloud.contract.verifier.util.ContentType.TEXT)1 ContentUtils (org.springframework.cloud.contract.verifier.util.ContentUtils)1