|  | 
| 2 | 2 | 
 | 
| 3 | 3 | import android.support.test.runner.AndroidJUnit4; | 
| 4 | 4 | import com.appboy.Appboy; | 
|  | 5 | +import com.appboy.AppboyUser; | 
| 5 | 6 | import com.appboy.configuration.AppboyConfig; | 
| 6 | 7 | import com.segment.analytics.Analytics; | 
| 7 | 8 | import com.segment.analytics.Traits; | 
| 8 | 9 | import com.segment.analytics.integrations.IdentifyPayload; | 
| 9 | 10 | import com.segment.analytics.integrations.Logger; | 
| 10 | 11 | import com.segment.analytics.test.IdentifyPayloadBuilder; | 
|  | 12 | +import org.junit.After; | 
|  | 13 | +import org.junit.Before; | 
| 11 | 14 | import org.junit.BeforeClass; | 
| 12 | 15 | import org.junit.Test; | 
| 13 | 16 | import org.junit.runner.RunWith; | 
|  | 17 | +import org.mockito.InOrder; | 
|  | 18 | +import org.mockito.Mock; | 
|  | 19 | +import org.mockito.Mockito; | 
|  | 20 | +import org.mockito.MockitoAnnotations; | 
| 14 | 21 | 
 | 
| 15 | 22 | import static android.support.test.InstrumentationRegistry.getContext; | 
| 16 | 23 | import static com.segment.analytics.Utils.createTraits; | 
| 17 |  | -import static org.junit.Assert.assertEquals; | 
|  | 24 | +import static org.mockito.Mockito.times; | 
|  | 25 | +import static org.mockito.Mockito.verify; | 
|  | 26 | +import static org.mockito.Mockito.when; | 
| 18 | 27 | 
 | 
