com.github.aoreshin.allure.rest.assured.ApiValidationSteps Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of allure-rest-assured Show documentation
Show all versions of allure-rest-assured Show documentation
Fluent API for Rest Assured and Allure reports
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);
}
}