AsahSegmentsEntryProvider implements SegmentsEntryProvider { @Override public long[] getSegmentsEntryIds( long groupId, String className, long classPK, Context context) { if (context == null) { return new long[0]; } if (GetterUtil.getBoolean(context.get(Context.SIGNED_IN))) { List<SegmentsEntry> segmentsEntries = _segmentsEntryLocalService.getSegmentsEntries( groupId, true, SegmentsEntryConstants.SOURCE_ASAH_FARO_BACKEND, className, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null); if (segmentsEntries.isEmpty()) { return new long[0]; } Stream<SegmentsEntry> stream = segmentsEntries.stream(); return stream.filter( segmentsEntry -> _segmentsEntryRelLocalService.hasSegmentsEntryRel( segmentsEntry.getSegmentsEntryId(), _portal.getClassNameId(className), classPK) ).sorted( (segmentsEntry1, segmentsEntry2) -> { Date modifiedDate = segmentsEntry2.getModifiedDate(); return modifiedDate.compareTo( segmentsEntry1.getModifiedDate()); } ).mapToLong( SegmentsEntry::getSegmentsEntryId ).toArray(); } String userId = GetterUtil.getString( context.get( SegmentsAsahRequestContextContributor. KEY_SEGMENTS_ANONYMOUS_USER_ID)); if (Validator.isNull(userId)) { return new long[0]; } long[] cachedSegmentsEntryIds = _asahSegmentsEntryCache.getSegmentsEntryIds(userId); if (cachedSegmentsEntryIds == null) { if (_log.isDebugEnabled()) { _log.debug( "Asah segments cache not found for user ID " + userId); } Group group = _groupLocalService.fetchGroup(groupId); if (group != null) { _sendMessage(group.getCompanyId(), userId); } return new long[0]; } return cachedSegmentsEntryIds; }  @Override long[] getSegmentsEntryClassPKs(
			long segmentsEntryId, int start, int end); @Override int getSegmentsEntryClassPKsCount(long segmentsEntryId); @Override long[] getSegmentsEntryIds(
		long groupId, String className, long classPK, Context context);  }
@Test public void testGetSegmentsEntryIdsWithCachedUserSegments() { String userId = RandomTestUtil.randomString(); long[] segmentsEntryIds = { RandomTestUtil.randomLong(), RandomTestUtil.randomLong() }; Mockito.when( _asahSegmentsEntryCache.getSegmentsEntryIds(userId) ).thenReturn( segmentsEntryIds ); Context context = new Context(); context.put( SegmentsAsahRequestContextContributor. KEY_SEGMENTS_ANONYMOUS_USER_ID, userId); Assert.assertArrayEquals( segmentsEntryIds, _asahSegmentsEntryProvider.getSegmentsEntryIds( RandomTestUtil.randomLong(), RandomTestUtil.randomString(), RandomTestUtil.randomLong(), context)); Mockito.verify( _messageBus, Mockito.never() ).sendMessage( Mockito.anyString(), Mockito.any(Message.class) ); }
@Test public void testGetSegmentsEntryIdsWithContextAndEmptyAcClientUserId() { Context context = new Context(); context.put( SegmentsAsahRequestContextContributor. KEY_SEGMENTS_ANONYMOUS_USER_ID, StringPool.BLANK); Assert.assertArrayEquals( new long[0], _asahSegmentsEntryProvider.getSegmentsEntryIds( RandomTestUtil.randomLong(), RandomTestUtil.randomString(), RandomTestUtil.randomLong(), context)); }
@Test public void testGetSegmentsEntryIdsWithEmptyContext() { Context context = new Context(); Assert.assertArrayEquals( new long[0], _asahSegmentsEntryProvider.getSegmentsEntryIds( RandomTestUtil.randomLong(), RandomTestUtil.randomString(), RandomTestUtil.randomLong(), context)); }
@Test public void testGetSegmentsEntryIdsWithNullContext() { Assert.assertArrayEquals( new long[0], _asahSegmentsEntryProvider.getSegmentsEntryIds( RandomTestUtil.randomLong(), RandomTestUtil.randomString(), RandomTestUtil.randomLong(), null)); }
@Test public void testGetSegmentsEntryIdsWithUncachedUserSegments() { long groupId = RandomTestUtil.randomLong(); Mockito.when( _groupLocalService.fetchGroup(groupId) ).thenReturn( Mockito.mock(Group.class) ); String userId = RandomTestUtil.randomString(); Mockito.when( _asahSegmentsEntryCache.getSegmentsEntryIds(userId) ).thenReturn( null ); Context context = new Context(); context.put( SegmentsAsahRequestContextContributor. KEY_SEGMENTS_ANONYMOUS_USER_ID, userId); Assert.assertArrayEquals( new long[0], _asahSegmentsEntryProvider.getSegmentsEntryIds( groupId, RandomTestUtil.randomString(), RandomTestUtil.randomLong(), context)); Mockito.verify( _messageBus, Mockito.times(1) ).sendMessage( Mockito.anyString(), Mockito.any(Message.class) ); }