Created
July 14, 2015 02:54
-
-
Save wischweh/ce617a006eb8cf7d8c72 to your computer and use it in GitHub Desktop.
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
package com.twobox.evaluategson_jsonapi; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import junit.framework.TestCase; | |
import java.util.Map; | |
import com.twobox.evaluategson_jsonapi.deserialize.JSONApiTypeAdapter; | |
import com.twobox.evaluategson_jsonapi.deserialize.JSONApiTypeAdapterFactory; | |
import com.twobox.evaluategson_jsonapi.model.Data; | |
import com.twobox.evaluategson_jsonapi.model.JsonAPIData; | |
import com.twobox.evaluategson_jsonapi.model.User; | |
import com.twobox.evaluategson_jsonapi.model.UsersResponse; | |
/** | |
* Created by wischweh1 on 02.07.15. | |
*/ | |
public class DeserializationTest extends TestCase { | |
public static final String USERS_JSON=" {\n" + | |
" \"data\": [\n" + | |
" {\n" + | |
" \"type\":\"users\",\n" + | |
" \"id\":\"929f2f80-e457-4045-841b-c7c2d97a2058\",\n" + | |
" \"attributes\":{\n" + | |
" \"email\":\"[email protected]\",\n" + | |
" \"token\":\"12345\"\n" + | |
" }\n" + | |
" }\n" + | |
" ]\n" + | |
" }" ; | |
public void testDefaultDeserialization() { | |
Gson gson=new GsonBuilder().create(); | |
Data[] datas=gson.fromJson(USERS_JSON, JsonAPIData.class).data; | |
assertEquals(1,datas.length); | |
Data data=datas[0]; | |
assertEquals("users",data.type); | |
assertEquals("929f2f80-e457-4045-841b-c7c2d97a2058",data.id); | |
Map<String,String> attributes=data.attributes; | |
assertEquals(2,attributes.keySet().size()); | |
assertEquals("[email protected]",attributes.get("email")); | |
assertEquals("12345",attributes.get("token")); | |
} | |
public void testGenericJSONApiDeserialization() { | |
Gson gson=new GsonBuilder().registerTypeAdapter(User.class,new JSONApiTypeAdapter<User>(User.class)).create(); | |
UsersResponse parsedResponse=gson.fromJson(USERS_JSON,UsersResponse.class); | |
assertEquals(1,parsedResponse.users.length); | |
User user=parsedResponse.users[0]; | |
assertNotNull(user); | |
assertEquals("929f2f80-e457-4045-841b-c7c2d97a2058",user.id); | |
assertEquals("[email protected]",user.email); | |
assertEquals("12345",user.token); | |
} | |
public void testCustomizedTypeAdapterDeserialization() { | |
Gson gson=new GsonBuilder().registerTypeAdapterFactory(new JSONApiTypeAdapterFactory<User>(User.class)).create(); | |
UsersResponse parsedResponse=gson.fromJson(USERS_JSON,UsersResponse.class); | |
assertEquals(1,parsedResponse.users.length); | |
User user=parsedResponse.users[0]; | |
assertNotNull(user); | |
assertEquals("929f2f80-e457-4045-841b-c7c2d97a2058",user.id); | |
assertEquals("[email protected]",user.email); | |
assertEquals("12345",user.token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment