forked from danshannon/javastrava-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start modularising tests to support testing of async API's with minimal
additional code Remove issue tests for #67 #69 and #74 all fixed by Strava (thanks!)
- Loading branch information
1 parent
382241e
commit 7e5e7dc
Showing
35 changed files
with
3,068 additions
and
688 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/** | ||
* | ||
*/ | ||
package test.api.rest; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.fail; | ||
import javastrava.api.v3.rest.API; | ||
import javastrava.api.v3.service.exception.NotFoundException; | ||
import javastrava.api.v3.service.exception.UnauthorizedException; | ||
|
||
import org.junit.Test; | ||
|
||
import test.utils.RateLimitedTestRunner; | ||
|
||
/** | ||
* @author danshannon | ||
* @param <T> | ||
* Class of object being created | ||
* @param <U> | ||
* Class of identifier of the parent (so mostly, Integer) | ||
* | ||
*/ | ||
public abstract class APICreateTest<T, U> extends APITest<T> { | ||
@Test | ||
public void create_invalidParent() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
T createdObject = null; | ||
try { | ||
createdObject = this.creationCallback.run(api, createObject(), invalidParentId()); | ||
} catch (final NotFoundException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(createdObject); | ||
fail("Created an object with an invalid parent!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void create_privateParentWithViewPrivate() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithFullAccess(); | ||
final T result = this.creationCallback.run(api, createObject(), privateParentId()); | ||
assertNotNull(result); | ||
validate(result); | ||
}); | ||
} | ||
|
||
@Test | ||
public void create_privateParentWithoutViewPrivate() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
T createdObject = null; | ||
try { | ||
createdObject = this.creationCallback.run(api, createObject(), privateParentId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(createdObject); | ||
fail("Created an object with a private parent, but without view_private"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void create_validParentNoWriteAccess() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = api(); | ||
T createdObject = null; | ||
try { | ||
createdObject = this.creationCallback.run(api, createObject(), validParentId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(createdObject); | ||
fail("Created an object with a valid parent, but without write access!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void create_privateParentBelongsToOtherUser() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithFullAccess(); | ||
T createdObject = null; | ||
try { | ||
createdObject = this.creationCallback.run(api, createObject(), privateParentOtherUserId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(createdObject); | ||
fail("Created an object with a private parent that belongs to another user!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void create_valid() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
final T result = this.creationCallback.run(api, createObject(), validParentId()); | ||
assertNotNull(result); | ||
validate(result); | ||
}); | ||
} | ||
|
||
protected abstract T createObject(); | ||
|
||
protected abstract U invalidParentId(); | ||
|
||
protected abstract U privateParentId(); | ||
|
||
protected abstract U validParentId(); | ||
|
||
protected abstract U privateParentOtherUserId(); | ||
|
||
protected abstract void forceDelete(T objectToDelete); | ||
|
||
protected TestCreateCallback<T, U> creationCallback; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* | ||
*/ | ||
package test.api.rest; | ||
|
||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.fail; | ||
import javastrava.api.v3.rest.API; | ||
import javastrava.api.v3.service.exception.NotFoundException; | ||
import javastrava.api.v3.service.exception.UnauthorizedException; | ||
|
||
import org.junit.Test; | ||
|
||
import test.utils.RateLimitedTestRunner; | ||
|
||
/** | ||
* @author danshannon | ||
* @param <T> | ||
* Class of object being created | ||
* @param <U> | ||
* Class of identifier of the parent (so mostly, Integer) | ||
* | ||
*/ | ||
public abstract class APIDeleteTest<T, U> extends APITest<T> { | ||
@Test | ||
public void delete_invalidParent() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
T deletedObject = null; | ||
try { | ||
deletedObject = this.callback.run(api, createObject(), invalidParentId()); | ||
} catch (final NotFoundException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(deletedObject); | ||
fail("Deleted an object with an invalid id!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void delete_privateParentWithViewPrivate() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithFullAccess(); | ||
final T result = this.callback.run(api, createObject(), privateParentId()); | ||
assertNull(result); | ||
}); | ||
} | ||
|
||
@Test | ||
public void delete_privateParentWithoutViewPrivate() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
T deletedObject = null; | ||
try { | ||
deletedObject = this.callback.run(api, createObject(), privateParentId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(deletedObject); | ||
fail("Deleted an object with a private parent, but without view_private"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void delete_validParentNoWriteAccess() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = api(); | ||
T deletedObject = null; | ||
try { | ||
deletedObject = this.callback.run(api, createObject(), validParentId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(deletedObject); | ||
fail("Deleted an object with a valid parent, but without write access!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void delete_privateParentBelongsToOtherUser() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithFullAccess(); | ||
T deletedObject = null; | ||
try { | ||
deletedObject = this.callback.run(api, createObject(), privateParentOtherUserId()); | ||
} catch (final UnauthorizedException e) { | ||
// Expected | ||
return; | ||
} | ||
forceDelete(deletedObject); | ||
fail("Created an object with a private parent that belongs to another user!"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void delete_valid() throws Exception { | ||
RateLimitedTestRunner.run(() -> { | ||
final API api = apiWithWriteAccess(); | ||
final T result = this.callback.run(api, createObject(), validParentId()); | ||
assertNull(result); | ||
}); | ||
} | ||
|
||
protected abstract T createObject(); | ||
|
||
protected abstract U invalidParentId(); | ||
|
||
protected abstract U privateParentId(); | ||
|
||
protected abstract U validParentId(); | ||
|
||
protected abstract U privateParentOtherUserId(); | ||
|
||
protected abstract void forceDelete(T objectToDelete); | ||
|
||
protected TestDeleteCallback<T, U> callback; | ||
} |
Oops, something went wrong.