Merge pull request #2479 from ergon/feature/update-mainline-retrofit

Update retrofit2 and retrofit2rx to use retrofit 2.0.1
This commit is contained in:
wing328 2016-04-28 11:44:55 +08:00
commit f1ba6f5598
24 changed files with 152 additions and 904 deletions

View File

@ -110,7 +110,7 @@ public class JavaClientCodegen extends DefaultCodegen implements CodegenConfig {
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1");
supportedLibraries.put(RETROFIT_1, "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.0-beta4). Enable the RxJava adapter using '-DuseRxJava=true'.");
supportedLibraries.put(RETROFIT_2, "HTTP client: OkHttp 2.5.0. JSON processing: Gson 2.4 (Retrofit 2.0.1). Enable the RxJava adapter using '-DuseRxJava=true'. (RxJava 1.1.2)");
CliOption library = new CliOption(CodegenConstants.LIBRARY, "library template (sub-template) to use");
library.setDefault(DEFAULT_LIBRARY);

View File

@ -95,8 +95,7 @@ if(hasProperty('target') && target == 'android') {
ext {
oltu_version = "1.0.1"
retrofit_version = "2.0.0-beta4"
gson_version = "2.6.2"
retrofit_version = "2.0.2"
swagger_annotations_version = "1.5.8"
junit_version = "4.12"
{{#useRxJava}}

View File

@ -112,6 +112,11 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
@ -122,11 +127,6 @@
<artifactId>converter-scalars</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
@ -153,8 +153,9 @@
</dependencies>
<properties>
<swagger-core-version>1.5.8</swagger-core-version>
<retrofit-version>2.0.0-beta4</retrofit-version>{{#useRxJava}}
<rxjava-version>1.1.3</rxjava-version>{{/useRxJava}}
<retrofit-version>2.0.2</retrofit-version>
{{#useRxJava}}<rxjava-version>1.1.3</rxjava-version>{{/useRxJava}}
<okhttp-version>3.2.0</okhttp-version>
<oltu-version>1.0.1</oltu-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.12</junit-version>

View File

@ -95,8 +95,7 @@ if(hasProperty('target') && target == 'android') {
ext {
oltu_version = "1.0.1"
retrofit_version = "2.0.0-beta4"
gson_version = "2.6.2"
retrofit_version = "2.0.2"
swagger_annotations_version = "1.5.8"
junit_version = "4.12"

View File

@ -1 +0,0 @@
Hello world!

View File

@ -100,8 +100,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>
1.7</source>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
@ -113,6 +112,11 @@
<artifactId>swagger-annotations</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>retrofit</artifactId>
@ -123,11 +127,6 @@
<artifactId>converter-scalars</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
@ -144,7 +143,9 @@
</dependencies>
<properties>
<swagger-core-version>1.5.8</swagger-core-version>
<retrofit-version>2.0.0-beta4</retrofit-version>
<retrofit-version>2.0.2</retrofit-version>
<okhttp-version>3.2.0</okhttp-version>
<oltu-version>1.0.1</oltu-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.12</junit-version>

View File

@ -1,6 +1,6 @@
package io.swagger.client;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-22T23:08:50.551+08:00")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-27T10:03:24.454+02:00")
public class StringUtil {
/**
* Check if the given array contains the given value (with case-insensitive comparison).

View File

@ -0,0 +1,43 @@
package io.swagger.client.api;
import io.swagger.client.CollectionFormats.*;
import retrofit2.Call;
import retrofit2.http.*;
import okhttp3.RequestBody;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface FakeApi {
/**
* Fake endpoint for testing various parameters
* Fake endpoint for testing various parameters
* @param number None (required)
* @param _double None (required)
* @param string None (required)
* @param _byte None (required)
* @param integer None (optional)
* @param int32 None (optional)
* @param int64 None (optional)
* @param _float None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional)
* @param password None (optional)
* @return Call<Void>
*/
@FormUrlEncoded
@POST("fake")
Call<Void> testEndpointParameters(
@Field("number") String number, @Field("double") Double _double, @Field("string") String string, @Field("byte") byte[] _byte, @Field("integer") Integer integer, @Field("int32") Integer int32, @Field("int64") Long int64, @Field("float") Float _float, @Field("binary") byte[] binary, @Field("date") Date date, @Field("dateTime") Date dateTime, @Field("password") String password
);
}

View File

@ -9,8 +9,8 @@ import retrofit2.http.*;
import okhttp3.RequestBody;
import io.swagger.client.model.Pet;
import java.io.File;
import io.swagger.client.model.ModelApiResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -51,6 +51,8 @@ public class FormatTest {
private String password = null;
/**
* minimum: 10.0
* maximum: 100.0
**/
@ApiModelProperty(value = "")
public Integer getInteger() {
@ -61,6 +63,8 @@ public class FormatTest {
}
/**
* minimum: 20.0
* maximum: 200.0
**/
@ApiModelProperty(value = "")
public Integer getInt32() {
@ -81,6 +85,8 @@ public class FormatTest {
}
/**
* minimum: 32.1
* maximum: 543.2
**/
@ApiModelProperty(required = true, value = "")
public BigDecimal getNumber() {
@ -91,6 +97,8 @@ public class FormatTest {
}
/**
* minimum: 54.3
* maximum: 987.6
**/
@ApiModelProperty(value = "")
public Float getFloat() {
@ -101,6 +109,8 @@ public class FormatTest {
}
/**
* minimum: 67.8
* maximum: 123.4
**/
@ApiModelProperty(value = "")
public Double getDouble() {
@ -122,7 +132,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public byte[] getByte() {
return _byte;
}
@ -142,7 +152,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public Date getDate() {
return date;
}
@ -162,7 +172,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public String getPassword() {
return password;
}

View File

@ -1,17 +0,0 @@
package io.swagger;
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);
}
}

View File

@ -1,190 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.CollectionFormats.*;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.*;
import retrofit2.Response;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import static org.junit.Assert.*;
public class PetApiTest {
PetApi api = null;
@Before
public void setup() {
api = new ApiClient().createService(PetApi.class);
}
@Test
public void testCreateAndGetPet() throws Exception {
Pet pet = createRandomPet();
Response<Void> rp2 = api.addPet(pet).execute();
Response<Pet> rp = api.getPetById(pet.getId()).execute();
Pet fetched = rp.body();
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
@Test
public void testUpdatePet() throws Exception {
Pet pet = createRandomPet();
pet.setName("programmer");
api.updatePet(pet).execute();
Pet fetched = api.getPetById(pet.getId()).execute().body();
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
@Test
public void testFindPetsByStatus() throws Exception {
Pet pet = createRandomPet();
pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
api.updatePet(pet).execute();
List<Pet> pets = api.findPetsByStatus(new CSVParams("available")).execute().body();
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
@Test
public void testFindPetsByTags() throws Exception {
Pet pet = createRandomPet();
pet.setName("monster");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<Tag> tags = new ArrayList<Tag>();
Tag tag1 = new Tag();
tag1.setName("friendly");
tags.add(tag1);
pet.setTags(tags);
api.updatePet(pet).execute();
List<Pet> pets = api.findPetsByTags(new CSVParams("friendly")).execute().body();
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
@Test
public void testUpdatePetWithForm() throws Exception {
Pet pet = createRandomPet();
pet.setName("frank");
api.addPet(pet).execute();
Pet fetched = api.getPetById(pet.getId()).execute().body();
api.updatePetWithForm(fetched.getId(), "furt", null).execute();
Pet updated = api.getPetById(fetched.getId()).execute().body();
assertEquals(updated.getName(), "furt");
}
@Test
public void testDeletePet() throws Exception {
Pet pet = createRandomPet();
api.addPet(pet).execute();
Pet fetched = api.getPetById(pet.getId()).execute().body();
api.deletePet(fetched.getId(), null).execute();
assertFalse(api.getPetById(fetched.getId()).execute().isSuccess());
}
@Test
public void testUploadFile() throws Exception {
Pet pet = createRandomPet();
api.addPet(pet).execute();
File file = new File("hello.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("Hello world!");
writer.close();
api.uploadFile(pet.getId(), null, RequestBody.create(MediaType.parse("text/plain"), file)).execute();
}
@Test
public void testEqualsAndHashCode() {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
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"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
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"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(TestUtils.nextId());
pet.setName("gorilla");
Category category = new Category();
category.setName("really-happy");
pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"});
pet.setPhotoUrls(photos);
return pet;
}
}

View File

@ -1,74 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
import java.lang.reflect.Field;
import java.util.Map;
import org.junit.*;
import retrofit2.Response;
import static org.junit.Assert.*;
public class StoreApiTest {
StoreApi api = null;
@Before
public void setup() {
api = new ApiClient().createService(StoreApi.class);
}
@Test
public void testGetInventory() throws Exception {
Map<String, Integer> inventory = api.getInventory().execute().body();
assertTrue(inventory.keySet().size() > 0);
}
@Test
public void testPlaceOrder() throws Exception {
Order order = createOrder();
api.placeOrder(order).execute();
Order fetched = api.getOrderById(order.getId()).execute().body();
assertEquals(order.getId(), fetched.getId());
assertEquals(order.getPetId(), fetched.getPetId());
assertEquals(order.getQuantity(), fetched.getQuantity());
}
@Test
public void testDeleteOrder() throws Exception {
Order order = createOrder();
Response<Order> aa = api.placeOrder(order).execute();
Order fetched = api.getOrderById(order.getId()).execute().body();
assertEquals(fetched.getId(), order.getId());
api.deleteOrder(String.valueOf(order.getId())).execute();
api.getOrderById(order.getId()).execute();
//also in retrofit 1 should return an error but don't, check server api impl.
}
private Order createOrder() {
Order order = new Order();
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());
order.setStatus(Order.StatusEnum.PLACED);
order.setComplete(true);
try {
Field idField = Order.class.getDeclaredField("id");
idField.setAccessible(true);
idField.set(order, TestUtils.nextId());
} catch (Exception e) {
throw new RuntimeException(e);
}
return order;
}
}

View File

@ -1,86 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.TestUtils;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
import java.util.Arrays;
import org.junit.*;
import static org.junit.Assert.*;
public class UserApiTest {
UserApi api = null;
@Before
public void setup() {
api = new ApiClient().createService(UserApi.class);
}
@Test
public void testCreateUser() throws Exception {
User user = createUser();
api.createUser(user).execute();
User fetched = api.getUserByName(user.getUsername()).execute().body();
assertEquals(user.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithArray() throws Exception {
User user1 = createUser();
user1.setUsername("user" + user1.getId());
User user2 = createUser();
user2.setUsername("user" + user2.getId());
api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})).execute();
User fetched = api.getUserByName(user1.getUsername()).execute().body();
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testCreateUsersWithList() throws Exception {
User user1 = createUser();
user1.setUsername("user" + user1.getId());
User user2 = createUser();
user2.setUsername("user" + user2.getId());
api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})).execute();
User fetched = api.getUserByName(user1.getUsername()).execute().body();
assertEquals(user1.getId(), fetched.getId());
}
@Test
public void testLoginUser() throws Exception {
User user = createUser();
api.createUser(user).execute();
String token = api.loginUser(user.getUsername(), user.getPassword()).execute().body();
assertTrue(token.startsWith("logged in user session:"));
}
@Test
public void logoutUser() throws Exception {
api.logoutUser().execute();
}
private User createUser() {
User user = new User();
user.setId(TestUtils.nextId());
user.setUsername("fred");
user.setFirstName("Fred");
user.setLastName("Meyer");
user.setEmail("fred@fredmeyer.com");
user.setPassword("xxXXxx");
user.setPhone("408-867-5309");
user.setUserStatus(123);
return user;
}
}

View File

@ -94,12 +94,11 @@ if(hasProperty('target') && target == 'android') {
}
ext {
oltu_version = "1.0.0"
retrofit_version = "2.0.0-beta4"
gson_version = "2.4"
swagger_annotations_version = "1.5.0"
oltu_version = "1.0.1"
retrofit_version = "2.0.2"
swagger_annotations_version = "1.5.8"
junit_version = "4.12"
rx_java_version = "1.0.16"
rx_java_version = "1.1.3"
}

View File

@ -100,8 +100,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>
1.7</source>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
@ -111,7 +110,12 @@
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations-version}</version>
<version>${swagger-core-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
@ -123,11 +127,6 @@
<artifactId>converter-scalars</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>com.squareup.retrofit2</groupId>
<artifactId>converter-gson</artifactId>
<version>${retrofit-version}</version>
</dependency>
<dependency>
<groupId>org.apache.oltu.oauth2</groupId>
<artifactId>org.apache.oltu.oauth2.client</artifactId>
@ -153,10 +152,11 @@
</dependency>
</dependencies>
<properties>
<swagger-annotations-version>1.5.0</swagger-annotations-version>
<retrofit-version>2.0.0-beta4</retrofit-version>
<rxjava-version>1.0.16</rxjava-version>
<oltu-version>1.0.0</oltu-version>
<swagger-core-version>1.5.8</swagger-core-version>
<retrofit-version>2.0.2</retrofit-version>
<rxjava-version>1.1.3</rxjava-version>
<okhttp-version>3.2.0</okhttp-version>
<oltu-version>1.0.1</oltu-version>
<maven-plugin-version>1.0.0</maven-plugin-version>
<junit-version>4.12</junit-version>
</properties>

View File

@ -1,6 +1,6 @@
package io.swagger.client;
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-22T23:10:58.658+08:00")
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2016-04-27T10:03:29.641+02:00")
public class StringUtil {
/**
* Check if the given array contains the given value (with case-insensitive comparison).

View File

@ -0,0 +1,43 @@
package io.swagger.client.api;
import io.swagger.client.CollectionFormats.*;
import rx.Observable;
import retrofit2.http.*;
import okhttp3.RequestBody;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public interface FakeApi {
/**
* Fake endpoint for testing various parameters
* Fake endpoint for testing various parameters
* @param number None (required)
* @param _double None (required)
* @param string None (required)
* @param _byte None (required)
* @param integer None (optional)
* @param int32 None (optional)
* @param int64 None (optional)
* @param _float None (optional)
* @param binary None (optional)
* @param date None (optional)
* @param dateTime None (optional)
* @param password None (optional)
* @return Call<Void>
*/
@FormUrlEncoded
@POST("fake")
Observable<Void> testEndpointParameters(
@Field("number") String number, @Field("double") Double _double, @Field("string") String string, @Field("byte") byte[] _byte, @Field("integer") Integer integer, @Field("int32") Integer int32, @Field("int64") Long int64, @Field("float") Float _float, @Field("binary") byte[] binary, @Field("date") Date date, @Field("dateTime") Date dateTime, @Field("password") String password
);
}

View File

@ -9,8 +9,8 @@ import retrofit2.http.*;
import okhttp3.RequestBody;
import io.swagger.client.model.Pet;
import java.io.File;
import io.swagger.client.model.ModelApiResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -51,6 +51,8 @@ public class FormatTest {
private String password = null;
/**
* minimum: 10.0
* maximum: 100.0
**/
@ApiModelProperty(value = "")
public Integer getInteger() {
@ -61,6 +63,8 @@ public class FormatTest {
}
/**
* minimum: 20.0
* maximum: 200.0
**/
@ApiModelProperty(value = "")
public Integer getInt32() {
@ -81,6 +85,8 @@ public class FormatTest {
}
/**
* minimum: 32.1
* maximum: 543.2
**/
@ApiModelProperty(required = true, value = "")
public BigDecimal getNumber() {
@ -91,6 +97,8 @@ public class FormatTest {
}
/**
* minimum: 54.3
* maximum: 987.6
**/
@ApiModelProperty(value = "")
public Float getFloat() {
@ -101,6 +109,8 @@ public class FormatTest {
}
/**
* minimum: 67.8
* maximum: 123.4
**/
@ApiModelProperty(value = "")
public Double getDouble() {
@ -122,7 +132,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public byte[] getByte() {
return _byte;
}
@ -142,7 +152,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public Date getDate() {
return date;
}
@ -162,7 +172,7 @@ public class FormatTest {
/**
**/
@ApiModelProperty(value = "")
@ApiModelProperty(required = true, value = "")
public String getPassword() {
return password;
}

View File

@ -1,255 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.client.CollectionFormats.*;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.*;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import static org.junit.Assert.*;
public class PetApiTest {
PetApi api = null;
@Before
public void setup() {
api = new ApiClient().createService(PetApi.class);
}
@Test
public void testCreateAndGetPet() throws Exception {
final Pet pet = createRandomPet();
api.addPet(pet).subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(Pet fetched) {
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
});
}
});
}
@Test
public void testUpdatePet() throws Exception {
final Pet pet = createRandomPet();
pet.setName("programmer");
api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(Pet fetched) {
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
}
});
}
});
}
@Test
public void testFindPetsByStatus() throws Exception {
final Pet pet = createRandomPet();
pet.setName("programmer");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.findPetsByStatus(new CSVParams("available")).subscribe(new SkeletonSubscriber<List<Pet>>() {
@Override
public void onNext(List<Pet> pets) {
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
});
}
});
}
@Test
public void testFindPetsByTags() throws Exception {
final Pet pet = createRandomPet();
pet.setName("monster");
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<Tag> tags = new ArrayList<Tag>();
Tag tag1 = new Tag();
tag1.setName("friendly");
tags.add(tag1);
pet.setTags(tags);
api.updatePet(pet).subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.findPetsByTags(new CSVParams("friendly")).subscribe(new SkeletonSubscriber<List<Pet>>() {
@Override
public void onNext(List<Pet> pets) {
assertNotNull(pets);
boolean found = false;
for (Pet fetched : pets) {
if (fetched.getId().equals(pet.getId())) {
found = true;
break;
}
}
assertTrue(found);
}
});
}
});
}
@Test
public void testUpdatePetWithForm() throws Exception {
final Pet pet = createRandomPet();
pet.setName("frank");
api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError());
api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(final Pet fetched) {
api.updatePetWithForm(fetched.getId(), "furt", null)
.subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.getPetById(fetched.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(Pet updated) {
assertEquals(updated.getName(), "furt");
}
});
}
});
}
});
}
@Test
public void testDeletePet() throws Exception {
Pet pet = createRandomPet();
api.addPet(pet).subscribe(SkeletonSubscriber.failTestOnError());
api.getPetById(pet.getId()).subscribe(new SkeletonSubscriber<Pet>() {
@Override
public void onNext(Pet fetched) {
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.
}
});
}
});
}
@Test
public void testUploadFile() throws Exception {
File file = File.createTempFile("test", "hello.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write("Hello world!");
writer.close();
Pet pet = createRandomPet();
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<ModelApiResponse>() {
@Override
public void onError(Throwable e) {
// this also yields a 400 for other tests, so I guess it's okay...
}
});
}
@Test
public void testEqualsAndHashCode() {
Pet pet1 = new Pet();
Pet pet2 = new Pet();
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
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"}));
assertFalse(pet1.equals(pet2));
assertFalse(pet2.equals(pet1));
assertFalse(pet1.hashCode() == (pet2.hashCode()));
assertTrue(pet2.equals(pet2));
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"}));
assertTrue(pet1.equals(pet2));
assertTrue(pet2.equals(pet1));
assertTrue(pet1.hashCode() == pet2.hashCode());
assertTrue(pet1.equals(pet1));
assertTrue(pet1.hashCode() == pet1.hashCode());
}
private Pet createRandomPet() {
Pet pet = new Pet();
pet.setId(System.currentTimeMillis());
pet.setName("gorilla");
Category category = new Category();
category.setName("really-happy");
pet.setCategory(category);
pet.setStatus(Pet.StatusEnum.AVAILABLE);
List<String> photos = Arrays.asList(new String[]{"http://foo.bar.com/1", "http://foo.bar.com/2"});
pet.setPhotoUrls(photos);
return pet;
}
}

