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

I have task: "Check that each Color field has a color value (format string "#FFFFFF")".

I have a solution:

@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 #");

}

I think I solved the problem incorrectly is there a more suitable solution?

Additional information: The URL https://reqres.in/api/unknown is public and returns e.g.

{
    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"
    },
    ...


Solution 1:[1]

This problem can be solved using regular expressions

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();

You can experiment with regular expressions here

Solution 2:[2]

In first place read the response with a json parser. I used Jackson:

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

After travers to data array-node and iterate every element and check the color node.

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;

}

Solution 3:[3]

I have same idea with Arthur but more specific what to be tested.

Check every color match the regex #[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}")));

Note: Hamcrest verion 1.x doesn't have method matchesPattern

Add version 2.2 to your pom.xml

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

More info: api docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 KunLun
Solution 3