diff --git a/README.md b/README.md index 542e35201e60..9b04f1af9acf 100644 --- a/README.md +++ b/README.md @@ -530,7 +530,7 @@ CONFIG OPTIONS retrofit - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0) retrofit2 - HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta2) google-api-client - HTTP client: google-api-client 1.23.0. JSON processing: Jackson 2.8.9 - rest-assured", "HTTP client: rest-assured : 3.0.6. JSON processing: JSON processing: Gson 2.6.1 + rest-assured - HTTP client: rest-assured : 3.0.6. JSON processing: Gson 2.6.1. Only for Java8 ``` Your config file for Java can look like diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java index 600daf5295b4..dc010c36fe11 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java @@ -84,7 +84,7 @@ public JavaClientCodegen() { supportedLibraries.put("resteasy", "HTTP client: Resteasy client 3.1.3.Final. JSON processing: Jackson 2.8.9"); supportedLibraries.put("vertx", "HTTP client: VertX client 3.2.4. JSON processing: Jackson 2.8.9"); supportedLibraries.put("google-api-client", "HTTP client: Google API client 1.23.0. JSON processing: Jackson 2.8.9"); - supportedLibraries.put("rest-assured", "HTTP client: rest-assured : 3.0.6. JSON processing: JSON processing: Gson 2.6.1"); + supportedLibraries.put("rest-assured", "HTTP client: rest-assured : 3.0.6. JSON processing: Gson 2.6.1. Only for Java8"); CliOption libraryOption = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use"); libraryOption.setEnum(supportedLibraries); @@ -250,9 +250,11 @@ public void processOpts() { additionalProperties.put("jackson", "true"); } else if (REST_ASSURED.equals(getLibrary())) { - additionalProperties.put("java8", "true"); additionalProperties.put("gson", "true"); apiTemplateFiles.put("api.mustache", ".java"); + supportingFiles.add(new SupportingFile("ResponseSpecBuilders.mustache", invokerFolder, "ResponseSpecBuilders.java")); + supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java")); + supportingFiles.add(new SupportingFile("GsonObjectMapper.mustache", invokerFolder, "GsonObjectMapper.java")); } else { LOGGER.error("Unknown library option (-l/--library): " + getLibrary()); } diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ApiClient.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ApiClient.mustache index 1989ac5b5e5a..2b2698548722 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ApiClient.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ApiClient.mustache @@ -1,3 +1,5 @@ +{{>licenseInfo}} + package {{invokerPackage}}; import {{apiPackage}}.*; diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/GsonObjectMapper.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/GsonObjectMapper.mustache new file mode 100644 index 000000000000..f85ea9c85b20 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/GsonObjectMapper.mustache @@ -0,0 +1,27 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import io.restassured.mapper.ObjectMapper; +import io.restassured.mapper.ObjectMapperDeserializationContext; +import io.restassured.mapper.ObjectMapperSerializationContext; + +public class GsonObjectMapper implements ObjectMapper { + + private GsonObjectMapper() { + } + + public static GsonObjectMapper gson() { + return new GsonObjectMapper(); + } + + @Override + public Object deserialize(ObjectMapperDeserializationContext context) { + return new JSON().deserialize(context.getDataToDeserialize().asString(), context.getType()); + } + + @Override + public Object serialize(ObjectMapperSerializationContext context) { + return new JSON().serialize(context.getObjectToSerialize()); + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/JSON.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/JSON.mustache new file mode 100644 index 000000000000..34ae1795cf86 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/JSON.mustache @@ -0,0 +1,490 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapter; +import com.google.gson.internal.bind.util.ISO8601Utils; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import com.google.gson.JsonElement; +import io.gsonfire.GsonFireBuilder; +import io.gsonfire.TypeSelector; +{{#joda}} +import org.joda.time.DateTime; +import org.joda.time.LocalDate; +import org.joda.time.format.DateTimeFormatter; +import org.joda.time.format.DateTimeFormatterBuilder; +import org.joda.time.format.ISODateTimeFormat; +{{/joda}} +{{#threetenbp}} +import org.threeten.bp.LocalDate; +import org.threeten.bp.OffsetDateTime; +import org.threeten.bp.format.DateTimeFormatter; +{{/threetenbp}} + +import {{modelPackage}}.*; + +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.ParsePosition; +{{#java8}} +import java.time.LocalDate; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; +{{/java8}} +import java.util.Date; +import java.util.Map; +import java.util.HashMap; + +public class JSON { + private Gson gson; + private boolean isLenientOnJson = false; + private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter(); + private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); + {{#joda}} + private DateTimeTypeAdapter dateTimeTypeAdapter = new DateTimeTypeAdapter(); + private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + {{/joda}} + {{#jsr310}} + private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); + private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + {{/jsr310}} + + public static GsonBuilder createGson() { + GsonFireBuilder fireBuilder = new GsonFireBuilder() + {{#parent}} + .registerTypeSelector({{classname}}.class, new TypeSelector() { + @Override + public Class getClassForElement(JsonElement readElement) { + Map classByDiscriminatorValue = new HashMap(); + {{#children}} + classByDiscriminatorValue.put("{{name}}".toUpperCase(), {{classname}}.class); + {{/children}} + classByDiscriminatorValue.put("{{classname}}".toUpperCase(), {{classname}}.class); + return getClassByDiscriminator( + classByDiscriminatorValue, + getDiscriminatorValue(readElement, "{{discriminator}}")); + } + }) + {{/parent}} + ; + return fireBuilder.createGsonBuilder(); + } + + private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) { + JsonElement element = readElement.getAsJsonObject().get(discriminatorField); + if(null == element) { + throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">"); + } + return element.getAsString(); + } + + private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) { + Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase()); + if(null == clazz) { + throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">"); + } + return clazz; + } + + public JSON() { + gson = createGson() + .registerTypeAdapter(Date.class, dateTypeAdapter) + .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) + {{#joda}} + .registerTypeAdapter(DateTime.class, dateTimeTypeAdapter) + .registerTypeAdapter(LocalDate.class, localDateTypeAdapter) + {{/joda}} + {{#jsr310}} + .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) + .registerTypeAdapter(LocalDate.class, localDateTypeAdapter) + {{/jsr310}} + .create(); + } + + /** + * Get Gson. + * + * @return Gson + */ + public Gson getGson() { + return gson; + } + + /** + * Set Gson. + * + * @param gson Gson + * @return JSON + */ + public JSON setGson(Gson gson) { + this.gson = gson; + return this; + } + + public JSON setLenientOnJson(boolean lenientOnJson) { + isLenientOnJson = lenientOnJson; + return this; + } + + /** + * Serialize the given Java object into JSON string. + * + * @param obj Object + * @return String representation of the JSON + */ + public String serialize(Object obj) { + return gson.toJson(obj); + } + + /** + * Deserialize the given JSON string to Java object. + * + * @param Type + * @param body The JSON string + * @param returnType The type to deserialize into + * @return The deserialized Java object + */ + @SuppressWarnings("unchecked") + public T deserialize(String body, Type returnType) { + try { + if (isLenientOnJson) { + JsonReader jsonReader = new JsonReader(new StringReader(body)); + // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean) + jsonReader.setLenient(true); + return gson.fromJson(jsonReader, returnType); + } else { + return gson.fromJson(body, returnType); + } + } catch (JsonParseException e) { + // Fallback processing when failed to parse JSON form response body: + // return the response body string directly for the String return type; + if (returnType.equals(String.class)) + return (T) body; + else throw (e); + } + } + + {{#joda}} + /** + * Gson TypeAdapter for Joda DateTime type + */ + public static class DateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public DateTimeTypeAdapter() { + this(new DateTimeFormatterBuilder() + .append(ISODateTimeFormat.dateTime().getPrinter(), ISODateTimeFormat.dateOptionalTimeParser().getParser()) + .toFormatter()); + } + + public DateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, DateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.print(date)); + } + } + + @Override + public DateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + return formatter.parseDateTime(date); + } + } + } + + /** + * Gson TypeAdapter for Joda LocalDate type + */ + public class LocalDateTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTypeAdapter() { + this(ISODateTimeFormat.date()); + } + + public LocalDateTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDate date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.print(date)); + } + } + + @Override + public LocalDate read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + return formatter.parseLocalDate(date); + } + } + } + + public JSON setDateTimeFormat(DateTimeFormatter dateFormat) { + dateTimeTypeAdapter.setFormat(dateFormat); + return this; + } + + public JSON setLocalDateFormat(DateTimeFormatter dateFormat) { + localDateTypeAdapter.setFormat(dateFormat); + return this; + } + + {{/joda}} + {{#jsr310}} + /** + * Gson TypeAdapter for JSR310 OffsetDateTime type + */ + public static class OffsetDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public OffsetDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_OFFSET_DATE_TIME); + } + + public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, OffsetDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public OffsetDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + if (date.endsWith("+0000")) { + date = date.substring(0, date.length()-5) + "Z"; + } + return OffsetDateTime.parse(date, formatter); + } + } + } + + /** + * Gson TypeAdapter for JSR310 LocalDate type + */ + public class LocalDateTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE); + } + + public LocalDateTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDate date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDate read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + return LocalDate.parse(date, formatter); + } + } + } + + public JSON setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { + offsetDateTimeTypeAdapter.setFormat(dateFormat); + return this; + } + + public JSON setLocalDateFormat(DateTimeFormatter dateFormat) { + localDateTypeAdapter.setFormat(dateFormat); + return this; + } + + {{/jsr310}} + /** + * Gson TypeAdapter for java.sql.Date type + * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used + * (more efficient than SimpleDateFormat). + */ + public static class SqlDateTypeAdapter extends TypeAdapter { + + private DateFormat dateFormat; + + public SqlDateTypeAdapter() { + } + + public SqlDateTypeAdapter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + public void setFormat(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + @Override + public void write(JsonWriter out, java.sql.Date date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + String value; + if (dateFormat != null) { + value = dateFormat.format(date); + } else { + value = date.toString(); + } + out.value(value); + } + } + + @Override + public java.sql.Date read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + if (dateFormat != null) { + return new java.sql.Date(dateFormat.parse(date).getTime()); + } + return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime()); + } catch (ParseException e) { + throw new JsonParseException(e); + } + } + } + } + + /** + * Gson TypeAdapter for java.util.Date type + * If the dateFormat is null, ISO8601Utils will be used. + */ + public static class DateTypeAdapter extends TypeAdapter { + + private DateFormat dateFormat; + + public DateTypeAdapter() { + } + + public DateTypeAdapter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + public void setFormat(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + @Override + public void write(JsonWriter out, Date date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + String value; + if (dateFormat != null) { + value = dateFormat.format(date); + } else { + value = ISO8601Utils.format(date, true); + } + out.value(value); + } + } + + @Override + public Date read(JsonReader in) throws IOException { + try { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + if (dateFormat != null) { + return dateFormat.parse(date); + } + return ISO8601Utils.parse(date, new ParsePosition(0)); + } catch (ParseException e) { + throw new JsonParseException(e); + } + } + } catch (IllegalArgumentException e) { + throw new JsonParseException(e); + } + } + } + + public JSON setDateFormat(DateFormat dateFormat) { + dateTypeAdapter.setFormat(dateFormat); + return this; + } + + public JSON setSqlDateFormat(DateFormat dateFormat) { + sqlDateTypeAdapter.setFormat(dateFormat); + return this; + } + +} diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ResponseSpecBuilders.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ResponseSpecBuilders.mustache new file mode 100644 index 000000000000..af7e7ac960f8 --- /dev/null +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/ResponseSpecBuilders.mustache @@ -0,0 +1,31 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import io.restassured.builder.ResponseSpecBuilder; +import io.restassured.response.Response; +import io.restassured.specification.ResponseSpecification; + +import java.util.function.Function; + +public class ResponseSpecBuilders { + + private ResponseSpecBuilders() { + } + + public static Function validatedWith(ResponseSpecification respSpec) { + return response -> response.then().spec(respSpec).extract().response(); + } + + public static Function validatedWith(ResponseSpecBuilder respSpec) { + return validatedWith(respSpec.build()); + } + + /** + * @param code expected status code + * @return ResponseSpecBuilder + */ + public static ResponseSpecBuilder shouldBeCode(int code) { + return new ResponseSpecBuilder().expectStatusCode(code); + } +} \ No newline at end of file diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api.mustache index 9b3cd874cbfb..bbb70c0e8e27 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api.mustache @@ -1,3 +1,5 @@ +{{>licenseInfo}} + package {{package}}; {{#imports}}import {{import}}; @@ -18,8 +20,8 @@ import java.util.function.Function; import java.util.function.Supplier; {{/fullJavaUtil}} +import static {{invokerPackage}}.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class {{classname}} { @@ -35,13 +37,13 @@ public class {{classname}} { {{#operations}} {{#operation}} + {{#isDeprecated}} @Deprecated {{/isDeprecated}} public {{operationIdCamelCase}}Oper {{operationId}}() { return new {{operationIdCamelCase}}Oper(reqSpec); } - {{/operation}} {{/operations}} @@ -52,7 +54,7 @@ public class {{classname}} { * {{notes}} * {{#allParams}} - * @see #{{#isPathParam}}{{paramName}}Path{{/isPathParam}}{{#isQueryParam}}{{paramName}}Query{{/isQueryParam}}{{#isFormParam}}{{paramName}}Form{{/isFormParam}}{{#isHeaderParam}}{{paramName}}Header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} + * @see #{{#isPathParam}}{{paramName}}Path{{/isPathParam}}{{#isQueryParam}}{{paramName}}Query{{/isQueryParam}}{{#isFormParam}}{{^isFile}}{{paramName}}Form{{/isFile}}{{#isFile}}{{paramName}}MultiPart{{/isFile}}{{/isFormParam}}{{#isHeaderParam}}{{paramName}}Header{{/isHeaderParam}}{{#isBodyParam}}body{{/isBodyParam}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} {{/allParams}} {{#returnType}} * return {{{returnType}}} @@ -78,11 +80,27 @@ public class {{classname}} { 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}} + {{#x-contentType}} + reqSpec.setContentType("{{x-contentType}}"); + {{/x-contentType}} + {{#x-accepts}} + reqSpec.setAccept("{{x-accepts}}"); + {{/x-accepts}} + {{/vendorExtensions}} this.respSpec = new ResponseSpecBuilder(); } @@ -92,42 +110,56 @@ public class {{classname}} { public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request({{httpMethod}}, REQ_URI)); } - {{#returnType}} {{#returnSimpleType}} + {{^returnTypeIsPrimitive}} + /** * {{httpMethod}} {{path}} * @return {{{returnType}}} */ public {{{returnType}}} executeAs(Function handler) { - return execute(handler).as({{{returnType}}}.class, GSON); + return execute(handler).as({{{returnType}}}.class, gson()); } + {{/returnTypeIsPrimitive}} {{/returnSimpleType}} - {{^returnSimpleType}} {{#isListContainer}} + /** * {{httpMethod}} {{path}} * @return {{{returnType}}} */ public {{{returnType}}} executeAs(Function handler) { - return Arrays.asList(execute(handler).as({{{returnBaseType}}}[].class, GSON)); + return Arrays.asList(execute(handler).as({{{returnBaseType}}}[].class, gson())); } {{/isListContainer}} {{/returnSimpleType}} - {{/returnType}} + {{#returnSimpleType}} + {{#returnTypeIsPrimitive}} + /** + * {{httpMethod}} {{path}} + * @return String + */ + public String executeAs(Function handler) { + return execute(handler).asString(); + } + {{/returnTypeIsPrimitive}} + {{/returnSimpleType}} + {{/returnType}} {{#bodyParams}} + /** * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} */ public {{operationIdCamelCase}}Oper body({{{dataType}}} {{paramName}}) { - reqSpec.setBody({{paramName}}, GSON); + reqSpec.setBody({{paramName}}, gson()); return this; } {{/bodyParams}} - {{#headerParams}} + /** * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} */ @@ -136,8 +168,8 @@ public class {{classname}} { return this; } {{/headerParams}} - {{#pathParams}} + /** * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} */ @@ -146,8 +178,8 @@ public class {{classname}} { return this; } {{/pathParams}} - {{#queryParams}} + /** * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} */ @@ -156,8 +188,9 @@ public class {{classname}} { return this; } {{/queryParams}} - {{#formParams}} + {{^isFile}} + /** * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} */ @@ -165,6 +198,21 @@ public class {{classname}} { reqSpec.addFormParam("{{baseName}}", {{paramName}}); return this; } + {{/isFile}} + {{/formParams}} + {{#formParams}} + {{#isFile}} + + /** + * It will assume that the control name is file and the is + * @see #reqSpec for customise + * @param {{paramName}} {{description}}{{#required}} (required){{/required}}{{^required}} (optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}} + */ + public {{operationIdCamelCase}}Oper {{paramName}}MultiPart({{{dataType}}} {{paramName}}) { + reqSpec.addMultiPart({{paramName}}); + return this; + } + {{/isFile}} {{/formParams}} /** diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api_test.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api_test.mustache index ef3c5551bc9d..a6b6c511d8d4 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api_test.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/api_test.mustache @@ -4,9 +4,10 @@ package {{package}}; {{#imports}}import {{import}}; {{/imports}} -import io.swagger.client.ApiClient; -import io.swagger.client.api.{{classname}}; +import {{invokerPackage}}.ApiClient; +import {{apiPackage}}.{{classname}}; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; import org.junit.Before; import org.junit.Test; import org.junit.Ignore; @@ -17,10 +18,10 @@ import java.util.HashMap; import java.util.List; import java.util.Map; {{/fullJavaUtil}} - - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.filter.log.LogDetail.ALL; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static {{invokerPackage}}.GsonObjectMapper.gson; /** * API tests for {{classname}} @@ -33,8 +34,9 @@ public class {{classname}}Test { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .log(ALL) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("{{basePath}}"))).{{classVarName}}(); } {{#operations}} @@ -51,8 +53,9 @@ public class {{classname}}Test { {{/allParams}} api.{{operationId}}(){{#allParams}}{{#required}}{{#isPathParam}} .{{paramName}}Path({{paramName}}){{/isPathParam}}{{#isQueryParam}} - .{{paramName}}Query({{paramName}}){{/isQueryParam}}{{#isFormParam}} - .{{paramName}}Form({{paramName}}){{/isFormParam}}{{#isHeaderParam}} + .{{paramName}}Query({{paramName}}){{/isQueryParam}}{{#isFormParam}}{{^isFile}} + .{{paramName}}Form({{paramName}}){{/isFile}}{{/isFormParam}}{{#isFormParam}}{{#isFile}} + .{{paramName}}MultiPart({{paramName}}){{/isFile}}{{/isFormParam}}{{#isHeaderParam}} .{{paramName}}Header({{paramName}}){{/isHeaderParam}}{{#isBodyParam}} .body({{paramName}}){{/isBodyParam}}{{/required}}{{/allParams}}.execute(r -> r.prettyPeek()); // TODO: test validations diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache index 87d7d7caf6c5..0b3d148d8654 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.gradle.mustache @@ -98,6 +98,7 @@ ext { rest_assured_version = "3.0.6" junit_version = "4.12" gson_version = "2.6.1" + gson_fire_version = "1.8.2" {{#joda}} jodatime_version = "2.9.9" {{/joda}} @@ -109,6 +110,7 @@ ext { dependencies { compile "io.swagger:swagger-annotations:$swagger_annotations_version" compile "io.rest-assured:scala-support:$rest_assured_version" + compile "io.gsonfire:gson-fire:$gson_fire_version" {{#joda}} compile "joda-time:joda-time:$jodatime_version" compile 'com.google.code.gson:gson:$gson_version' diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache index 201ac884a912..da24a8fdd8c8 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/build.sbt.mustache @@ -12,6 +12,7 @@ lazy val root = (project in file(".")). "io.swagger" % "swagger-annotations" % "1.5.15", "io.rest-assured" % "scala-support" % "3.0.6" "com.google.code.gson" % "gson" % "2.6.1", + "io.gsonfire" % "gson-fire" % "1.8.2" % "compile", {{#joda}} "joda-time" % "joda-time" % "2.9.9" % "compile", {{/joda}} diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache index 8263fe83641a..8024f08a9d0b 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/rest-assured/pom.mustache @@ -232,12 +232,18 @@ ${junit-version} test + + io.gsonfire + gson-fire + ${gson-fire-version} + UTF-8 1.5.15 3.0.6 2.6.1 + 1.8.2 1.0.0 {{#joda}} 2.9.9 diff --git a/samples/client/petstore/java/rest-assured/build.gradle b/samples/client/petstore/java/rest-assured/build.gradle index a2437c82912f..7f6177babb85 100644 --- a/samples/client/petstore/java/rest-assured/build.gradle +++ b/samples/client/petstore/java/rest-assured/build.gradle @@ -98,12 +98,14 @@ ext { rest_assured_version = "3.0.6" junit_version = "4.12" gson_version = "2.6.1" + gson_fire_version = "1.8.2" threetenbp_version = "1.3.5" } dependencies { compile "io.swagger:swagger-annotations:$swagger_annotations_version" compile "io.rest-assured:scala-support:$rest_assured_version" + compile "io.gsonfire:gson-fire:$gson_fire_version" compile "org.threeten:threetenbp:$threetenbp_version" testCompile "junit:junit:$junit_version" } diff --git a/samples/client/petstore/java/rest-assured/build.sbt b/samples/client/petstore/java/rest-assured/build.sbt index 54d719289ec9..13a2cedb077c 100644 --- a/samples/client/petstore/java/rest-assured/build.sbt +++ b/samples/client/petstore/java/rest-assured/build.sbt @@ -12,6 +12,7 @@ lazy val root = (project in file(".")). "io.swagger" % "swagger-annotations" % "1.5.15", "io.rest-assured" % "scala-support" % "3.0.6" "com.google.code.gson" % "gson" % "2.6.1", + "io.gsonfire" % "gson-fire" % "1.8.2" % "compile", "org.threeten" % "threetenbp" % "1.3.5" % "compile", "junit" % "junit" % "4.12" % "test", "com.novocode" % "junit-interface" % "0.10" % "test" diff --git a/samples/client/petstore/java/rest-assured/pom.xml b/samples/client/petstore/java/rest-assured/pom.xml index a4fe6920eb59..ee53ae7ada56 100644 --- a/samples/client/petstore/java/rest-assured/pom.xml +++ b/samples/client/petstore/java/rest-assured/pom.xml @@ -223,12 +223,18 @@ ${junit-version} test + + io.gsonfire + gson-fire + ${gson-fire-version} + UTF-8 1.5.15 3.0.6 2.6.1 + 1.8.2 1.0.0 1.3.5 4.12 diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ApiClient.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ApiClient.java index 084e1b0d7b36..92080aa1a52c 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ApiClient.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ApiClient.java @@ -1,18 +1,26 @@ -package io.swagger.client; +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ -import io.swagger.client.api.*; +package io.swagger.client; -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 io.swagger.client.api.AnotherFakeApi; +import io.swagger.client.api.FakeApi; +import io.swagger.client.api.FakeClassnameTags123Api; +import io.swagger.client.api.PetApi; +import io.swagger.client.api.StoreApi; +import io.swagger.client.api.UserApi; + import java.util.function.Supplier; public class ApiClient { diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/GsonObjectMapper.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/GsonObjectMapper.java new file mode 100644 index 000000000000..1af7fead69cb --- /dev/null +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/GsonObjectMapper.java @@ -0,0 +1,38 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + +package io.swagger.client; + +import io.restassured.mapper.ObjectMapper; +import io.restassured.mapper.ObjectMapperDeserializationContext; +import io.restassured.mapper.ObjectMapperSerializationContext; + +public class GsonObjectMapper implements ObjectMapper { + + private GsonObjectMapper() { + } + + public static GsonObjectMapper gson() { + return new GsonObjectMapper(); + } + + @Override + public Object deserialize(ObjectMapperDeserializationContext context) { + return new JSON().deserialize(context.getDataToDeserialize().asString(), context.getType()); + } + + @Override + public Object serialize(ObjectMapperSerializationContext context) { + return new JSON().serialize(context.getObjectToSerialize()); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java new file mode 100644 index 000000000000..df84d970239b --- /dev/null +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/JSON.java @@ -0,0 +1,374 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + +package io.swagger.client; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonParseException; +import com.google.gson.TypeAdapter; +import com.google.gson.internal.bind.util.ISO8601Utils; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import com.google.gson.JsonElement; +import io.gsonfire.GsonFireBuilder; +import io.gsonfire.TypeSelector; +import org.threeten.bp.LocalDate; +import org.threeten.bp.OffsetDateTime; +import org.threeten.bp.format.DateTimeFormatter; + +import io.swagger.client.model.*; + +import java.io.IOException; +import java.io.StringReader; +import java.lang.reflect.Type; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.ParsePosition; +import java.util.Date; +import java.util.Map; +import java.util.HashMap; + +public class JSON { + private Gson gson; + private boolean isLenientOnJson = false; + private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter(); + private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter(); + private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter(); + private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter(); + + public static GsonBuilder createGson() { + GsonFireBuilder fireBuilder = new GsonFireBuilder() + .registerTypeSelector(Animal.class, new TypeSelector() { + @Override + public Class getClassForElement(JsonElement readElement) { + Map classByDiscriminatorValue = new HashMap(); + classByDiscriminatorValue.put("Cat".toUpperCase(), Cat.class); + classByDiscriminatorValue.put("Dog".toUpperCase(), Dog.class); + classByDiscriminatorValue.put("Animal".toUpperCase(), Animal.class); + return getClassByDiscriminator( + classByDiscriminatorValue, + getDiscriminatorValue(readElement, "className")); + } + }) + ; + return fireBuilder.createGsonBuilder(); + } + + private static String getDiscriminatorValue(JsonElement readElement, String discriminatorField) { + JsonElement element = readElement.getAsJsonObject().get(discriminatorField); + if(null == element) { + throw new IllegalArgumentException("missing discriminator field: <" + discriminatorField + ">"); + } + return element.getAsString(); + } + + private static Class getClassByDiscriminator(Map classByDiscriminatorValue, String discriminatorValue) { + Class clazz = (Class) classByDiscriminatorValue.get(discriminatorValue.toUpperCase()); + if(null == clazz) { + throw new IllegalArgumentException("cannot determine model class of name: <" + discriminatorValue + ">"); + } + return clazz; + } + + public JSON() { + gson = createGson() + .registerTypeAdapter(Date.class, dateTypeAdapter) + .registerTypeAdapter(java.sql.Date.class, sqlDateTypeAdapter) + .registerTypeAdapter(OffsetDateTime.class, offsetDateTimeTypeAdapter) + .registerTypeAdapter(LocalDate.class, localDateTypeAdapter) + .create(); + } + + /** + * Get Gson. + * + * @return Gson + */ + public Gson getGson() { + return gson; + } + + /** + * Set Gson. + * + * @param gson Gson + * @return JSON + */ + public JSON setGson(Gson gson) { + this.gson = gson; + return this; + } + + public JSON setLenientOnJson(boolean lenientOnJson) { + isLenientOnJson = lenientOnJson; + return this; + } + + /** + * Serialize the given Java object into JSON string. + * + * @param obj Object + * @return String representation of the JSON + */ + public String serialize(Object obj) { + return gson.toJson(obj); + } + + /** + * Deserialize the given JSON string to Java object. + * + * @param Type + * @param body The JSON string + * @param returnType The type to deserialize into + * @return The deserialized Java object + */ + @SuppressWarnings("unchecked") + public T deserialize(String body, Type returnType) { + try { + if (isLenientOnJson) { + JsonReader jsonReader = new JsonReader(new StringReader(body)); + // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean) + jsonReader.setLenient(true); + return gson.fromJson(jsonReader, returnType); + } else { + return gson.fromJson(body, returnType); + } + } catch (JsonParseException e) { + // Fallback processing when failed to parse JSON form response body: + // return the response body string directly for the String return type; + if (returnType.equals(String.class)) + return (T) body; + else throw (e); + } + } + + /** + * Gson TypeAdapter for JSR310 OffsetDateTime type + */ + public static class OffsetDateTimeTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public OffsetDateTimeTypeAdapter() { + this(DateTimeFormatter.ISO_OFFSET_DATE_TIME); + } + + public OffsetDateTimeTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, OffsetDateTime date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public OffsetDateTime read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + if (date.endsWith("+0000")) { + date = date.substring(0, date.length()-5) + "Z"; + } + return OffsetDateTime.parse(date, formatter); + } + } + } + + /** + * Gson TypeAdapter for JSR310 LocalDate type + */ + public class LocalDateTypeAdapter extends TypeAdapter { + + private DateTimeFormatter formatter; + + public LocalDateTypeAdapter() { + this(DateTimeFormatter.ISO_LOCAL_DATE); + } + + public LocalDateTypeAdapter(DateTimeFormatter formatter) { + this.formatter = formatter; + } + + public void setFormat(DateTimeFormatter dateFormat) { + this.formatter = dateFormat; + } + + @Override + public void write(JsonWriter out, LocalDate date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + out.value(formatter.format(date)); + } + } + + @Override + public LocalDate read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + return LocalDate.parse(date, formatter); + } + } + } + + public JSON setOffsetDateTimeFormat(DateTimeFormatter dateFormat) { + offsetDateTimeTypeAdapter.setFormat(dateFormat); + return this; + } + + public JSON setLocalDateFormat(DateTimeFormatter dateFormat) { + localDateTypeAdapter.setFormat(dateFormat); + return this; + } + + /** + * Gson TypeAdapter for java.sql.Date type + * If the dateFormat is null, a simple "yyyy-MM-dd" format will be used + * (more efficient than SimpleDateFormat). + */ + public static class SqlDateTypeAdapter extends TypeAdapter { + + private DateFormat dateFormat; + + public SqlDateTypeAdapter() { + } + + public SqlDateTypeAdapter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + public void setFormat(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + @Override + public void write(JsonWriter out, java.sql.Date date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + String value; + if (dateFormat != null) { + value = dateFormat.format(date); + } else { + value = date.toString(); + } + out.value(value); + } + } + + @Override + public java.sql.Date read(JsonReader in) throws IOException { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + if (dateFormat != null) { + return new java.sql.Date(dateFormat.parse(date).getTime()); + } + return new java.sql.Date(ISO8601Utils.parse(date, new ParsePosition(0)).getTime()); + } catch (ParseException e) { + throw new JsonParseException(e); + } + } + } + } + + /** + * Gson TypeAdapter for java.util.Date type + * If the dateFormat is null, ISO8601Utils will be used. + */ + public static class DateTypeAdapter extends TypeAdapter { + + private DateFormat dateFormat; + + public DateTypeAdapter() { + } + + public DateTypeAdapter(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + public void setFormat(DateFormat dateFormat) { + this.dateFormat = dateFormat; + } + + @Override + public void write(JsonWriter out, Date date) throws IOException { + if (date == null) { + out.nullValue(); + } else { + String value; + if (dateFormat != null) { + value = dateFormat.format(date); + } else { + value = ISO8601Utils.format(date, true); + } + out.value(value); + } + } + + @Override + public Date read(JsonReader in) throws IOException { + try { + switch (in.peek()) { + case NULL: + in.nextNull(); + return null; + default: + String date = in.nextString(); + try { + if (dateFormat != null) { + return dateFormat.parse(date); + } + return ISO8601Utils.parse(date, new ParsePosition(0)); + } catch (ParseException e) { + throw new JsonParseException(e); + } + } + } catch (IllegalArgumentException e) { + throw new JsonParseException(e); + } + } + } + + public JSON setDateFormat(DateFormat dateFormat) { + dateTypeAdapter.setFormat(dateFormat); + return this; + } + + public JSON setSqlDateFormat(DateFormat dateFormat) { + sqlDateTypeAdapter.setFormat(dateFormat); + return this; + } + +} diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ResponseSpecBuilders.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ResponseSpecBuilders.java new file mode 100644 index 000000000000..8feefa1bd35a --- /dev/null +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/ResponseSpecBuilders.java @@ -0,0 +1,42 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + +package io.swagger.client; + +import io.restassured.builder.ResponseSpecBuilder; +import io.restassured.response.Response; +import io.restassured.specification.ResponseSpecification; + +import java.util.function.Function; + +public class ResponseSpecBuilders { + + private ResponseSpecBuilders() { + } + + public static Function validatedWith(ResponseSpecification respSpec) { + return response -> response.then().spec(respSpec).extract().response(); + } + + public static Function validatedWith(ResponseSpecBuilder respSpec) { + return validatedWith(respSpec.build()); + } + + /** + * @param code expected status code + * @return ResponseSpecBuilder + */ + public static ResponseSpecBuilder shouldBeCode(int code) { + return new ResponseSpecBuilder().expectStatusCode(code); + } +} \ No newline at end of file diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/AnotherFakeApi.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/AnotherFakeApi.java index 003b285ff85b..1357bea187ae 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/AnotherFakeApi.java @@ -1,22 +1,29 @@ -package io.swagger.client.api; +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ -import io.swagger.client.model.Client; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +package io.swagger.client.api; + import io.restassured.RestAssured; import io.restassured.builder.RequestSpecBuilder; import io.restassured.builder.ResponseSpecBuilder; import io.restassured.response.Response; +import io.swagger.client.model.Client; + import java.util.function.Consumer; import java.util.function.Function; -import java.util.function.Supplier; -import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.http.Method.PATCH; +import static io.swagger.client.GsonObjectMapper.gson; public class AnotherFakeApi { @@ -30,11 +37,11 @@ public static AnotherFakeApi anotherFake(RequestSpecBuilder reqSpec) { return new AnotherFakeApi(reqSpec); } + public TestSpecialTagsOper testSpecialTags() { return new TestSpecialTagsOper(reqSpec); } - /** * To test special tags * To test special tags @@ -52,11 +59,15 @@ public class TestSpecialTagsOper { public TestSpecialTagsOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public TestSpecialTagsOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -72,22 +83,17 @@ public T execute(Function handler) { * @return Client */ public Client executeAs(Function handler) { - return execute(handler).as(Client.class, GSON); + return execute(handler).as(Client.class, gson()); } - /** * @param body client model (required) */ public TestSpecialTagsOper body(Client body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeApi.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeApi.java index 40998cdc24a4..98f5c2c99b38 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeApi.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeApi.java @@ -1,3 +1,16 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + package io.swagger.client.api; import java.math.BigDecimal; @@ -19,8 +32,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static io.swagger.client.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class FakeApi { @@ -34,6 +47,7 @@ public static FakeApi fake(RequestSpecBuilder reqSpec) { return new FakeApi(reqSpec); } + public FakeOuterBooleanSerializeOper fakeOuterBooleanSerialize() { return new FakeOuterBooleanSerializeOper(reqSpec); } @@ -70,7 +84,6 @@ public TestJsonFormDataOper testJsonFormData() { return new TestJsonFormDataOper(reqSpec); } - /** * * Test serialization of outer boolean types @@ -88,11 +101,15 @@ public class FakeOuterBooleanSerializeOper { public FakeOuterBooleanSerializeOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FakeOuterBooleanSerializeOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -105,25 +122,20 @@ public T execute(Function handler) { /** * POST /fake/outer/boolean - * @return Boolean + * @return String */ - public Boolean executeAs(Function handler) { - return execute(handler).as(Boolean.class, GSON); + public String executeAs(Function handler) { + return execute(handler).asString(); } - /** * @param body Input boolean as post body (optional) */ public FakeOuterBooleanSerializeOper body(Boolean body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -157,11 +169,15 @@ public class FakeOuterCompositeSerializeOper { public FakeOuterCompositeSerializeOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FakeOuterCompositeSerializeOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -177,22 +193,17 @@ public T execute(Function handler) { * @return OuterComposite */ public OuterComposite executeAs(Function handler) { - return execute(handler).as(OuterComposite.class, GSON); + return execute(handler).as(OuterComposite.class, gson()); } - /** * @param body Input composite as post body (optional) */ public FakeOuterCompositeSerializeOper body(OuterComposite body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -226,11 +237,15 @@ public class FakeOuterNumberSerializeOper { public FakeOuterNumberSerializeOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FakeOuterNumberSerializeOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -246,22 +261,17 @@ public T execute(Function handler) { * @return BigDecimal */ public BigDecimal executeAs(Function handler) { - return execute(handler).as(BigDecimal.class, GSON); + return execute(handler).as(BigDecimal.class, gson()); } - /** * @param body Input number as post body (optional) */ public FakeOuterNumberSerializeOper body(BigDecimal body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -295,11 +305,15 @@ public class FakeOuterStringSerializeOper { public FakeOuterStringSerializeOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FakeOuterStringSerializeOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -315,22 +329,17 @@ public T execute(Function handler) { * @return String */ public String executeAs(Function handler) { - return execute(handler).as(String.class, GSON); + return execute(handler).asString(); } - /** * @param body Input string as post body (optional) */ public FakeOuterStringSerializeOper body(String body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -364,11 +373,15 @@ public class TestClientModelOper { public TestClientModelOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public TestClientModelOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -384,22 +397,17 @@ public T execute(Function handler) { * @return Client */ public Client executeAs(Function handler) { - return execute(handler).as(Client.class, GSON); + return execute(handler).as(Client.class, gson()); } - /** * @param body client model (required) */ public TestClientModelOper body(Client body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -445,11 +453,15 @@ public class TestEndpointParametersOper { public TestEndpointParametersOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/xml; charset=utf-8"); + reqSpec.setAccept("application/xml; charset=utf-8,application/json; charset=utf-8"); this.respSpec = new ResponseSpecBuilder(); } public TestEndpointParametersOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/xml; charset=utf-8"); + reqSpec.setAccept("application/xml; charset=utf-8,application/json; charset=utf-8"); this.respSpec = new ResponseSpecBuilder(); } @@ -460,11 +472,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - - - - - /** * @param integer None (optional) */ @@ -472,6 +479,7 @@ public TestEndpointParametersOper integerForm(Integer integer) { reqSpec.addFormParam("integer", integer); return this; } + /** * @param int32 None (optional) */ @@ -479,6 +487,7 @@ public TestEndpointParametersOper int32Form(Integer int32) { reqSpec.addFormParam("int32", int32); return this; } + /** * @param int64 None (optional) */ @@ -486,6 +495,7 @@ public TestEndpointParametersOper int64Form(Long int64) { reqSpec.addFormParam("int64", int64); return this; } + /** * @param number None (required) */ @@ -493,6 +503,7 @@ public TestEndpointParametersOper numberForm(BigDecimal number) { reqSpec.addFormParam("number", number); return this; } + /** * @param _float None (optional) */ @@ -500,6 +511,7 @@ public TestEndpointParametersOper _floatForm(Float _float) { reqSpec.addFormParam("float", _float); return this; } + /** * @param _double None (required) */ @@ -507,6 +519,7 @@ public TestEndpointParametersOper _doubleForm(Double _double) { reqSpec.addFormParam("double", _double); return this; } + /** * @param string None (optional) */ @@ -514,6 +527,7 @@ public TestEndpointParametersOper stringForm(String string) { reqSpec.addFormParam("string", string); return this; } + /** * @param patternWithoutDelimiter None (required) */ @@ -521,6 +535,7 @@ public TestEndpointParametersOper patternWithoutDelimiterForm(String patternWith reqSpec.addFormParam("pattern_without_delimiter", patternWithoutDelimiter); return this; } + /** * @param _byte None (required) */ @@ -528,6 +543,7 @@ public TestEndpointParametersOper _byteForm(byte[] _byte) { reqSpec.addFormParam("byte", _byte); return this; } + /** * @param binary None (optional) */ @@ -535,6 +551,7 @@ public TestEndpointParametersOper binaryForm(byte[] binary) { reqSpec.addFormParam("binary", binary); return this; } + /** * @param date None (optional) */ @@ -542,6 +559,7 @@ public TestEndpointParametersOper dateForm(LocalDate date) { reqSpec.addFormParam("date", date); return this; } + /** * @param dateTime None (optional) */ @@ -549,6 +567,7 @@ public TestEndpointParametersOper dateTimeForm(OffsetDateTime dateTime) { reqSpec.addFormParam("dateTime", dateTime); return this; } + /** * @param password None (optional) */ @@ -556,6 +575,7 @@ public TestEndpointParametersOper passwordForm(String password) { reqSpec.addFormParam("password", password); return this; } + /** * @param paramCallback None (optional) */ @@ -603,11 +623,15 @@ public class TestEnumParametersOper { public TestEnumParametersOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("*/*"); + reqSpec.setAccept("*/*"); this.respSpec = new ResponseSpecBuilder(); } public TestEnumParametersOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("*/*"); + reqSpec.setAccept("*/*"); this.respSpec = new ResponseSpecBuilder(); } @@ -618,8 +642,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - - /** * @param enumHeaderStringArray Header parameter enum test (string array) (optional) */ @@ -627,6 +649,7 @@ public TestEnumParametersOper enumHeaderStringArrayHeader(String enumHeaderStrin reqSpec.addHeader("enum_header_string_array", enumHeaderStringArray); return this; } + /** * @param enumHeaderString Header parameter enum test (string) (optional, default to -efg) */ @@ -635,7 +658,6 @@ public TestEnumParametersOper enumHeaderStringHeader(String enumHeaderString) { return this; } - /** * @param enumQueryStringArray Query parameter enum test (string array) (optional) */ @@ -643,6 +665,7 @@ public TestEnumParametersOper enumQueryStringArrayQuery(List enumQuerySt reqSpec.addQueryParam("enum_query_string_array", enumQueryStringArray); return this; } + /** * @param enumQueryString Query parameter enum test (string) (optional, default to -efg) */ @@ -650,6 +673,7 @@ public TestEnumParametersOper enumQueryStringQuery(String enumQueryString) { reqSpec.addQueryParam("enum_query_string", enumQueryString); return this; } + /** * @param enumQueryInteger Query parameter enum test (double) (optional) */ @@ -665,6 +689,7 @@ public TestEnumParametersOper enumFormStringArrayForm(List enumFormStrin reqSpec.addFormParam("enum_form_string_array", enumFormStringArray); return this; } + /** * @param enumFormString Form parameter enum test (string) (optional, default to -efg) */ @@ -672,6 +697,7 @@ public TestEnumParametersOper enumFormStringForm(String enumFormString) { reqSpec.addFormParam("enum_form_string", enumFormString); return this; } + /** * @param enumQueryDouble Query parameter enum test (double) (optional) */ @@ -712,11 +738,15 @@ public class TestInlineAdditionalPropertiesOper { public TestInlineAdditionalPropertiesOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public TestInlineAdditionalPropertiesOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -727,19 +757,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - /** * @param param request body (required) */ public TestInlineAdditionalPropertiesOper body(Object param) { - reqSpec.setBody(param, GSON); + reqSpec.setBody(param, gson()); return this; } - - - - /** * Customise request specification */ @@ -773,11 +798,15 @@ public class TestJsonFormDataOper { public TestJsonFormDataOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public TestJsonFormDataOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -788,11 +817,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - - - - - /** * @param param field1 (required) */ @@ -800,6 +824,7 @@ public TestJsonFormDataOper paramForm(String param) { reqSpec.addFormParam("param", param); return this; } + /** * @param param2 field2 (required) */ diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java index f41c2d6420c4..6eb135de1c43 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/FakeClassnameTags123Api.java @@ -1,3 +1,16 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + package io.swagger.client.api; import io.swagger.client.model.Client; @@ -15,8 +28,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static io.swagger.client.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class FakeClassnameTags123Api { @@ -30,11 +43,11 @@ public static FakeClassnameTags123Api fakeClassnameTags123(RequestSpecBuilder re return new FakeClassnameTags123Api(reqSpec); } + public TestClassnameOper testClassname() { return new TestClassnameOper(reqSpec); } - /** * To test class name in snake case * @@ -52,11 +65,15 @@ public class TestClassnameOper { public TestClassnameOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public TestClassnameOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -72,22 +89,17 @@ public T execute(Function handler) { * @return Client */ public Client executeAs(Function handler) { - return execute(handler).as(Client.class, GSON); + return execute(handler).as(Client.class, gson()); } - /** * @param body client model (required) */ public TestClassnameOper body(Client body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/PetApi.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/PetApi.java index 26a88af234aa..ba1ecaefc55f 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/PetApi.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/PetApi.java @@ -1,3 +1,16 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + package io.swagger.client.api; import java.io.File; @@ -17,8 +30,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static io.swagger.client.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class PetApi { @@ -32,6 +45,7 @@ public static PetApi pet(RequestSpecBuilder reqSpec) { return new PetApi(reqSpec); } + public AddPetOper addPet() { return new AddPetOper(reqSpec); } @@ -65,7 +79,6 @@ public UploadFileOper uploadFile() { return new UploadFileOper(reqSpec); } - /** * Add a new pet to the store * @@ -82,11 +95,15 @@ public class AddPetOper { public AddPetOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public AddPetOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -97,19 +114,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - /** * @param body Pet object that needs to be added to the store (required) */ public AddPetOper body(Pet body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -143,11 +155,13 @@ public class DeletePetOper { public DeletePetOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public DeletePetOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -158,8 +172,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(DELETE, REQ_URI)); } - - /** * @param apiKey (optional) */ @@ -176,8 +188,6 @@ public DeletePetOper petIdPath(Long petId) { return this; } - - /** * Customise request specification */ @@ -211,11 +221,13 @@ public class FindPetsByStatusOper { public FindPetsByStatusOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FindPetsByStatusOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -226,18 +238,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - /** * GET /pet/findByStatus * @return List */ public List executeAs(Function handler) { - return Arrays.asList(execute(handler).as(Pet[].class, GSON)); + return Arrays.asList(execute(handler).as(Pet[].class, gson())); } - - - /** * @param status Status values that need to be considered for filter (required) */ @@ -246,7 +254,6 @@ public FindPetsByStatusOper statusQuery(List status) { return this; } - /** * Customise request specification */ @@ -282,11 +289,13 @@ public class FindPetsByTagsOper { public FindPetsByTagsOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public FindPetsByTagsOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -297,18 +306,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - /** * GET /pet/findByTags * @return List */ public List executeAs(Function handler) { - return Arrays.asList(execute(handler).as(Pet[].class, GSON)); + return Arrays.asList(execute(handler).as(Pet[].class, gson())); } - - - /** * @param tags Tags to filter by (required) */ @@ -317,7 +322,6 @@ public FindPetsByTagsOper tagsQuery(List tags) { return this; } - /** * Customise request specification */ @@ -351,11 +355,13 @@ public class GetPetByIdOper { public GetPetByIdOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public GetPetByIdOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -371,12 +377,9 @@ public T execute(Function handler) { * @return Pet */ public Pet executeAs(Function handler) { - return execute(handler).as(Pet.class, GSON); + return execute(handler).as(Pet.class, gson()); } - - - /** * @param petId ID of pet to return (required) */ @@ -385,8 +388,6 @@ public GetPetByIdOper petIdPath(Long petId) { return this; } - - /** * Customise request specification */ @@ -419,11 +420,15 @@ public class UpdatePetOper { public UpdatePetOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public UpdatePetOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -434,19 +439,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(PUT, REQ_URI)); } - /** * @param body Pet object that needs to be added to the store (required) */ public UpdatePetOper body(Pet body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -481,11 +481,15 @@ public class UpdatePetWithFormOper { public UpdatePetWithFormOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/x-www-form-urlencoded"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public UpdatePetWithFormOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/x-www-form-urlencoded"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -496,9 +500,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - - - /** * @param petId ID of pet that needs to be updated (required) */ @@ -507,7 +508,6 @@ public UpdatePetWithFormOper petIdPath(Long petId) { return this; } - /** * @param name Updated name of the pet (optional) */ @@ -515,6 +515,7 @@ public UpdatePetWithFormOper nameForm(String name) { reqSpec.addFormParam("name", name); return this; } + /** * @param status Updated status of the pet (optional) */ @@ -545,7 +546,7 @@ public UpdatePetWithFormOper respSpec(Consumer consumer) { * * @see #petIdPath ID of pet to update (required) * @see #additionalMetadataForm Additional data to pass to server (optional) - * @see #fileForm file to upload (optional) + * @see #fileMultiPart file to upload (optional) * return ModelApiResponse */ public class UploadFileOper { @@ -558,11 +559,15 @@ public class UploadFileOper { public UploadFileOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("multipart/form-data"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public UploadFileOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("multipart/form-data"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -578,12 +583,9 @@ public T execute(Function handler) { * @return ModelApiResponse */ public ModelApiResponse executeAs(Function handler) { - return execute(handler).as(ModelApiResponse.class, GSON); + return execute(handler).as(ModelApiResponse.class, gson()); } - - - /** * @param petId ID of pet to update (required) */ @@ -592,7 +594,6 @@ public UploadFileOper petIdPath(Long petId) { return this; } - /** * @param additionalMetadata Additional data to pass to server (optional) */ @@ -600,11 +601,14 @@ public UploadFileOper additionalMetadataForm(String additionalMetadata) { reqSpec.addFormParam("additionalMetadata", additionalMetadata); return this; } + /** + * It will assume that the control name is file and the is + * @see #reqSpec for customise * @param file file to upload (optional) */ - public UploadFileOper fileForm(File file) { - reqSpec.addFormParam("file", file); + public UploadFileOper fileMultiPart(File file) { + reqSpec.addMultiPart(file); return this; } diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/StoreApi.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/StoreApi.java index 5b9935d64dea..bbc03f25bb74 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/StoreApi.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/StoreApi.java @@ -1,3 +1,16 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + package io.swagger.client.api; import io.swagger.client.model.Order; @@ -15,8 +28,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static io.swagger.client.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class StoreApi { @@ -30,6 +43,7 @@ public static StoreApi store(RequestSpecBuilder reqSpec) { return new StoreApi(reqSpec); } + public DeleteOrderOper deleteOrder() { return new DeleteOrderOper(reqSpec); } @@ -46,7 +60,6 @@ public PlaceOrderOper placeOrder() { return new PlaceOrderOper(reqSpec); } - /** * Delete purchase order by ID * For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors @@ -63,11 +76,13 @@ public class DeleteOrderOper { public DeleteOrderOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public DeleteOrderOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -78,9 +93,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(DELETE, REQ_URI)); } - - - /** * @param orderId ID of the order that needs to be deleted (required) */ @@ -89,8 +101,6 @@ public DeleteOrderOper orderIdPath(String orderId) { return this; } - - /** * Customise request specification */ @@ -123,11 +133,13 @@ public class GetInventoryOper { public GetInventoryOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public GetInventoryOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -138,13 +150,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - - - - - - - /** * Customise request specification */ @@ -178,11 +183,13 @@ public class GetOrderByIdOper { public GetOrderByIdOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public GetOrderByIdOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -198,12 +205,9 @@ public T execute(Function handler) { * @return Order */ public Order executeAs(Function handler) { - return execute(handler).as(Order.class, GSON); + return execute(handler).as(Order.class, gson()); } - - - /** * @param orderId ID of pet that needs to be fetched (required) */ @@ -212,8 +216,6 @@ public GetOrderByIdOper orderIdPath(Long orderId) { return this; } - - /** * Customise request specification */ @@ -247,11 +249,15 @@ public class PlaceOrderOper { public PlaceOrderOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public PlaceOrderOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -267,22 +273,17 @@ public T execute(Function handler) { * @return Order */ public Order executeAs(Function handler) { - return execute(handler).as(Order.class, GSON); + return execute(handler).as(Order.class, gson()); } - /** * @param body order placed for purchasing the pet (required) */ public PlaceOrderOper body(Order body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/UserApi.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/UserApi.java index f366596680ce..b393dcefe1e2 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/UserApi.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/api/UserApi.java @@ -1,3 +1,16 @@ +/* + * Swagger Petstore + * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ + * + * OpenAPI spec version: 1.0.0 + * Contact: apiteam@swagger.io + * + * NOTE: This class is auto generated by the swagger code generator program. + * https://github.com/swagger-api/swagger-codegen.git + * Do not edit the class manually. + */ + + package io.swagger.client.api; import io.swagger.client.model.User; @@ -15,8 +28,8 @@ import java.util.function.Function; import java.util.function.Supplier; +import static io.swagger.client.GsonObjectMapper.gson; import static io.restassured.http.Method.*; -import static io.restassured.mapper.ObjectMapperType.GSON; public class UserApi { @@ -30,6 +43,7 @@ public static UserApi user(RequestSpecBuilder reqSpec) { return new UserApi(reqSpec); } + public CreateUserOper createUser() { return new CreateUserOper(reqSpec); } @@ -62,7 +76,6 @@ public UpdateUserOper updateUser() { return new UpdateUserOper(reqSpec); } - /** * Create user * This can only be done by the logged in user. @@ -79,11 +92,15 @@ public class CreateUserOper { public CreateUserOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public CreateUserOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -94,19 +111,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - /** * @param body Created user object (required) */ public CreateUserOper body(User body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -139,11 +151,15 @@ public class CreateUsersWithArrayInputOper { public CreateUsersWithArrayInputOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public CreateUsersWithArrayInputOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -154,19 +170,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - /** * @param body List of user object (required) */ public CreateUsersWithArrayInputOper body(List body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -199,11 +210,15 @@ public class CreateUsersWithListInputOper { public CreateUsersWithListInputOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public CreateUsersWithListInputOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -214,19 +229,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(POST, REQ_URI)); } - /** * @param body List of user object (required) */ public CreateUsersWithListInputOper body(List body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - - - - /** * Customise request specification */ @@ -259,11 +269,13 @@ public class DeleteUserOper { public DeleteUserOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public DeleteUserOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -274,9 +286,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(DELETE, REQ_URI)); } - - - /** * @param username The name that needs to be deleted (required) */ @@ -285,8 +294,6 @@ public DeleteUserOper usernamePath(String username) { return this; } - - /** * Customise request specification */ @@ -320,11 +327,13 @@ public class GetUserByNameOper { public GetUserByNameOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public GetUserByNameOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -340,12 +349,9 @@ public T execute(Function handler) { * @return User */ public User executeAs(Function handler) { - return execute(handler).as(User.class, GSON); + return execute(handler).as(User.class, gson()); } - - - /** * @param username The name that needs to be fetched. Use user1 for testing. (required) */ @@ -354,8 +360,6 @@ public GetUserByNameOper usernamePath(String username) { return this; } - - /** * Customise request specification */ @@ -390,11 +394,13 @@ public class LoginUserOper { public LoginUserOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public LoginUserOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -410,13 +416,9 @@ public T execute(Function handler) { * @return String */ public String executeAs(Function handler) { - return execute(handler).as(String.class, GSON); + return execute(handler).asString(); } - - - - /** * @param username The user name for login (required) */ @@ -424,6 +426,7 @@ public LoginUserOper usernameQuery(String username) { reqSpec.addQueryParam("username", username); return this; } + /** * @param password The password for login in clear text (required) */ @@ -432,7 +435,6 @@ public LoginUserOper passwordQuery(String password) { return this; } - /** * Customise request specification */ @@ -464,11 +466,13 @@ public class LogoutUserOper { public LogoutUserOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public LogoutUserOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -479,12 +483,6 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(GET, REQ_URI)); } - - - - - - /** * Customise request specification */ @@ -518,11 +516,15 @@ public class UpdateUserOper { public UpdateUserOper() { this.reqSpec = new RequestSpecBuilder(); + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } public UpdateUserOper(RequestSpecBuilder reqSpec) { this.reqSpec = reqSpec; + reqSpec.setContentType("application/json"); + reqSpec.setAccept("application/json"); this.respSpec = new ResponseSpecBuilder(); } @@ -533,16 +535,14 @@ public T execute(Function handler) { return handler.apply(RestAssured.given().spec(reqSpec.build()).expect().spec(respSpec.build()).when().request(PUT, REQ_URI)); } - /** * @param body Updated user object (required) */ public UpdateUserOper body(User body) { - reqSpec.setBody(body, GSON); + reqSpec.setBody(body, gson()); return this; } - /** * @param username name that need to be deleted (required) */ @@ -551,8 +551,6 @@ public UpdateUserOper usernamePath(String username) { return this; } - - /** * Customise request specification */ diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/AdditionalPropertiesClass.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/AdditionalPropertiesClass.java index a39ff1ede1a9..a1d984bdcf16 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/AdditionalPropertiesClass.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/AdditionalPropertiesClass.java @@ -45,7 +45,7 @@ public AdditionalPropertiesClass mapProperty(Map mapProperty) { public AdditionalPropertiesClass putMapPropertyItem(String key, String mapPropertyItem) { if (this.mapProperty == null) { - this.mapProperty = new HashMap<>(); + this.mapProperty = new HashMap(); } this.mapProperty.put(key, mapPropertyItem); return this; @@ -71,7 +71,7 @@ public AdditionalPropertiesClass mapOfMapProperty(Map mapOfMapPropertyItem) { if (this.mapOfMapProperty == null) { - this.mapOfMapProperty = new HashMap<>(); + this.mapOfMapProperty = new HashMap>(); } this.mapOfMapProperty.put(key, mapOfMapPropertyItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfArrayOfNumberOnly.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfArrayOfNumberOnly.java index a31bce489273..fdcfdba65227 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfArrayOfNumberOnly.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfArrayOfNumberOnly.java @@ -42,7 +42,7 @@ public ArrayOfArrayOfNumberOnly arrayArrayNumber(List> arrayArr public ArrayOfArrayOfNumberOnly addArrayArrayNumberItem(List arrayArrayNumberItem) { if (this.arrayArrayNumber == null) { - this.arrayArrayNumber = new ArrayList<>(); + this.arrayArrayNumber = new ArrayList>(); } this.arrayArrayNumber.add(arrayArrayNumberItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfNumberOnly.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfNumberOnly.java index 0fc22eb97a0f..26b4422e7b2d 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfNumberOnly.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayOfNumberOnly.java @@ -42,7 +42,7 @@ public ArrayOfNumberOnly arrayNumber(List arrayNumber) { public ArrayOfNumberOnly addArrayNumberItem(BigDecimal arrayNumberItem) { if (this.arrayNumber == null) { - this.arrayNumber = new ArrayList<>(); + this.arrayNumber = new ArrayList(); } this.arrayNumber.add(arrayNumberItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayTest.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayTest.java index a84507241728..df413692f8bf 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayTest.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/ArrayTest.java @@ -48,7 +48,7 @@ public ArrayTest arrayOfString(List arrayOfString) { public ArrayTest addArrayOfStringItem(String arrayOfStringItem) { if (this.arrayOfString == null) { - this.arrayOfString = new ArrayList<>(); + this.arrayOfString = new ArrayList(); } this.arrayOfString.add(arrayOfStringItem); return this; @@ -74,7 +74,7 @@ public ArrayTest arrayArrayOfInteger(List> arrayArrayOfInteger) { public ArrayTest addArrayArrayOfIntegerItem(List arrayArrayOfIntegerItem) { if (this.arrayArrayOfInteger == null) { - this.arrayArrayOfInteger = new ArrayList<>(); + this.arrayArrayOfInteger = new ArrayList>(); } this.arrayArrayOfInteger.add(arrayArrayOfIntegerItem); return this; @@ -100,7 +100,7 @@ public ArrayTest arrayArrayOfModel(List> arrayArrayOfModel) public ArrayTest addArrayArrayOfModelItem(List arrayArrayOfModelItem) { if (this.arrayArrayOfModel == null) { - this.arrayArrayOfModel = new ArrayList<>(); + this.arrayArrayOfModel = new ArrayList>(); } this.arrayArrayOfModel.add(arrayArrayOfModelItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/EnumArrays.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/EnumArrays.java index fe8eb7b4505e..6e69be28d66d 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/EnumArrays.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/EnumArrays.java @@ -156,7 +156,7 @@ public EnumArrays arrayEnum(List arrayEnum) { public EnumArrays addArrayEnumItem(ArrayEnumEnum arrayEnumItem) { if (this.arrayEnum == null) { - this.arrayEnum = new ArrayList<>(); + this.arrayEnum = new ArrayList(); } this.arrayEnum.add(arrayEnumItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MapTest.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MapTest.java index 1062128da197..26cf37c65d8d 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MapTest.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MapTest.java @@ -92,7 +92,7 @@ public MapTest mapMapOfString(Map> mapMapOfString) { public MapTest putMapMapOfStringItem(String key, Map mapMapOfStringItem) { if (this.mapMapOfString == null) { - this.mapMapOfString = new HashMap<>(); + this.mapMapOfString = new HashMap>(); } this.mapMapOfString.put(key, mapMapOfStringItem); return this; @@ -118,7 +118,7 @@ public MapTest mapOfEnumString(Map mapOfEnumString) { public MapTest putMapOfEnumStringItem(String key, InnerEnum mapOfEnumStringItem) { if (this.mapOfEnumString == null) { - this.mapOfEnumString = new HashMap<>(); + this.mapOfEnumString = new HashMap(); } this.mapOfEnumString.put(key, mapOfEnumStringItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MixedPropertiesAndAdditionalPropertiesClass.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MixedPropertiesAndAdditionalPropertiesClass.java index df874b8dad5e..435dc7ec0ee4 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MixedPropertiesAndAdditionalPropertiesClass.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/MixedPropertiesAndAdditionalPropertiesClass.java @@ -87,7 +87,7 @@ public MixedPropertiesAndAdditionalPropertiesClass map(Map map) public MixedPropertiesAndAdditionalPropertiesClass putMapItem(String key, Animal mapItem) { if (this.map == null) { - this.map = new HashMap<>(); + this.map = new HashMap(); } this.map.put(key, mapItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/Pet.java b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/Pet.java index c8b13749e9af..22c29a1bedeb 100644 --- a/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/Pet.java +++ b/samples/client/petstore/java/rest-assured/src/main/java/io/swagger/client/model/Pet.java @@ -43,7 +43,7 @@ public class Pet { private String name = null; @SerializedName("photoUrls") - private List photoUrls = new ArrayList<>(); + private List photoUrls = new ArrayList(); @SerializedName("tags") private List tags = null; @@ -184,7 +184,7 @@ public Pet tags(List tags) { public Pet addTagsItem(Tag tagsItem) { if (this.tags == null) { - this.tags = new ArrayList<>(); + this.tags = new ArrayList(); } this.tags.add(tagsItem); return this; diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/AnotherFakeApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/AnotherFakeApiTest.java index ced4097a7ab1..36648c85a259 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/AnotherFakeApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/AnotherFakeApiTest.java @@ -13,22 +13,18 @@ package io.swagger.client.api; -import io.swagger.client.model.Client; -import io.swagger.client.ApiClient; -import io.swagger.client.api.AnotherFakeApi; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; +import io.swagger.client.model.Client; import org.junit.Before; -import org.junit.Test; import org.junit.Ignore; +import org.junit.Test; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static io.restassured.filter.log.LogDetail.ALL; +import static io.swagger.client.GsonObjectMapper.gson; /** * API tests for AnotherFakeApi @@ -41,8 +37,9 @@ public class AnotherFakeApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .log(ALL) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).anotherFake(); } diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeApiTest.java index 80fa1356df45..cc32aaf83d57 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeApiTest.java @@ -13,26 +13,24 @@ package io.swagger.client.api; -import java.math.BigDecimal; +import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; import io.swagger.client.model.Client; -import org.threeten.bp.LocalDate; -import org.threeten.bp.OffsetDateTime; import io.swagger.client.model.OuterComposite; -import io.swagger.client.ApiClient; -import io.swagger.client.api.FakeApi; -import io.restassured.builder.RequestSpecBuilder; import org.junit.Before; -import org.junit.Test; import org.junit.Ignore; +import org.junit.Test; +import org.threeten.bp.LocalDate; +import org.threeten.bp.OffsetDateTime; -import java.util.ArrayList; -import java.util.HashMap; +import java.math.BigDecimal; import java.util.List; -import java.util.Map; - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static io.restassured.filter.log.LogDetail.ALL; +import static io.swagger.client.GsonObjectMapper.gson; /** * API tests for FakeApi @@ -45,8 +43,9 @@ public class FakeApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .log(ALL) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).fake(); } diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeClassnameTags123ApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeClassnameTags123ApiTest.java index 95c2513af93f..7b8b8fb209d3 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeClassnameTags123ApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/FakeClassnameTags123ApiTest.java @@ -13,22 +13,18 @@ package io.swagger.client.api; -import io.swagger.client.model.Client; -import io.swagger.client.ApiClient; -import io.swagger.client.api.FakeClassnameTags123Api; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; +import io.swagger.client.model.Client; import org.junit.Before; -import org.junit.Test; import org.junit.Ignore; +import org.junit.Test; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static io.restassured.filter.log.LogDetail.ALL; +import static io.swagger.client.GsonObjectMapper.gson; /** * API tests for FakeClassnameTags123Api @@ -41,8 +37,9 @@ public class FakeClassnameTags123ApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .log(ALL) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).fakeClassnameTags123(); } diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/PetApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/PetApiTest.java index 0f7d4af2798d..4a16a3c2d880 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/PetApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/PetApiTest.java @@ -13,29 +13,40 @@ package io.swagger.client.api; -import java.io.File; -import io.swagger.client.model.ModelApiResponse; -import io.swagger.client.model.Pet; -import io.swagger.client.ApiClient; -import io.swagger.client.api.PetApi; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; +import io.swagger.client.model.Category; +import io.swagger.client.model.Pet; import org.junit.Before; import org.junit.Test; -import org.junit.Ignore; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; +import java.util.Arrays; import java.util.List; -import java.util.Map; - - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import java.util.stream.Collectors; + +import static io.restassured.RestAssured.config; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.swagger.client.GsonObjectMapper.gson; +import static io.swagger.client.ResponseSpecBuilders.shouldBeCode; +import static io.swagger.client.ResponseSpecBuilders.validatedWith; +import static io.swagger.client.model.Pet.StatusEnum.AVAILABLE; +import static io.swagger.client.model.Pet.StatusEnum.PENDING; +import static io.swagger.client.model.Pet.StatusEnum.SOLD; +import static org.apache.http.HttpStatus.SC_NOT_FOUND; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.MatcherAssert.assertThat; /** * API tests for PetApi */ -@Ignore public class PetApiTest { private PetApi api; @@ -43,175 +54,67 @@ public class PetApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).pet(); } - /** - * Invalid input - */ @Test - public void shouldSee405AfterAddPet() { - Pet body = null; - api.addPet() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + public void statusListTest() { + Pet pet = getPet(); + api.addPet().body(pet.status(PENDING)).execute(validatedWith(shouldBeCode(SC_OK))); + List statusList = new ArrayList<>(); + statusList.add(PENDING.getValue()); + List petsId = api.findPetsByStatus() + .statusQuery(statusList).executeAs(validatedWith(shouldBeCode(SC_OK))) + .stream().map(Pet::getId).collect(Collectors.toList()); + assertThat(petsId, hasItem(pet.getId())); } - - /** - * Invalid pet value - */ @Test - public void shouldSee400AfterDeletePet() { - Long petId = null; - String apiKey = null; - api.deletePet() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations + public void getPetByIdTest() { + Pet pet = getPet(); + api.addPet().body(pet.status(PENDING)).execute(validatedWith(shouldBeCode(SC_OK))); + Pet fetchedPet = api.getPetById() + .petIdPath(pet.getId()).executeAs(validatedWith(shouldBeCode(SC_OK))); + assertThat(fetchedPet.getId(), equalTo(pet.getId())); } - - /** - * successful operation - */ @Test - public void shouldSee200AfterFindPetsByStatus() { - List status = null; - api.findPetsByStatus() - .statusQuery(status).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Invalid status value - */ - @Test - public void shouldSee400AfterFindPetsByStatus() { - List status = null; - api.findPetsByStatus() - .statusQuery(status).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee200AfterFindPetsByTags() { - List tags = null; - api.findPetsByTags() - .tagsQuery(tags).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Invalid tag value - */ - @Test - public void shouldSee400AfterFindPetsByTags() { - List tags = null; - api.findPetsByTags() - .tagsQuery(tags).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee200AfterGetPetById() { - Long petId = null; - api.getPetById() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Invalid ID supplied - */ - @Test - public void shouldSee400AfterGetPetById() { - Long petId = null; - api.getPetById() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Pet not found - */ - @Test - public void shouldSee404AfterGetPetById() { - Long petId = null; + public void deletePet() { + Pet pet = getPet(); + api.addPet().body(pet.status(PENDING)).execute(validatedWith(shouldBeCode(SC_OK))); + api.deletePet().petIdPath(pet.getId()).execute(validatedWith(shouldBeCode(SC_OK))); api.getPetById() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * Invalid ID supplied - */ - @Test - public void shouldSee400AfterUpdatePet() { - Pet body = null; - api.updatePet() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + .petIdPath(pet.getId()).execute(validatedWith(shouldBeCode(SC_NOT_FOUND))); } - /** - * Pet not found - */ @Test - public void shouldSee404AfterUpdatePet() { - Pet body = null; - api.updatePet() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + public void uploadFileTest() throws IOException { + Pet pet = getPet(); + api.addPet().body(pet).execute(validatedWith(shouldBeCode(SC_OK))); + File file = new File("hello.txt"); + BufferedWriter writer = new BufferedWriter(new FileWriter(file)); + writer.write("Hello world!"); + writer.close(); + api.uploadFile().fileMultiPart(file) + .petIdPath(pet.getId()).execute(validatedWith(shouldBeCode(SC_OK))); } - /** - * Validation exception - */ @Test - public void shouldSee405AfterUpdatePet() { - Pet body = null; - api.updatePet() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + public void updatePetTest() { + Pet pet = getPet(); + api.addPet().body(pet).execute(validatedWith(shouldBeCode(SC_OK))); + api.updatePet().body(pet.status(SOLD)).execute(validatedWith(shouldBeCode(SC_OK))); + Pet.StatusEnum statusEnum = api.getPetById().petIdPath(pet.getId()).executeAs(validatedWith(shouldBeCode(SC_OK))).getStatus(); + assertThat(statusEnum, equalTo(SOLD)); } - /** - * Invalid input - */ - @Test - public void shouldSee405AfterUpdatePetWithForm() { - Long petId = null; - String name = null; - String status = null; - api.updatePetWithForm() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee200AfterUploadFile() { - Long petId = null; - String additionalMetadata = null; - File file = null; - api.uploadFile() - .petIdPath(petId).execute(r -> r.prettyPeek()); - // TODO: test validations + private Pet getPet() { + return new Pet().id(TestUtils.nextId()).name("alex").status(AVAILABLE) + .category(new Category().id(TestUtils.nextId()).name("dog")) + .photoUrls(Arrays.asList("http://foo.bar.com/1")); } } \ No newline at end of file diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/StoreApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/StoreApiTest.java index 1e4e019ac0fe..0d33845f22b3 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/StoreApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/StoreApiTest.java @@ -13,27 +13,30 @@ package io.swagger.client.api; -import io.swagger.client.model.Order; -import io.swagger.client.ApiClient; -import io.swagger.client.api.StoreApi; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; +import io.swagger.client.model.Order; import org.junit.Before; import org.junit.Test; -import org.junit.Ignore; +import org.threeten.bp.OffsetDateTime; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static io.swagger.client.GsonObjectMapper.gson; +import static io.swagger.client.ResponseSpecBuilders.shouldBeCode; +import static io.swagger.client.ResponseSpecBuilders.validatedWith; +import static org.apache.http.HttpStatus.SC_NOT_FOUND; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; /** * API tests for StoreApi */ -@Ignore public class StoreApiTest { private StoreApi api; @@ -41,98 +44,40 @@ public class StoreApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).store(); } - /** - * Invalid ID supplied - */ - @Test - public void shouldSee400AfterDeleteOrder() { - String orderId = null; - api.deleteOrder() - .orderIdPath(orderId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Order not found - */ - @Test - public void shouldSee404AfterDeleteOrder() { - String orderId = null; - api.deleteOrder() - .orderIdPath(orderId).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ @Test - public void shouldSee200AfterGetInventory() { - api.getInventory().execute(r -> r.prettyPeek()); - // TODO: test validations + public void getInventoryTest() { + Map inventory = api.getInventory().execute(validatedWith(shouldBeCode(SC_OK))) + .as(HashMap.class); + assertThat(inventory.keySet().size(), greaterThan(0)); } - - /** - * successful operation - */ @Test - public void shouldSee200AfterGetOrderById() { - Long orderId = null; + public void getOrderByIdTest() { + Order order = getOrder(); + api.placeOrder().body(order).execute(validatedWith(shouldBeCode(SC_OK))); api.getOrderById() - .orderIdPath(orderId).execute(r -> r.prettyPeek()); - // TODO: test validations + .orderIdPath(order.getId()).execute(validatedWith(shouldBeCode(SC_OK))); } - /** - * Invalid ID supplied - */ @Test - public void shouldSee400AfterGetOrderById() { - Long orderId = null; - api.getOrderById() - .orderIdPath(orderId).execute(r -> r.prettyPeek()); - // TODO: test validations - } + public void placeAndDeleteOrderTest() { + Order order = getOrder(); + Long id = api.placeOrder() + .body(order).executeAs(validatedWith(shouldBeCode(SC_OK))).getId(); + api.deleteOrder().orderIdPath(id.toString()) + .execute(validatedWith(shouldBeCode(SC_OK))); + api.getOrderById().orderIdPath(order.getId()).execute(validatedWith(shouldBeCode(SC_NOT_FOUND))); - /** - * Order not found - */ - @Test - public void shouldSee404AfterGetOrderById() { - Long orderId = null; - api.getOrderById() - .orderIdPath(orderId).execute(r -> r.prettyPeek()); - // TODO: test validations } - - /** - * successful operation - */ - @Test - public void shouldSee200AfterPlaceOrder() { - Order body = null; - api.placeOrder() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * Invalid Order - */ - @Test - public void shouldSee400AfterPlaceOrder() { - Order body = null; - api.placeOrder() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + private Order getOrder() { + return new Order().id(TestUtils.nextId()).complete(false).petId(0L) + .quantity(111).status(Order.StatusEnum.DELIVERED).shipDate(OffsetDateTime.now().withNano(123000000)); } } \ No newline at end of file diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/TestUtils.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/TestUtils.java new file mode 100644 index 000000000000..776dd8aa63f1 --- /dev/null +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/TestUtils.java @@ -0,0 +1,18 @@ +package io.swagger.client.api; + +import java.util.Random; +import java.util.concurrent.atomic.AtomicLong; + +public class TestUtils { + private static final AtomicLong atomicId = createAtomicId(); + + public static long nextId() { + return atomicId.getAndIncrement(); + } + + private static AtomicLong createAtomicId() { + int baseId = new Random(System.currentTimeMillis()).nextInt(1000000) + 20000; + return new AtomicLong((long) baseId); + } +} + diff --git a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/UserApiTest.java b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/UserApiTest.java index 03fb434c1ae2..a5520366cf71 100644 --- a/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/UserApiTest.java +++ b/samples/client/petstore/java/rest-assured/src/test/java/io/swagger/client/api/UserApiTest.java @@ -13,27 +13,30 @@ package io.swagger.client.api; -import io.swagger.client.model.User; -import io.swagger.client.ApiClient; -import io.swagger.client.api.UserApi; import io.restassured.builder.RequestSpecBuilder; +import io.restassured.filter.log.ErrorLoggingFilter; +import io.swagger.client.ApiClient; +import io.swagger.client.model.User; import org.junit.Before; import org.junit.Test; -import org.junit.Ignore; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; - -import static io.restassured.http.ContentType.JSON; -import static io.restassured.mapper.ObjectMapperType.GSON; +import static io.restassured.config.ObjectMapperConfig.objectMapperConfig; +import static io.restassured.config.RestAssuredConfig.config; +import static io.swagger.client.GsonObjectMapper.gson; +import static io.swagger.client.ResponseSpecBuilders.shouldBeCode; +import static io.swagger.client.ResponseSpecBuilders.validatedWith; +import static io.swagger.client.api.TestUtils.nextId; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; /** * API tests for UserApi */ -@Ignore public class UserApiTest { private UserApi api; @@ -41,165 +44,72 @@ public class UserApiTest { @Before public void createApi() { api = ApiClient.api(ApiClient.Config.apiConfig().reqSpecSupplier( - () -> new RequestSpecBuilder() - .setContentType(JSON) + () -> new RequestSpecBuilder().setConfig(config().objectMapperConfig(objectMapperConfig().defaultObjectMapper(gson()))) + .addFilter(new ErrorLoggingFilter()) .setBaseUri("http://petstore.swagger.io:80/v2"))).user(); } - /** - * successful operation - */ @Test - public void shouldSee0AfterCreateUser() { - User body = null; - api.createUser() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee0AfterCreateUsersWithArrayInput() { - List body = null; + public void createUsersWithArrayInputTest() { + List body = new ArrayList<>(); + body.add(new User().id(nextId())); + body.add(new User().id(nextId())); + body.add(new User().id(nextId())); api.createUsersWithArrayInput() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + .body(body).execute(validatedWith(shouldBeCode(SC_OK))); } - - /** - * successful operation - */ @Test - public void shouldSee0AfterCreateUsersWithListInput() { - List body = null; + public void createUsersWithListInputTest() { + List body = new ArrayList<>(); + body.add(new User().id(nextId())); + body.add(new User().id(nextId())); + body.add(new User().id(nextId())); api.createUsersWithListInput() - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations + .body(body).execute(validatedWith(shouldBeCode(SC_OK))); } - - /** - * Invalid username supplied - */ @Test - public void shouldSee400AfterDeleteUser() { - String username = null; - api.deleteUser() - .usernamePath(username).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * User not found - */ - @Test - public void shouldSee404AfterDeleteUser() { - String username = null; - api.deleteUser() - .usernamePath(username).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee200AfterGetUserByName() { - String username = null; + public void createUserTest() { + String userName = "userName"; + User newUser = getUser().username(userName); + api.createUser().body(newUser).execute(validatedWith(shouldBeCode(SC_OK))); api.getUserByName() - .usernamePath(username).execute(r -> r.prettyPeek()); - // TODO: test validations + .usernamePath(userName).executeAs(validatedWith(shouldBeCode(SC_OK))); + assertThat(newUser.getUsername(), equalTo(userName)); } - /** - * Invalid username supplied - */ @Test - public void shouldSee400AfterGetUserByName() { - String username = null; - api.getUserByName() - .usernamePath(username).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - /** - * User not found - */ - @Test - public void shouldSee404AfterGetUserByName() { - String username = null; - api.getUserByName() - .usernamePath(username).execute(r -> r.prettyPeek()); - // TODO: test validations - } - - - /** - * successful operation - */ - @Test - public void shouldSee200AfterLoginUser() { - String username = null; - String password = null; - api.loginUser() + public void loginTest() { + String username = "a"; + String password = "b"; + String result = api.loginUser() .usernameQuery(username) - .passwordQuery(password).execute(r -> r.prettyPeek()); - // TODO: test validations - } + .passwordQuery(password).executeAs(validatedWith(shouldBeCode(SC_OK))); + assertThat(result, containsString("logged in user session")); - /** - * Invalid username/password supplied - */ - @Test - public void shouldSee400AfterLoginUser() { - String username = null; - String password = null; - api.loginUser() - .usernameQuery(username) - .passwordQuery(password).execute(r -> r.prettyPeek()); - // TODO: test validations } - - /** - * successful operation - */ @Test - public void shouldSee0AfterLogoutUser() { - api.logoutUser().execute(r -> r.prettyPeek()); - // TODO: test validations + public void logoutTest() { + api.logoutUser().execute(validatedWith(shouldBeCode(SC_OK))); } - - /** - * Invalid user supplied - */ @Test - public void shouldSee400AfterUpdateUser() { - String username = null; - User body = null; + public void updateUserTest() { + String username = "me"; + User newUser = new User().username(username); + api.createUser().body(newUser).execute(validatedWith(shouldBeCode(SC_OK))); api.updateUser() .usernamePath(username) - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations - } + .body(newUser).execute(validatedWith(shouldBeCode(SC_OK))); + String result = api.getUserByName().usernamePath(username).executeAs(validatedWith(shouldBeCode(SC_OK))).getUsername(); + assertThat(result, equalTo(username)); - /** - * User not found - */ - @Test - public void shouldSee404AfterUpdateUser() { - String username = null; - User body = null; - api.updateUser() - .usernamePath(username) - .body(body).execute(r -> r.prettyPeek()); - // TODO: test validations } + private User getUser() { + return new User().id(nextId()).username("Username") + .email("blah@blah.com").firstName("Firstname").lastName("Lastname").userStatus(1); + } } \ No newline at end of file