| 19 | 28 | @RunWith(AndroidJUnit4.class) | 
| 20 | 29 | public class AppboyIntegrationOptionsAndroidTest { | 
| 21 | 30 | 
 | 
| 22 |  | -  private static final String ORIGINAL_USER_ID = "testUser"; | 
|  | 31 | +  private static final String USER_ID = "testUser"; | 
|  | 32 | +  private static final String OTHER_USER_ID = "testUser2"; | 
| 23 | 33 |   private static final String TRANSFORMED_USER_ID = "transformedUser"; | 
|  | 34 | +  private static final String TRAIT_EMAIL = "test@segment.com"; | 
|  | 35 | +  private static final String TRAIT_EMAIL_UPDATED = "updated@segment.com"; | 
|  | 36 | +  private static final String TRAIT_CITY = "city"; | 
|  | 37 | +  private static final String TRAIT_COUNTRY = "country"; | 
|  | 38 | +  private static final String CUSTOM_TRAIT_KEY = "custom_trait_key"; | 
|  | 39 | +  private static final String CUSTOM_KEY_VALUE = "custom_key_value"; | 
|  | 40 | + | 
|  | 41 | +  @Mock Appboy appboy; | 
|  | 42 | +  @Mock AppboyUser appboyUser; | 
|  | 43 | + | 
|  | 44 | +  private AppboyIntegration appboyIntegration; | 
|  | 45 | +  private PreferencesTraitsCache traitsCache; | 
| 24 | 46 | 
 | 
| 25 | 47 |   @BeforeClass | 
| 26 | 48 |   public static void beforeClass() { | 
| 27 | 49 |     AppboyConfig appboyConfig = new AppboyConfig.Builder().setApiKey("testkey").build(); | 
| 28 | 50 |     Appboy.configure(getContext(), appboyConfig); | 
| 29 | 51 |   } | 
| 30 | 52 | 
 | 
|  | 53 | +  @Before | 
|  | 54 | +  public void setUp() throws Exception { | 
|  | 55 | +    MockitoAnnotations.initMocks(this); | 
|  | 56 | + | 
|  | 57 | +    when(appboy.getCurrentUser()).thenReturn(appboyUser); | 
|  | 58 | + | 
|  | 59 | +    Logger logger = Logger.with(Analytics.LogLevel.DEBUG); | 
|  | 60 | + | 
|  | 61 | +    traitsCache = new PreferencesTraitsCache(getContext()); | 
|  | 62 | + | 
|  | 63 | +    appboyIntegration = new AppboyIntegration( | 
|  | 64 | +        appboy, "foo", logger, true, | 
|  | 65 | +        traitsCache, new ReplaceUserIdMapper()); | 
|  | 66 | +  } | 
|  | 67 | + | 
|  | 68 | +  @After | 
|  | 69 | +  public void tearDown() throws Exception { | 
|  | 70 | +    traitsCache.clear(); | 
|  | 71 | +  } | 
|  | 72 | + | 
| 31 | 73 |   @Test | 
| 32 | 74 |   public void testUserIdMapperTransformsAppboyUserId() { | 
| 33 |  | -    Traits traits = createTraits(ORIGINAL_USER_ID); | 
| 34 |  | -    IdentifyPayload identifyPayload = new IdentifyPayloadBuilder().traits(traits).build(); | 
|  | 75 | +    Traits traits = createTraits(USER_ID); | 
| 35 | 76 | 
 | 
| 36 |  | -    Logger logger = Logger.with(Analytics.LogLevel.DEBUG); | 
|  | 77 | +    callIdentifyWithTraits(traits); | 
|  | 78 | + | 
|  | 79 | +    verify(appboy).changeUser(TRANSFORMED_USER_ID); | 
|  | 80 | +  } | 
|  | 81 | + | 
|  | 82 | +  @Test | 
|  | 83 | +  public void testShouldNotTriggerAppboyUpdateIfTraitDoesntChange() { | 
|  | 84 | +    Traits traits = createTraits(USER_ID); | 
|  | 85 | + | 
|  | 86 | +    Traits.Address address = new Traits.Address(); | 
|  | 87 | +    address.putCity(TRAIT_CITY); | 
|  | 88 | +    address.putCountry(TRAIT_COUNTRY); | 
| 37 | 89 | 
 | 
| 38 |  | -    AppboyIntegration integration = new AppboyIntegration(Appboy.getInstance(getContext()), "foo", logger, true, new ReplaceUserIdMapper()); | 
|  | 90 | +    traits.putAddress(address); | 
|  | 91 | +    traits.putEmail(TRAIT_EMAIL); | 
|  | 92 | +    traits.put(CUSTOM_TRAIT_KEY, CUSTOM_KEY_VALUE); | 
|  | 93 | + | 
|  | 94 | +    callIdentifyWithTraits(traits); | 
|  | 95 | +    callIdentifyWithTraits(traits); | 
|  | 96 | +    callIdentifyWithTraits(traits); | 
|  | 97 | + | 
|  | 98 | +    verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL); | 
|  | 99 | +    verify(appboyUser, times(1)).setHomeCity(TRAIT_CITY); | 
|  | 100 | +    verify(appboyUser, times(1)).setCountry(TRAIT_COUNTRY); | 
|  | 101 | +    verify(appboyUser, times(1)).setCustomUserAttribute(CUSTOM_TRAIT_KEY, CUSTOM_KEY_VALUE); | 
|  | 102 | +  } | 
|  | 103 | + | 
|  | 104 | +  @Test | 
|  | 105 | +  public void testShouldTriggerUpdateIfTraitChanges() { | 
|  | 106 | +    Traits traits = createTraits(USER_ID); | 
|  | 107 | +    traits.putEmail(TRAIT_EMAIL); | 
|  | 108 | +    callIdentifyWithTraits(traits); | 
|  | 109 | +    callIdentifyWithTraits(traits); | 
| 39 | 110 | 
 | 
| 40 |  | -    integration.identify(identifyPayload); | 
|  | 111 | +    Traits traitsUpdate = createTraits(USER_ID); | 
|  | 112 | +    traitsUpdate.putEmail(TRAIT_EMAIL_UPDATED); | 
|  | 113 | +    callIdentifyWithTraits(traitsUpdate); | 
|  | 114 | +    callIdentifyWithTraits(traitsUpdate); | 
|  | 115 | + | 
|  | 116 | +    InOrder inOrder = Mockito.inOrder(appboyUser); | 
|  | 117 | +    inOrder.verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL); | 
|  | 118 | +    inOrder.verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL_UPDATED); | 
|  | 119 | +  } | 
|  | 120 | + | 
|  | 121 | +  @Test | 
|  | 122 | +  public void testAvoidTriggeringRepeatedUserIdUpdates() { | 
|  | 123 | +    Traits traits = createTraits(USER_ID); | 
|  | 124 | +    traits.putEmail(TRAIT_EMAIL); | 
|  | 125 | + | 
|  | 126 | +    callIdentifyWithTraits(traits); | 
|  | 127 | +    callIdentifyWithTraits(traits); | 
|  | 128 | + | 
|  | 129 | +    verify(appboyUser, times(1)).setEmail(TRAIT_EMAIL); | 
|  | 130 | +    verify(appboy, times(1)).changeUser(TRANSFORMED_USER_ID); | 
|  | 131 | +  } | 
|  | 132 | + | 
|  | 133 | +  @Test | 
|  | 134 | +  public void clearCacheIfUserIdChanges() { | 
|  | 135 | +    Traits traits = createTraits(USER_ID); | 
|  | 136 | +    traits.putEmail(TRAIT_EMAIL); | 
|  | 137 | + | 
|  | 138 | +    Traits traitsUpdate = createTraits(OTHER_USER_ID); | 
|  | 139 | +    traitsUpdate.putEmail(TRAIT_EMAIL); | 
|  | 140 | + | 
|  | 141 | +    callIdentifyWithTraits(traits); | 
|  | 142 | +    callIdentifyWithTraits(traitsUpdate); | 
|  | 143 | + | 
|  | 144 | +    verify(appboyUser, times(2)).setEmail(TRAIT_EMAIL); | 
|  | 145 | +  } | 
|  | 146 | + | 
|  | 147 | +  @Test | 
|  | 148 | +  public void clearCacheOnReset() { | 
|  | 149 | +    Traits traits = createTraits(USER_ID); | 
|  | 150 | +    traits.putEmail(TRAIT_EMAIL); | 
|  | 151 | + | 
|  | 152 | +    callIdentifyWithTraits(traits); | 
|  | 153 | + | 
|  | 154 | +    appboyIntegration.reset(); | 
|  | 155 | + | 
|  | 156 | +    callIdentifyWithTraits(traits); | 
|  | 157 | + | 
|  | 158 | +    verify(appboyUser, times(2)).setEmail(TRAIT_EMAIL); | 
|  | 159 | +  } | 
|  | 160 | + | 
|  | 161 | +  public void callIdentifyWithTraits(Traits traits) { | 
|  | 162 | +    IdentifyPayload identifyPayload = new IdentifyPayloadBuilder().traits(traits).build(); | 
| 41 | 163 | 
 | 
| 42 |  | -    assertEquals(TRANSFORMED_USER_ID, Appboy.getInstance(getContext()).getCurrentUser().getUserId()); | 
|  | 164 | +    appboyIntegration.identify(identifyPayload); | 
| 43 | 165 |   } | 
| 44 | 166 | 
 | 
| 45 | 167 |   class ReplaceUserIdMapper implements UserIdMapper { | 
| 46 | 168 |     @Override | 
| 47 | 169 |     public String transformUserId(String segmentUserId) { | 
| 48 |  | -      return segmentUserId.replaceFirst(ORIGINAL_USER_ID, TRANSFORMED_USER_ID); | 
|  | 170 | +      return segmentUserId.replaceFirst(USER_ID, TRANSFORMED_USER_ID); | 
| 49 | 171 |     } | 
| 50 | 172 |   } | 
| 51 | 173 | } | 
|  | 
0 commit comments