forked from loafle/openapi-generator-original
Rebuild Java clients for the petstore sample
This commit is contained in:
@@ -2,6 +2,9 @@ package io.swagger.client;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Callback for asynchronous API call.
|
||||
*
|
||||
@@ -10,13 +13,19 @@ import java.io.IOException;
|
||||
public interface ApiCallback<T> {
|
||||
/**
|
||||
* This is called when the API call fails.
|
||||
*
|
||||
* @param e The exception causing the failure
|
||||
* @param statusCode Status code of the response if available, otherwise it would be 0
|
||||
* @param responseHeaders Headers of the response if available, otherwise it would be null
|
||||
*/
|
||||
void onFailure(ApiException e);
|
||||
void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders);
|
||||
|
||||
/**
|
||||
* This is called when the API call succeeded.
|
||||
*
|
||||
* @param result The result deserialized from response
|
||||
* @param statusCode Status code of the response
|
||||
* @param responseHeaders Headers of the response
|
||||
*/
|
||||
void onSuccess(T result);
|
||||
void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders);
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ public class ApiClient {
|
||||
|
||||
private Map<String, Authentication> authentications;
|
||||
|
||||
private int statusCode;
|
||||
private Map<String, List<String>> responseHeaders;
|
||||
|
||||
private String dateFormat;
|
||||
private DateFormat dateFormatter;
|
||||
private int dateLength;
|
||||
@@ -106,6 +109,24 @@ public class ApiClient {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the status code of the previous request.
|
||||
* NOTE: Status code of last async response is not recorded here, it is
|
||||
* passed to the callback methods instead.
|
||||
*/
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the response headers of the previous request.
|
||||
* NOTE: Headers of last async response is not recorded here, it is passed
|
||||
* to callback methods instead.
|
||||
*/
|
||||
public Map<String, List<String>> getResponseHeaders() {
|
||||
return responseHeaders;
|
||||
}
|
||||
|
||||
public String getDateFormat() {
|
||||
return dateFormat;
|
||||
}
|
||||
@@ -533,6 +554,8 @@ public class ApiClient {
|
||||
public <T> T execute(Call call, Type returnType) throws ApiException {
|
||||
try {
|
||||
Response response = call.execute();
|
||||
this.statusCode = response.code();
|
||||
this.responseHeaders = response.headers().toMultimap();
|
||||
return handleResponse(response, returnType);
|
||||
} catch (IOException e) {
|
||||
throw new ApiException(e);
|
||||
@@ -556,7 +579,7 @@ public class ApiClient {
|
||||
call.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(Request request, IOException e) {
|
||||
callback.onFailure(new ApiException(e));
|
||||
callback.onFailure(new ApiException(e), 0, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -565,10 +588,10 @@ public class ApiClient {
|
||||
try {
|
||||
result = (T) handleResponse(response, returnType);
|
||||
} catch (ApiException e) {
|
||||
callback.onFailure(e);
|
||||
callback.onFailure(e, response.code(), response.headers().toMultimap());
|
||||
return;
|
||||
}
|
||||
callback.onSuccess(result);
|
||||
callback.onSuccess(result, response.code(), response.headers().toMultimap());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package io.swagger.client;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class ApiException extends Exception {
|
||||
private int code = 0;
|
||||
private Map<String, List<String>> responseHeaders = null;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.swagger.client;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class Configuration {
|
||||
private static ApiClient defaultApiClient = new ApiClient();
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.swagger.client;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class Pair {
|
||||
private String name = "";
|
||||
private String value = "";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package io.swagger.client;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class StringUtil {
|
||||
/**
|
||||
* Check if the given array contains the given value (with case-insensitive comparison).
|
||||
|
||||
@@ -354,7 +354,7 @@ public class UserApi {
|
||||
/**
|
||||
* Get user by user name
|
||||
*
|
||||
* @param username The name that needs to be fetched. Use user1 for testing.
|
||||
* @param username The name that needs to be fetched. Use user1 for testing.
|
||||
* @return User
|
||||
*/
|
||||
public User getUserByName(String username) throws ApiException {
|
||||
@@ -366,7 +366,7 @@ public class UserApi {
|
||||
/**
|
||||
* Get user by user name (asynchronously)
|
||||
*
|
||||
* @param username The name that needs to be fetched. Use user1 for testing.
|
||||
* @param username The name that needs to be fetched. Use user1 for testing.
|
||||
* @param callback The callback to be executed when the API call finishes
|
||||
* @return The request call
|
||||
*/
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.swagger.client.Pair;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class ApiKeyAuth implements Authentication {
|
||||
private final String location;
|
||||
private final String paramName;
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.swagger.client.Pair;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public interface Authentication {
|
||||
/** Apply authentication settings to header and query params. */
|
||||
void applyToParams(List<Pair> queryParams, Map<String, String> headerParams);
|
||||
|
||||
@@ -5,7 +5,7 @@ import io.swagger.client.Pair;
|
||||
import java.util.Map;
|
||||
import java.util.List;
|
||||
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-08-31T19:27:38.337+08:00")
|
||||
@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaClientCodegen", date = "2015-09-11T13:13:26.059+08:00")
|
||||
public class OAuth implements Authentication {
|
||||
@Override
|
||||
public void applyToParams(List<Pair> queryParams, Map<String, String> headerParams) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.swagger.client.ApiClient;
|
||||
import io.swagger.client.ApiException;
|
||||
import io.swagger.client.Configuration;
|
||||
|
||||
import io.swagger.client.ApiCallback;
|
||||
import io.swagger.client.api.*;
|
||||
import io.swagger.client.auth.*;
|
||||
import io.swagger.client.model.*;
|
||||
@@ -13,7 +14,9 @@ import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.*;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -68,6 +71,81 @@ public class PetApiTest {
|
||||
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAndGetPetAsync() throws Exception {
|
||||
Pet pet = createRandomPet();
|
||||
api.addPet(pet);
|
||||
// to store returned Pet or error message/exception
|
||||
final Map<String, Object> result = new HashMap<String, Object>();
|
||||
|
||||
api.getPetByIdAsync(pet.getId(), new ApiCallback<Pet>() {
|
||||
@Override
|
||||
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
result.put("error", e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Pet pet, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
result.put("pet", pet);
|
||||
}
|
||||
});
|
||||
// the API call should be executed asynchronously, so result should be empty at the moment
|
||||
assertTrue(result.isEmpty());
|
||||
|
||||
// wait for the asynchronous call to finish (at most 10 seconds)
|
||||
final int maxTry = 10;
|
||||
int tryCount = 1;
|
||||
Pet fetched = null;
|
||||
do {
|
||||
if (tryCount > maxTry) fail("have not got result of getPetByIdAsync after 10 seconds");
|
||||
Thread.sleep(1000);
|
||||
tryCount += 1;
|
||||
if (result.get("error") != null) fail((String) result.get("error"));
|
||||
if (result.get("pet") != null) {
|
||||
fetched = (Pet) result.get("pet");
|
||||
break;
|
||||
}
|
||||
} while (result.isEmpty());
|
||||
assertNotNull(fetched);
|
||||
assertEquals(pet.getId(), fetched.getId());
|
||||
assertNotNull(fetched.getCategory());
|
||||
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
|
||||
|
||||
// test getting a nonexistent pet
|
||||
result.clear();
|
||||
api.getPetByIdAsync(new Long(-10000), new ApiCallback<Pet>() {
|
||||
@Override
|
||||
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
result.put("exception", e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSuccess(Pet pet, int statusCode, Map<String, List<String>> responseHeaders) {
|
||||
result.put("pet", pet);
|
||||
}
|
||||
});
|
||||
// the API call should be executed asynchronously, so result should be empty at the moment
|
||||
assertTrue(result.isEmpty());
|
||||
|
||||
// wait for the asynchronous call to finish (at most 10 seconds)
|
||||
tryCount = 1;
|
||||
ApiException exception = null;
|
||||
do {
|
||||
if (tryCount > maxTry) fail("have not got result of getPetByIdAsync after 10 seconds");
|
||||
Thread.sleep(1000);
|
||||
tryCount += 1;
|
||||
if (result.get("pet") != null) fail("expected an error");
|
||||
if (result.get("exception") != null) {
|
||||
exception = (ApiException) result.get("exception");
|
||||
break;
|
||||
}
|
||||
} while (result.isEmpty());
|
||||
assertNotNull(exception);
|
||||
assertEquals(404, exception.getCode());
|
||||
assertEquals("Not Found", exception.getMessage());
|
||||
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdatePet() throws Exception {
|
||||
Pet pet = createRandomPet();
|
||||
|
||||
Reference in New Issue
Block a user