Best Webtau code snippet using org.testingisdocumenting.webtau.openapi.OpenApi
Source:OpenApiSpec.java
...36import static java.lang.String.*;37import static org.testingisdocumenting.webtau.http.Http.*;38import static org.testingisdocumenting.webtau.reporter.IntegrationTestsMessageBuilder.*;39import static org.testingisdocumenting.webtau.reporter.TokenizedMessage.*;40public class OpenApiSpec {41 private final OpenAPI api;42 private final ApiOperationResolver apiOperationResolver;43 private final OpenApiSpecLocation specLocation;44 private final String specContent;45 private final boolean isSpecDefined;46 private final Map<OpenApiOperation, Set<String>> operationsAndResponses;47 public OpenApiSpec(OpenApiSpecLocation specLocation) {48 this.specLocation = specLocation;49 isSpecDefined = specLocation.isDefined();50 specContent = isSpecDefined ? readSpecContent() : "";51 api = createOpenAPI();52 apiOperationResolver = api != null ? new ApiOperationResolver(api, null) : null;53 operationsAndResponses = isSpecDefined ? enumerateOperations() : Collections.emptyMap();54 }55 public boolean isSpecDefined() {56 return isSpecDefined;57 }58 public String getSpecContent() {59 return specContent;60 }61 public Stream<OpenApiOperation> availableOperationsStream() {62 return operationsAndResponses.keySet().stream();63 }64 public Optional<OpenApiOperation> findApiOperation(String method, String path) {65 String relativePath = UrlUtils.extractPath(path);66 Request.Method requestMethod = Enum.valueOf(Request.Method.class, method);67 ApiOperationMatch apiOperation = apiOperationResolver.findApiOperation(relativePath, requestMethod);68 if (!apiOperation.isPathFound() || !apiOperation.isOperationAllowed()) {69 return Optional.empty();70 }71 return Optional.of(new OpenApiOperation(method, apiOperation.getApiOperation().getApiPath().original()));72 }73 public Set<String> getDeclaredResponses(OpenApiOperation op) {74 return operationsAndResponses.getOrDefault(op, Collections.emptySet());75 }76 private Map<OpenApiOperation, Set<String>> enumerateOperations() {77 Map<OpenApiOperation, Set<String>> operationsAndResponses = new LinkedHashMap<>();78 api.getPaths().forEach((url, path) -> {79 if (path.getGet() != null) {80 addOperation(operationsAndResponses, "GET", url, path.getGet());81 }82 if (path.getPut() != null) {83 addOperation(operationsAndResponses, "PUT", url, path.getPut());84 }85 if (path.getPost() != null) {86 addOperation(operationsAndResponses, "POST", url, path.getPost());87 }88 if (path.getDelete() != null) {89 addOperation(operationsAndResponses, "DELETE", url, path.getDelete());90 }91 });92 return operationsAndResponses;93 }94 private static void addOperation(Map<OpenApiOperation, Set<String>> operationsAndResponses, String method, String fullUrl, Operation operation) {95 Set<String> responseCodes = operation.getResponses() == null ? Collections.emptySet() : operation.getResponses().keySet();96 operationsAndResponses.put(new OpenApiOperation(method, fullUrl), responseCodes);97 }98 private OpenAPI createOpenAPI() {99 if (!isSpecDefined) {100 return null;101 }102 ParseOptions parseOptions = new ParseOptions();103 parseOptions.setResolve(true);104 SwaggerParseResult swaggerParseResult = new OpenAPIParser().readContents(105 specContent, Collections.emptyList(), parseOptions);106 OpenAPI openAPI = swaggerParseResult.getOpenAPI();107 if (openAPI == null) {108 throw new IllegalArgumentException(109 format("Unable to load API descriptor from provided %s: %s", specLocation.getAsString(),110 parseResultMessage(swaggerParseResult)));111 }112 return openAPI;113 }114 private static String parseResultMessage(SwaggerParseResult swaggerParseResult) {115 List<String> messages = swaggerParseResult.getMessages();116 if (messages == null) {117 return "check if it is accessible";118 }119 return "\n " + String.join("\n ", messages);120 }121 private String readSpecContent() {122 MessageToken openApiToken = classifier("open API spec");123 MessageToken typeToken = classifier(specLocation.isFileSystem() ? "file system" : "http url");124 WebTauStep step = WebTauStep.createStep(125 tokenizedMessage(action("reading"), openApiToken,126 FROM, typeToken, urlValue(specLocation.getOriginalValue())),127 () -> tokenizedMessage(action("read"), openApiToken,128 FROM, typeToken, urlValue(specLocation.getAsString())),129 () -> specLocation.isFileSystem() ?130 readSpecContentFromFs() :131 readSpecContentFromUrl()132 );133 return step.execute(StepReportOptions.REPORT_ALL);134 }135 private String readSpecContentFromUrl() {136 // we use http.get method here to get all the report tracing137 AtomicReference<Object> specBody = new AtomicReference<>();138 OpenApi.withoutValidation(() -> specBody.set(http.get(specLocation.getUrl(), (header, body) -> body)));139 return JsonUtils.serialize(specBody.get());140 }141 private String readSpecContentFromFs() {142 Path path = specLocation.getPath();143 return FileUtils.fileTextContent(path);144 }145}...
Source:OpenApiReportDataProvider.java
...21import org.testingisdocumenting.webtau.reporter.WebTauTestList;22import java.util.*;23import java.util.stream.Collectors;24import java.util.stream.Stream;25public class OpenApiReportDataProvider implements ReportDataProvider {26 @Override27 public Stream<WebTauReportCustomData> provide(WebTauTestList tests, WebTauReportLog log) {28 if (OpenApi.isCoverageUninitialized()) {29 return Stream.empty();30 }31 List<? extends Map<String, ?>> nonCoveredOperations = OpenApi.getCoverage().nonCoveredOperations()32 .map(OpenApiOperation::toMap)33 .collect(Collectors.toList());34 List<? extends Map<String, ?>> coveredOperations = OpenApi.getCoverage().coveredOperations()35 .map(OpenApiOperation::toMap)36 .collect(Collectors.toList());37 List<? extends Map<String, ?>> coveredResponses = convertResponses(OpenApi.getCoverage().coveredResponses());38 List<? extends Map<String, ?>> nonCoveredResponses = convertResponses(OpenApi.getCoverage().nonCoveredResponses());39 return Stream.of(40 new WebTauReportCustomData("openApiSkippedOperations", nonCoveredOperations),41 new WebTauReportCustomData("openApiCoveredOperations", coveredOperations),42 new WebTauReportCustomData("openApiHttpCallIdsPerOperation",43 OpenApi.getCoverage().httpCallIdsByOperationAsMap()),44 new WebTauReportCustomData("openApiHttpCallsPerOperation",45 OpenApi.getCoverage().httpCallsByOperationAsMap()),46 new WebTauReportCustomData("openApiCoveredResponses", coveredResponses),47 new WebTauReportCustomData("openApiSkippedResponses", nonCoveredResponses));48 }49 private static List<? extends Map<String, ?>> convertResponses(Map<OpenApiOperation, Set<String>> responses) {50 return responses.entrySet()51 .stream()52 .flatMap(entry ->53 entry.getValue().stream().map(statusCode -> {54 Map<String, Object> responseMap = new HashMap<>(entry.getKey().toMap());55 responseMap.put("statusCode", statusCode);56 return responseMap;57 }))58 .collect(Collectors.toList());59 }60}...
Source:OpenApiSpecConfig.java
...23import java.nio.file.Paths;24import java.util.stream.Stream;25import static org.testingisdocumenting.webtau.cfg.ConfigValue.declare;26import static org.testingisdocumenting.webtau.cfg.WebTauConfig.getCfg;27public class OpenApiSpecConfig implements WebTauConfigHandler {28 static final ConfigValue specUrl = declare("openApiSpecUrl",29 "url of OpenAPI 2 spec against which to validate http calls", () -> "");30 static final ConfigValue ignoreAdditionalProperties = declare("openApiIgnoreAdditionalProperties",31 "ignore additional OpenAPI properties ", () -> false);32 static OpenApiSpecLocation determineSpecFullPathOrUrl() {33 return OpenApiSpecLocation.fromStringValue(specUrl.getAsString());34 }35 @Override36 public void onAfterCreate(WebTauConfig cfg) {37 OpenApi.reset();38 }39 @Override40 public Stream<ConfigValue> additionalConfigValues() {41 return Stream.of(specUrl, ignoreAdditionalProperties);42 }43}...
OpenApi
Using AI Code Generation
1import org.testingisdocumenting.webtau.openapi.OpenApi;2import org.testingisdocumenting.webtau.openapi.OpenApiExpectation;3import org.testingisdocumenting.webtau.openapi.OpenApiOperation;4import org.testingisdocumenting.webtau.openapi.OpenApiRequestBody;5import org.testingisdocumenting.webtau.openapi.OpenApiResponse;6import org.testingisdocumenting.webtau.openapi.OpenApiResponseHeader;7import org.testingisdocumenting.webtau.openapi.OpenApiResponseHeaders;8import org.testingisdocumenting.webtau.openapi.OpenApiResponseBody;9import org.testingisdocumenting.webtau.openapi.OpenApiSchema;10import org.testingisdocumenting.webtau.openapi.OpenApiSchemaField;11import org.testingisdocumenting.webtau.openapi.OpenApiSchemaFieldArray;12import org.testingisdocumenting.webtau.openapi.OpenApiSchemaFieldBoolean;13import org.testingisdocumenting.webtau.openapi.OpenApiSchemaFieldInteger;14import org.testingisdocumenting.webtau.openapi.OpenApiSchemaFieldObject;15import org.testingisdocumenting.webtau.openapi.OpenApiSchemaFieldString;16import org.testingisdocumenting.webtau.openapi.OpenApiSchemaType;17import org.testingisdocumenting.webtau.openapi.OpenApiSpec;18import org.testingisdocumenting.webtau.openapi.OpenApiSpecUrl;19import org.testingisdocumenting.webtau.openapi.OpenApiValidationResult;20import org.testingisdocumenting.webtau.openapi.OpenApiValidationResults;21import java.util.List;22import java.util.Map;23import static org.testingisdocumenting.webtau.Ddjt.*;24import static org.testingisdocumenting.webtau.OpenApiDsl.*;25public class OpenApiSpecValidation {26 public void validatePetStore() {27 OpenApiSpec spec = OpenApiSpec.load(url);28 OpenApiValidationResults results = OpenApi.validate(spec);29 assertOpenApiValidationResults(results);30 }31 public void validatePetStoreUsingJson() {32 OpenApiSpec spec = OpenApiSpec.loadJson(json);33 OpenApiValidationResults results = OpenApi.validate(spec);34 assertOpenApiValidationResults(results);35 }36 public void validatePetStoreUsingYaml() {
OpenApi
Using AI Code Generation
1package org.testingisdocumenting.webtau.openapi;2import org.testingisdocumenting.webtau.Ddjt;3import org.testingisdocumenting.webtau.http.HttpHeader;4import org.testingisdocumenting.webtau.http.HttpParameter;5import org.testingisdocumenting.webtau.http.HttpRequestBody;6import org.testingisdocumenting.webtau.http.HttpValidationOptions;7import org.testingisdocumenting.webtau.http.datanode.DataNode;8import org.testingisdocumenting.webtau.reporter.StepReportOptions;9import java.util.List;10import java.util.Map;11public class OpenApi {12 public static OpenApiSpec spec(String specContent) {13 return Ddjt.create(OpenApiSpec.class, specContent);14 }15 public static OpenApiSpec spec(String specContent, StepReportOptions stepReportOptions) {16 return Ddjt.create(OpenApiSpec.class, specContent, stepReportOptions);17 }18 public static OpenApiSpec specUrl(String specUrl) {19 return Ddjt.create(OpenApiSpec.class, specUrl);20 }21 public static OpenApiSpec specUrl(String specUrl, StepReportOptions stepReportOptions) {22 return Ddjt.create(OpenApiSpec.class, specUrl, stepReportOptions);23 }24 public static OpenApiSpec specPath(String specPath) {25 return Ddjt.create(OpenApiSpec.class, specPath);26 }27 public static OpenApiSpec specPath(String specPath, StepReportOptions stepReportOptions) {28 return Ddjt.create(OpenApiSpec.class, specPath, stepReportOptions);29 }
OpenApi
Using AI Code Generation
1import org.testingisdocumenting.webtau.http.Http;2import org.testingisdocumenting.webtau.openapi.OpenApi;3import org.testingisdocumenting.webtau.utils.FileUtils;4import static org.testingisdocumenting.webtau.Ddjt.*;5OpenApi openApi = OpenApi.fromYaml(FileUtils.getResourceAsString("petstore.yaml"));6Http.get("/pet/1", openApi.validate("getPetById"));7import org.testingisdocumenting.webtau.http.Http;8import org.testingisdocumenting.webtau.openapi.OpenApi;9import org.testingisdocumenting.webtau.utils.FileUtils;10import static org.testingisdocumenting.webtau.Ddjt.*;11OpenApi openApi = OpenApi.fromYaml(FileUtils.getResourceAsString("petstore.yaml"));12Http.get("/pet/1", openApi.validate("getPetById"));13import org.testingisdocumenting.webtau.http.Http;14import org.testingisdocumenting.webtau.openapi.OpenApi;15import org.testingisdocumenting.webtau.utils.FileUtils;16import static org.testingisdocumenting.webtau.Ddjt.*;17OpenApi openApi = OpenApi.fromYaml(FileUtils.getResourceAsString("petstore.yaml"));18Http.get("/pet/1", openApi.validate("getPetById"));19import org.testingisdocumenting.webtau.http.Http;20import org.testingisdocumenting.webtau.openapi.OpenApi;21import org.testingisdocumenting.webtau.utils.FileUtils;22import static org.testingisdocumenting.webtau.Ddjt.*;23OpenApi openApi = OpenApi.fromYaml(FileUtils.getResourceAsString("petstore.yaml"));24Http.get("/pet/1", openApi.validate("getPetById"));25import org.testingisdocument
OpenApi
Using AI Code Generation
1import org.testingisdocumenting.webtau.openapi.OpenApi;2import org.testingisdocumenting.webtau.openapi.OpenApiOperation;3import static org.testingisdocumenting.webtau.WebTauDsl.*;4OpenApi api = OpenApi.create("api.yaml");5OpenApiOperation operation = api.operation("get /api/employees/{id}");6operation.request()7 .pathParam("id", 1)8 .send();9operation.response()10 .statusCode(200)11 .body(12 json(13 map(14 .header("Content-Type", "application/json");
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!!