|
| 1 | +package com.batch.android.actions; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import androidx.test.core.app.ApplicationProvider; |
| 6 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 7 | +import androidx.test.filters.MediumTest; |
| 8 | + |
| 9 | +import com.batch.android.Batch; |
| 10 | +import com.batch.android.Config; |
| 11 | +import com.batch.android.di.DITestUtils; |
| 12 | +import com.batch.android.di.providers.RuntimeManagerProvider; |
| 13 | +import com.batch.android.json.JSONException; |
| 14 | +import com.batch.android.json.JSONObject; |
| 15 | +import com.batch.android.module.ActionModule; |
| 16 | +import com.batch.android.module.UserModule; |
| 17 | + |
| 18 | +import org.junit.Assert; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.runner.RunWith; |
| 23 | +import org.mockito.ArgumentCaptor; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.Mockito; |
| 26 | +import org.mockito.invocation.InvocationOnMock; |
| 27 | +import org.mockito.stubbing.Answer; |
| 28 | +import org.powermock.api.mockito.PowerMockito; |
| 29 | +import org.powermock.core.classloader.annotations.PowerMockIgnore; |
| 30 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 31 | +import org.powermock.modules.junit4.rule.PowerMockRule; |
| 32 | +import org.robolectric.res.android.Asset; |
| 33 | +import org.robolectric.shadows.ShadowLog; |
| 34 | + |
| 35 | +import static org.mockito.ArgumentMatchers.eq; |
| 36 | + |
| 37 | +@RunWith(AndroidJUnit4.class) |
| 38 | +@MediumTest |
| 39 | +@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*"}) |
| 40 | +@PrepareForTest({UserModule.class}) |
| 41 | +public class UserEventActionTest |
| 42 | +{ |
| 43 | + |
| 44 | + private Context context; |
| 45 | + |
| 46 | + @Rule |
| 47 | + public PowerMockRule rule = new PowerMockRule(); |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() |
| 51 | + { |
| 52 | + ShadowLog.stream = System.out; |
| 53 | + |
| 54 | + context = ApplicationProvider.getApplicationContext(); |
| 55 | + |
| 56 | + RuntimeManagerProvider.get().setContext(context); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testTrackEventAction() throws JSONException |
| 61 | + { |
| 62 | + ActionModule actionModule = new ActionModule(); |
| 63 | + UserModule userModule = DITestUtils.mockSingletonDependency(UserModule.class, null); |
| 64 | + |
| 65 | + PowerMockito.doNothing().when(userModule).trackPublicEvent(Mockito.anyString(), |
| 66 | + Mockito.anyString(), |
| 67 | + Mockito.any(JSONObject.class)); |
| 68 | + |
| 69 | + ArgumentCaptor<JSONObject> eventDataCaptor = ArgumentCaptor.forClass(JSONObject.class); |
| 70 | + |
| 71 | + String eventJSON = "{'e':'event_test', 'l':'label_test'}"; |
| 72 | + actionModule.performAction(context, "batch.user.event", new JSONObject(eventJSON), null); |
| 73 | + Mockito.verify(userModule).trackPublicEvent(eq("event_test"), |
| 74 | + eq("label_test"), |
| 75 | + eventDataCaptor.capture()); |
| 76 | + |
| 77 | + JSONObject eventData = eventDataCaptor.getValue(); |
| 78 | + Assert.assertNotNull(eventData); |
| 79 | + Assert.assertNotNull(eventData.getJSONObject("attributes")); |
| 80 | + Assert.assertTrue(eventData.getJSONObject("attributes").keySet().isEmpty()); |
| 81 | + Assert.assertNotNull(eventData.getJSONArray("tags")); |
| 82 | + Assert.assertEquals(0, eventData.getJSONArray("tags").length()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testTrackEventWithoutLabelAction() throws JSONException |
| 87 | + { |
| 88 | + ActionModule actionModule = new ActionModule(); |
| 89 | + UserModule userModule = DITestUtils.mockSingletonDependency(UserModule.class, null); |
| 90 | + |
| 91 | + PowerMockito.doNothing().when(userModule).trackPublicEvent(Mockito.anyString(), |
| 92 | + Mockito.anyString(), |
| 93 | + Mockito.any(JSONObject.class)); |
| 94 | + |
| 95 | + ArgumentCaptor<JSONObject> eventDataCaptor = ArgumentCaptor.forClass(JSONObject.class); |
| 96 | + |
| 97 | + String eventJSON = "{'e':'event_test'}"; |
| 98 | + actionModule.performAction(context, "batch.user.event", new JSONObject(eventJSON), null); |
| 99 | + Mockito.verify(userModule).trackPublicEvent(eq("event_test"), |
| 100 | + eq(null), |
| 101 | + eventDataCaptor.capture()); |
| 102 | + |
| 103 | + JSONObject eventData = eventDataCaptor.getValue(); |
| 104 | + Assert.assertNotNull(eventData); |
| 105 | + Assert.assertNotNull(eventData.getJSONObject("attributes")); |
| 106 | + Assert.assertTrue(eventData.getJSONObject("attributes").keySet().isEmpty()); |
| 107 | + Assert.assertNotNull(eventData.getJSONArray("tags")); |
| 108 | + Assert.assertEquals(0, eventData.getJSONArray("tags").length()); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testTrackEventWithTagsAction() throws JSONException |
| 113 | + { |
| 114 | + ActionModule actionModule = new ActionModule(); |
| 115 | + UserModule userModule = DITestUtils.mockSingletonDependency(UserModule.class, null); |
| 116 | + |
| 117 | + PowerMockito.doNothing().when(userModule).trackPublicEvent(Mockito.anyString(), |
| 118 | + Mockito.anyString(), |
| 119 | + Mockito.any(JSONObject.class)); |
| 120 | + |
| 121 | + ArgumentCaptor<JSONObject> eventDataCaptor = ArgumentCaptor.forClass(JSONObject.class); |
| 122 | + |
| 123 | + String eventJSON = "{'e':'event_test', 'l':'label_test', 't':['tag1', 'tag2', 'tag3']}"; |
| 124 | + actionModule.performAction(context, "batch.user.event", new JSONObject(eventJSON), null); |
| 125 | + Mockito.verify(userModule).trackPublicEvent(eq("event_test"), |
| 126 | + eq("label_test"), |
| 127 | + eventDataCaptor.capture()); |
| 128 | + |
| 129 | + JSONObject eventData = eventDataCaptor.getValue(); |
| 130 | + Assert.assertNotNull(eventData); |
| 131 | + Assert.assertNotNull(eventData.getJSONObject("attributes")); |
| 132 | + Assert.assertTrue(eventData.getJSONObject("attributes").keySet().isEmpty()); |
| 133 | + Assert.assertNotNull(eventData.getJSONArray("tags")); |
| 134 | + Assert.assertEquals(3, eventData.getJSONArray("tags").length()); |
| 135 | + Assert.assertEquals("tag1", eventData.getJSONArray("tags").optString(0)); |
| 136 | + Assert.assertEquals("tag2", eventData.getJSONArray("tags").optString(1)); |
| 137 | + Assert.assertEquals("tag3", eventData.getJSONArray("tags").optString(2)); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testTrackEventWithAttrAction() throws JSONException |
| 142 | + { |
| 143 | + ActionModule actionModule = new ActionModule(); |
| 144 | + UserModule userModule = DITestUtils.mockSingletonDependency(UserModule.class, null); |
| 145 | + |
| 146 | + PowerMockito.doNothing().when(userModule).trackPublicEvent(Mockito.anyString(), |
| 147 | + Mockito.anyString(), |
| 148 | + Mockito.any(JSONObject.class)); |
| 149 | + |
| 150 | + ArgumentCaptor<JSONObject> eventDataCaptor = ArgumentCaptor.forClass(JSONObject.class); |
| 151 | + |
| 152 | + String eventJSON = "{'e':'event_test', 'l':'label_test', 'a':{'bool':true, 'int':64, 'double': 68987.256, 'string':'tototo', 'date': '2020-08-09T12:12:23.943Z'}}}"; |
| 153 | + actionModule.performAction(context, "batch.user.event", new JSONObject(eventJSON), null); |
| 154 | + Mockito.verify(userModule).trackPublicEvent(eq("event_test"), |
| 155 | + eq("label_test"), |
| 156 | + eventDataCaptor.capture()); |
| 157 | + |
| 158 | + JSONObject eventData = eventDataCaptor.getValue(); |
| 159 | + Assert.assertNotNull(eventData); |
| 160 | + |
| 161 | + Assert.assertNotNull(eventData.getJSONObject("attributes")); |
| 162 | + Assert.assertTrue(eventData.getJSONObject("attributes").optBoolean("bool.b")); |
| 163 | + Assert.assertEquals(64, eventData.getJSONObject("attributes").optInt("int.i")); |
| 164 | + Assert.assertEquals(68987.256, |
| 165 | + eventData.getJSONObject("attributes").optDouble("double.f"), |
| 166 | + 0); |
| 167 | + Assert.assertEquals("tototo", |
| 168 | + eventData.getJSONObject("attributes").optString("string.s")); |
| 169 | + Assert.assertEquals(1596975143943L, |
| 170 | + eventData.getJSONObject("attributes").optLong("date.t")); |
| 171 | + |
| 172 | + Assert.assertNotNull(eventData.getJSONArray("tags")); |
| 173 | + Assert.assertEquals(0, eventData.getJSONArray("tags").length()); |
| 174 | + } |
| 175 | +} |
0 commit comments