View File

@ -1,30 +0,0 @@
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

@ -1,96 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
import io.swagger.client.model.*;
import java.lang.reflect.Field;
import java.util.Map;
import org.junit.*;
import retrofit2.Response;
import static org.junit.Assert.*;
public class StoreApiTest {
StoreApi api = null;
@Before
public void setup() {
api = new ApiClient().createService(StoreApi.class);
}
@Test
public void testGetInventory() throws Exception {
api.getInventory().subscribe(new SkeletonSubscriber<Map<String, Integer>>() {
@Override
public void onNext(Map<String, Integer> inventory) {
assertTrue(inventory.keySet().size() > 0);
}
});
}
@Test
public void testPlaceOrder() throws Exception {
final Order order = createOrder();
api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError());
api.getOrderById(order.getId()).subscribe(new SkeletonSubscriber<Order>() {
@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 {
final Order order = createOrder();
api.placeOrder(order).subscribe(SkeletonSubscriber.failTestOnError());
api.getOrderById(order.getId()).subscribe(new SkeletonSubscriber<Order>() {
@Override
public void onNext(Order fetched) {
assertEquals(fetched.getId(), order.getId());
}
});
api.deleteOrder(String.valueOf(order.getId())).subscribe(SkeletonSubscriber.failTestOnError());
api.getOrderById(order.getId())
.subscribe(new SkeletonSubscriber<Order>() {
@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() {
Order order = new Order();
order.setPetId(new Long(200));
order.setQuantity(new Integer(13));
order.setShipDate(new java.util.Date());
order.setStatus(Order.StatusEnum.PLACED);
order.setComplete(true);
try {
Field idField = Order.class.getDeclaredField("id");
idField.setAccessible(true);
idField.set(order, System.currentTimeMillis());
} catch (Exception e) {
throw new RuntimeException(e);
}
return order;
}
}

View File

@ -1,108 +0,0 @@
package io.swagger.petstore.test;
import io.swagger.client.ApiClient;
import io.swagger.client.api.*;
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;
@Before
public void setup() {
api = new ApiClient().createService(UserApi.class);
}
@Test
public void testCreateUser() throws Exception {
final User user = createUser();
api.createUser(user).subscribe(new SkeletonSubscriber<Void>() {
@Override
public void onCompleted() {
api.getUserByName(user.getUsername()).subscribe(new SkeletonSubscriber<User>() {
@Override
public void onNext(User fetched) {
assertEquals(user.getId(), fetched.getId());
}
});
}
});
}
@Test
public void testCreateUsersWithArray() throws Exception {
final User user1 = createUser();
user1.setUsername("abc123");
User user2 = createUser();
user2.setUsername("123abc");
api.createUsersWithArrayInput(Arrays.asList(new User[]{user1, user2})).subscribe(SkeletonSubscriber.failTestOnError());
api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber<User>() {
@Override
public void onNext(User fetched) {
assertEquals(user1.getId(), fetched.getId());
}
});
}
@Test
public void testCreateUsersWithList() throws Exception {
final User user1 = createUser();
user1.setUsername("abc123");
User user2 = createUser();
user2.setUsername("123abc");
api.createUsersWithListInput(Arrays.asList(new User[]{user1, user2})).subscribe(SkeletonSubscriber.failTestOnError());
api.getUserByName(user1.getUsername()).subscribe(new SkeletonSubscriber<User>() {
@Override
public void onNext(User fetched) {
assertEquals(user1.getId(), fetched.getId());
}
});
}
@Test
public void testLoginUser() throws Exception {
User user = createUser();
api.createUser(user);
api.loginUser(user.getUsername(), user.getPassword()).subscribe(new SkeletonSubscriber<String>() {
@Override
public void onNext(String token) {
assertTrue(token.startsWith("logged in user session:"));
}
});
}
@Test
public void logoutUser() throws Exception {
api.logoutUser();
}
private User createUser() {
User user = new User();
user.setId(System.currentTimeMillis());
user.setUsername("fred");
user.setFirstName("Fred");
user.setLastName("Meyer");
user.setEmail("fred@fredmeyer.com");
user.setPassword("xxXXxx");
user.setPhone("408-867-5309");
user.setUserStatus(123);
return user;
}
}