Last active
September 25, 2016 12:15
-
-
Save dherges/3bd3338ae10adb95e42735567b07d504 to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
This file contains hidden or 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
public class ContentTypeRoute implements Route { | |
private final String contentType; | |
private final Route delegate; | |
private final Optional<ResponseTransformer> responseTransformer; | |
private ContentTypeRoute(String contentType, Route delegate, | |
ResponseTransformer responseTransformer) { | |
this.contentType = contentType; | |
this.delegate = delegate; | |
this.responseTransformer = Optional.ofNullable(responseTransformer); | |
} | |
@Override | |
public Object handle(Request request, Response response) throws Exception { | |
final Object result = delegateAndTransform(request, response); | |
response.header("Content-Type", contentType); | |
return result; | |
} | |
private Object delegateAndTransform(Request request, Response response) | |
throws Exception { | |
final Object data = delegate.handle(request, response); | |
if (responseTransformer.isPresent()) { | |
return responseTransformer.get().render(data); | |
} | |
return data; | |
} | |
public static Route create(Route delegate, String contentType) { | |
return create(delegate, contentType, null); | |
} | |
public static Route create(Route delegate, String contentType, | |
ResponseTransformer responseTransformer) { | |
return new ContentTypeRoute(contentType, delegate, responseTransformer); | |
} | |
} |
This file contains hidden or 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
public class MoshiResponseTransformer<T> implements ResponseTransformer { | |
private final Moshi moshi; | |
private final Class<T> type; | |
private MoshiResponseTransformer(Moshi moshi, Class<T> type) { | |
this.moshi = moshi; | |
this.type = type; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public String render(Object model) throws Exception { | |
return moshi.adapter(type).toJson((T) model); | |
} | |
public static ResponseTransformer create(Class<?> type) { | |
return create(new Moshi.Builder().build(), type); | |
} | |
public static ResponseTransformer create(Moshi moshi, Class<?> type) { | |
return new MoshiResponseTransformer<>(moshi, type); | |
} | |
} |
This file contains hidden or 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
public class RetweetsIdRoute implements Route { | |
@Override | |
public Object handle(Request request, Response response) throws Exception { | |
final String idParam = request.params(":id"); | |
final String count = request.queryParams("count"); | |
return fetchTweets( | |
Long.valueOf(idParam), | |
Integer.valueOf(count != null && count.length() > 0 ? count : "100") | |
); | |
} | |
private List<Tweet> fetchTweets(long id, int count) { | |
final List<Tweet> tweets = new ArrayList<Tweet>(count); | |
for (int i = 0; i < count; i++) { | |
final Tweet tweet = new Tweet(); | |
tweet.id = id + i; | |
tweet.text = String.format("Retweet #%s of %s for %s", i, count, id); | |
tweet.favorited = i % 2 == 0; | |
tweets.add(tweet); | |
} | |
return tweets; | |
} | |
public static Route create() { | |
return new RetweetsIdRoute(); | |
} | |
} |
This file contains hidden or 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
import static spark.Spark.get; | |
public class TwitterApp implements SparkApplication { | |
@Override | |
public void init() { | |
get( | |
"statuses/retweets/:id", | |
"*/*", | |
ContentTypeRoute.create( | |
RetweetsIdRoute.create(), | |
"application/json", | |
MoshiResponseTransformer.create( | |
new Moshi.Builder() | |
.add(Date.class, | |
new DateJsonAdapter("EEE MMM dd kk:mm:ss z yyyy").nullSafe()) | |
.build(), | |
List.class | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment