Skip to content

Commit

Permalink
Merge 8121ec5 into abe5aa4
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon6193 committed Jul 16, 2018
2 parents abe5aa4 + 8121ec5 commit f6d9dc0
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 65 deletions.
Expand Up @@ -45,7 +45,6 @@ PostInstallLink getPendingLink(String applicationId, String ifa,

@Nullable
@WorkerThread
Void postActivity(String applicationId, String sourceToken, String ifa,
boolean limitAdTrackingEnabled, String timestamp,
Order order) throws ButtonNetworkException;
Void postActivity(String applicationId, String sourceToken, String timestamp, Order order)
throws ButtonNetworkException;
}
Expand Up @@ -177,35 +177,23 @@ private void initializeUrlConnection(HttpURLConnection urlConnection) throws Pro
}

@Override
public Void postActivity(String applicationId, String sourceToken, String ifa,
boolean limitAdTrackingEnabled,
String timestamp, Order order)
throws ButtonNetworkException {
public Void postActivity(String applicationId, String sourceToken, String timestamp,
Order order) throws ButtonNetworkException {
HttpURLConnection urlConnection = null;

try {
// create order json object
JSONObject orderJson = new JSONObject();
orderJson.put("order_id", order.getId());
orderJson.put("amount", order.getAmount());
orderJson.put("currency_code", order.getCurrencyCode());

// create device json object
JSONObject deviceJson = new JSONObject();
deviceJson.put("ifa", ifa);
deviceJson.put("ifa_limited", limitAdTrackingEnabled);

// create request body
JSONObject requestBody = new JSONObject();
requestBody.put("application_id", applicationId);
requestBody.put("device", deviceJson);
requestBody.put("app_id", applicationId);
requestBody.put("user_local_time", timestamp);
requestBody.put("type", "order-checkout");
requestBody.put("btn_ref", sourceToken);
requestBody.put("order", orderJson);
requestBody.put("order_id", order.getId());
requestBody.put("total", order.getAmount());
requestBody.put("currency", order.getCurrencyCode());
requestBody.put("source", "merchant-library");

// setup url connection
final URL url = new URL(baseUrl + "/v2/session/useractivity");
final URL url = new URL(baseUrl + "/v1/activity/order");
urlConnection = (HttpURLConnection) url.openConnection();
initializeUrlConnection(urlConnection);

Expand Down
Expand Up @@ -267,7 +267,6 @@ private PackageInfo getPackageInfo() {
String packageName = context.getPackageName();
PackageManager packageManager = context.getPackageManager();
return packageManager.getPackageInfo(packageName, 0);

} catch (PackageManager.NameNotFoundException ignored) {

}
Expand Down
Expand Up @@ -49,7 +49,6 @@ public void run() {
if (listener != null) {
listener.onTaskComplete(object);
}

} catch (Exception e) {
if (listener != null) {
listener.onTaskError(e);
Expand All @@ -67,6 +66,7 @@ public void run() {
*/
interface Listener<T> {
void onTaskComplete(@Nullable T object);

void onTaskError(Throwable throwable);
}
}
Expand Up @@ -52,7 +52,7 @@ class UserActivityTask extends Task<Void> {
@Nullable
@Override
Void execute() throws Exception {
return buttonApi.postActivity(applicationId, sourceToken, deviceManager.getAdvertisingId(),
deviceManager.isLimitAdTrackingEnabled(), deviceManager.getTimeStamp(), order);
return buttonApi.postActivity(applicationId, sourceToken, deviceManager.getTimeStamp(),
order);
}
}
Expand Up @@ -123,15 +123,17 @@ public void getPendingLink_validateRequest() throws Exception {
}

@Test(expected = ButtonNetworkException.class)
public void getPendingLink_returnErrorResponseStatusCodeOver400_catchException() throws Exception {
public void getPendingLink_returnErrorResponseStatusCodeOver400_catchException()
throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
return new MockResponse().setResponseCode(404);
}
});

buttonApi.getPendingLink("valid_application_id", "valid_ifa", true, Collections.<String, String>emptyMap());
buttonApi.getPendingLink("valid_application_id", "valid_ifa", true,
Collections.<String, String>emptyMap());
}

@Test(expected = ButtonNetworkException.class)
Expand All @@ -154,7 +156,8 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
}

@Test(expected = ButtonNetworkException.class)
public void postUserActivity_returnErrorResponseStatusCodeOver400_catchException() throws Exception {
public void postUserActivity_returnErrorResponseStatusCodeOver400_catchException()
throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
Expand All @@ -163,16 +166,15 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
});

Order order = new Order.Builder("123").setCurrencyCode("AUG").build();
buttonApi.postActivity("valid_application_id", "valid_aid",
"valid_ifa", true, "valid_ts", order);
buttonApi.postActivity("valid_application_id", "valid_aid", "valid_ts", order);
}

@Test(expected = ButtonNetworkException.class)
public void postUserActivity_returnErrorMessage() throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/v2/session/useractivity") && request.getMethod()
if (request.getPath().equals("/v1/activity/order") && request.getMethod()
.equals("POST")) {
return new MockResponse()
.setResponseCode(200)
Expand All @@ -184,8 +186,7 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
});

Order order = new Order.Builder("123").setCurrencyCode("AUG").build();
buttonApi.postActivity("valid_application_id", "valid_aid",
"valid_ifa", true, "valid_ts", order);
buttonApi.postActivity("valid_application_id", "valid_aid", "valid_ts", order);
}

@Test
Expand All @@ -194,16 +195,12 @@ public void postUserActivity_validateRequest() throws Exception {
.setBody("{\"meta\":{\"status\":\"ok\"}}\n"));

