diff --git a/bin/spring-cloud-feign-petstore.sh b/bin/spring-cloud-feign-petstore.sh index a5605217b07..9f76a40ded5 100755 --- a/bin/spring-cloud-feign-petstore.sh +++ b/bin/spring-cloud-feign-petstore.sh @@ -26,7 +26,7 @@ fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" -ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l spring -c bin/spring-cloud-feign-petstore.json -o samples/client/petstore/spring-cloud -DhideGenerationTimestamp=true" +ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l spring -c bin/spring-cloud-feign-petstore.json -o samples/client/petstore/spring-cloud -DhideGenerationTimestamp=true,responseWrapper=HystrixCommand" echo "Removing files and folders under samples/client/petstore/spring-cloud/src/main" rm -rf samples/client/petstore/spring-cloud/src/main diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java index 43c0a90e4c2..2b0cb059f21 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/SpringCodegen.java @@ -17,6 +17,7 @@ public class SpringCodegen extends AbstractJavaCodegen { public static final String SINGLE_CONTENT_TYPES = "singleContentTypes"; public static final String JAVA_8 = "java8"; public static final String ASYNC = "async"; + public static final String RESPONSE_WRAPPER = "responseWrapper"; public static final String SPRING_MVC_LIBRARY = "spring-mvc"; public static final String SPRING_CLOUD_LIBRARY = "spring-cloud"; @@ -27,6 +28,7 @@ public class SpringCodegen extends AbstractJavaCodegen { protected boolean singleContentTypes = false; protected boolean java8 = false; protected boolean async = false; + protected String responseWrapper = ""; public SpringCodegen() { super(); @@ -51,6 +53,7 @@ public class SpringCodegen extends AbstractJavaCodegen { cliOptions.add(CliOption.newBoolean(SINGLE_CONTENT_TYPES, "Whether to select only one produces/consumes content-type by operation.")); cliOptions.add(CliOption.newBoolean(JAVA_8, "use java8 default interface")); cliOptions.add(CliOption.newBoolean(ASYNC, "use async Callable controllers")); + cliOptions.add(new CliOption(RESPONSE_WRAPPER, "wrap the responses in given type (Future,Callable,CompletableFuture,ListenableFuture,DeferredResult,HystrixCommand,RxObservable,RxSingle or fully qualified type)")); supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration."); supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration."); @@ -117,6 +120,10 @@ public class SpringCodegen extends AbstractJavaCodegen { this.setAsync(Boolean.valueOf(additionalProperties.get(ASYNC).toString())); } + if (additionalProperties.containsKey(RESPONSE_WRAPPER)) { + this.setResponseWrapper((String) additionalProperties.get(RESPONSE_WRAPPER)); + } + supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml")); supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); @@ -173,11 +180,43 @@ public class SpringCodegen extends AbstractJavaCodegen { if (this.java8) { additionalProperties.put("javaVersion", "1.8"); additionalProperties.put("jdk8", "true"); + if (this.async) { + additionalProperties.put(RESPONSE_WRAPPER, "CompletableFuture"); + } typeMapping.put("date", "LocalDate"); typeMapping.put("DateTime", "OffsetDateTime"); importMapping.put("LocalDate", "java.time.LocalDate"); importMapping.put("OffsetDateTime", "java.time.OffsetDateTime"); + } else if (this.async) { + additionalProperties.put(RESPONSE_WRAPPER, "Callable"); } + + // Some well-known Spring or Spring-Cloud response wrappers + switch (this.responseWrapper) { + case "Future": + case "Callable": + case "CompletableFuture": + additionalProperties.put(RESPONSE_WRAPPER, "java.util.concurrent" + this.responseWrapper); + break; + case "ListenableFuture": + additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.util.concurrent.ListenableFuture"); + break; + case "DeferredResult": + additionalProperties.put(RESPONSE_WRAPPER, "org.springframework.web.context.request.DeferredResult"); + break; + case "HystrixCommand": + additionalProperties.put(RESPONSE_WRAPPER, "com.netflix.hystrix.HystrixCommand"); + break; + case "RxObservable": + additionalProperties.put(RESPONSE_WRAPPER, "rx.Observable"); + break; + case "RxSingle": + additionalProperties.put(RESPONSE_WRAPPER, "rx.Single"); + break; + default: + break; + } + } @Override @@ -355,6 +394,8 @@ public class SpringCodegen extends AbstractJavaCodegen { public void setAsync(boolean async) { this.async = async; } + public void setResponseWrapper(String responseWrapper) { this.responseWrapper = responseWrapper; } + @Override public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { super.postProcessModelProperty(model, property); diff --git a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache index 8852a36b9b9..4a97c5d5f33 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpring/api.mustache @@ -43,7 +43,7 @@ public interface {{classname}} { produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}} consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}} method = RequestMethod.{{httpMethod}}) - {{#jdk8}}default {{/jdk8}}{{#async}}{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture<{{/jdk8}}{{/async}}ResponseEntity<{{>returnTypes}}>{{#async}}>{{/async}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, + {{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} { // do some magic! return {{#async}}CompletableFuture.completedFuture({{/async}}new ResponseEntity<{{>returnTypes}}>(HttpStatus.OK){{#async}}){{/async}}; diff --git a/modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache b/modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache index 84e0b0c2f99..f8f7d16f02b 100644 --- a/modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache +++ b/modules/swagger-codegen/src/main/resources/JavaSpring/libraries/spring-cloud/pom.mustache @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.3.6.RELEASE + 1.4.1.RELEASE src/main/java @@ -25,7 +25,7 @@ org.springframework.cloud spring-cloud-starter-parent - Brixton.SR2 + Camden.SR1 pom import diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java index 3cc8bfee148..de7caa0d6c7 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/options/SpringOptionsProvider.java @@ -15,6 +15,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider { public static final String SINGLE_CONTENT_TYPES = "true"; public static final String JAVA_8 = "true"; public static final String ASYNC = "true"; + public static final String RESPONSE_WRAPPER = "Callable"; @Override public String getLanguage() { @@ -32,6 +33,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider { options.put(SpringCodegen.SINGLE_CONTENT_TYPES, SINGLE_CONTENT_TYPES); options.put(SpringCodegen.JAVA_8, JAVA_8); options.put(SpringCodegen.ASYNC, ASYNC); + options.put(SpringCodegen.RESPONSE_WRAPPER, RESPONSE_WRAPPER); return options; } diff --git a/modules/swagger-codegen/src/test/java/io/swagger/codegen/spring/SpringOptionsTest.java b/modules/swagger-codegen/src/test/java/io/swagger/codegen/spring/SpringOptionsTest.java index 8adaa67679d..4084c0a80e9 100644 --- a/modules/swagger-codegen/src/test/java/io/swagger/codegen/spring/SpringOptionsTest.java +++ b/modules/swagger-codegen/src/test/java/io/swagger/codegen/spring/SpringOptionsTest.java @@ -64,6 +64,8 @@ public class SpringOptionsTest extends JavaClientOptionsTest { times = 1; clientCodegen.setAsync(Boolean.valueOf(SpringOptionsProvider.ASYNC)); times = 1; + clientCodegen.setResponseWrapper(SpringOptionsProvider.RESPONSE_WRAPPER); + times = 1; }}; } diff --git a/samples/client/petstore/spring-cloud/pom.xml b/samples/client/petstore/spring-cloud/pom.xml index 159fdae3e61..38b81bfaae5 100644 --- a/samples/client/petstore/spring-cloud/pom.xml +++ b/samples/client/petstore/spring-cloud/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.3.6.RELEASE + 1.4.1.RELEASE src/main/java @@ -25,7 +25,7 @@ org.springframework.cloud spring-cloud-starter-parent - Brixton.SR2 + Camden.SR1 pom import diff --git a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/PetApi.java b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/PetApi.java index 4bba62c661d..812ca04e270 100644 --- a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/PetApi.java +++ b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/PetApi.java @@ -33,7 +33,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.POST) - ResponseEntity addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @RequestBody Pet body); + com.netflix.hystrix.HystrixCommand> addPet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @RequestBody Pet body); @ApiOperation(value = "Deletes a pet", notes = "", response = Void.class, authorizations = { @@ -48,7 +48,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.DELETE) - ResponseEntity deletePet(@ApiParam(value = "Pet id to delete",required=true ) @PathVariable("petId") Long petId, + com.netflix.hystrix.HystrixCommand> deletePet(@ApiParam(value = "Pet id to delete",required=true ) @PathVariable("petId") Long petId, @ApiParam(value = "" ) @RequestHeader(value="api_key", required=false) String apiKey); @@ -65,7 +65,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity> findPetsByStatus(@ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "AVAILABLE, PENDING, SOLD") @RequestParam(value = "status", required = true) List status); + com.netflix.hystrix.HystrixCommand>> findPetsByStatus(@ApiParam(value = "Status values that need to be considered for filter", required = true, allowableValues = "AVAILABLE, PENDING, SOLD") @RequestParam(value = "status", required = true) List status); @ApiOperation(value = "Finds Pets by tags", notes = "Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.", response = Pet.class, responseContainer = "List", authorizations = { @@ -81,7 +81,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity> findPetsByTags(@ApiParam(value = "Tags to filter by", required = true) @RequestParam(value = "tags", required = true) List tags); + com.netflix.hystrix.HystrixCommand>> findPetsByTags(@ApiParam(value = "Tags to filter by", required = true) @RequestParam(value = "tags", required = true) List tags); @ApiOperation(value = "Find pet by ID", notes = "Returns a single pet", response = Pet.class, authorizations = { @@ -95,7 +95,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity getPetById(@ApiParam(value = "ID of pet to return",required=true ) @PathVariable("petId") Long petId); + com.netflix.hystrix.HystrixCommand> getPetById(@ApiParam(value = "ID of pet to return",required=true ) @PathVariable("petId") Long petId); @ApiOperation(value = "Update an existing pet", notes = "", response = Void.class, authorizations = { @@ -112,7 +112,7 @@ public interface PetApi { produces = "application/json", consumes = "application/json", method = RequestMethod.PUT) - ResponseEntity updatePet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @RequestBody Pet body); + com.netflix.hystrix.HystrixCommand> updatePet(@ApiParam(value = "Pet object that needs to be added to the store" ,required=true ) @RequestBody Pet body); @ApiOperation(value = "Updates a pet in the store with form data", notes = "", response = Void.class, authorizations = { @@ -127,7 +127,7 @@ public interface PetApi { produces = "application/json", consumes = "application/x-www-form-urlencoded", method = RequestMethod.POST) - ResponseEntity updatePetWithForm(@ApiParam(value = "ID of pet that needs to be updated",required=true ) @PathVariable("petId") Long petId, + com.netflix.hystrix.HystrixCommand> updatePetWithForm(@ApiParam(value = "ID of pet that needs to be updated",required=true ) @PathVariable("petId") Long petId, @ApiParam(value = "Updated name of the pet" ) @RequestParam(value="name", required=false) String name, @ApiParam(value = "Updated status of the pet" ) @RequestParam(value="status", required=false) String status); @@ -144,7 +144,7 @@ public interface PetApi { produces = "application/json", consumes = "multipart/form-data", method = RequestMethod.POST) - ResponseEntity uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId, + com.netflix.hystrix.HystrixCommand> uploadFile(@ApiParam(value = "ID of pet to update",required=true ) @PathVariable("petId") Long petId, @ApiParam(value = "Additional data to pass to server" ) @RequestParam(value="additionalMetadata", required=false) String additionalMetadata, @ApiParam(value = "file detail") @RequestParam("file") MultipartFile file); diff --git a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/StoreApi.java b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/StoreApi.java index 1eb724e96c9..4442f2ff784 100644 --- a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/StoreApi.java +++ b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/StoreApi.java @@ -28,7 +28,7 @@ public interface StoreApi { produces = "application/json", consumes = "application/json", method = RequestMethod.DELETE) - ResponseEntity deleteOrder(@ApiParam(value = "ID of the order that needs to be deleted",required=true ) @PathVariable("orderId") String orderId); + com.netflix.hystrix.HystrixCommand> deleteOrder(@ApiParam(value = "ID of the order that needs to be deleted",required=true ) @PathVariable("orderId") String orderId); @ApiOperation(value = "Returns pet inventories by status", notes = "Returns a map of status codes to quantities", response = Integer.class, responseContainer = "Map", authorizations = { @@ -40,7 +40,7 @@ public interface StoreApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity> getInventory(); + com.netflix.hystrix.HystrixCommand>> getInventory(); @ApiOperation(value = "Find purchase order by ID", notes = "For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions", response = Order.class, tags={ "store", }) @@ -52,7 +52,7 @@ public interface StoreApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity getOrderById(@ApiParam(value = "ID of pet that needs to be fetched",required=true ) @PathVariable("orderId") Long orderId); + com.netflix.hystrix.HystrixCommand> getOrderById(@ApiParam(value = "ID of pet that needs to be fetched",required=true ) @PathVariable("orderId") Long orderId); @ApiOperation(value = "Place an order for a pet", notes = "", response = Order.class, tags={ "store", }) @@ -63,6 +63,6 @@ public interface StoreApi { produces = "application/json", consumes = "application/json", method = RequestMethod.POST) - ResponseEntity placeOrder(@ApiParam(value = "order placed for purchasing the pet" ,required=true ) @RequestBody Order body); + com.netflix.hystrix.HystrixCommand> placeOrder(@ApiParam(value = "order placed for purchasing the pet" ,required=true ) @RequestBody Order body); } diff --git a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/UserApi.java b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/UserApi.java index 40bdddedbb8..2cfdd979685 100644 --- a/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/UserApi.java +++ b/samples/client/petstore/spring-cloud/src/main/java/io/swagger/api/UserApi.java @@ -27,7 +27,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.POST) - ResponseEntity createUser(@ApiParam(value = "Created user object" ,required=true ) @RequestBody User body); + com.netflix.hystrix.HystrixCommand> createUser(@ApiParam(value = "Created user object" ,required=true ) @RequestBody User body); @ApiOperation(value = "Creates list of users with given input array", notes = "", response = Void.class, tags={ "user", }) @@ -37,7 +37,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.POST) - ResponseEntity createUsersWithArrayInput(@ApiParam(value = "List of user object" ,required=true ) @RequestBody List body); + com.netflix.hystrix.HystrixCommand> createUsersWithArrayInput(@ApiParam(value = "List of user object" ,required=true ) @RequestBody List body); @ApiOperation(value = "Creates list of users with given input array", notes = "", response = Void.class, tags={ "user", }) @@ -47,7 +47,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.POST) - ResponseEntity createUsersWithListInput(@ApiParam(value = "List of user object" ,required=true ) @RequestBody List body); + com.netflix.hystrix.HystrixCommand> createUsersWithListInput(@ApiParam(value = "List of user object" ,required=true ) @RequestBody List body); @ApiOperation(value = "Delete user", notes = "This can only be done by the logged in user.", response = Void.class, tags={ "user", }) @@ -58,7 +58,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.DELETE) - ResponseEntity deleteUser(@ApiParam(value = "The name that needs to be deleted",required=true ) @PathVariable("username") String username); + com.netflix.hystrix.HystrixCommand> deleteUser(@ApiParam(value = "The name that needs to be deleted",required=true ) @PathVariable("username") String username); @ApiOperation(value = "Get user by user name", notes = "", response = User.class, tags={ "user", }) @@ -70,7 +70,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity getUserByName(@ApiParam(value = "The name that needs to be fetched. Use user1 for testing. ",required=true ) @PathVariable("username") String username); + com.netflix.hystrix.HystrixCommand> getUserByName(@ApiParam(value = "The name that needs to be fetched. Use user1 for testing. ",required=true ) @PathVariable("username") String username); @ApiOperation(value = "Logs user into the system", notes = "", response = String.class, tags={ "user", }) @@ -81,7 +81,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity loginUser(@ApiParam(value = "The user name for login", required = true) @RequestParam(value = "username", required = true) String username, + com.netflix.hystrix.HystrixCommand> loginUser(@ApiParam(value = "The user name for login", required = true) @RequestParam(value = "username", required = true) String username, @ApiParam(value = "The password for login in clear text", required = true) @RequestParam(value = "password", required = true) String password); @@ -92,7 +92,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.GET) - ResponseEntity logoutUser(); + com.netflix.hystrix.HystrixCommand> logoutUser(); @ApiOperation(value = "Updated user", notes = "This can only be done by the logged in user.", response = Void.class, tags={ "user", }) @@ -103,7 +103,7 @@ public interface UserApi { produces = "application/json", consumes = "application/json", method = RequestMethod.PUT) - ResponseEntity updateUser(@ApiParam(value = "name that need to be deleted",required=true ) @PathVariable("username") String username, + com.netflix.hystrix.HystrixCommand> updateUser(@ApiParam(value = "name that need to be deleted",required=true ) @PathVariable("username") String username, @ApiParam(value = "Updated user object" ,required=true ) @RequestBody User body); } diff --git a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/Application.java b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/Application.java new file mode 100644 index 00000000000..372b8da31e7 --- /dev/null +++ b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/Application.java @@ -0,0 +1,20 @@ +package io.swagger; + +import feign.Logger; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@EnableFeignClients +public class Application { + public static void main(String[] args) { + new SpringApplicationBuilder(Application.class).run(args); + } + + @Bean + Logger.Level feignLoggerLevel() { + return Logger.Level.FULL; + } +} diff --git a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/PetApiTest.java b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/PetApiTest.java index a08dccc2a60..f369ac786ca 100644 --- a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/PetApiTest.java +++ b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/PetApiTest.java @@ -1,30 +1,28 @@ package io.swagger.api; -import feign.FeignException; +import com.netflix.hystrix.exception.HystrixRuntimeException; +import io.swagger.Application; import io.swagger.TestUtils; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - import io.swagger.model.Category; import io.swagger.model.Pet; import io.swagger.model.Tag; -import org.junit.*; +import org.junit.Ignore; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.cloud.netflix.feign.EnableFeignClients; -import org.springframework.http.ResponseEntity; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mock.web.MockMultipartFile; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = PetApiTest.Application.class) +@SpringBootTest(classes = Application.class) public class PetApiTest { @Autowired @@ -33,9 +31,8 @@ public class PetApiTest { @Test public void testCreateAndGetPet() { Pet pet = createRandomPet(); - client.addPet(pet); - ResponseEntity rp = client.getPetById(pet.getId()); - Pet fetched = rp.getBody(); + client.addPet(pet).execute(); + Pet fetched = client.getPetById(pet.getId()).execute().getBody(); assertNotNull(fetched); assertEquals(pet.getId(), fetched.getId()); assertNotNull(fetched.getCategory()); @@ -47,9 +44,9 @@ public class PetApiTest { Pet pet = createRandomPet(); pet.setName("programmer"); - client.updatePet(pet); + client.updatePet(pet).execute(); - Pet fetched = client.getPetById(pet.getId()).getBody(); + Pet fetched = client.getPetById(pet.getId()).execute().getBody(); assertNotNull(fetched); assertEquals(pet.getId(), fetched.getId()); assertNotNull(fetched.getCategory()); @@ -63,9 +60,9 @@ public class PetApiTest { pet.setName("programmer"); pet.setStatus(Pet.StatusEnum.AVAILABLE); - client.updatePet(pet); + client.updatePet(pet).execute(); - List pets = client.findPetsByStatus(Arrays.asList(new String[]{"available"})).getBody(); + List pets = client.findPetsByStatus(Collections.singletonList("available")).execute().getBody(); assertNotNull(pets); boolean found = false; @@ -92,9 +89,9 @@ public class PetApiTest { tags.add(tag1); pet.setTags(tags); - client.updatePet(pet); + client.updatePet(pet).execute(); - List pets = client.findPetsByTags(Arrays.asList(new String[]{"friendly"})).getBody(); + List pets = client.findPetsByTags(Collections.singletonList("friendly")).execute().getBody(); assertNotNull(pets); boolean found = false; @@ -111,12 +108,12 @@ public class PetApiTest { public void testUpdatePetWithForm() throws Exception { Pet pet = createRandomPet(); pet.setName("frank"); - client.addPet(pet); + client.addPet(pet).execute(); - Pet fetched = client.getPetById(pet.getId()).getBody(); + Pet fetched = client.getPetById(pet.getId()).execute().getBody(); - client.updatePetWithForm(fetched.getId(), "furt", null); - Pet updated = client.getPetById(fetched.getId()).getBody(); + client.updatePetWithForm(fetched.getId(), "furt", null).execute(); + Pet updated = client.getPetById(fetched.getId()).execute().getBody(); assertEquals(updated.getName(), "furt"); } @@ -124,16 +121,16 @@ public class PetApiTest { @Test public void testDeletePet() throws Exception { Pet pet = createRandomPet(); - client.addPet(pet); + client.addPet(pet).execute(); - Pet fetched = client.getPetById(pet.getId()).getBody(); - client.deletePet(fetched.getId(), null); + Pet fetched = client.getPetById(pet.getId()).execute().getBody(); + client.deletePet(fetched.getId(), null).execute(); try { - client.getPetById(fetched.getId()); + client.getPetById(fetched.getId()).execute(); fail("expected an error"); - } catch (FeignException e) { - assertTrue(e.getMessage().startsWith("status 404 ")); + } catch (HystrixRuntimeException e) { + assertTrue(e.getCause().getMessage().startsWith("status 404 ")); } } @@ -141,10 +138,10 @@ public class PetApiTest { @Test public void testUploadFile() throws Exception { Pet pet = createRandomPet(); - client.addPet(pet); + client.addPet(pet).execute(); MockMultipartFile filePart = new MockMultipartFile("file", "bar".getBytes()); - client.uploadFile(pet.getId(), "a test file", filePart); + client.uploadFile(pet.getId(), "a test file", filePart).execute(); } @Test @@ -158,7 +155,7 @@ public class PetApiTest { assertTrue(pet1.hashCode() == pet1.hashCode()); pet2.setName("really-happy"); - pet2.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); + pet2.setPhotoUrls(Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2")); assertFalse(pet1.equals(pet2)); assertFalse(pet2.equals(pet1)); assertFalse(pet1.hashCode() == (pet2.hashCode())); @@ -166,7 +163,7 @@ public class PetApiTest { assertTrue(pet2.hashCode() == pet2.hashCode()); pet1.setName("really-happy"); - pet1.setPhotoUrls(Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"})); + pet1.setPhotoUrls(Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2")); assertTrue(pet1.equals(pet2)); assertTrue(pet2.equals(pet1)); assertTrue(pet1.hashCode() == pet2.hashCode()); @@ -184,24 +181,10 @@ public class PetApiTest { pet.setCategory(category); pet.setStatus(Pet.StatusEnum.AVAILABLE); - List photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"}); + List photos = Arrays.asList("http://foo.bar.com/1", "http://foo.bar.com/2"); pet.setPhotoUrls(photos); return pet; } - - @SpringBootApplication - @EnableFeignClients - protected static class Application { - public static void main(String[] args) { - new SpringApplicationBuilder(Application.class).run(args); - } - } - - - - - - } diff --git a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/StoreApiTest.java b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/StoreApiTest.java index 223289baf15..a5538aaeacd 100644 --- a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/StoreApiTest.java +++ b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/StoreApiTest.java @@ -1,15 +1,13 @@ package io.swagger.api; -import feign.FeignException; +import com.netflix.hystrix.exception.HystrixRuntimeException; +import io.swagger.Application; import io.swagger.TestUtils; import io.swagger.model.Order; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.lang.reflect.Field; @@ -18,7 +16,7 @@ import java.util.Map; import static org.junit.Assert.*; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = StoreApiTest.Application.class) +@SpringBootTest(classes = Application.class) public class StoreApiTest { @Autowired @@ -26,16 +24,16 @@ public class StoreApiTest { @Test public void testGetInventory() { - Map inventory = client.getInventory().getBody(); + Map inventory = client.getInventory().execute().getBody(); assertTrue(inventory.keySet().size() > 0); } @Test public void testPlaceOrder() { Order order = createOrder(); - client.placeOrder(order); + client.placeOrder(order).execute(); - Order fetched = client.getOrderById(order.getId()).getBody(); + Order fetched = client.getOrderById(order.getId()).execute().getBody(); assertEquals(order.getId(), fetched.getId()); assertEquals(order.getPetId(), fetched.getPetId()); assertEquals(order.getQuantity(), fetched.getQuantity()); @@ -45,25 +43,25 @@ public class StoreApiTest { @Test public void testDeleteOrder() { Order order = createOrder(); - client.placeOrder(order); + client.placeOrder(order).execute(); - Order fetched = client.getOrderById(order.getId()).getBody(); + Order fetched = client.getOrderById(order.getId()).execute().getBody(); assertEquals(fetched.getId(), order.getId()); - client.deleteOrder(String.valueOf(order.getId())); + client.deleteOrder(String.valueOf(order.getId())).execute(); try { - client.getOrderById(order.getId()); + client.getOrderById(order.getId()).execute(); fail("expected an error"); - } catch (FeignException e) { - assertTrue(e.getMessage().startsWith("status 404 ")); + } catch (HystrixRuntimeException e) { + assertTrue(e.getCause().getMessage().startsWith("status 404 ")); } } private Order createOrder() { Order order = new Order(); - order.setPetId(new Long(200)); - order.setQuantity(new Integer(13)); + order.setPetId(200L); + order.setQuantity(13); order.setShipDate(org.joda.time.DateTime.now()); order.setStatus(Order.StatusEnum.PLACED); order.setComplete(true); @@ -79,11 +77,4 @@ public class StoreApiTest { return order; } - @SpringBootApplication - @EnableFeignClients - protected static class Application { - public static void main(String[] args) { - new SpringApplicationBuilder(StoreApiTest.Application.class).run(args); - } - } } diff --git a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/UserApiTest.java b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/UserApiTest.java index 4fb9b5e1a12..b0ffb72e7b5 100644 --- a/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/UserApiTest.java +++ b/samples/client/petstore/spring-cloud/src/test/java/io/swagger/api/UserApiTest.java @@ -1,23 +1,21 @@ package io.swagger.api; +import io.swagger.Application; import io.swagger.TestUtils; import io.swagger.model.User; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.cloud.netflix.feign.EnableFeignClients; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Arrays; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = UserApiTest.Application.class) - +@SpringBootTest(classes = Application.class) public class UserApiTest { @Autowired @@ -27,9 +25,9 @@ public class UserApiTest { public void testCreateUser() { User user = createUser(); - client.createUser(user); + client.createUser(user).execute(); - User fetched = client.getUserByName(user.getUsername()).getBody(); + User fetched = client.getUserByName(user.getUsername()).execute().getBody(); assertEquals(user.getId(), fetched.getId()); } @@ -40,9 +38,9 @@ public class UserApiTest { User user2 = createUser(); user2.setUsername("user" + user2.getId()); - client.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})); + client.createUsersWithArrayInput(Arrays.asList(user1, user2)).execute(); - User fetched = client.getUserByName(user1.getUsername()).getBody(); + User fetched = client.getUserByName(user1.getUsername()).execute().getBody(); assertEquals(user1.getId(), fetched.getId()); } @@ -53,24 +51,24 @@ public class UserApiTest { User user2 = createUser(); user2.setUsername("user" + user2.getId()); - client.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})); + client.createUsersWithListInput(Arrays.asList(user1, user2)).execute(); - User fetched = client.getUserByName(user1.getUsername()).getBody(); + User fetched = client.getUserByName(user1.getUsername()).execute().getBody(); assertEquals(user1.getId(), fetched.getId()); } @Test public void testLoginUser() { User user = createUser(); - client.createUser(user); + client.createUser(user).execute(); - String token = client.loginUser(user.getUsername(), user.getPassword()).getBody(); + String token = client.loginUser(user.getUsername(), user.getPassword()).execute().getBody(); assertTrue(token.startsWith("logged in user session:")); } @Test public void logoutUser() { - client.logoutUser(); + client.logoutUser().execute(); } private User createUser() { @@ -87,11 +85,4 @@ public class UserApiTest { return user; } - @SpringBootApplication - @EnableFeignClients - protected static class Application { - public static void main(String[] args) { - new SpringApplicationBuilder(UserApiTest.Application.class).run(args); - } - } } diff --git a/samples/client/petstore/spring-cloud/src/test/resources/application.yml b/samples/client/petstore/spring-cloud/src/test/resources/application.yml index 07a52419295..8526829f0eb 100644 --- a/samples/client/petstore/spring-cloud/src/test/resources/application.yml +++ b/samples/client/petstore/spring-cloud/src/test/resources/application.yml @@ -2,9 +2,7 @@ spring: application: name: petstore-test -feign.hystrix.enabled: false +hystrix.command.default.execution.timeout.enabled: false + +logging.level.io.swagger.api: DEBUG -logging.level.io.swagger.api: - PetApiClient: DEBUG - StoreApiClient: DEBUG - UserApiClient: DEBUG