All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.aoreshin.allure.rest.assured.ApiValidationSteps Maven / Gradle / Ivy

There is a newer version: 83
Show newest version
package com.github.aoreshin.allure.rest.assured;

import com.github.aoreshin.junit5.allure.steps.StepWrapperSteps;
import io.qameta.allure.Step;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.function.Executable;

import java.util.List;
import java.util.Map;

import static com.github.aoreshin.allure.rest.assured.ApiRequestSteps.apiRequest;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.*;

public final class ApiValidationSteps extends StepWrapperSteps {
    private final Response response;

    ApiValidationSteps(Response response) {
        this.response = response;
    }

    @Step("Проверка кода {status}")
    public ApiValidationSteps statusCode(int status) {
        assertEquals(status, response.statusCode());
        return this;
    }

    @Step("Проверка Content-Type {contentType}")
    public ApiValidationSteps contentType(ContentType contentType) {
        assertEquals(contentType, ContentType.fromContentType(response.getContentType()));
        return this;
    }

    @Step("Проверка содержания в {jsonPath} значения {expected}")
    public  ApiValidationSteps assertEqualsJson(String jsonPath, T expected) {
        T actual = response.getBody().jsonPath().get(jsonPath);
        assertEquals(expected, actual);
        return this;
    }

    @Step("Проверка содержания полями соответствующих значений {values}")
    public ApiValidationSteps assertContainsStringJson(Map values) {
        List executables = values
                .entrySet()
                .stream()
                .map(entry -> {
                    String expected = entry.getValue();
                    String actual = response.getBody().jsonPath().get(entry.getKey());
                    return (Executable) () -> assertTrue(actual.contains(expected), actual + " не содержит " + expected);
                })
                .collect(toList());

        assertAll(executables);
        return this;
    }

    @Step("Проверка пустоты {jsonPath}")
    public ApiValidationSteps assertNullJson(String jsonPath) {
        assertNull(response.getBody().jsonPath().get(jsonPath));
        return this;
    }

    @Step("Проверка не пустые {jsonPaths}")
    public ApiValidationSteps assertNotNullJson(List jsonPaths) {
        List executables = jsonPaths
                .stream()
                .map(jsonPath -> (Executable) () -> assertNotNull(response.getBody().jsonPath().get(jsonPath)))
                .collect(toList());

        assertAll(executables);
        return this;
    }

    public ApiRequestSteps next() {
        return apiRequest();
    }

    public ApiExtractingSteps extract() {
        return new ApiExtractingSteps(response);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy