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

com.github.ngeor.yak4j.ResponseEntityAssert Maven / Gradle / Ivy

There is a newer version: 0.22.0
Show newest version
package com.github.ngeor.yak4j;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

/**
 * Assertion DSL for {@link ResponseEntity}.
 *
 * @param  The body of the response.
 */
@SuppressWarnings({"unused", "WeakerAccess", "UnusedReturnValue"})
public class ResponseEntityAssert extends AbstractAssert, ResponseEntity> {
    ResponseEntityAssert(ResponseEntity responseEntity) {
        super(responseEntity, ResponseEntityAssert.class);
    }

    /**
     * Verifies that the response contains the given object as body.
     *
     * @param body The expected body.
     * @return This instance.
     */
    public ResponseEntityAssert hasBody(T body) {
        isNotNull();
        T actualBody = actual.getBody();
        Assertions.assertThat(actualBody)
            .withFailMessage("Expecting response entity body to be %s but was %s", body, actualBody)
            .isNotNull()
            .isEqualTo(body);
        return this;
    }

    /**
     * Verifies that the response has the given HTTP status code.
     *
     * @param httpStatus The expected HTTP status code.
     * @return This instance.
     */
    public ResponseEntityAssert hasStatus(HttpStatus httpStatus) {
        isNotNull();
        HttpStatus actualStatusCode = actual.getStatusCode();
        Assertions.assertThat(actualStatusCode)
            .withFailMessage("Expecting response status code to be %s but was %s", httpStatus, actualStatusCode)
            .isEqualTo(httpStatus);
        return this;
    }

    public ResponseEntityAssert isBadRequest() {
        return hasStatus(HttpStatus.BAD_REQUEST);
    }

    public ResponseEntityAssert isConflict() {
        return hasStatus(HttpStatus.CONFLICT);
    }

    public ResponseEntityAssert isCreated() {
        return hasStatus(HttpStatus.CREATED);
    }

    public ResponseEntityAssert isForbidden() {
        return hasStatus(HttpStatus.FORBIDDEN);
    }

    public ResponseEntityAssert isInternalServerError() {
        return hasStatus(HttpStatus.INTERNAL_SERVER_ERROR);
    }

    public ResponseEntityAssert isNotFound() {
        return hasStatus(HttpStatus.NOT_FOUND);
    }

    public ResponseEntityAssert isOk() {
        return hasStatus(HttpStatus.OK);
    }

    public ResponseEntityAssert isUnauthorized() {
        return hasStatus(HttpStatus.UNAUTHORIZED);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy