Change templates and test code so that the sample is 1.7 compatible

Alas.
This commit is contained in:
Silvio Heuberger 2016-01-22 16:46:12 +01:00
parent 8bb774f821
commit d60f7d1186
9 changed files with 300 additions and 139 deletions

View File

@ -71,8 +71,8 @@ if(hasProperty('target') && target == 'android') {
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven' apply plugin: 'maven'
sourceCompatibility = JavaVersion.VERSION_1_8 sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_7
install { install {
repositories.mavenInstaller { repositories.mavenInstaller {

View File

@ -100,8 +100,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
<configuration> <configuration>
<source>1.8</source> <source>1.7</source>
<target>1.8</target> <target>1.7</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -15,26 +15,77 @@ repositories {
jcenter() jcenter()
} }
allprojects {
apply plugin: 'java'
apply plugin: 'maven'
sourceCompatibility = 1.8 if(hasProperty('target') && target == 'android') {
targetCompatibility = 1.8
}
install { apply plugin: 'com.android.library'
repositories.mavenInstaller { apply plugin: 'com.github.dcendents.android-maven'
pom.artifactId = 'swagger-petstore-retrofit2'
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 { ext {
okhttp_version = "2.5.0" okhttp_version = "2.5.0"
oltu_version = "1.0.0" oltu_version = "1.0.0"
@ -43,6 +94,7 @@ ext {
swagger_annotations_version = "1.5.0" swagger_annotations_version = "1.5.0"
junit_version = "4.12" junit_version = "4.12"
rx_java_version = "1.0.15" rx_java_version = "1.0.15"
} }
dependencies { dependencies {

View File

@ -100,8 +100,8 @@
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version> <version>2.3.2</version>
<configuration> <configuration>
<source>1.8</source> <source>1.7</source>
<target>1.8</target> <target>1.7</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
@ -137,16 +137,18 @@
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>${okhttp-version}</version> <version>${okhttp-version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.reactivex</groupId> <dependency>
<artifactId>rxjava</artifactId> <groupId>io.reactivex</groupId>
<version>${rxjava-version}</version> <artifactId>rxjava</artifactId>
</dependency> <version>${rxjava-version}</version>
<dependency> </dependency>
<groupId>com.squareup.retrofit</groupId> <dependency>
<artifactId>adapter-rxjava</artifactId> <groupId>com.squareup.retrofit</groupId>
<version>${retrofit-version}</version> <artifactId>adapter-rxjava</artifactId>
</dependency> <version>${retrofit-version}</version>
</dependency>
<!-- test dependencies --> <!-- test dependencies -->
<dependency> <dependency>

View File

@ -1,6 +1,6 @@
package io.swagger.client; 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 { public class StringUtil {
/** /**
* Check if the given array contains the given value (with case-insensitive comparison). * Check if the given array contains the given value (with case-insensitive comparison).

View File

@ -7,21 +7,14 @@ import io.swagger.client.model.*;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.*; import org.junit.*;
import rx.Notification;
import rx.Observable;
import com.squareup.okhttp.MediaType; import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody; import com.squareup.okhttp.RequestBody;
import rx.Subscriber;
import rx.functions.Action1;
import rx.observers.TestSubscriber;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -36,14 +29,22 @@ public class PetApiTest {
@Test @Test
public void testCreateAndGetPet() throws Exception { public void testCreateAndGetPet() throws Exception {
final Pet pet = createRandomPet(); final Pet pet = createRandomPet();
api.addPet(pet).subscribe(aVoid -> { api.addPet(pet).subscribe(new SkeletonSubscriber<Void>() {
api.getPetById(pet.getId()).subscribe(fetched -> { @Override
assertNotNull(fetched); public void onCompleted() {
assertEquals(pet.getId(), fetched.getId()); api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
assertNotNull(fetched.getCategory()); @Override
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); public void onNext(Pet fetched) {
}); assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
});
}
}); });
} }
@Test @Test
@ -51,14 +52,22 @@ public class PetApiTest {
final Pet pet = createRandomPet(); final Pet pet = createRandomPet();
pet.setName("programmer"); pet.setName("programmer");
api.updatePet(pet).subscribe(aVoid -> { api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
api.getPetById(pet.getId()).subscribe(fetched -> { @Override
assertNotNull(fetched); public void onCompleted() {
assertEquals(pet.getId(), fetched.getId()); api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
assertNotNull(fetched.getCategory()); @Override
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName()); public void onNext(Pet fetched) {
}); assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
});
}
}); });
} }
@Test @Test
@ -67,21 +76,29 @@ public class PetApiTest {
pet.setName("programmer"); pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.AVAILABLE); pet.setStatus(Pet.StatusEnum.AVAILABLE);
api.updatePet(pet).subscribe(aVoid -> { api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
api.findPetsByStatus(Arrays.asList(new String[]{"available"})).subscribe(pets -> { @Override
assertNotNull(pets); public void onCompleted() {
api.findPetsByStatus(Arrays.asList(new String[]{"available"})).subscribe(new SkeletonSubscriber<List<Pet>>() {
@Override
public void onNext(List<Pet> pets) {
assertNotNull(pets);
boolean found = false; boolean found = false;
for (Pet fetched : pets) { for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) { if (fetched.getId().equals(pet.getId())) {
found = true; found = true;
break; break;
}
}
assertTrue(found);
} }
} });
assertTrue(found); }
});
}); });
} }
@Test @Test
@ -96,55 +113,79 @@ public class PetApiTest {
tags.add(tag1); tags.add(tag1);
pet.setTags(tags); pet.setTags(tags);
api.updatePet(pet).subscribe(aVoid -> { api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
api.findPetsByTags(Arrays.asList(new String[]{"friendly"})).subscribe(pets -> { @Override
assertNotNull(pets); public void onCompleted() {
api.findPetsByTags(Arrays.asList(new String[]{"friendly"})).subscribe(new SkeletonSubscriber<List<Pet>>() {
@Override
public void onNext(List<Pet> pets) {
assertNotNull(pets);
boolean found = false; boolean found = false;
for (Pet fetched : pets) { for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) { if (fetched.getId().equals(pet.getId())) {
found = true; found = true;
break; break;
}
}
assertTrue(found);
} }
} });
assertTrue(found);
}); }
}); });
} }
@Test @Test
public void testUpdatePetWithForm() throws Exception { public void testUpdatePetWithForm() throws Exception {
final Pet pet = createRandomPet(); final Pet pet = createRandomPet();
pet.setName("frank"); pet.setName("frank");
api.addPet(pet).subscribe(aVoid1 -> { api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError());
api.getPetById(pet.getId()).subscribe(fetched -> { api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(final Pet fetched) {
api.updatePetWithForm(String.valueOf(fetched.getId()), "furt", null) api.updatePetWithForm(String.valueOf(fetched.getId()), "furt", null)
.subscribe(aVoid -> { .subscribe(new SkeletonSubscriber<Void>() {
api.getPetById(fetched.getId()).subscribe(updated -> { @Override
assertEquals(updated.getName(), "furt"); public void onCompleted() {
}); api.getPetById(fetched.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(Pet updated) {
assertEquals(updated.getName(), "furt");
}
});
}
}); });
}); }
}); });
} }
@Test @Test
public void testDeletePet() throws Exception { public void testDeletePet() throws Exception {
Pet pet = createRandomPet(); Pet pet = createRandomPet();
api.addPet(pet).subscribe(aVoid -> { api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError());
});
api.getPetById(pet.getId()).subscribe(fetched -> { api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
api.deletePet(fetched.getId(), null).subscribe(aVoid -> { @Override
api.getPetById(fetched.getId()).subscribe( public void onNext(Pet fetched) {
deletedPet -> {
fail("Should not have found deleted pet.");
},
exception -> {
// expected, because the pet has been deleted.
});
}); api.deletePet(fetched.getId(), null).subscribe(SkeletonSubscriber.failTestOnError());
api.getPetById(fetched.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@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(); writer.close();
Pet pet = createRandomPet(); Pet pet = createRandomPet();
api.addPet(pet).subscribe(aVoid -> { api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError());
RequestBody body = RequestBody.create(MediaType.parse("text/plain"), file);
api.uploadFile(pet.getId(), "a test file", body).subscribe( RequestBody body = RequestBody.create(MediaType.parse("text/plain"), file);
aVoid1 -> { api.uploadFile(pet.getId(), "a test file", body).subscribe(new SkeletonSubscriber<Void>() {
// intentionally left blank. @Override
}, public void onError(Throwable e) {
error -> { // this also yields a 400 for other tests, so I guess it's okay...
// this also yields a 400 for other tests, so I guess it's okay... }
});
}); });
} }

View File

@ -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<T> extends Subscriber<T>{
public static <T> SkeletonSubscriber<T> failTestOnError() {
return new SkeletonSubscriber<T>() {
};
}
@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);
}
}

View File

@ -9,6 +9,7 @@ import java.util.Map;
import org.junit.*; import org.junit.*;
import retrofit.Response; import retrofit.Response;
import static org.junit.Assert.*; import static org.junit.Assert.*;
public class StoreApiTest { public class StoreApiTest {
@ -21,40 +22,56 @@ public class StoreApiTest {
@Test @Test
public void testGetInventory() throws Exception { public void testGetInventory() throws Exception {
api.getInventory().subscribe(inventory -> { api.getInventory().subscribe(new SkeletonSubscriber<Map<String, Integer>>() {
assertTrue(inventory.keySet().size() > 0); @Override
public void onNext(Map<String, Integer> inventory) {
assertTrue(inventory.keySet().size() > 0);
}
}); });
} }
@Test @Test
public void testPlaceOrder() throws Exception { public void testPlaceOrder() throws Exception {
Order order = createOrder(); final Order order = createOrder();
api.placeOrder(order).subscribe(placed -> {}); api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError());
api.getOrderById(String.valueOf(order.getId())).subscribe(fetched -> { api.getOrderById(String.valueOf(order.getId())).subscribe(new SkeletonSubscriber<Order>() {
assertEquals(order.getId(), fetched.getId()); @Override
assertEquals(order.getPetId(), fetched.getPetId()); public void onNext(Order fetched) {
assertEquals(order.getQuantity(), fetched.getQuantity()); assertEquals(order.getId(), fetched.getId());
assertEquals(order.getPetId(), fetched.getPetId());
assertEquals(order.getQuantity(), fetched.getQuantity());
}
}); });
} }
@Test @Test
public void testDeleteOrder() throws Exception { public void testDeleteOrder() throws Exception {
Order order = createOrder(); final Order order = createOrder();
api.placeOrder(order).subscribe(order1 -> { api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError());
api.getOrderById(String.valueOf(order.getId())).subscribe(new SkeletonSubscriber<Order>() {
@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.deleteOrder(String.valueOf(order.getId())).subscribe(SkeletonSubscriber.failTestOnError());
}); api.getOrderById(String.valueOf(order.getId()))
api.getOrderById(String.valueOf(order.getId())).subscribe( .subscribe(new SkeletonSubscriber<Order>() {
order1 -> { @Override
throw new RuntimeException("Should not have found deleted order."); public void onNext(Order order) {
}, throw new RuntimeException("Should not have found deleted order.");
error -> {} }
);
@Override
public void onError(Throwable e) {
// should not find deleted order.
}
}
);
} }
private Order createOrder() { private Order createOrder() {

View File

@ -8,8 +8,13 @@ import io.swagger.client.model.*;
import java.util.Arrays; import java.util.Arrays;
import org.junit.*; import org.junit.*;
import static org.junit.Assert.*; 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 { public class UserApiTest {
UserApi api = null; UserApi api = null;
@ -20,40 +25,52 @@ public class UserApiTest {
@Test @Test
public void testCreateUser() throws Exception { public void testCreateUser() throws Exception {
User user = createUser(); final User user = createUser();
api.createUser(user).subscribe(aVoid -> { api.createUser(user).subscribe(new SkeletonSubscriber<Void>() {
api.getUserByName(user.getUsername()).subscribe(fetched -> { @Override
assertEquals(user.getId(), fetched.getId()); public void onCompleted() {
}); api.getUserByName(user.getUsername()).subscribe(new SkeletonSubscriber<User>() {
@Override
public void onNext(User fetched) {
assertEquals(user.getId(), fetched.getId());
}
});
}
}); });
} }
@Test @Test
public void testCreateUsersWithArray() throws Exception { public void testCreateUsersWithArray() throws Exception {
User user1 = createUser(); final User user1 = createUser();
user1.setUsername("abc123"); user1.setUsername("abc123");
User user2 = createUser(); User user2 = createUser();
user2.setUsername("123abc"); 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 -> { api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber<User>() {
assertEquals(user1.getId(), fetched.getId()); @Override
public void onNext(User fetched) {
assertEquals(user1.getId(), fetched.getId());
}
}); });
} }
@Test @Test
public void testCreateUsersWithList() throws Exception { public void testCreateUsersWithList() throws Exception {
User user1 = createUser(); final User user1 = createUser();
user1.setUsername("abc123"); user1.setUsername("abc123");
User user2 = createUser(); User user2 = createUser();
user2.setUsername("123abc"); 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 -> { api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber<User>() {
assertEquals(user1.getId(), fetched.getId()); @Override
public void onNext(User fetched) {
assertEquals(user1.getId(), fetched.getId());
}
}); });
} }
@ -62,8 +79,11 @@ public class UserApiTest {
User user = createUser(); User user = createUser();
api.createUser(user); api.createUser(user);
api.loginUser(user.getUsername(), user.getPassword()).subscribe(token -> { api.loginUser(user.getUsername(), user.getPassword()).subscribe(new SkeletonSubscriber<String>() {
assertTrue(token.startsWith("logged in user session:")); @Override
public void onNext(String token) {
assertTrue(token.startsWith("logged in user session:"));
}
}); });
} }