AudienceManager { @NonNull public List<AttributeMutation> getAttributeOverrides() { List<AttributeMutation> mutations = new ArrayList<>(); mutations.addAll(historian.getAttributeHistory(clock.currentTimeMillis() - DEFAULT_PREFER_LOCAL_DATA_TIME_MS)); mutations.addAll(namedUser.getPendingAttributeUpdates()); mutations.addAll(airshipChannel.getPendingAttributeUpdates()); return AttributeMutation.collapseMutations(mutations); } AudienceManager(@NonNull AirshipRuntimeConfig runtimeConfig,
                           @NonNull AirshipChannel airshipChannel,
                           @NonNull NamedUser namedUser,
                           @NonNull PreferenceDataStore dataStore); @VisibleForTesting  AudienceManager(@NonNull TagGroupLookupApiClient client, @NonNull AirshipChannel airshipChannel,
                    @NonNull NamedUser namedUser, @NonNull AudienceHistorian historian,
                    @NonNull PreferenceDataStore dataStore, @NonNull Clock clock); void setEnabled(boolean enabled); boolean isEnabled(); void setCacheMaxAgeTime(@IntRange(from = 0) long duration, @NonNull TimeUnit unit); long getCacheMaxAgeTimeMilliseconds(); void setCacheStaleReadTime(@IntRange(from = MIN_CACHE_MAX_AGE_TIME_MS) long duration, @NonNull TimeUnit unit); long getCacheStaleReadTimeMilliseconds(); void setRequestTagsCallback(@Nullable RequestTagsCallback callback); long getPreferLocalTagDataTime(); void setPreferLocalTagDataTime(@IntRange(from = 0) long duration, @NonNull TimeUnit unit); @NonNull @WorkerThread synchronized TagGroupResult getTags(@NonNull Map<String, Set<String>> tags); @NonNull List<TagGroupsMutation> getTagOverrides(); @NonNull List<AttributeMutation> getAttributeOverrides(); static final long MIN_CACHE_MAX_AGE_TIME_MS; static final long DEFAULT_CACHE_MAX_AGE_TIME_MS; static final long DEFAULT_CACHE_STALE_READ_TIME_MS; static final long DEFAULT_PREFER_LOCAL_DATA_TIME_MS; }
@Test public void testGetAttributeOverrides() { clock.currentTimeMillis = System.currentTimeMillis(); when(mockNamedUser.getId()).thenReturn("named-user-id"); List<AttributeMutation> history = new ArrayList<>(); history.add(AttributeMutation.newRemoveAttributeMutation("foo", 100)); history.add(AttributeMutation.newSetAttributeMutation("bar", JsonValue.wrapOpt(100), 100)); history.add(AttributeMutation.newSetAttributeMutation("baz", JsonValue.wrapOpt("baz"), 100)); when(mockHistorian.getAttributeHistory(clock.currentTimeMillis - manager.DEFAULT_PREFER_LOCAL_DATA_TIME_MS)).thenReturn(history); List<AttributeMutation> pendingChannelAttributes = Collections.singletonList(AttributeMutation.newSetAttributeMutation("baz", JsonValue.wrapOpt("updated baz"), 100)); when(mockChannel.getPendingAttributeUpdates()).thenReturn(pendingChannelAttributes); List<AttributeMutation> pendingNamedUserAttributes = Collections.singletonList(AttributeMutation.newSetAttributeMutation("bar", JsonValue.wrapOpt("updated bar"), 100)); when(mockNamedUser.getPendingAttributeUpdates()).thenReturn(pendingNamedUserAttributes); List<AttributeMutation> expected = new ArrayList<>(); expected.addAll(history); expected.addAll(pendingNamedUserAttributes); expected.addAll(pendingChannelAttributes); assertEquals(AttributeMutation.collapseMutations(expected), manager.getAttributeOverrides()); }