### Question:
LikeScriptService implements ScriptService { public Optional<Long> like(EntityReference entityReference) { XWikiContext xWikiContext = this.contextProvider.get(); DocumentReference currentUser = xWikiContext.getUserReference(); if (entityReference instanceof DocumentReference) { DocumentReference documentReference = (DocumentReference) entityReference; if (isAuthorized(documentReference)) { UserReference userReference = this.userReferenceResolver.resolve(currentUser); try { return Optional.of(this.likeManager.saveLike(userReference, documentReference)); } catch (LikeException e) { this.logger.warn("Error while liking [{}] by [{}]", documentReference, currentUser, ExceptionUtils.getRootCause(e)); } } else { this.logger.warn("[{}] is not authorized to like [{}].", currentUser, documentReference); } } else { this.logger.warn("Like is only implemented for document for now. (Called with [{}])", entityReference); } return Optional.empty(); }  boolean isAuthorized(EntityReference entityReference); boolean displayButton(EntityReference entityReference); Optional<Long> like(EntityReference entityReference); Optional<Long> unlike(EntityReference entityReference); Optional<Long> getLikes(EntityReference entityReference); List<EntityReference> getUserLikes(UserReference userReference, int offset, int limit); boolean isLiked(EntityReference entityReference); @Unstable List<UserReference> getLikers(EntityReference target, int offset, int limit); void cleanCacheUIX();  }### Answer:
@Test void like() throws LikeException { EntityReference entityReference = new DocumentReference("xwiki", "Foo", "Foo"); when(this.authorizationManager.hasAccess(this.likeRight, this.docUserReference, entityReference)) .thenReturn(true); when(this.likeManager.saveLike(userReference, entityReference)).thenReturn(42L); Optional<Long> optionalLikedEntity = this.likeScriptService.like(entityReference); assertTrue(optionalLikedEntity.isPresent()); assertSame(42L, optionalLikedEntity.get()); }
@Test void likeNotAuthorized() { EntityReference entityReference = new DocumentReference("xwiki", "Foo", "Foo"); assertFalse(this.likeScriptService.like(entityReference).isPresent()); assertEquals("[xwiki:XWiki.User] is not authorized to like [xwiki:Foo.Foo].", logCapture.getMessage(0)); }
@Test void likeNotDocument() { EntityReference entityReference = new EntityReference("something", EntityType.ATTACHMENT); assertFalse(this.likeScriptService.like(entityReference).isPresent()); assertEquals("Like is only implemented for document for now. (Called with [Attachment something])", logCapture.getMessage(0)); }
@Test void likeError() throws LikeException { EntityReference entityReference = new DocumentReference("xwiki", "Foo", "Foo"); when(this.authorizationManager.hasAccess(this.likeRight, this.docUserReference, entityReference)) .thenReturn(true); when(this.likeManager.saveLike(userReference, entityReference)).thenThrow(new LikeException("Problem")); assertFalse(this.likeScriptService.like(entityReference).isPresent()); assertEquals("Error while liking [xwiki:Foo.Foo] by [xwiki:XWiki.User]", logCapture.getMessage(0)); }