Skip to content
Permalink
Browse files

Add more tets to java client (#2365)

  • Loading branch information...
wing328 committed Mar 12, 2019
1 parent fde3252 commit d2244a3bafb90f55896d1a83c33799cae7e8c7e0
@@ -22,6 +22,9 @@
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -80,10 +83,7 @@ public void testCreateAndGetPet() throws Exception {
api.addPet(pet);

Pet fetched = api.getPetById(pet.getId());
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
assertPetMatches(pet, fetched);
}

@Test
@@ -95,10 +95,8 @@ public void testCreateAndGetPetWithHttpInfo() throws Exception {
assertEquals(200, resp.getStatusCode());
assertEquals("application/json", resp.getHeaders().get("Content-Type").get(0));
Pet fetched = resp.getData();
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());

assertPetMatches(pet, fetched);
}

@Test
@@ -146,10 +144,7 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
break;
}
} while (result.isEmpty());
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
assertPetMatches(pet, fetched);

// test getting a nonexistent pet
result.clear();
@@ -194,6 +189,65 @@ public void onDownloadProgress(long bytesRead, long contentLength, boolean done)
assertEquals("application/json", exception.getResponseHeaders().get("Content-Type").get(0));
}

@Test
public void testCreateAndGetMultiplePetsAsync() throws Exception {
Pet pet1 = createPet();
Pet pet2 = createPet();

final CountDownLatch addLatch = new CountDownLatch(2);
final TestApiCallback<Void> addCallback1 = new TestApiCallback<Void>(addLatch);
final TestApiCallback<Void> addCallback2 = new TestApiCallback<Void>(addLatch);

// Make 2 simultaneous calls
api.addPetAsync(pet1, addCallback1);
api.addPetAsync(pet2, addCallback2);

// wait for both asynchronous calls to finish (at most 10 seconds)
assertTrue(addLatch.await(10, TimeUnit.SECONDS));

assertTrue(addCallback1.isDone());
assertTrue(addCallback2.isDone());

if (!addCallback1.isSuccess()) throw addCallback1.getException();
if (!addCallback2.isSuccess()) throw addCallback2.getException();

assertValidProgress(addCallback1.getUploadProgress());
assertValidProgress(addCallback2.getUploadProgress());

final CountDownLatch getLatch = new CountDownLatch(3);
final TestApiCallback<Pet> getCallback1 = new TestApiCallback<Pet>(getLatch);
final TestApiCallback<Pet> getCallback2 = new TestApiCallback<Pet>(getLatch);
final TestApiCallback<Pet> getCallback3 = new TestApiCallback<Pet>(getLatch);

api.getPetByIdAsync(pet1.getId(), getCallback1);
api.getPetByIdAsync(pet2.getId(), getCallback2);
// Get nonexistent pet
api.getPetByIdAsync(-10000L, getCallback3);

// wait for all asynchronous calls to finish (at most 10 seconds)
assertTrue(getLatch.await(10, TimeUnit.SECONDS));

assertTrue(getCallback1.isDone());
assertTrue(getCallback2.isDone());
assertTrue(getCallback3.isDone());

if (!getCallback1.isSuccess()) throw getCallback1.getException();
if (!getCallback2.isSuccess()) throw getCallback2.getException();

assertPetMatches(pet1, getCallback1.getResult());
assertPetMatches(pet2, getCallback2.getResult());

assertValidProgress(getCallback1.getDownloadProgress());
assertValidProgress(getCallback2.getDownloadProgress());

// Last callback should fail with ApiException
assertFalse(getCallback3.isSuccess());
final ApiException exception = getCallback3.getException();
assertNotNull(exception);
assertEquals(404, exception.getCode());
}


@Test
public void testUpdatePet() throws Exception {
Pet pet = createPet();
@@ -202,10 +256,7 @@ public void testUpdatePet() throws Exception {
api.updatePet(pet);

Pet fetched = api.getPetById(pet.getId());
assertNotNull(fetched);
assertEquals(pet.getId(), fetched.getId());
assertNotNull(fetched.getCategory());
assertEquals(fetched.getCategory().getName(), pet.getCategory().getName());
assertPetMatches(pet, fetched);
}

@Test
@@ -233,6 +284,7 @@ public void testFindPetsByStatus() throws Exception {
}

@Test
@Ignore
public void testFindPetsByTags() throws Exception {
Pet pet = createPet();
pet.setName("monster");
@@ -355,4 +407,127 @@ private String serializeJson(Object o, ApiClient apiClient) {
private <T> T deserializeJson(String json, Type type, ApiClient apiClient) {
return (T) apiClient.getJSON().deserialize(json, type);
}

private void assertPetMatches(Pet expected, Pet actual) {
assertNotNull(actual);
assertEquals(expected.getId(), actual.getId());
assertNotNull(actual.getCategory());
assertEquals(expected.getCategory().getName(),
actual.getCategory().getName());
}

/**
* Assert that the given upload/download progress list satisfies the
* following constraints:
*
* - List is not empty
* - Byte count should be nondecreasing
* - The last element, and only the last element, should have done=true
*/
private void assertValidProgress(List<Progress> progressList) {
assertFalse(progressList.isEmpty());

Progress prev = null;
int index = 0;
for (Progress progress : progressList) {
if (prev != null) {
if (prev.done || prev.bytes > progress.bytes) {
fail("Progress list out of order at index " + index
+ ": " + progressList);
}
}
prev = progress;
index += 1;
}

if (!prev.done) {
fail("Last progress item should have done=true: " + progressList);
}
}

private static class TestApiCallback<T> implements ApiCallback<T> {

private final CountDownLatch latch;
private final ConcurrentLinkedQueue<Progress> uploadProgress =
new ConcurrentLinkedQueue<Progress>();
private final ConcurrentLinkedQueue<Progress> downloadProgress =
new ConcurrentLinkedQueue<Progress>();

private boolean done;
private boolean success;
private ApiException exception;
private T result;

public TestApiCallback(CountDownLatch latch) {
this.latch = latch;
this.done = false;
}

@Override
public void onFailure(ApiException e, int statusCode, Map<String, List<String>> responseHeaders) {
exception = e;
this.done = true;
this.success = false;
latch.countDown();
}

@Override
public void onSuccess(T result, int statusCode, Map<String, List<String>> responseHeaders) {
this.result = result;
this.done = true;
this.success = true;
latch.countDown();
}

@Override
public void onUploadProgress(long bytesWritten, long contentLength, boolean done) {
uploadProgress.add(new Progress(bytesWritten, contentLength, done));
}

@Override
public void onDownloadProgress(long bytesRead, long contentLength, boolean done) {
downloadProgress.add(new Progress(bytesRead, contentLength, done));
}

public boolean isDone() {
return done;
}

public boolean isSuccess() {
return success;
}

public ApiException getException() {
return exception;
}

public T getResult() {
return result;
}

public List<Progress> getUploadProgress() {
return new ArrayList<Progress>(uploadProgress);
}

public List<Progress> getDownloadProgress() {
return new ArrayList<Progress>(downloadProgress);
}
}

private static class Progress {
public final long bytes;
public final long contentLength;
public final boolean done;

public Progress(long bytes, long contentLength, boolean done) {
this.bytes = bytes;
this.contentLength = contentLength;
this.done = done;
}

@Override
public String toString() {
return "<Progress " + bytes + " " + contentLength + " " + done + ">";
}
}
}
Oops, something went wrong.

0 comments on commit d2244a3

Please sign in to comment.
You can’t perform that action at this time.