Skip to content

Commit a4f1f0c

Browse files
author
Brandon Wong
authored
Merge pull request #62 from NewAmsterdamLabs/master
Improve Purchase and PurchaseItem models with addition of user agent and images
2 parents e81dbc6 + c3083f4 commit a4f1f0c

7 files changed

Lines changed: 108 additions & 39 deletions

File tree

pom.xml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,46 @@
5454
<dependency>
5555
<groupId>junit</groupId>
5656
<artifactId>junit</artifactId>
57-
<version>4.9</version>
57+
<version>4.13</version>
5858
<type>jar</type>
5959
<scope>test</scope>
6060
<optional>true</optional>
6161
</dependency>
6262
<dependency>
6363
<groupId>com.google.code.gson</groupId>
6464
<artifactId>gson</artifactId>
65-
<version>2.2.4</version>
65+
<version>2.8.6</version>
6666
<scope>compile</scope>
6767
</dependency>
6868
<dependency>
6969
<groupId>org.apache.httpcomponents</groupId>
7070
<artifactId>httpclient</artifactId>
71-
<version>4.3.6</version>
71+
<version>4.5.12</version>
7272
</dependency>
7373
<dependency>
7474
<groupId>org.apache.httpcomponents</groupId>
7575
<artifactId>httpcore</artifactId>
76-
<version>4.3</version>
76+
<version>4.4.13</version>
7777
</dependency>
7878
<dependency>
7979
<groupId>org.apache.httpcomponents</groupId>
8080
<artifactId>httpmime</artifactId>
81-
<version>4.3.1</version>
81+
<version>4.5.12</version>
8282
</dependency>
8383
<dependency>
8484
<groupId>org.slf4j</groupId>
8585
<artifactId>slf4j-api</artifactId>
86-
<version>1.7.5</version>
86+
<version>1.7.30</version>
8787
</dependency>
8888
<dependency>
8989
<groupId>org.slf4j</groupId>
9090
<artifactId>slf4j-simple</artifactId>
91-
<version>1.7.5</version>
91+
<version>1.7.30</version>
9292
</dependency>
9393
<dependency>
9494
<groupId>org.mockito</groupId>
9595
<artifactId>mockito-core</artifactId>
96-
<version>1.10.17</version>
96+
<version>3.4.6</version>
9797
<scope>test</scope>
9898
</dependency>
9999
<dependency>
@@ -114,7 +114,7 @@
114114
<plugin>
115115
<groupId>org.sonatype.plugins</groupId>
116116
<artifactId>nexus-staging-maven-plugin</artifactId>
117-
<version>1.6.3</version>
117+
<version>1.6.8</version>
118118
<extensions>true</extensions>
119119
<configuration>
120120
<serverId>sonatype-nexus-staging</serverId>
@@ -125,7 +125,7 @@
125125
<plugin>
126126
<groupId>org.apache.maven.plugins</groupId>
127127
<artifactId>maven-compiler-plugin</artifactId>
128-
<version>2.3.2</version>
128+
<version>3.8.1</version>
129129
<configuration>
130130
<source>1.6</source>
131131
<target>1.6</target>
@@ -135,7 +135,7 @@
135135
<plugin>
136136
<groupId>org.apache.maven.plugins</groupId>
137137
<artifactId>maven-source-plugin</artifactId>
138-
<version>2.2.1</version>
138+
<version>3.2.1</version>
139139
<executions>
140140
<execution>
141141
<id>attach-sources</id>
@@ -153,7 +153,7 @@
153153
<nohelp>true</nohelp>
154154
<doclint>none</doclint>
155155
</configuration>
156-
<version>3.0.0</version>
156+
<version>3.2.0</version>
157157
<executions>
158158
<execution>
159159
<id>attach-javadocs</id>
@@ -171,7 +171,7 @@
171171
<plugin>
172172
<groupId>org.apache.maven.plugins</groupId>
173173
<artifactId>maven-javadoc-plugin</artifactId>
174-
<version>3.0.0</version>
174+
<version>3.2.0</version>
175175
<configuration>
176176
<stylesheetfile>${basedir}/src/main/javadoc/stylesheet.css</stylesheetfile>
177177
<show>public</show>

src/main/com/sailthru/client/SailthruUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,23 @@ public static Gson createGson() {
5454
.registerTypeHierarchyAdapter(Map.class, new NullSerializingMapTypeAdapter())
5555
.create();
5656
}
57+
58+
/**
59+
* Add a new image entry to the images map.
60+
*
61+
* @param images map containing the references of images. Can be null, in that case the returned map will be a new instance with the entry
62+
* @param key key for the map, either "full" or "thumb"
63+
* @param url url for the image to use
64+
* @return a new map instance of the images parameter was null otherwise the updated map.
65+
*/
66+
public static Map<String, Map<String, String>> putImage(Map<String, Map<String, String>> images, String key, String url) {
67+
if (images == null) {
68+
images = new HashMap<String, Map<String, String>>();
69+
}
70+
Map<String, String> urlMap = new HashMap<String, String>();
71+
urlMap.put("url", url);
72+
images.put(key, urlMap);
73+
return images;
74+
}
75+
5776
}

src/main/com/sailthru/client/params/Content.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import com.google.gson.reflect.TypeToken;
44
import com.sailthru.client.ApiAction;
5+
import com.sailthru.client.SailthruUtil;
6+
57
import java.lang.reflect.Type;
68
import java.util.Date;
7-
import java.util.HashMap;
89
import java.util.Map;
910
import java.util.List;
1011
import java.util.ArrayList;
@@ -21,7 +22,7 @@ public class Content extends AbstractApiParams implements ApiParams {
2122
protected String title;
2223
protected String date;
2324
protected String expire_date;
24-
protected List tags;
25+
protected List<String> tags;
2526
protected Map<String, Object> vars;
2627
protected Map<String, Map<String, String>> images;
2728
protected List<Double> location;
@@ -112,22 +113,12 @@ public Content setImages(Map<String, Map<String, String>> images) {
112113
}
113114

114115
public Content setFullImage(String url) {
115-
if (images == null) {
116-
images = new HashMap<String, Map<String, String>>();
117-
}
118-
Map<String, String> urlMap = new HashMap<String, String>();
119-
urlMap.put("url", url);
120-
images.put("full", urlMap);
116+
this.images = SailthruUtil.putImage(this.images, "full", url);
121117
return this;
122118
}
123119

124120
public Content setThumbImage(String url) {
125-
if (images == null) {
126-
images = new HashMap<String, Map<String, String>>();
127-
}
128-
Map<String, String> urlMap = new HashMap<String, String>();
129-
urlMap.put("url", url);
130-
images.put("thumb", urlMap);
121+
this.images = SailthruUtil.putImage(this.images, "thumb", url);
131122
return this;
132123
}
133124

src/main/com/sailthru/client/params/Purchase.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public enum Channel {app, offline, online}
4848
@SerializedName("device_id")
4949
protected String deviceId;
5050

51+
@SerializedName("user_agent")
52+
protected String userAgent;
53+
5154
public Purchase setEmail(String email) {
5255
this.email = email;
5356
return this;
@@ -153,6 +156,15 @@ public Purchase setDeviceId(String deviceId) {
153156
return this;
154157
}
155158

159+
public String getUserAgent() {
160+
return userAgent;
161+
}
162+
163+
public Purchase setUserAgent(String userAgent) {
164+
this.userAgent = userAgent;
165+
return this;
166+
}
167+
156168
@Override
157169
public ApiAction getApiCall() {
158170
return ApiAction.purchase;

src/main/com/sailthru/client/params/PurchaseItem.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ public class PurchaseItem {
1717
protected String price;
1818
protected String id;
1919
protected String url;
20-
protected List tags;
20+
protected List<String> tags;
2121
protected Map<String, Object> vars;
22+
protected Map<String, Map<String, String>> images;
2223

2324
public PurchaseItem(Integer qty, String title, Integer price, String id, String url) {
2425
this.qty = qty.toString();
@@ -38,6 +39,34 @@ public PurchaseItem setVars(Map<String, Object> vars) {
3839
return this;
3940
}
4041

42+
public Map<String, Map<String, String>> getImages() {
43+
return images;
44+
}
45+
46+
/*
47+
* A map of image names to { “url” : <url> } image maps.
48+
* Use the name “full” to denote the full-sized image, and “thumb” to denote
49+
* the thumbnail-sized image. Other image names are not reserved.
50+
*
51+
* @see #setFullImage(String)
52+
* @see #setThumbImage(String)
53+
*/
54+
public PurchaseItem setImages(Map<String, Map<String, String>> images) {
55+
this.images = images;
56+
return this;
57+
}
58+
59+
public PurchaseItem setFullImage(String url) {
60+
this.images = SailthruUtil.putImage(this.images, "full", url);
61+
return this;
62+
}
63+
64+
public PurchaseItem setThumbImage(String url) {
65+
this.images = SailthruUtil.putImage(this.images, "thumb", url);
66+
return this;
67+
}
68+
69+
4170
public Map<String, Object> toHashMap() {
4271
Gson gson = SailthruUtil.createGson();
4372
String json = gson.toJson(this);

src/test/com/sailthru/client/AbstractSailthruClientTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sailthru.client;
22

33
import com.google.common.collect.ImmutableMap;
4-
import com.google.common.collect.Lists;
54
import com.sailthru.client.http.SailthruHttpClient;
65
import com.sailthru.client.params.Send;
76
import org.apache.http.HttpHost;
@@ -18,17 +17,17 @@
1817
import org.junit.Before;
1918
import org.junit.Test;
2019
import org.junit.runner.RunWith;
21-
import org.mockito.runners.MockitoJUnitRunner;
20+
import org.mockito.junit.MockitoJUnitRunner;
2221

2322
import java.io.ByteArrayInputStream;
2423
import java.io.IOException;
2524
import java.util.Collections;
2625
import java.util.Date;
2726
import java.util.Map;
2827

29-
import static junit.framework.Assert.assertEquals;
30-
import static org.mockito.Matchers.any;
31-
import static org.mockito.Matchers.eq;
28+
import static org.junit.Assert.assertEquals;
29+
import static org.mockito.ArgumentMatchers.any;
30+
import static org.mockito.ArgumentMatchers.eq;
3231
import static org.mockito.Mockito.doReturn;
3332
import static org.mockito.Mockito.mock;
3433
import static org.mockito.Mockito.spy;
@@ -59,7 +58,7 @@ public void testGetLastRateLimitInfo() throws IOException {
5958
long resetTs = ((new Date().getTime() / 1000) * 1000) + 18000; // pretend the top of the next minute is 18 seconds from now
6059
Date resetDate = new Date(resetTs);
6160
CloseableHttpResponse httpResponse = getMockHttpResponseWithRateLimitHeaders(limit, remaining, resetDate);
62-
doReturn(httpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
61+
doReturn(httpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());
6362

6463
sailthruClient.apiGet(ApiAction.send, ImmutableMap.<String,Object>of(Send.PARAM_SEND_ID, "some valid send id"));
6564

@@ -95,7 +94,7 @@ public void testGetLastRateLimitInfoDifferentActions() throws IOException {
9594
doReturn(sendHttpResponse).doReturn(listHttpResponse)
9695
.doReturn(returnHttpResponse)
9796
.when(httpClient)
98-
.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
97+
.execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());
9998

10099
sailthruClient.apiGet(ApiAction.send, ImmutableMap.<String,Object>of(Send.PARAM_SEND_ID, "some valid send id"));
101100
sailthruClient.apiGet(ApiAction.list, ImmutableMap.<String,Object>of("list", "some list"));
@@ -133,7 +132,7 @@ public void testGetLastRateLimitInfoDifferentMethods() throws IOException {
133132
Date postResetDate = new Date(postResetTs);
134133
CloseableHttpResponse postHttpResponse = getMockHttpResponseWithRateLimitHeaders(postLimit, postRemaining, postResetDate);
135134

136-
doReturn(getHttpResponse).doReturn(postHttpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
135+
doReturn(getHttpResponse).doReturn(postHttpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());
137136

138137
sailthruClient.apiGet(ApiAction.list, ImmutableMap.<String,Object>of("list", "some list"));
139138
sailthruClient.apiPost(ApiAction.list, ImmutableMap.<String,Object>of("list", "some new list"));
@@ -152,10 +151,10 @@ public void testGetLastRateLimitInfoDifferentMethods() throws IOException {
152151
@Test
153152
public void testReturnUrl() throws IOException {
154153
CloseableHttpResponse response = getMockHttpResponseWithRateLimitHeaders(1, 1, new Date());
155-
doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
154+
doReturn(response).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());
156155
sailthruClient.apiPost(ApiAction.RETURN, Collections.<String, Object>emptyMap());
157156
verify(httpClient).executeHttpRequest(eq("https://api.sailthru.com/return"), eq(AbstractSailthruClient.HttpRequestMethod.POST),
158-
any(Map.class), any(ResponseHandler.class), any(Map.class));
157+
any(Map.class), any(ResponseHandler.class), (Map)any());
159158
}
160159

161160
private CloseableHttpResponse getMockHttpResponseWithRateLimitHeaders(int limit, int remaining, Date reset) {

src/test/com/sailthru/client/SailthruUtilTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,31 @@ public void testGson() {
7777
@Test
7878
public void testGsonNull() {
7979
gson = SailthruUtil.createGson();
80-
Map map = new HashMap();
80+
Map<String, Object> map = new HashMap<String, Object>();
8181
map.put("baz", null);
8282

8383
String expected = "{\"baz\":null}";
8484
String result = gson.toJson(map);
8585

8686
assertEquals(expected, result);
8787
}
88+
89+
@Test
90+
public void imagesMapIsUpdated() {
91+
Map<String, Map<String, String>> map = SailthruUtil.putImage(null, "full", "https://something/full.jpg");
92+
assertEquals(1, map.size());
93+
assertEquals("https://something/full.jpg", map.get("full").get("url"));
94+
95+
map = SailthruUtil.putImage(map, "thumb", "https://something/thumb.jpg");
96+
assertEquals(2, map.size());
97+
assertEquals("https://something/thumb.jpg", map.get("thumb").get("url"));
98+
99+
map = SailthruUtil.putImage(map, "custom", "https://something/custom.jpg");
100+
assertEquals(3, map.size());
101+
assertEquals("https://something/custom.jpg", map.get("custom").get("url"));
102+
103+
map = SailthruUtil.putImage(map, "thumb", "https://something/anotherthumb.jpg");
104+
assertEquals(3, map.size());
105+
assertEquals("https://something/anotherthumb.jpg", map.get("thumb").get("url"));
106+
}
88107
}

0 commit comments

Comments
 (0)