Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Purchase and PurchaseItem models with addition of user agent and images #62

Merged
merged 2 commits into from
Aug 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,46 +54,46 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<version>4.13</version>
<type>jar</type>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.3</version>
<version>4.4.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.17</version>
<version>3.4.6</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -114,7 +114,7 @@
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
Expand All @@ -125,7 +125,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<version>3.8.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
Expand All @@ -135,7 +135,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
Expand All @@ -153,7 +153,7 @@
<nohelp>true</nohelp>
<doclint>none</doclint>
</configuration>
<version>3.0.0</version>
<version>3.2.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -171,7 +171,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.0.0</version>
<version>3.2.0</version>
<configuration>
<stylesheetfile>${basedir}/src/main/javadoc/stylesheet.css</stylesheetfile>
<show>public</show>
Expand Down
19 changes: 19 additions & 0 deletions src/main/com/sailthru/client/SailthruUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ public static Gson createGson() {
.registerTypeHierarchyAdapter(Map.class, new NullSerializingMapTypeAdapter())
.create();
}

/**
* Add a new image entry to the images map.
*
* @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
* @param key key for the map, either "full" or "thumb"
* @param url url for the image to use
* @return a new map instance of the images parameter was null otherwise the updated map.
*/
public static Map<String, Map<String, String>> putImage(Map<String, Map<String, String>> images, String key, String url) {
if (images == null) {
images = new HashMap<String, Map<String, String>>();
}
Map<String, String> urlMap = new HashMap<String, String>();
urlMap.put("url", url);
images.put(key, urlMap);
return images;
}

}
19 changes: 5 additions & 14 deletions src/main/com/sailthru/client/params/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.google.gson.reflect.TypeToken;
import com.sailthru.client.ApiAction;
import com.sailthru.client.SailthruUtil;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
Expand All @@ -21,7 +22,7 @@ public class Content extends AbstractApiParams implements ApiParams {
protected String title;
protected String date;
protected String expire_date;
protected List tags;
protected List<String> tags;
protected Map<String, Object> vars;
protected Map<String, Map<String, String>> images;
protected List<Double> location;
Expand Down Expand Up @@ -112,22 +113,12 @@ public Content setImages(Map<String, Map<String, String>> images) {
}

public Content setFullImage(String url) {
if (images == null) {
images = new HashMap<String, Map<String, String>>();
}
Map<String, String> urlMap = new HashMap<String, String>();
urlMap.put("url", url);
images.put("full", urlMap);
this.images = SailthruUtil.putImage(this.images, "full", url);
return this;
}

public Content setThumbImage(String url) {
if (images == null) {
images = new HashMap<String, Map<String, String>>();
}
Map<String, String> urlMap = new HashMap<String, String>();
urlMap.put("url", url);
images.put("thumb", urlMap);
this.images = SailthruUtil.putImage(this.images, "thumb", url);
return this;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/com/sailthru/client/params/Purchase.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public enum Channel {app, offline, online}
@SerializedName("device_id")
protected String deviceId;

@SerializedName("user_agent")
protected String userAgent;

public Purchase setEmail(String email) {
this.email = email;
return this;
Expand Down Expand Up @@ -153,6 +156,15 @@ public Purchase setDeviceId(String deviceId) {
return this;
}

public String getUserAgent() {
return userAgent;
}

public Purchase setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}

@Override
public ApiAction getApiCall() {
return ApiAction.purchase;
Expand Down
31 changes: 30 additions & 1 deletion src/main/com/sailthru/client/params/PurchaseItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public class PurchaseItem {
protected String price;
protected String id;
protected String url;
protected List tags;
protected List<String> tags;
protected Map<String, Object> vars;
protected Map<String, Map<String, String>> images;

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

public Map<String, Map<String, String>> getImages() {
return images;
}

/*
* A map of image names to { “url” : <url> } image maps.
* Use the name “full” to denote the full-sized image, and “thumb” to denote
* the thumbnail-sized image. Other image names are not reserved.
*
* @see #setFullImage(String)
* @see #setThumbImage(String)
*/
public PurchaseItem setImages(Map<String, Map<String, String>> images) {
this.images = images;
return this;
}

public PurchaseItem setFullImage(String url) {
this.images = SailthruUtil.putImage(this.images, "full", url);
return this;
}

public PurchaseItem setThumbImage(String url) {
this.images = SailthruUtil.putImage(this.images, "thumb", url);
return this;
}


public Map<String, Object> toHashMap() {
Gson gson = SailthruUtil.createGson();
String json = gson.toJson(this);
Expand Down
19 changes: 9 additions & 10 deletions src/test/com/sailthru/client/AbstractSailthruClientTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sailthru.client;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.sailthru.client.http.SailthruHttpClient;
import com.sailthru.client.params.Send;
import org.apache.http.HttpHost;
Expand All @@ -18,17 +17,17 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.Map;

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

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

Expand Down Expand Up @@ -95,7 +94,7 @@ public void testGetLastRateLimitInfoDifferentActions() throws IOException {
doReturn(sendHttpResponse).doReturn(listHttpResponse)
.doReturn(returnHttpResponse)
.when(httpClient)
.execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
.execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());

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

doReturn(getHttpResponse).doReturn(postHttpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), any(HttpContext.class));
doReturn(getHttpResponse).doReturn(postHttpResponse).when(httpClient).execute(any(HttpHost.class), any(HttpRequest.class), (HttpContext)any());

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

private CloseableHttpResponse getMockHttpResponseWithRateLimitHeaders(int limit, int remaining, Date reset) {
Expand Down
21 changes: 20 additions & 1 deletion src/test/com/sailthru/client/SailthruUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,31 @@ public void testGson() {
@Test
public void testGsonNull() {
gson = SailthruUtil.createGson();
Map map = new HashMap();
Map<String, Object> map = new HashMap<String, Object>();
map.put("baz", null);

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

assertEquals(expected, result);
}

@Test
public void imagesMapIsUpdated() {
Map<String, Map<String, String>> map = SailthruUtil.putImage(null, "full", "https://something/full.jpg");
assertEquals(1, map.size());
assertEquals("https://something/full.jpg", map.get("full").get("url"));

map = SailthruUtil.putImage(map, "thumb", "https://something/thumb.jpg");
assertEquals(2, map.size());
assertEquals("https://something/thumb.jpg", map.get("thumb").get("url"));

map = SailthruUtil.putImage(map, "custom", "https://something/custom.jpg");
assertEquals(3, map.size());
assertEquals("https://something/custom.jpg", map.get("custom").get("url"));

map = SailthruUtil.putImage(map, "thumb", "https://something/anotherthumb.jpg");
assertEquals(3, map.size());
assertEquals("https://something/anotherthumb.jpg", map.get("thumb").get("url"));
}
}