Order order = new Order.Builder("123").setAmount(999).setCurrencyCode("AUG").build();
buttonApi.postActivity("valid_application_id", "valid_aid",
"valid_ifa", true, "valid_ts", order);
buttonApi.postActivity("valid_application_id", "valid_aid", "valid_ts", order);

RecordedRequest recordedRequest = server.takeRequest();
Headers headers = recordedRequest.getHeaders();
String body = recordedRequest.getBody().readUtf8();
JSONObject bodyJson = new JSONObject(body);
JSONObject orderJson = bodyJson.getJSONObject("order");
JSONObject deviceJson = bodyJson.getJSONObject("device");

JSONObject requestBodyJson = new JSONObject(body);
// method
assertEquals("POST", recordedRequest.getMethod());

Expand All @@ -213,27 +210,21 @@ public void postUserActivity_validateRequest() throws Exception {
assertEquals("application/json", headers.get("Content-Type"));

// request body
assertEquals("valid_application_id", bodyJson.getString("application_id"));
assertEquals("valid_aid", bodyJson.getString("btn_ref"));
assertEquals("order-checkout", bodyJson.getString("type"));
assertEquals("valid_ts", bodyJson.getString("user_local_time"));

// order body
assertEquals("123", orderJson.getString("order_id"));
assertEquals(999, orderJson.getLong("amount"));
assertEquals("AUG", orderJson.getString("currency_code"));

// device body
assertEquals("valid_ifa", deviceJson.getString("ifa"));
assertEquals(true, deviceJson.getBoolean("ifa_limited"));
assertEquals("valid_application_id", requestBodyJson.getString("app_id"));
assertEquals("valid_ts", requestBodyJson.getString("user_local_time"));
assertEquals("valid_aid", requestBodyJson.getString("btn_ref"));
assertEquals("123", requestBodyJson.getString("order_id"));
assertEquals(999, requestBodyJson.getLong("total"));
assertEquals("AUG", requestBodyJson.getString("currency"));
assertEquals("merchant-library", requestBodyJson.getString("source"));
}

@Test(expected = ButtonNetworkException.class)
public void postUserActivity_returnInvalidJson_catchException() throws Exception {
server.setDispatcher(new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/v2/session/useractivity") && request.getMethod()
if (request.getPath().equals("/v1/activity/order") && request.getMethod()
.equals("POST")) {
return new MockResponse()
.setResponseCode(200)
Expand All @@ -244,7 +235,6 @@ public MockResponse dispatch(RecordedRequest request) throws InterruptedExceptio
});

Order order = new Order.Builder("123").setCurrencyCode("AUG").build();
buttonApi.postActivity("valid_application_id", "valid_aid",
"valid_ifa", true, "valid_ts", order);
buttonApi.postActivity("valid_application_id", "valid_aid", "valid_ts", order);
}
}
Expand Up @@ -209,7 +209,9 @@ public void getUserAgent_verifyUserAgent() throws Exception {
String sdkVersionName = BuildConfig.VERSION_NAME;
int sdkVersionCode = BuildConfig.VERSION_CODE;

String expectedUserAgent = String.format("com.usebutton.merchant/%s+%d (Android null; null null; com.usebutton.app/1.1.0+11; Scale/2.0; en_us)", sdkVersionName, sdkVersionCode);
String expectedUserAgent = String.format(
"com.usebutton.merchant/%s+%d (Android null; null null; com.usebutton.app/1.1.0+11; Scale/2.0; en_us)",
sdkVersionName, sdkVersionCode);
assertEquals(expectedUserAgent, userAgent);
}
}
Expand Up @@ -55,7 +55,8 @@ public class PersistenceManagerImplTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(context.getSharedPreferences("button_shared_preferences", Context.MODE_PRIVATE)).thenReturn(sharedPreferences);
when(context.getSharedPreferences("button_shared_preferences",
Context.MODE_PRIVATE)).thenReturn(sharedPreferences);
when(sharedPreferences.edit()).thenReturn(editor);
when(editor.putString(anyString(), anyString())).thenReturn(editor);
when(editor.putBoolean(anyString(), anyBoolean())).thenReturn(editor);
Expand Down Expand Up @@ -90,7 +91,8 @@ public void clear_clearSharedPrefs() {

@Test
public void checkedDeferredDeepLink_returnTrue() {
when(sharedPreferences.getBoolean(PersistenceManagerImpl.Key.CHECKED_DEFERRED_DEEP_LINK, false)).thenReturn(true);
when(sharedPreferences.getBoolean(PersistenceManagerImpl.Key.CHECKED_DEFERRED_DEEP_LINK,
false)).thenReturn(true);
boolean checkedDeferredDeepLink = persistenceManager.checkedDeferredDeepLink();
assertEquals(true, checkedDeferredDeepLink);
}
Expand Down
Expand Up @@ -70,5 +70,4 @@ String execute() throws Exception {
verify(listener).onTaskError(any(RuntimeException.class));
verifyNoMoreInteractions(listener);
}

}
Expand Up @@ -59,12 +59,9 @@ public void setUp() {

@Test
public void execute_verifyApiCall() throws Exception {
when(deviceManager.getAdvertisingId()).thenReturn("valid_ifa");
when(deviceManager.isLimitAdTrackingEnabled()).thenReturn(true);
when(deviceManager.getTimeStamp()).thenReturn("valid_ts");

task.execute();
verify(buttonApi).postActivity(applicationId, sourceToken, "valid_ifa",
true, "valid_ts", order);
verify(buttonApi).postActivity(applicationId, sourceToken, "valid_ts", order);
}
}

0 comments on commit f6d9dc0

Please sign in to comment.