繁体   English   中英

检查每个颜色字段是否具有颜色值(格式字符串“#FFFFFF”)

[英]Check that each Color field has a color value (format string "#FFFFFF")

我有任务:“检查每个颜色字段是否具有颜色值(格式字符串“#FFFFFF”)”。

我有一个解决方案:

@Test
public void sixTest() {
    Specification.installSpec(Specification.requestSpec(), Specification.responseSpec());
    Response response = given()
            .when()
            .get("https://reqres.in/api/unknown")
            .then()
            .log().all()
            .extract().response();

    ResponseBody body = response.getBody();
    String bodyAsString = body.asString();
    Assert.assertEquals(bodyAsString.contains("#"), true, "Response body contains #");

}

我想我错误地解决了问题有没有更合适的解决方案?

附加信息:URL https://reqres.in/api/unknown是公开的并返回例如

{
    page: 1,
    per_page: 6,
    total: 12,
    total_pages: 2,
    data: [
    {
    id: 1,
    name: "cerulean",
    year: 2000,
    color: "#98B2D1",
    pantone_value: "15-4020"
    },
    {
    id: 2,
    name: "fuchsia rose",
    year: 2001,
    color: "#C74375",
    pantone_value: "17-2031"
    },
    ...

这个问题可以用正则表达式解决

ResponseBody body = response.getBody();
String bodyAsString = body.asString();

//Regexp for a HEX color
String patternString = "#[a-zA-Z0-9]{6}"

Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(text);

//Check that the text matches the regular expression
boolean matches = matcher.matches();

您可以 在此处尝试使用正则表达式

我和 Arthur 有同样的想法,但更具体的是要测试什么。

检查每种颜色是否匹配正则表达式#[0-9A-Z]{6}

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

Response res = given().get("https://reqres.in/api/unknown");
List<String> colors = res.jsonPath().getList("data.color");
assertThat(colors, everyItem(matchesPattern("#[0-9A-Z]{6}")));

注意:Hamcrest verion 1.x 没有方法matchesPattern

将 2.2 版添加到您的 pom.xml

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest</artifactId>
    <version>2.2</version>
</dependency>

更多信息: api 文档

首先使用 json 解析器读取响应。 我用过杰克逊:

https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core/2.13.0 https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.13.0

在遍历到data数组节点并迭代每个元素并检查color节点之后。

public static boolean everythingIsColor(String json) throws JsonProcessingException{

    ObjectMapper objectMapper = new ObjectMapper();
    JsonNode jsonBody = objectMapper.readTree(json); //JsonProcessingException

    Pattern pattern = Pattern.compile("#[0-9A-F]{6}"); //regex for color format

    JsonNode dataNode = jsonBody.get("data");
    for(JsonNode node : dataNode){

        JsonNode colorNode = node.get("color");
        if(!colorNode.isTextual()){ //is not text node
            return false;
        }

        Matcher matcher = pattern.matcher(colorNode.asText());
        if(!matcher.matches()){ //does not match with color format
            return false;
        }

    }

    //if reach here means every color node has a color value
    return true;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM