Skip to content

Commit

Permalink
Start modularising tests to support testing of async API's with minimal
Browse files Browse the repository at this point in the history
additional code
Remove issue tests for #67 #69 and #74 all fixed by Strava (thanks!)
  • Loading branch information
dshannonau committed Apr 27, 2015
1 parent 382241e commit 7e5e7dc
Show file tree
Hide file tree
Showing 35 changed files with 3,068 additions and 688 deletions.
32 changes: 16 additions & 16 deletions src/main/java/test/api/model/StravaActivityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

import javastrava.api.v3.model.StravaActivity;
import javastrava.api.v3.model.StravaBestRunningEffort;
import javastrava.api.v3.model.StravaSegmentEffort;
Expand All @@ -27,8 +29,7 @@ public static void validateActivity(final StravaActivity activity) {
validateActivity(activity, activity.getId(), activity.getResourceState());
}

public static void validateActivity(final StravaActivity activity, final Integer id,
final StravaResourceState state) {
public static void validateActivity(final StravaActivity activity, final Integer id, final StravaResourceState state) {
assertNotNull(activity);
assertEquals(id, activity.getId());
assertEquals(state, activity.getResourceState());
Expand All @@ -43,8 +44,7 @@ public static void validateActivity(final StravaActivity activity, final Integer
assertNotNull(activity.getAthlete());
assertTrue((activity.getAthlete().getResourceState() == StravaResourceState.META)
|| (activity.getAthlete().getResourceState() == StravaResourceState.SUMMARY));
StravaAthleteTest.validateAthlete(activity.getAthlete(), activity.getAthlete().getId(),
activity.getAthlete().getResourceState());
StravaAthleteTest.validateAthlete(activity.getAthlete(), activity.getAthlete().getId(), activity.getAthlete().getResourceState());
assertNotNull(activity.getName());
assertNotNull(activity.getDistance());
assertTrue(activity.getDistance() >= 0);
Expand Down Expand Up @@ -103,12 +103,10 @@ public static void validateActivity(final StravaActivity activity, final Integer
// Optional assertNotNull(activity.getExternalId());
assertNotNull(activity.getFlagged());
if (activity.getGear() != null) {
StravaGearTest.validateGear(activity.getGear(), activity.getGearId(),
activity.getGear().getResourceState());
StravaGearTest.validateGear(activity.getGear(), activity.getGearId(), activity.getGear().getResourceState());
}
assertNotNull(activity.getHasKudoed());
if ((activity.getType() == StravaActivityType.RIDE) && !activity.getManual() && !activity.getTrainer()
&& (activity.getCalories() != null)) {
if ((activity.getType() == StravaActivityType.RIDE) && !activity.getManual() && !activity.getTrainer() && (activity.getCalories() != null)) {
assertNotNull(activity.getKilojoules());
}
assertNotNull(activity.getKudosCount());
Expand All @@ -118,8 +116,7 @@ public static void validateActivity(final StravaActivity activity, final Integer
assertNotNull(activity.getManual());
assertNotNull(activity.getMap());
if (!activity.getManual() && !activity.getTrainer()) {
StravaMapTest.validateMap(activity.getMap(), activity.getMap().getId(),
activity.getMap().getResourceState(), activity);
StravaMapTest.validateMap(activity.getMap(), activity.getMap().getId(), activity.getMap().getResourceState(), activity);
}
assertNotNull(activity.getMaxSpeed());
assertTrue(activity.getMaxSpeed() >= 0);
Expand Down Expand Up @@ -159,8 +156,7 @@ public static void validateActivity(final StravaActivity activity, final Integer
assertNotNull(activity.getAthlete());
assertTrue((activity.getAthlete().getResourceState() == StravaResourceState.META)
|| (activity.getAthlete().getResourceState() == StravaResourceState.SUMMARY));
StravaAthleteTest.validateAthlete(activity.getAthlete(), activity.getAthlete().getId(),
activity.getAthlete().getResourceState());
StravaAthleteTest.validateAthlete(activity.getAthlete(), activity.getAthlete().getId(), activity.getAthlete().getResourceState());
assertNotNull(activity.getName());
assertNotNull(activity.getDistance());
assertTrue(activity.getDistance() >= 0);
Expand Down Expand Up @@ -207,8 +203,7 @@ public static void validateActivity(final StravaActivity activity, final Integer
assertNull(activity.getGear());
// Optional assertNotNull(activity.getGearId());
assertNotNull(activity.getHasKudoed());
if ((activity.getType() == StravaActivityType.RIDE) && !activity.getManual() && !activity.getTrainer()
&& (activity.getCalories() != null)) {
if ((activity.getType() == StravaActivityType.RIDE) && !activity.getManual() && !activity.getTrainer() && (activity.getCalories() != null)) {
assertNotNull(activity.getKilojoules());
}
assertNotNull(activity.getKudosCount());
Expand All @@ -218,8 +213,7 @@ public static void validateActivity(final StravaActivity activity, final Integer
assertNotNull(activity.getManual());
assertNotNull(activity.getMap());
if (!activity.getManual() && !activity.getTrainer()) {
StravaMapTest.validateMap(activity.getMap(), activity.getMap().getId(),
activity.getMap().getResourceState(), activity);
StravaMapTest.validateMap(activity.getMap(), activity.getMap().getId(), activity.getMap().getResourceState(), activity);
}
assertNotNull(activity.getMaxSpeed());
assertTrue(activity.getMaxSpeed() >= 0);
Expand Down Expand Up @@ -314,4 +308,10 @@ protected Class<StravaActivity> getClassUnderTest() {
return StravaActivity.class;
}

public static void validateList(final List<StravaActivity> activities) {
for (final StravaActivity activity : activities) {
validateActivity(activity);
}
}

}
19 changes: 15 additions & 4 deletions src/main/java/test/api/model/StravaCommentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import java.util.List;

import javastrava.api.v3.model.StravaComment;
import javastrava.api.v3.model.reference.StravaResourceState;
import test.utils.BeanTest;
Expand All @@ -26,17 +29,15 @@ public static void validateComment(final StravaComment comment, final Integer id
if (state == StravaResourceState.DETAILED) {
assertNotNull(comment.getActivityId());
assertNotNull(comment.getAthlete());
StravaAthleteTest.validateAthlete(comment.getAthlete(), comment.getAthlete().getId(), comment.getAthlete()
.getResourceState());
StravaAthleteTest.validateAthlete(comment.getAthlete(), comment.getAthlete().getId(), comment.getAthlete().getResourceState());
assertNotNull(comment.getCreatedAt());
assertNotNull(comment.getText());
return;
}
if (state == StravaResourceState.SUMMARY) {
assertNotNull(comment.getActivityId());
assertNotNull(comment.getAthlete());
StravaAthleteTest.validateAthlete(comment.getAthlete(), comment.getAthlete().getId(), comment.getAthlete()
.getResourceState());
StravaAthleteTest.validateAthlete(comment.getAthlete(), comment.getAthlete().getId(), comment.getAthlete().getResourceState());
assertNotNull(comment.getCreatedAt());
assertNotNull(comment.getText());
return;
Expand All @@ -55,4 +56,14 @@ public static void validateComment(final StravaComment comment, final Integer id
protected Class<StravaComment> getClassUnderTest() {
return StravaComment.class;
}

/**
* @param results
*/
public static void validateCommentList(final List<StravaComment> comments) {
for (final StravaComment comment : comments) {
validateComment(comment);
}

}
}
122 changes: 122 additions & 0 deletions src/main/java/test/api/rest/APICreateTest.java
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;
}
120 changes: 120 additions & 0 deletions src/main/java/test/api/rest/APIDeleteTest.java
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;
}
Loading

0 comments on commit 7e5e7dc

Please sign in to comment.