Skip to content

Commit

Permalink
[JAVA][Rest-assured] add more information about operations (OpenAPITo…
Browse files Browse the repository at this point in the history
…ols#815)

Add swagger annotations to client and set default baseReqSpec and baseContextConsumer in ApiClient
  • Loading branch information
viclovsky authored and jmini committed Aug 15, 2018
1 parent e8647b1 commit 5753eeb
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import {{apiPackage}}.*;

{{#imports}}import {{import}};
{{/imports}}

{{^fullJavaUtil}}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.response.Response;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import static io.restassured.config.ObjectMapperConfig.objectMapperConfig;
import static io.restassured.config.RestAssuredConfig.config;
import static org.openapitools.client.GsonObjectMapper.gson;
{{/fullJavaUtil}}

public class ApiClient {
{{#basePath}}
public static final String BASE_URI = "{{basePath}}";
{{/basePath}}

private final Config config;

Expand All @@ -42,7 +40,9 @@ public class ApiClient {
{{/apiInfo}}

public static class Config {
private Supplier<RequestSpecBuilder> baseReqSpec;
private Supplier<RequestSpecBuilder> baseReqSpec = () -> new RequestSpecBuilder()
{{#basePath}}.setBaseUri(BASE_URI){{/basePath}}
.setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson())));

/**
* Use common specification for all operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import java.util.Map;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.swagger.annotations.*;

import java.lang.reflect.Type;
import java.util.function.Consumer;
Expand All @@ -41,6 +43,15 @@ public class {{classname}} {
{{#operations}}
{{#operation}}

@ApiOperation(value = "{{{summary}}}", notes = "{{{notes}}}"{{#hasAuthMethods}}, authorizations = {
{{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = {
{{#scopes}}@AuthorizationScope(scope = "{{scope}}", description = "{{description}}"){{#hasMore}},
{{/hasMore}}{{/scopes}}
}{{/isOAuth}}){{#hasMore}},
{{/hasMore}}{{/authMethods}}
}{{/hasAuthMethods}}, tags={ {{#tags}}{{#name}}"{{{name}}}"{{/name}}{{^-last}}, {{/-last}}{{/tags}} })
@ApiResponses(value = { {{#responses}}
@ApiResponse(code = {{{code}}}, message = "{{{message}}}") {{#hasMore}},{{/hasMore}}{{/responses}} })
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
Expand Down Expand Up @@ -83,29 +94,14 @@ public class {{classname}} {
{{#isDeprecated}}
@Deprecated
{{/isDeprecated}}
public class {{operationIdCamelCase}}Oper {
public static class {{operationIdCamelCase}}Oper {
public static final String REQ_METHOD = "{{httpMethod}}";
public static final Method REQ_METHOD = {{httpMethod}};
public static final String REQ_URI = "{{path}}";
public static final String SUMMARY = "{{{summary}}}";

private RequestSpecBuilder reqSpec;
private ResponseSpecBuilder respSpec;

public {{operationIdCamelCase}}Oper() {
this.reqSpec = new RequestSpecBuilder();
{{#vendorExtensions}}
{{#x-contentType}}
reqSpec.setContentType("{{x-contentType}}");
{{/x-contentType}}
{{#x-accepts}}
reqSpec.setAccept("{{x-accepts}}");
{{/x-accepts}}
{{/vendorExtensions}}
this.respSpec = new ResponseSpecBuilder();
}

public {{operationIdCamelCase}}Oper(RequestSpecBuilder reqSpec) {
this.reqSpec = reqSpec;
{{#vendorExtensions}}
Expand All @@ -126,7 +122,7 @@ public class {{classname}} {
* @return type
*/
public <T> T execute(Function<Response, T> handler) {
return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request({{httpMethod}}, REQ_URI));
return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(REQ_METHOD, REQ_URI));
}
{{#returnType}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,16 @@

import org.openapitools.client.api.*;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.response.Response;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

import static io.restassured.config.ObjectMapperConfig.objectMapperConfig;
import static io.restassured.config.RestAssuredConfig.config;
import static org.openapitools.client.GsonObjectMapper.gson;

public class ApiClient {
public static final String BASE_URI = "http://petstore.swagger.io:80/v2";

private final Config config;

Expand Down Expand Up @@ -60,7 +56,9 @@ public UserApi user() {
}

public static class Config {
private Supplier<RequestSpecBuilder> baseReqSpec;
private Supplier<RequestSpecBuilder> baseReqSpec = () -> new RequestSpecBuilder()
.setBaseUri(BASE_URI)
.setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson())));

/**
* Use common specification for all operations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.builder.ResponseSpecBuilder;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.swagger.annotations.*;

import java.lang.reflect.Type;
import java.util.function.Consumer;
Expand All @@ -47,6 +49,9 @@ public static AnotherFakeApi anotherFake(RequestSpecBuilder reqSpec) {
}


@ApiOperation(value = "To test special tags", notes = "To test special tags and operation ID starting with number", tags={ "$another-fake?" })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "successful operation") })
public Call123testSpecialTagsOper call123testSpecialTags() {
return new Call123testSpecialTagsOper(reqSpec);
}
Expand All @@ -68,23 +73,14 @@ public AnotherFakeApi reqSpec(Consumer<RequestSpecBuilder> consumer) {
* @see #body client model (required)
* return Client
*/
public class Call123testSpecialTagsOper {
public static class Call123testSpecialTagsOper {

public static final String REQ_METHOD = "PATCH";
public static final Method REQ_METHOD = PATCH;
public static final String REQ_URI = "/another-fake/dummy";
public static final String SUMMARY = "To test special tags";

private RequestSpecBuilder reqSpec;

private ResponseSpecBuilder respSpec;

public Call123testSpecialTagsOper() {
this.reqSpec = new RequestSpecBuilder();
reqSpec.setContentType("application/json");
reqSpec.setAccept("application/json");
this.respSpec = new ResponseSpecBuilder();
}

public Call123testSpecialTagsOper(RequestSpecBuilder reqSpec) {
this.reqSpec = reqSpec;
reqSpec.setContentType("application/json");
Expand All @@ -99,7 +95,7 @@ public Call123testSpecialTagsOper(RequestSpecBuilder reqSpec) {
* @return type
*/
public <T> T execute(Function<Response, T> handler) {
return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(PATCH, REQ_URI));
return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(REQ_METHOD, REQ_URI));
}

/**
Expand Down
Loading

0 comments on commit 5753eeb

Please sign in to comment.