Permalink
Browse files

added tests for http and uri builder util classes. created testng con…

…fig. updated readme
  • Loading branch information...
1 parent 46034dd commit 99cd7e4175ebb57b946be3c9c0eab0a7842e8363 @nitindhar7 committed Jan 22, 2012
View
@@ -9,14 +9,14 @@ Android platform to be tested soon!
Build & Use
-----------
-Please check [Gilt API documentation](http://api.gilt.com) for the official API definitions and descriptions.
+Please check [Gilt API documentation](http://api.gilt.com) for the official API descriptions and to get your private API key (required for use).
To build:
$ ant
Examples:
- GiltAPI gilt = new GiltAPI();
+ GiltAPI gilt = new GiltAPI(API_KEY);
// Get active sales for all Gilt stores
gilt.getSales(null, Sale.ACTIVE, null);
@@ -41,7 +41,7 @@ Gilt API Endpoints
--------------------
Currently there is 1 API endpoint available with the base url being `https://api.gilt.com/v1/`.
-- ** sales **
+- sales
- Retrieve Gilt sales
- `https://api.gilt.com/v1/sales`
- `String getSales(String store, String action, Map<String,String> params)`
@@ -29,5 +29,9 @@ public String getSales(String store, String action, Map<String,String> params) {
return HttpUtil.get(uri);
}
+
+ public String getAPIKey() {
+ return apiKey;
+ }
}
@@ -5,36 +5,43 @@
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLConnection;
import javax.net.ssl.HttpsURLConnection;
public class HttpUtil {
- public static String get(String url) {
- String response = null;
- HttpsURLConnection conn = null;
+ private static final String ENCODING = "UTF-8";
+
+ public static String get(String endpointUrl) {
+ StringBuilder sb = null;
+ URLConnection urlConn = null;
try {
- conn = (HttpsURLConnection) (new URL(url)).openConnection();
- conn.connect();
-
- BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- StringBuilder sb = new StringBuilder();
+ sb = new StringBuilder();
+ URL url = new URL(endpointUrl);
+
+ if (url.getProtocol().toLowerCase().equals("https")) {
+ urlConn = (HttpsURLConnection) url.openConnection();
+ } else {
+ urlConn = url.openConnection();
+ }
- for (String line = in.readLine(); line != null; line = in.readLine()) {
+ BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), ENCODING));
+
+ String line;
+ while ((line = in.readLine()) != null) {
sb.append(line);
}
-
- response = sb.toString();
+
+ in.close();
} catch (MalformedURLException e) {
throw new RuntimeException("Error connecting to url", e);
} catch (IOException e) {
throw new RuntimeException("Error reading from url", e);
- } finally {
- conn.disconnect();
}
- return response;
+ return sb.toString();
}
}
@@ -54,9 +54,9 @@ public String build() {
if(action != null)
sb.append(action).append(".json").append("?");
if(apikey != null)
- sb.append("apikey=").append(apikey).append("&");
+ sb.append("apikey=").append(apikey);
if(params != null)
- sb.append(params);
+ sb.append("&").append(params);
return sb.toString();
}
@@ -0,0 +1,19 @@
+package com.giltjava.util;
+
+import java.net.URL;
+
+import junit.framework.TestCase;
+
+import org.testng.annotations.Test;
+
+public class HttpUtilTests {
+
+ @Test (groups={"ready"})
+ public void testGet() {
+ URL url = getClass().getClassLoader().getResource("sales_without_options.json");
+ String json = HttpUtil.get(url.toString());
+ TestCase.assertNotNull(json);
+ TestCase.assertEquals(861, json.length());
+ }
+
+}
@@ -0,0 +1,64 @@
+package com.giltjava.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.testng.annotations.Test;
+
+public class URIBuilderTests {
+
+ @Test (groups={"ready"})
+ public void testBuild() {
+ String resource = "sales";
+ String store = "kids";
+ String action = "active";
+ String apikey = "abc";
+ String params = null;
+
+ URIBuilder builder = new URIBuilder();
+ builder.resource(resource);
+ builder.store(store);
+ builder.action(action);
+ builder.apikey(apikey);
+ builder.params(params);
+ TestCase.assertEquals("https://api.gilt.com/v1/sales/kids/active.json?apikey=abc", builder.build());
+
+ params = "product_detail=true";
+ builder = new URIBuilder();
+ builder.resource(resource);
+ builder.store(store);
+ builder.action(action);
+ builder.apikey(apikey);
+ builder.params(params);
+ TestCase.assertEquals("https://api.gilt.com/v1/sales/kids/active.json?apikey=abc&product_detail=true", builder.build());
+
+ store = null;
+ builder = new URIBuilder();
+ builder.resource(resource);
+ builder.store(store);
+ builder.action(action);
+ builder.apikey(apikey);
+ builder.params(params);
+ TestCase.assertEquals("https://api.gilt.com/v1/sales/active.json?apikey=abc&product_detail=true", builder.build());
+
+ action = null;
+ builder = new URIBuilder();
+ builder.resource(resource);
+ builder.store(store);
+ builder.action(action);
+ builder.apikey(apikey);
+ builder.params(params);
+ TestCase.assertEquals("https://api.gilt.com/v1/sales/apikey=abc&product_detail=true", builder.build());
+ }
+
+ @Test (groups={"ready"})
+ public void testParametrizeMap() {
+ Map<String,String> params = new HashMap<String,String>();
+ params.put("product_detail", "true");
+ params.put("abc", "def");
+ TestCase.assertEquals("abc=def&product_detail=true", URIBuilder.parametrizeMap(params));
+ }
+
+}
@@ -0,0 +1 @@
+[{"name":"CitySmart:SlimTrousers,Blazers&Boots","sale_url":"http://www.gilt.com/sale/men/city-smart-slim-trou","image_urls":["http://cdn1.gilt.com/images/share/uploads/0000/0001/3427/134273291/orig.jpg"],"begins":"2012-01-21T17:00:00Z","ends":"2012-01-23T05:00:00Z","description":"Callitaone-stopshopforthediscerningurbanite.Thissalehaseverythingyouneedtolookyourbestonadayoutinthecity—fromfittedsportcoatsandblazerstotrimtrousersandbootsbettersuitedtothesidewalkthanthebackwoods.Grabsomethinggreat,andtakeonthetowninstyle."},{"name":"ThePeninsulaNewYorkHotel","sale_url":"http://www.jetsetter.com/hotels/new-york/new-york/24/the-peninsula-new-york?oaff=20120121thepeninsulanewyork&plt=mosaic&rr=giltman","image_urls":["http://cdn1.gilt.com/images/share/uploads/0000/0001/3433/134337987/orig.jpg"],"begins":"2012-01-21T17:00:00Z","ends":"2012-01-23T05:00:00Z"}]
@@ -11,6 +11,7 @@
</groups>
<packages>
<package name="com.giltjava"/>
+ <package name="com.giltjava.util"/>
</packages>
</test>
</suite>

0 comments on commit 99cd7e4

Please sign in to comment.