package com.shopify.api.resources;

import java.util.List;

import android.test.InstrumentationTestCase;

import com.shopify.api.resources.json.ShopifyRequestWriter;
import com.shopify.api.resources.json.ShopifyResponseReader;
import com.shopify.assets.AssetLoader;

public class ArticleTest extends InstrumentationTestCase {
	private ShopifyResponseReader reader;
	private ShopifyRequestWriter writer;
	
	public void setUp() throws Exception {
		super.setUp();
		AssetLoader.instrumentation = getInstrumentation();
		reader = new ShopifyResponseReader();
		writer = new ShopifyRequestWriter();
		
	}
	
	public void testLoadingSingleAsset() throws Exception {
		String articleJson = AssetLoader.loadAsset("fixtures/Article/article.json");
		
		Article result = reader.read(articleJson, Article.class).get(0);
		{
			assertEquals("2008-07-31T20:00:00-04:00", result.getCreated_at());
			assertEquals("<p>Do <em>you</em> have an <strong>IPod</strong> yet?</p>", result.getBody_html());
			assertEquals("get on the train now", result.getTitle());
			assertEquals("Dennis", result.getAuthor());
			assertEquals("2008-07-31T20:00:00-04:00", result.getUpdated_at());
			assertEquals(null, result.getSummary_html());
			assertEquals(241253187, result.getBlog_id());
			assertEquals("Announcing", result.getTags());
			assertEquals(134645308, result.getId());
			assertEquals(799407056, result.getUser_id());
			assertEquals("2008-07-31T20:00:00-04:00", result.getPublished_at());
		}
	}
	
	public void testLoadingSeveralAssets() throws Exception {
		String articlesJson = AssetLoader.loadAsset("fixtures/Article/articles.json");
		
		List<Article> articles = reader.read(articlesJson, Article.class);
		
		assertEquals(2, articles.size());
		
		for(Article a : articles) {
			switch(a.getId()) {
			case 989034056:
				assertEquals("John", a.getAuthor());
				assertEquals("Some crazy article I'm coming up with", a.getTitle());
				break;
			case 134645308:
				assertEquals("Dennis", a.getAuthor());
				assertEquals("Announcing", a.getTags());
				break;
			default:
				fail("Encountered unexpected article " + a.getId());
			}
		}
	}

}

@@ -40,191 +40,131 @@
 ort 
-java.util.List;%0A%0Aimport android.test.InstrumentationTestCas
+org.codehaus.jackson.JsonNod
 e;%0A
-%0A
 import 
-com.shopify.api.resources.json.ShopifyRequestWrit
+org.codehaus.jackson.map.ObjectMapp
 er;%0A
+%0A
 import 
-com.shopify.api.resources.json.ShopifyResponseReader;
+android.test.InstrumentationTestCase;%0A
 %0Aimp
@@ -272,72 +272,25 @@
 ate 
-ShopifyResponseReader reader;%0A%09private ShopifyRequestWriter writ
+ObjectMapper mapp
 er;%0A
@@ -408,88 +408,37 @@
 ;%0A%09%09
-reader = new ShopifyResponseReader();%0A%09%09writer = new ShopifyRequestWrit
+mapper = new ObjectMapp
 er();%0A
-%09%09%0A
 %09%7D%0A%09
@@ -574,18 +574,81 @@
 json%22);%0A
+%0A
 %09%09
+JsonNode node = mapper.readValue(articleJson, JsonNode.class);
 %0A%09%09Artic
@@ -663,31 +663,47 @@
 t = 
-read
+mapp
 er.read
-(articleJson
+Value(node.iterator().next()
 , Ar
@@ -718,15 +718,8 @@
 ass)
-.get(0)
 ;%0A%09%09
@@ -1538,21 +1538,83 @@
 %09%0A%09%09
-List%3C
+JsonNode node = mapper.readValue(articlesJson, JsonNode.class);%0A%09%09
 Article
-%3E
+%5B%5D
  art
@@ -1625,32 +1625,47 @@
 s = 
-read
+mapp
 er.read
-(articlesJson
+Value(node.iterator().next()
 , Ar
@@ -1661,32 +1661,34 @@
 .next(), Article
+%5B%5D
 .class);%0A%09%09%0A%09%09as
@@ -1714,14 +1714,14 @@
 les.
-size()
+length
 );%0A%09

Use jackson's tree model to skip the wrapper hash in JSON data.