Best Webtau code snippet using org.testingisdocumenting.webtau.openapi.OpenApiSpecValidator.OpenApiSpecValidator
Source:OpenApiSpecValidator.java
...26import java.util.Optional;27import static java.util.stream.Collectors.joining;28import static org.testingisdocumenting.webtau.utils.UrlUtils.extractPath;29import static org.testingisdocumenting.webtau.utils.UrlUtils.extractQueryParams;30public class OpenApiSpecValidator {31 private final OpenApiInteractionValidator openApiValidator;32 private final OpenApiSpec openAPISpec;33 public OpenApiSpecValidator(OpenApiSpec openApiSpec, OpenApiValidationConfig validationConfig) {34 this.openAPISpec = openApiSpec;35 this.openApiValidator = openApiSpec.isSpecDefined() ?36 OpenApiInteractionValidator37 .createForInlineApiSpecification(openApiSpec.getSpecContent())38 .withLevelResolver(createLevelResolver(validationConfig))39 .build() :40 null;41 }42 public boolean isSpecDefined() {43 return openAPISpec.isSpecDefined();44 }45 public void validateApiSpec(HttpValidationResult result, OpenApiValidationMode openApiValidationMode) {46 Optional<OpenApiOperation> apiOperation = openAPISpec.findApiOperation(result.getRequestMethod(), result.getFullUrl());47 if (!apiOperation.isPresent()) {48 ConsoleOutputs.out(Color.YELLOW, "Path, ", result.getFullUrl(), " is not found in OpenAPI spec");49 result.addWarning("path " + result.getFullUrl() + " is not found in OpenAPI spec");50 return;51 }52 SimpleRequest request = buildRequest(result);53 SimpleResponse response = buildResponse(result);54 ValidationReport validationReport = validate(openApiValidationMode, request, response);55 validationReport.getMessages().forEach(message ->56 result.addMismatch("API spec validation failure: " + renderMessage(message)));57 if (!validationReport.getMessages().isEmpty()) {58 throw new AssertionError("schema is not valid:\n" + validationReport59 .getMessages().stream()60 .map(OpenApiSpecValidator::renderMessage)61 .collect(joining("\n")));62 }63 }64 private ValidationReport validate(OpenApiValidationMode openApiValidationMode, SimpleRequest request, SimpleResponse response) {65 switch (openApiValidationMode) {66 case RESPONSE_ONLY:67 return openApiValidator.validateResponse(request.getPath(), request.getMethod(), response);68 case REQUEST_ONLY:69 return openApiValidator.validateRequest(request);70 case NONE:71 return ValidationReport.empty();72 case ALL:73 default:74 return openApiValidator.validate(request, response);...
Source:OpenApi.java
...18import java.util.concurrent.atomic.AtomicReference;19public class OpenApi {20 private static final OpenApiValidationMode DEFAULT_MODE = OpenApiValidationMode.ALL;21 private static final AtomicReference<OpenApiSpec> spec = new AtomicReference<>();22 private static final AtomicReference<OpenApiSpecValidator> validator = new AtomicReference<>();23 private static final AtomicReference<OpenApiCoverage> coverage = new AtomicReference<>();24 static final ThreadLocal<OpenApiValidationMode> validationMode = ThreadLocal.withInitial(() -> DEFAULT_MODE);25 synchronized static OpenApiSpecValidator getValidator() {26 if (validator.get() == null) {27 initialize();28 }29 return validator.get();30 }31 synchronized static boolean isCoverageUninitialized() {32 return coverage.get() == null;33 }34 synchronized static OpenApiCoverage getCoverage() {35 if (isCoverageUninitialized()) {36 initialize();37 }38 return coverage.get();39 }40 synchronized static OpenApiSpec getSpec() {41 if (spec.get() == null) {42 initialize();43 }44 return spec.get();45 }46 public static void withoutValidation(Runnable code) {47 withMode(OpenApiValidationMode.NONE, code);48 }49 public static void responseOnlyValidation(Runnable code) {50 withMode(OpenApiValidationMode.RESPONSE_ONLY, code);51 }52 public static void requestOnlyValidation(Runnable code) {53 withMode(OpenApiValidationMode.REQUEST_ONLY, code);54 }55 static void withMode(OpenApiValidationMode mode, Runnable code) {56 validationMode.set(mode);57 try {58 code.run();59 } finally {60 validationMode.set(DEFAULT_MODE);61 }62 }63 static void reset() {64 spec.set(null);65 validator.set(null);66 coverage.set(null);67 }68 static void initialize() {69 if (validationMode.get() == OpenApiValidationMode.NONE) {70 return;71 }72 spec.set(new OpenApiSpec(OpenApiSpecConfig.determineSpecFullPathOrUrl()));73 validator.set(new OpenApiSpecValidator(spec.get(), validationConfig()));74 coverage.set(new OpenApiCoverage(spec.get()));75 }76 private static OpenApiValidationConfig validationConfig() {77 OpenApiValidationConfig config = new OpenApiValidationConfig();78 config.setIgnoreAdditionalProperties(OpenApiSpecConfig.ignoreAdditionalProperties.getAsBoolean());79 return config;80 }81}...
Source:OpenApiResponseValidator.java
...27 OpenApiValidationMode mode = OpenApi.validationMode.get();28 if (mode.equals(OpenApiValidationMode.NONE)) {29 return;30 }31 OpenApiSpecValidator validator = OpenApi.getValidator();32 if (!validator.isSpecDefined()) {33 return;34 }35 String modeLabel = validationModeLabel(mode);36 WebTauStep.createAndExecuteStep(37 tokenizedMessage(action("validating"), classifier(modeLabel)),38 () -> tokenizedMessage(action("validated"), classifier(modeLabel)),39 () -> validator.validateApiSpec(validationResult, mode));40 }41 private static String validationModeLabel(OpenApiValidationMode mode) {42 switch (mode) {43 case ALL:44 return "request and response";45 case REQUEST_ONLY:...
OpenApiSpecValidator
Using AI Code Generation
1import org.testingisdocumenting.webtau.openapi.OpenApiSpecValidator;2import org.testingisdocumenting.webtau.Ddjt;3import org.testingisdocumenting.webtau.http.Http;4import org.testingisdocumenting.webtau.http.HttpHeader;5import org.testingisdocumenting.webtau.http.HttpResponse;6import org.testingisdocumenting.webtau.http.validation.HttpValidationResult;7import org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder;8import org.testingisdocumenting.webtau.reporter.WebTauStep;9import java.util.Map;10import static org.testingisdocumenting.webtau.WebTauDsl.*;11import static org.testingisdocumenting.webtau.http.validation.HttpValidators.*;12public class OpenApiSpecValidatorTest extends Ddjt {13 public void validateResponseAgainstOpenAPI() {14 HttpResponse response = Http.get("/v2/pet/findByStatus?status=available");15 HttpValidationResult validationResult = OpenApiSpecValidator.validateResponse(response);16 validationResult.assertValid();17 }
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!