Java Code Examples for com.nike.backstopper.apierror.ApiError#getName()

The following examples show how to use com.nike.backstopper.apierror.ApiError#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectApiErrors.java    From backstopper with Apache License 2.0 6 votes vote down vote up
/**
 * Throws an {@link IllegalStateException} if the given special error is null or if it can't be found in the given
 * full list of project API errors.
 *
 * @param specialError           The special error to check.
 * @param projectApiErrors       The full list of project API errors (both core and project-specific).
 * @param specialErrorMethodName The method name that returns the given special error. Used in the exception message
 *                               if an {@link IllegalStateException} needs to be thrown so you know which method to
 *                               fix.
 */
protected void verifySpecialErrorIsContainedInApiErrorList(ApiError specialError, List<ApiError> projectApiErrors,
                                                           String specialErrorMethodName) {
    if (specialError == null) {
        throw new IllegalStateException(
            "Special error method " + specialErrorMethodName + "() cannot return null. Class with illegal state: "
            + this.getClass().getName()
        );
    }

    if (!projectApiErrors.contains(specialError)) {
        throw new IllegalStateException(
            "Special error method " + specialErrorMethodName + "() returned an ApiError (" + specialError.getName()
            + ") that was not found in the full getProjectApiErrors() list. This is not allowed - all special "
            + "errors must be contained in the getProjectApiErrors() list. Class with illegal state: "
            + this.getClass().getName()
        );
    }
}
 
Example 2
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void should_not_contain_same_error_codes_for_different_instances_that_are_not_wrappers() {
    Set<String> allowedDuplicateErrorCodes = allowedDuplicateErrorCodes();
    Map<String, ApiError> codeToErrorMap = new HashMap<>();
    for (ApiError apiError : getProjectApiErrors().getProjectApiErrors()) {
        ApiError errorWithSameCode = codeToErrorMap.get(apiError.getErrorCode());

        if (errorWithSameCode != null && !areWrappersOfEachOther(apiError, errorWithSameCode)
            && !allowedDuplicateErrorCodes.contains(apiError.getErrorCode()))
        {
            throw new AssertionError(
                "There are ApiError instances in the ProjectApiErrors that share duplicate error codes and are not "
                + "wrappers of each other. error_code=" + apiError.getErrorCode() + ", conflicting_api_errors=["
                + apiError.getName() + ", " + errorWithSameCode.getName() + "]"
            );
        }

        codeToErrorMap.put(apiError.getErrorCode(), apiError);
    }
}
 
Example 3
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void allErrorsShouldBeCoreApiErrorsOrCoreApiErrorWrappersOrFallInProjectSpecificErrorRange() {
    ProjectSpecificErrorCodeRange projectSpecificErrorCodeRange = getProjectApiErrors().getProjectSpecificErrorCodeRange();

    for (ApiError error : getProjectApiErrors().getProjectApiErrors()) {
        boolean valid = false;
        if (getProjectApiErrors().getCoreApiErrors().contains(error) || getProjectApiErrors().isWrapperAroundCoreError(error, getProjectApiErrors().getCoreApiErrors()))
            valid = true;
        else if (projectSpecificErrorCodeRange != null && projectSpecificErrorCodeRange.isInRange(error))
            valid = true;

        if (!valid) {
            throw new AssertionError("Found an ApiError in the ProjectApiErrors that is not a core error or wrapper around a core error, and its error code does not fall in the  " +
                                     "range of getProjectApiErrors().getProjectSpecificErrorCodeRange(). getProjectApiErrors().getProjectSpecificErrorCodeRange(): " + projectSpecificErrorCodeRange +
                                     ". Offending error info: name=" + error.getName() + ", errorCode=" + error.getErrorCode() + ", message=\"" + error.getMessage() + "\", httpStatusCode=" +
                                     error.getHttpStatusCode() + ", class=" + error.getClass().getName());
        }
    }
}
 
Example 4
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void should_not_contain_same_error_codes_for_different_instances_that_are_not_wrappers() {
    Set<String> allowedDuplicateErrorCodes = allowedDuplicateErrorCodes();
    Map<String, ApiError> codeToErrorMap = new HashMap<>();
    for (ApiError apiError : getProjectApiErrors().getProjectApiErrors()) {
        ApiError errorWithSameCode = codeToErrorMap.get(apiError.getErrorCode());

        if (errorWithSameCode != null && !areWrappersOfEachOther(apiError, errorWithSameCode)
            && !allowedDuplicateErrorCodes.contains(apiError.getErrorCode())) {
            throw new AssertionError(
                "There are ApiError instances in the ProjectApiErrors that share duplicate error codes and are not "
                + "wrappers of each other. error_code=" + apiError.getErrorCode() + ", conflicting_api_errors=["
                + apiError.getName() + ", " + errorWithSameCode.getName() + "]"
            );
        }

        codeToErrorMap.put(apiError.getErrorCode(), apiError);
    }
}
 
