validationswaggerswagger-2.0lagom

How can I use SwaggerValidator(com.atlassian.oai.validator) to validate Lagom API Response?


Well, I am stuck in this scenario that I want to use Swagger API to validate response of my Lagom Service API. Here is some sample code:

@Test
public void shouldPayloadFromFileConformToSchema() throws Exception {
    // first test the plain json is valid with schema
    SwaggerRequestResponseValidator validator = SwaggerRequestResponseValidator
        .createFor("my-service-schema.yaml").build();
    final Request validRequest = SimpleRequest.Builder.get("/myService/AL20170730/11111555556161/191919")
        .withHeader("api-key", "TESTKEY")
        .build();
    Response validResponse = SimpleResponse.Builder.ok()
        .withBody(ValidatorTestUtil.loadResponse("my_service_sample_response_2017_03_16")).build();
    ValidationReport reportForText = validator.validate(validRequest, validResponse);
    logger.info(
        "shouldPayloadFromFileConformToSchema() ################# VALIDATION PAYLOAD REPORT ##################");
    reportForText.getMessages().forEach((m) -> {
        logger.info("{}", m);
    });
    assertFalse(reportForText.hasErrors());
    logger.info(
        "shouldPayloadFromFileConformToSchema() ################# VALIDATION PAYLOAD REPORT END ##################");
    logger.info(validRequest.getHeaders().toString());
    SwaggerModule swagger = new SwaggerModule();


}

When This code runs It seems that it goes into the service(as it prints some logs of the service.) but not invokes the method which will the database with the given values.

I need to do something here that it invokes the method of the service and validates the response on the basis of this swagger spec.

I saw this link but didn't get the solution How to validate API in tests with Swagger?


Solution

  • If you're looking to validate an actual interaction against your running service I'd recommend using the RestAssured module (https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-restassured/)

    This will let you execute a request against your running service and then validate that the request/response interaction matches your swagger specification.

    There's an example of its use in the examples module - https://bitbucket.org/atlassian/swagger-request-validator/src/master/swagger-request-validator-examples/src/test/java/com/atlassian/oai/validator/examples/restassured/SwaggerValidationFilterTestExample.java (note that the example there uses WireMock to stub out a real service, but you would replace that with your actual running service).