diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/build.gradle.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/build.gradle.mustache index 5a867bc1828..b0f47011b10 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/build.gradle.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/build.gradle.mustache @@ -71,8 +71,8 @@ if(hasProperty('target') && target == 'android') { apply plugin: 'java' apply plugin: 'maven' - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_1_8 + sourceCompatibility = JavaVersion.VERSION_1_7 + targetCompatibility = JavaVersion.VERSION_1_7 install { repositories.mavenInstaller { diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/pom.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/pom.mustache index 370f68922b9..22e98ae07e1 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/pom.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/pom.mustache @@ -100,8 +100,8 @@ maven-compiler-plugin 2.3.2 - 1.8 - 1.8 + 1.7 + 1.7 diff --git a/samples/client/petstore/java/retrofit2rx/build.gradle b/samples/client/petstore/java/retrofit2rx/build.gradle index 8f0007e5594..299c7b2681e 100644 --- a/samples/client/petstore/java/retrofit2rx/build.gradle +++ b/samples/client/petstore/java/retrofit2rx/build.gradle @@ -15,26 +15,77 @@ repositories { jcenter() } -allprojects { - apply plugin: 'java' - apply plugin: 'maven' - sourceCompatibility = 1.8 - targetCompatibility = 1.8 -} +if(hasProperty('target') && target == 'android') { -install { - repositories.mavenInstaller { - pom.artifactId = 'swagger-petstore-retrofit2' + apply plugin: 'com.android.library' + apply plugin: 'com.github.dcendents.android-maven' + + android { + compileSdkVersion 22 + buildToolsVersion '22.0.0' + defaultConfig { + minSdkVersion 14 + targetSdkVersion 22 + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_7 + targetCompatibility JavaVersion.VERSION_1_7 + } + + // Rename the aar correctly + libraryVariants.all { variant -> + variant.outputs.each { output -> + def outputFile = output.outputFile + if (outputFile != null && outputFile.name.endsWith('.aar')) { + def fileName = "${project.name}-${variant.baseName}-${version}.aar" + output.outputFile = new File(outputFile.parent, fileName) + } + } + } + } + + afterEvaluate { + android.libraryVariants.all { variant -> + def task = project.tasks.create "jar${variant.name.capitalize()}", Jar + task.description = "Create jar artifact for ${variant.name}" + task.dependsOn variant.javaCompile + task.from variant.javaCompile.destinationDir + task.destinationDir = project.file("${project.buildDir}/outputs/jar") + task.archiveName = "${project.name}-${variant.baseName}-${version}.jar" + artifacts.add('archives', task); + } + } + + task sourcesJar(type: Jar) { + from android.sourceSets.main.java.srcDirs + classifier = 'sources' + } + + artifacts { + archives sourcesJar + } + +} else { + + apply plugin: 'java' + apply plugin: 'maven' + + sourceCompatibility = JavaVersion.VERSION_1_7 + targetCompatibility = JavaVersion.VERSION_1_7 + + install { + repositories.mavenInstaller { + pom.artifactId = 'swagger-petstore-retrofit2' + } + } + + task execute(type:JavaExec) { + main = System.getProperty('mainClass') + classpath = sourceSets.main.runtimeClasspath } } -task execute(type:JavaExec) { - main = System.getProperty('mainClass') - classpath = sourceSets.main.runtimeClasspath -} - - ext { okhttp_version = "2.5.0" oltu_version = "1.0.0" @@ -43,6 +94,7 @@ ext { swagger_annotations_version = "1.5.0" junit_version = "4.12" rx_java_version = "1.0.15" + } dependencies { diff --git a/samples/client/petstore/java/retrofit2rx/pom.xml b/samples/client/petstore/java/retrofit2rx/pom.xml index 4792c856d4a..35f17854337 100644 --- a/samples/client/petstore/java/retrofit2rx/pom.xml +++ b/samples/client/petstore/java/retrofit2rx/pom.xml @@ -100,8 +100,8 @@ maven-compiler-plugin 2.3.2 - 1.8 - 1.8 + 1.7 + 1.7 @@ -137,16 +137,18 @@ okhttp ${okhttp-version} - - io.reactivex - rxjava - ${rxjava-version} - - - com.squareup.retrofit - adapter-rxjava - ${retrofit-version} - + + + io.reactivex + rxjava + ${rxjava-version} + + + com.squareup.retrofit + adapter-rxjava + ${retrofit-version} + + diff --git a/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/StringUtil.java b/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/StringUtil.java index ab160d65a19..7283ceffd9c 100644 --- a/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/StringUtil.java +++ b/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/StringUtil.java @@ -1,6 +1,6 @@ package io.swagger.client; -@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-21T16:52:39.577+01:00") +@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-01-22T16:44:03.093+01:00") public class StringUtil { /** * Check if the given array contains the given value (with case-insensitive comparison). diff --git a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/PetApiTest.java b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/PetApiTest.java index 7b010e76051..d8450642f6f 100644 --- a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/PetApiTest.java +++ b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/PetApiTest.java @@ -7,21 +7,14 @@ import io.swagger.client.model.*; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.*; -import rx.Notification; -import rx.Observable; - import com.squareup.okhttp.MediaType; import com.squareup.okhttp.RequestBody; -import rx.Subscriber; -import rx.functions.Action1; -import rx.observers.TestSubscriber; import static org.junit.Assert.*; @@ -36,14 +29,22 @@ public class PetApiTest { @Test public void testCreateAndGetPet() throws Exception { final Pet pet = createRandomPet(); - api.addPet(pet).subscribe(aVoid -> { - api.getPetById(pet.getId()).subscribe(fetched -> { - assertNotNull(fetched); - assertEquals(pet.getId(), fetched.getId()); - assertNotNull(fetched.getCategory()); - assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); - }); + api.addPet(pet).subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Pet fetched) { + assertNotNull(fetched); + assertEquals(pet.getId(), fetched.getId()); + assertNotNull(fetched.getCategory()); + assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); + } + }); + + } }); + } @Test @@ -51,14 +52,22 @@ public class PetApiTest { final Pet pet = createRandomPet(); pet.setName("programmer"); - api.updatePet(pet).subscribe(aVoid -> { - api.getPetById(pet.getId()).subscribe(fetched -> { - assertNotNull(fetched); - assertEquals(pet.getId(), fetched.getId()); - assertNotNull(fetched.getCategory()); - assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); - }); + api.updatePet(pet).subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Pet fetched) { + assertNotNull(fetched); + assertEquals(pet.getId(), fetched.getId()); + assertNotNull(fetched.getCategory()); + assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); + } + }); + + } }); + } @Test @@ -67,21 +76,29 @@ public class PetApiTest { pet.setName("programmer"); pet.setStatus(Pet.StatusEnum.AVAILABLE); - api.updatePet(pet).subscribe(aVoid -> { - api.findPetsByStatus(Arrays.asList(new String[]{"available"})).subscribe(pets -> { - assertNotNull(pets); + api.updatePet(pet).subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.findPetsByStatus(Arrays.asList(new String[]{"available"})).subscribe(new SkeletonSubscriber>() { + @Override + public void onNext(List pets) { + assertNotNull(pets); - boolean found = false; - for (Pet fetched : pets) { - if (fetched.getId().equals(pet.getId())) { - found = true; - break; + boolean found = false; + for (Pet fetched : pets) { + if (fetched.getId().equals(pet.getId())) { + found = true; + break; + } + } + + assertTrue(found); } - } + }); - assertTrue(found); - }); + } }); + } @Test @@ -96,55 +113,79 @@ public class PetApiTest { tags.add(tag1); pet.setTags(tags); - api.updatePet(pet).subscribe(aVoid -> { - api.findPetsByTags(Arrays.asList(new String[]{"friendly"})).subscribe(pets -> { - assertNotNull(pets); + api.updatePet(pet).subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.findPetsByTags(Arrays.asList(new String[]{"friendly"})).subscribe(new SkeletonSubscriber>() { + @Override + public void onNext(List pets) { + assertNotNull(pets); - boolean found = false; - for (Pet fetched : pets) { - if (fetched.getId().equals(pet.getId())) { - found = true; - break; + boolean found = false; + for (Pet fetched : pets) { + if (fetched.getId().equals(pet.getId())) { + found = true; + break; + } + } + assertTrue(found); } - } - assertTrue(found); - }); + }); + + } }); + } @Test public void testUpdatePetWithForm() throws Exception { final Pet pet = createRandomPet(); pet.setName("frank"); - api.addPet(pet).subscribe(aVoid1 -> { - api.getPetById(pet.getId()).subscribe(fetched -> { + api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError()); + api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(final Pet fetched) { api.updatePetWithForm(String.valueOf(fetched.getId()), "furt", null) - .subscribe(aVoid -> { - api.getPetById(fetched.getId()).subscribe(updated -> { - assertEquals(updated.getName(), "furt"); - }); + .subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.getPetById(fetched.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Pet updated) { + assertEquals(updated.getName(), "furt"); + } + }); + + } }); - }); + } }); + + } @Test public void testDeletePet() throws Exception { Pet pet = createRandomPet(); - api.addPet(pet).subscribe(aVoid -> { - }); + api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError()); - api.getPetById(pet.getId()).subscribe(fetched -> { - api.deletePet(fetched.getId(), null).subscribe(aVoid -> { - api.getPetById(fetched.getId()).subscribe( - deletedPet -> { - fail("Should not have found deleted pet."); - }, - exception -> { - // expected, because the pet has been deleted. - }); + api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Pet fetched) { - }); + api.deletePet(fetched.getId(), null).subscribe(SkeletonSubscriber.failTestOnError()); + api.getPetById(fetched.getId()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Pet deletedPet) { + fail("Should not have found deleted pet."); + } + + @Override + public void onError(Throwable e) { + // expected, because the pet has been deleted. + } + }); + } }); } @@ -157,15 +198,14 @@ public class PetApiTest { writer.close(); Pet pet = createRandomPet(); - api.addPet(pet).subscribe(aVoid -> { - RequestBody body = RequestBody.create(MediaType.parse("text/plain"), file); - api.uploadFile(pet.getId(), "a test file", body).subscribe( - aVoid1 -> { - // intentionally left blank. - }, - error -> { - // this also yields a 400 for other tests, so I guess it's okay... - }); + api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError()); + + RequestBody body = RequestBody.create(MediaType.parse("text/plain"), file); + api.uploadFile(pet.getId(), "a test file", body).subscribe(new SkeletonSubscriber() { + @Override + public void onError(Throwable e) { + // this also yields a 400 for other tests, so I guess it's okay... + } }); } diff --git a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/SkeletonSubscriber.java b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/SkeletonSubscriber.java new file mode 100644 index 00000000000..c7f602a0b8b --- /dev/null +++ b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/SkeletonSubscriber.java @@ -0,0 +1,30 @@ +package io.swagger.petstore.test; + +import junit.framework.TestFailure; +import rx.Subscriber; + +/** + * Skeleton subscriber for tests that will fail when onError() is called unexpectedly. + */ +public abstract class SkeletonSubscriber extends Subscriber{ + + public static SkeletonSubscriber failTestOnError() { + return new SkeletonSubscriber() { + }; + } + + @Override + public void onCompleted() { + // space for rent + } + + @Override + public void onNext(T t) { + // space for rent + } + + @Override + public void onError(Throwable e) { + throw new RuntimeException("Subscriber onError() called with unhandled exception!", e); + } +} diff --git a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/StoreApiTest.java b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/StoreApiTest.java index 9dacaced7b6..d9e2c641398 100644 --- a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/StoreApiTest.java +++ b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/StoreApiTest.java @@ -9,6 +9,7 @@ import java.util.Map; import org.junit.*; import retrofit.Response; + import static org.junit.Assert.*; public class StoreApiTest { @@ -21,40 +22,56 @@ public class StoreApiTest { @Test public void testGetInventory() throws Exception { - api.getInventory().subscribe(inventory -> { - assertTrue(inventory.keySet().size() > 0); + api.getInventory().subscribe(new SkeletonSubscriber>() { + @Override + public void onNext(Map inventory) { + assertTrue(inventory.keySet().size() > 0); + } }); + } @Test public void testPlaceOrder() throws Exception { - Order order = createOrder(); - api.placeOrder(order).subscribe(placed -> {}); - api.getOrderById(String.valueOf(order.getId())).subscribe(fetched -> { - assertEquals(order.getId(), fetched.getId()); - assertEquals(order.getPetId(), fetched.getPetId()); - assertEquals(order.getQuantity(), fetched.getQuantity()); + final Order order = createOrder(); + api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError()); + api.getOrderById(String.valueOf(order.getId())).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Order fetched) { + assertEquals(order.getId(), fetched.getId()); + assertEquals(order.getPetId(), fetched.getPetId()); + assertEquals(order.getQuantity(), fetched.getQuantity()); + } }); } @Test public void testDeleteOrder() throws Exception { - Order order = createOrder(); - api.placeOrder(order).subscribe(order1 -> { + final Order order = createOrder(); + api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError()); + + api.getOrderById(String.valueOf(order.getId())).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Order fetched) { + assertEquals(fetched.getId(), order.getId()); + } }); - api.getOrderById(String.valueOf(order.getId())).subscribe(fetched -> { - assertEquals(fetched.getId(), order.getId()); - }); - api.deleteOrder(String.valueOf(order.getId())).subscribe(aVoid -> { - }); - api.getOrderById(String.valueOf(order.getId())).subscribe( - order1 -> { - throw new RuntimeException("Should not have found deleted order."); - }, - error -> {} - ); + api.deleteOrder(String.valueOf(order.getId())).subscribe(SkeletonSubscriber.failTestOnError()); + api.getOrderById(String.valueOf(order.getId())) + .subscribe(new SkeletonSubscriber() { + @Override + public void onNext(Order order) { + throw new RuntimeException("Should not have found deleted order."); + } + + @Override + public void onError(Throwable e) { + // should not find deleted order. + } + } + ); } private Order createOrder() { diff --git a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/UserApiTest.java b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/UserApiTest.java index b72918ab380..59e238b457b 100644 --- a/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/UserApiTest.java +++ b/samples/client/petstore/java/retrofit2rx/src/test/java/io/swagger/petstore/test/UserApiTest.java @@ -8,8 +8,13 @@ import io.swagger.client.model.*; import java.util.Arrays; import org.junit.*; + import static org.junit.Assert.*; +/** + * NOTE: This serves as a sample and test case for the generator, which is why this is java-7 code. + * Much looks much nicer with no anonymous classes. + */ public class UserApiTest { UserApi api = null; @@ -20,40 +25,52 @@ public class UserApiTest { @Test public void testCreateUser() throws Exception { - User user = createUser(); + final User user = createUser(); - api.createUser(user).subscribe(aVoid -> { - api.getUserByName(user.getUsername()).subscribe(fetched -> { - assertEquals(user.getId(), fetched.getId()); - }); + api.createUser(user).subscribe(new SkeletonSubscriber() { + @Override + public void onCompleted() { + api.getUserByName(user.getUsername()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(User fetched) { + assertEquals(user.getId(), fetched.getId()); + } + }); + } }); } @Test public void testCreateUsersWithArray() throws Exception { - User user1 = createUser(); + final User user1 = createUser(); user1.setUsername("abc123"); User user2 = createUser(); user2.setUsername("123abc"); - api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})).subscribe(aVoid -> {}); + api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})).subscribe(SkeletonSubscriber.failTestOnError()); - api.getUserByName(user1.getUsername()).subscribe(fetched -> { - assertEquals(user1.getId(), fetched.getId()); + api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(User fetched) { + assertEquals(user1.getId(), fetched.getId()); + } }); } @Test public void testCreateUsersWithList() throws Exception { - User user1 = createUser(); + final User user1 = createUser(); user1.setUsername("abc123"); User user2 = createUser(); user2.setUsername("123abc"); - api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})).subscribe(aVoid -> {}); + api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})).subscribe(SkeletonSubscriber.failTestOnError()); - api.getUserByName(user1.getUsername()).subscribe(fetched -> { - assertEquals(user1.getId(), fetched.getId()); + api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(User fetched) { + assertEquals(user1.getId(), fetched.getId()); + } }); } @@ -62,8 +79,11 @@ public class UserApiTest { User user = createUser(); api.createUser(user); - api.loginUser(user.getUsername(), user.getPassword()).subscribe(token -> { - assertTrue(token.startsWith("logged in user session:")); + api.loginUser(user.getUsername(), user.getPassword()).subscribe(new SkeletonSubscriber() { + @Override + public void onNext(String token) { + assertTrue(token.startsWith("logged in user session:")); + } }); }