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

fix: Add callback functionality to upload method #492

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions android-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ android {
jvmArgs += ['--add-opens', 'java.base/java.util=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.text=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.math=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.lang.reflect=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.util.concurrent=ALL-UNNAMED']
jvmArgs += ['--add-opens', 'java.base/java.lang.ref=ALL-UNNAMED']
}
Expand Down
5 changes: 5 additions & 0 deletions android-core/src/main/java/com/mparticle/MParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,11 @@ public interface ResetListener {
void onReset();
}

public interface UploadCallback {
void onSuccess();

void onFailed();
}

/**
* @hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class MessageManager implements MessageManagerCallbacks, ReportingManager
private ConfigManager mConfigManager = null;
private MParticleDBManager mMParticleDBManager;
private MParticle.OperatingSystem mOperatingSystem;
private MParticle.UploadCallback uploadCallback;


/**
Expand Down Expand Up @@ -525,6 +526,13 @@ public void startUploadLoop() {
mUploadHandler.sendMessageDelayed(mUploadHandler.obtainMessage(UploadHandler.UPLOAD_MESSAGES, mConfigManager.getMpid()), Constants.INITIAL_UPLOAD_DELAY);
}

public void doUpload(MParticle.UploadCallback callback) {
if (callback != null) {
uploadCallback = callback;
}
doUpload();
}

public void doUpload() {
mMessageHandler.sendMessage(mMessageHandler.obtainMessage(MessageHandler.CLEAR_MESSAGES_FOR_UPLOAD));
}
Expand Down Expand Up @@ -979,6 +987,14 @@ void setFirstRunForAST(boolean firstRun) {
.apply();
}

public MParticle.UploadCallback getUploadCallback() {
return uploadCallback;
}

public void setUploadCallback(MParticle.UploadCallback uploadCallback) {
this.uploadCallback = uploadCallback;
}

@SuppressLint("MissingPermission")
private class StatusBroadcastReceiver extends BroadcastReceiver {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,28 @@ protected boolean upload(boolean history) {
}
}
}
if (mMessageManager != null && mMessageManager.getUploadCallback() != null) {
mMessageManager.getUploadCallback().onSuccess();
}
} catch (MParticleApiClientImpl.MPThrottleException e) {
if (mMessageManager != null && mMessageManager.getUploadCallback() != null) {
mMessageManager.getUploadCallback().onFailed();
}
} catch (SSLHandshakeException ssle) {
Logger.debug("SSL handshake failed while preparing uploads - possible MITM attack detected.");
if (mMessageManager != null && mMessageManager.getUploadCallback() != null) {
mMessageManager.getUploadCallback().onFailed();
}
} catch (MParticleApiClientImpl.MPConfigException e) {
Logger.error("Bad API request - is the correct API key and secret configured?");
if (mMessageManager != null && mMessageManager.getUploadCallback() != null) {
mMessageManager.getUploadCallback().onFailed();
}
} catch (Exception e) {
Logger.error(e, "Error processing batch uploads in mParticle DB.");
if (mMessageManager != null && mMessageManager.getUploadCallback() != null) {
mMessageManager.getUploadCallback().onFailed();
}
}
return processingSessionEnd;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Answers
import org.mockito.Mockito
Expand All @@ -30,8 +30,11 @@ import java.io.PrintWriter
import java.io.StringWriter
import java.util.Random
import java.util.concurrent.atomic.AtomicLong
import kotlin.test.Test
import kotlin.test.assertNull

@RunWith(PowerMockRunner::class)
@PrepareForTest(MessageManager::class)
class MessageManagerTest {
private lateinit var context: MockContext
private lateinit var configManager: ConfigManager
Expand Down Expand Up @@ -545,6 +548,32 @@ class MessageManagerTest {
)
}

@Test
@Throws(Exception::class)
fun testDoUpload_With_callback() {
val mockCallback = Mockito.mock(MParticle.UploadCallback::class.java)
manager.doUpload(mockCallback)
Mockito.verify(messageHandler, Mockito.times(1)).sendMessage(
Mockito.any(
Message::class.java
)
)
assertNotNull(manager.uploadCallback)
}

@Test
@Throws(Exception::class)
fun testDoUpload_With_set_NUll_callback() {
manager.uploadCallback = null
manager.doUpload()
Mockito.verify(messageHandler, Mockito.times(1)).sendMessage(
Mockito.any(
Message::class.java
)
)
assertNull(manager.uploadCallback)
}

@Test
@Throws(Exception::class)
fun testSetLocation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import java.io.IOException
import java.lang.reflect.Field
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.test.assertNotNull
import kotlin.test.assertNull

@RunWith(PowerMockRunner::class)
class UploadHandlerTest {
Expand Down Expand Up @@ -180,6 +183,54 @@ class UploadHandlerTest {
Mockito.verify(mockApiClient).sendMessageBatch(Mockito.eq("a message batch"))
}

@Test
@Throws(Exception::class)
fun testUploadWithCallBack_OnComplete() {
handler.handleMessage(Message())
val mockMessageManager = Mockito.mock(MessageManager::class.java)
val mockCallback = Mockito.mock(MParticle.UploadCallback::class.java)
Mockito.`when`(mockMessageManager.uploadCallback).thenReturn(mockCallback)
val field: Field = UploadHandler::class.java.getDeclaredField("mMessageManager")
field.apply {
isAccessible = true
}
field.set(handler, mockMessageManager)

Mockito.`when`(mConfigManager.includeSessionHistory).thenReturn(false)
val mockCursor = Mockito.mock(
Cursor::class.java
)
Mockito.`when`(mockCursor.moveToNext()).thenReturn(true, false)
Mockito.`when`(mockCursor.getInt(Mockito.anyInt())).thenReturn(123)
Mockito.`when`(mockCursor.getString(Mockito.anyInt())).thenReturn("cool message batch!")
handler.upload(true)
assertNotNull(field.get(handler))
}

@Test
@Throws(Exception::class)
fun testUpload_When_MessageManager_IS_NULL() {
handler.handleMessage(Message())
val mockMessageManager = Mockito.mock(MessageManager::class.java)
val mockCallback = Mockito.mock(MParticle.UploadCallback::class.java)
Mockito.`when`(mockMessageManager.uploadCallback).thenReturn(mockCallback)
val field: Field = UploadHandler::class.java.getDeclaredField("mMessageManager")
field.apply {
isAccessible = true
}
field.set(handler, null)

Mockito.`when`(mConfigManager.includeSessionHistory).thenReturn(false)
val mockCursor = Mockito.mock(
Cursor::class.java
)
Mockito.`when`(mockCursor.moveToNext()).thenReturn(true, false)
Mockito.`when`(mockCursor.getInt(Mockito.anyInt())).thenReturn(123)
Mockito.`when`(mockCursor.getString(Mockito.anyInt())).thenReturn("cool message batch!")
handler.upload(true)
assertNull(field.get(handler))
}

@Test
@Throws(
IOException::class,
Expand Down
Loading