Skip to content

Asserting within steps

Ričardas edited this page Nov 17, 2022 · 2 revisions

It is recommended to use com.nortal.test.asserts.ValidationService for data validation. This service provides convenience methods for different types of assertions, supports SpringEL expressions and generates readable report entries.

Example with two assertions: status code is 201 and field is NOT NULL:

public class CertificationServicesApiStepDefs extends BaseStepDefs {

  @When("Certification service is created")
  public void createCertificationService() throws Exception {
    MultipartFile certificate = new MockMultipartFile("certificate", generateAuthCert());
    String certificateProfileInfo = "ee.ria.xroad.common.certificateprofile.impl.FiVRKCertificateProfileInfoProvider";

    final ResponseEntity<ApprovedCertificationServiceDto> response = certificationServicesApi
      .addCertificationService(certificate, certificateProfileInfo, "false");

    final Validation.Builder validationBuilder = new Validation.Builder()
      .context(response) // We specify that response will be root element for SpEL expressions.
      .title("Validate response") // What is being validated?
      .assertion(equalsStatusCodeAssertion(CREATED))
      .assertion(new Assertion.Builder()
        .message("Body has ID")
        .expression("body.id")
        .operation(AssertionOperation.NOT_NULL).build());
    validationService.validate(validationBuilder.build());

    putStepData(CERTIFICATION_SERVICE_ID, response.getBody().getId());
  }
}

Example with value comparison:

public class IntermediateCasApiStepDefs extends BaseStepDefs {
  @Then("intermediate CA has the updated OCSP responder")
  public void intermediateCAHasUpdatedOCSPResponder() {
    final ResponseEntity<Set<OcspResponderDto>> response = intermediateCasApi
      .getIntermediateCaOcspResponders(intermediateCaId);

    final String newOcspResponderUrl = getRequiredStepData(NEW_OCSP_RESPONDER_URL);

    final Validation.Builder validationBuilder = new Validation.Builder()
      .context(response)
      .title("Validate response")
      .assertion(equalsStatusCodeAssertion(OK))
      .assertion(equalsAssertion(Boolean.TRUE, "body[0].hasCertificate", "Verify OCSP responder has certificate"))
      .assertion(equalsAssertion(newOcspResponderUrl, "body[0].url", "OCSP responder url matches"));
    validationService.validate(validationBuilder.build());
  }
}

Result will look like this: validation-result-example

A more detailed description for various options can be found in JavaDoc.