Example 5
Source File: ProjectApiErrorsTestBase.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@Test
public void allErrorsShouldBeCoreApiErrorsOrCoreApiErrorWrappersOrFallInProjectSpecificErrorRange() {
    ProjectSpecificErrorCodeRange projectSpecificErrorCodeRange =
        getProjectApiErrors().getProjectSpecificErrorCodeRange();

    for (ApiError error : getProjectApiErrors().getProjectApiErrors()) {
        boolean valid = false;
        if (getProjectApiErrors().getCoreApiErrors().contains(error) || getProjectApiErrors()
            .isWrapperAroundCoreError(error, getProjectApiErrors().getCoreApiErrors()))
            valid = true;
        else if (projectSpecificErrorCodeRange != null && projectSpecificErrorCodeRange.isInRange(error))
            valid = true;

        if (!valid) {
            throw new AssertionError(
                "Found an ApiError in the ProjectApiErrors that is not a core error or wrapper around a core error, and its error code does not fall in the  "
                +
                "range of getProjectApiErrors().getProjectSpecificErrorCodeRange(). getProjectApiErrors().getProjectSpecificErrorCodeRange(): "
                + projectSpecificErrorCodeRange +
                ". Offending error info: name=" + error.getName() + ", errorCode=" + error.getErrorCode()
                + ", message=\"" + error.getMessage() + "\", httpStatusCode=" +
                error.getHttpStatusCode() + ", class=" + error.getClass().getName());
        }
    }
}
 
Example 6
Source File: VerifyMiscellaneousFunctionalityComponentTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@Test
public void verify_proxy_router_response_modification_works_as_expected() throws IOException, InterruptedException {

    ExtractableResponse response =
        given()
            .baseUri("http://127.0.0.1")
            .port(serverConfig.endpointsPort())
            .basePath(ProxyRouterResponseModificationEndpoint.MATCHING_PATH)
            .log().all()
        .when()
            .get()
        .then()
            .log().headers()
            .extract();

    ApiError unmodifiedError = EmptyMetadataErrorThrower.ERROR_NO_METADATA;
    assertThat(response.statusCode()).isEqualTo(ProxyRouterResponseModificationEndpoint.MODIFIED_HTTP_STATUS_RESPONSE_CODE);
    assertThat(response.header(ProxyRouterResponseModificationEndpoint.ORIG_HTTP_STATUS_CODE_RESPONSE_HEADER_KEY))
        .isEqualTo(String.valueOf(unmodifiedError.getHttpStatusCode()));
    ApiError expectedModifiedError = new ApiErrorBase(unmodifiedError.getName(), unmodifiedError.getErrorCode(), unmodifiedError.getMessage(),
                                                      ProxyRouterResponseModificationEndpoint.MODIFIED_HTTP_STATUS_RESPONSE_CODE);
    verifyErrorReceived(response, expectedModifiedError);
    assertThat(response.asString()).doesNotContain("metadata");
}
 
Example 7
Source File: ProjectApiErrors.java    From backstopper with Apache License 2.0 5 votes vote down vote up
/**
 * Compares the list of project-specific errors against {@link #getProjectSpecificErrorCodeRange()} to verify that
 * all errors fall within this project's valid range. If there is an error that falls outside the valid range then
 * an {@link java.lang.IllegalStateException} will be thrown.
 *
 * <p>The given list of core errors is used to determine if any of the project-specific errors are "wrappers" around
 * a core error, which is the only allowed case where a project-specific error can have an error code outside the
 * allowed range.
 *
 * <p>An {@link java.lang.IllegalStateException} will also be thrown if {@link #getProjectSpecificErrorCodeRange()}
 * is null but you have any project-specific errors that are not core error wrappers (the
 * {@link #getProjectSpecificErrorCodeRange()} is allowed to be null only if you have 100% core errors or core error
 * wrappers).
 */
protected void verifyErrorsAreInRange(List<ApiError> projectSpecificErrors, List<ApiError> coreErrors) {
    ProjectSpecificErrorCodeRange validRange = getProjectSpecificErrorCodeRange();

    for (ApiError projectError : projectSpecificErrors) {
        // Ignore wrappers around core errors
        boolean isCoreError = isWrapperAroundCoreError(projectError, coreErrors);

        if (!isCoreError) {
            // It's not a wrapper around a core error.

            // If validRange is null at this point then that constitutes an error since
            //      getProjectSpecificErrorCodeRange() is only allowed to be null if the project is
            //      100% core errors.
            if (validRange == null) {
                throw new IllegalStateException(
                    "The ProjectSpecificErrorCodeRange for this project is null, but there is an ApiError that is "
                    + "not a core error. This project must have a ProjectSpecificErrorCodeRange that covers all "
                    + "non-core errors. Offending ApiError: " + projectError.getName()
                );
            }

            // Check to make sure the project-specific error falls into the project's error range.
            if (!validRange.isInRange(projectError)) {
                throw new IllegalStateException(
                    "Found ApiError for this project with an error code that does not fall within the valid range "
                    + "specified by " + validRange.getName() + ". ApiError: " + projectError.getName()
                    + ". Error code value: " + projectError.getErrorCode()
                );
            }
        }
    }
}