@Test void saveLike() throws Exception { when(this.authorizationManager.hasAccess(this.likeRight, this.userDocReference, target)).thenReturn(true); Map<RatingsManager.RatingQueryField, Object> queryMap = new LinkedHashMap<>(); queryMap.put(RatingsManager.RatingQueryField.ENTITY_TYPE, target.getType()); queryMap.put(RatingsManager.RatingQueryField.ENTITY_REFERENCE, target); when(this.ratingsManager.countRatings(queryMap)).thenReturn(42L); assertEquals(42L, this.defaultLikeManager.saveLike(this.userReference, target)); verify(this.ratingsManager).saveRating(target, this.userReference, 1); verify(this.observationManager).notify(any(LikeEvent.class), eq(this.userReference), eq(target)); verify(this.likedEntityCache).remove("xwiki:Foo.WebHome"); verify(this.likedEntityCache).set("xwiki:Foo.WebHome", 42L); verify(this.likeExistCache).set("xwiki:XWiki.User_xwiki:Foo.WebHome", true); }
@Override public long saveLike(UserReference source, EntityReference target) throws LikeException { DocumentReference userDoc = this.userReferenceDocumentSerializer.serialize(source); if (this.authorizationManager.hasAccess(this.likeRight, userDoc, target)) { try { this.ratingsManager.saveRating(target, source, DEFAULT_LIKE_VOTE); this.likeCountCache.remove(this.entityReferenceSerializer.serialize(target)); this.likeExistCache.set(getExistCacheKey(source, target), true); long newCount = this.getEntityLikes(target); this.observationManager.notify(new LikeEvent(), source, target); return newCount; } catch (RatingsException e) { throw new LikeException(String.format("Error while liking entity [%s]", target), e); } } else { throw new LikeException(String.format("User [%s] is not authorized to perform a like on [%s]", source, target)); } }
DefaultLikeManager implements LikeManager, Initializable, Disposable { @Override public long saveLike(UserReference source, EntityReference target) throws LikeException { DocumentReference userDoc = this.userReferenceDocumentSerializer.serialize(source); if (this.authorizationManager.hasAccess(this.likeRight, userDoc, target)) { try { this.ratingsManager.saveRating(target, source, DEFAULT_LIKE_VOTE); this.likeCountCache.remove(this.entityReferenceSerializer.serialize(target)); this.likeExistCache.set(getExistCacheKey(source, target), true); long newCount = this.getEntityLikes(target); this.observationManager.notify(new LikeEvent(), source, target); return newCount; } catch (RatingsException e) { throw new LikeException(String.format("Error while liking entity [%s]", target), e); } } else { throw new LikeException(String.format("User [%s] is not authorized to perform a like on [%s]", source, target)); } } }
DefaultLikeManager implements LikeManager, Initializable, Disposable { @Override public long saveLike(UserReference source, EntityReference target) throws LikeException { DocumentReference userDoc = this.userReferenceDocumentSerializer.serialize(source); if (this.authorizationManager.hasAccess(this.likeRight, userDoc, target)) { try { this.ratingsManager.saveRating(target, source, DEFAULT_LIKE_VOTE); this.likeCountCache.remove(this.entityReferenceSerializer.serialize(target)); this.likeExistCache.set(getExistCacheKey(source, target), true); long newCount = this.getEntityLikes(target); this.observationManager.notify(new LikeEvent(), source, target); return newCount; } catch (RatingsException e) { throw new LikeException(String.format("Error while liking entity [%s]", target), e); } } else { throw new LikeException(String.format("User [%s] is not authorized to perform a like on [%s]", source, target)); } }  }
DefaultLikeManager implements LikeManager, Initializable, Disposable { @Override public long saveLike(UserReference source, EntityReference target) throws LikeException { DocumentReference userDoc = this.userReferenceDocumentSerializer.serialize(source); if (this.authorizationManager.hasAccess(this.likeRight, userDoc, target)) { try { this.ratingsManager.saveRating(target, source, DEFAULT_LIKE_VOTE); this.likeCountCache.remove(this.entityReferenceSerializer.serialize(target)); this.likeExistCache.set(getExistCacheKey(source, target), true); long newCount = this.getEntityLikes(target); this.observationManager.notify(new LikeEvent(), source, target); return newCount; } catch (RatingsException e) { throw new LikeException(String.format("Error while liking entity [%s]", target), e); } } else { throw new LikeException(String.format("User [%s] is not authorized to perform a like on [%s]", source, target)); } }  @Override void initialize(); @Override void dispose(); @Override long saveLike(UserReference source, EntityReference target); @Override List<EntityReference> getUserLikes(UserReference source, int offset, int limit); @Override long getEntityLikes(EntityReference target); @Override boolean removeLike(UserReference source, EntityReference target); @Override boolean isLiked(UserReference source, EntityReference target); @Override List<UserReference> getLikers(EntityReference target, int offset, int limit); @Override Right getLikeRight(); }
DefaultLikeManager implements LikeManager, Initializable, Disposable { @Override public long saveLike(UserReference source, EntityReference target) throws LikeException { DocumentReference userDoc = this.userReferenceDocumentSerializer.serialize(source); if (this.authorizationManager.hasAccess(this.likeRight, userDoc, target)) { try { this.ratingsManager.saveRating(target, source, DEFAULT_LIKE_VOTE); this.likeCountCache.remove(this.entityReferenceSerializer.serialize(target)); this.likeExistCache.set(getExistCacheKey(source, target), true); long newCount = this.getEntityLikes(target); this.observationManager.notify(new LikeEvent(), source, target); return newCount; } catch (RatingsException e) { throw new LikeException(String.format("Error while liking entity [%s]", target), e); } } else { throw new LikeException(String.format("User [%s] is not authorized to perform a like on [%s]", source, target)); } }  @Override void initialize(); @Override void dispose(); @Override long saveLike(UserReference source, EntityReference target); @Override List<EntityReference> getUserLikes(UserReference source, int offset, int limit); @Override long getEntityLikes(EntityReference target); @Override boolean removeLike(UserReference source, EntityReference target); @Override boolean isLiked(UserReference source, EntityReference target); @Override List<UserReference> getLikers(EntityReference target, int offset, int limit); @Override Right getLikeRight();  }