Skip to content

Commit

Permalink
Fix TypeToken usage on JsonDeserializer (closes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahan committed Jan 13, 2014
1 parent e0db545 commit 9b71d1b
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.lonepulse</groupId>
<artifactId>robozombie-parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.3-SNAPSHOT</version>
<packaging>pom</packaging>

<parent>
Expand Down
4 changes: 2 additions & 2 deletions robozombie-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<parent>
<groupId>com.lonepulse</groupId>
<artifactId>robozombie-parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand All @@ -25,7 +25,7 @@
<dependency>
<groupId>com.lonepulse</groupId>
<artifactId>robozombie</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion robozombie/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>com.lonepulse</groupId>
<artifactId>robozombie-parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>1.3.3-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import com.lonepulse.robozombie.annotation.Config;
import com.lonepulse.robozombie.proxy.Zombie;
import com.lonepulse.robozombie.proxy.Zombie.Configuration;

Expand Down Expand Up @@ -121,10 +122,9 @@ public Configuration register(Class<?> endpointClass) {

try {

if(endpointClass.isAnnotationPresent(com.lonepulse.robozombie.annotation.Config.class)) {
if(endpointClass.isAnnotationPresent(Config.class)) {

Configuration configuration = endpointClass.getAnnotation(
com.lonepulse.robozombie.annotation.Config.class).value().newInstance();
Configuration configuration = endpointClass.getAnnotation(Config.class).value().newInstance();

HttpClient httpClient = configuration.httpClient();
HttpClientDirectory.INSTANCE.bind(endpointClass, httpClient); //currently the only configurable property
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ final class JsonDeserializer extends AbstractDeserializer<Object> {
Gson_fromJson = Gson.getDeclaredMethod("fromJson", String.class, Type.class);

TypeToken = Class.forName("com.google.gson.reflect.TypeToken");
TypeToken_GET = TypeToken.getDeclaredMethod("get", Class.class);
TypeToken_GET = TypeToken.getDeclaredMethod("get", java.lang.reflect.Type.class);
TypeToken_getType = TypeToken.getDeclaredMethod("getType");

gson = Gson.newInstance();
Expand Down Expand Up @@ -156,7 +156,7 @@ protected Object deserialize(InvocationContext context, HttpResponse response) {
try {

return entity == null? null :Gson_fromJson.invoke(gson, EntityUtils.toString(entity),
TypeToken_getType.invoke(TypeToken_GET.invoke(null, context.getRequest().getReturnType())));
TypeToken_getType.invoke(TypeToken_GET.invoke(null, context.getRequest().getGenericReturnType())));
}
catch(Exception e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Copyright (C) 2013 Lonepulse
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with th e License.
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
Expand Down Expand Up @@ -146,7 +146,7 @@ protected Object deserialize(InvocationContext context, HttpResponse response) {
HttpEntity entity = response.getEntity();

return entity == null? null :Persister_read.invoke(persister,
context.getRequest().getReturnType(), EntityUtils.toString(entity));
context.getRequest().getGenericReturnType(), EntityUtils.toString(entity));
}
catch(Exception e) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static com.lonepulse.robozombie.annotation.Entity.ContentType.PLAIN;
import static com.lonepulse.robozombie.annotation.Entity.ContentType.XML;

import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http42.util.EntityUtils;

Expand Down Expand Up @@ -73,6 +75,16 @@ public interface DeserializerEndpoint {
@GET("/json")
User deserializeJson();

/**
* <p>A mock request which receives a JSON array response that is deserialized to a generic type.</p>
*
* @return the deserialized response entity list
*
* @since 1.3.3
*/
@GET("/jsonarray")
List<User> deserializeJsonToGenericType();

/**
* <p>A mock request which receives an XML response that is deserialized to its model.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;

import org.hamcrest.core.Is;
import org.junit.Before;
Expand Down Expand Up @@ -137,6 +139,42 @@ public final void testParseJson() {
assertEquals(user.isImmortal(), deserializedUser.isImmortal());
}

/**
* <p>Test for {@link Deserializers#JSON} with a generic type.</p>
*
* @since 1.3.3
*/
@Test
public final void testDeserializeJsonToGenericType() {

Robolectric.getFakeHttpLayer().interceptHttpRequests(false);

String subpath = "/jsonarray";

User user1 = new User(0, "Tenzen0", "Yakushiji0", 300, true);
User user2 = new User(1, "Tenzen1", "Yakushiji1", 300, true);

stubFor(get(urlEqualTo(subpath))
.willReturn(aResponse()
.withStatus(200)
.withBody(new Gson().toJson(Arrays.asList(user1, user2)))));

List<User> deserializedUsers = deserializerEndpoint.deserializeJsonToGenericType();

verify(getRequestedFor(urlEqualTo(subpath)));

for (int i = 0; i < deserializedUsers.size(); i++) {

User user = deserializedUsers.get(i);

assertEquals(i, user.getId());
assertEquals("Tenzen" + String.valueOf(i), user.getFirstName());
assertEquals("Yakushiji" + String.valueOf(i), user.getLastName());
assertEquals(300, user.getAge());
assertEquals(true, user.isImmortal());
}
}

/**
* <p>Test for {@link Deserializers#XML}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import static com.lonepulse.robozombie.annotation.Entity.ContentType.PLAIN;
import static com.lonepulse.robozombie.annotation.Entity.ContentType.XML;

import java.util.List;

import com.google.gson.Gson;
import com.lonepulse.robozombie.annotation.Detach;
import com.lonepulse.robozombie.annotation.Endpoint;
Expand Down Expand Up @@ -62,6 +64,17 @@ public interface SerializerEndpoint {
@PUT("/json")
void serializeJson(@Entity User user);

/**
* <p>A mock request which receives a generic type that is serialized to a JSON array.</p>
*
* @param users
* the list of {@link User}s to be serialized to a JSON array
*
* @since 1.3.3
*/
@PUT("/jsonarray")
void serializeGenericTypeToJson(@Entity List<User> users);

/**
* <p>A mock request which sends an XML serialized model.</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.apache.http.ParseException;
import org.apache.http.entity.SerializableEntity;
Expand Down Expand Up @@ -108,6 +110,33 @@ public final void testSerializeJson() throws ParseException, IOException {
.withRequestBody(equalTo(new Gson().toJson(user))));
}

/**
* <p>Test for {@link Serializers#JSON} with a generic type.</p>
*
* @since 1.3.0
*/
@Test
public final void testSerializeGenericTypeToJson() throws ParseException, IOException {

Robolectric.getFakeHttpLayer().interceptHttpRequests(false);

String subpath = "/jsonarray";

User user1 = new User(0, "Tenzen0", "Yakushiji0", 300, true);
User user2 = new User(1, "Tenzen1", "Yakushiji1", 300, true);

stubFor(put(urlEqualTo(subpath))
.willReturn(aResponse()
.withStatus(200)));

serializerEndpoint.serializeGenericTypeToJson(Arrays.asList(user1, user2));

List<User> users = Arrays.asList(user1, user2);

verify(putRequestedFor(urlEqualTo(subpath))
.withRequestBody(equalTo(new Gson().toJson(users, users.getClass()))));
}

/**
* <p>Test for {@link Serializers#XML}.</p>
*
Expand Down

0 comments on commit 9b71d1b

Please sign in to comment.