PingbackMethodImpl implements Method { @Override public Response execute(long companyId) { try { Response response = addPingback(companyId); if (response != null) { return response; } return XmlRpcUtil.createSuccess("Pingback accepted"); } catch (DuplicateCommentException duplicateCommentException) { return XmlRpcUtil.createFault( PINGBACK_ALREADY_REGISTERED, "Pingback is already registered: " + duplicateCommentException.getMessage()); } catch (Exception exception) { if (_log.isDebugEnabled()) { _log.debug(exception, exception); } return XmlRpcUtil.createFault( TARGET_URI_INVALID, "Unable to parse target URI"); } }  @Override Response execute(long companyId); @Override String getMethodName(); @Override String getToken(); @Override boolean setArguments(Object[] arguments); void setInetAddressLookup(InetAddressLookup inetAddressLookup); void setPingbackProperties(PingbackProperties pingbackProperties); void setPortletIdLookup(PortletIdLookup portletIdLookup); static final int ACCESS_DENIED; static final int GENERIC_FAULT; static final int PINGBACK_ALREADY_REGISTERED; static final int SERVER_ERROR; static final int SOURCE_URI_DOES_NOT_EXIST; static final int SOURCE_URI_INVALID; static final int TARGET_URI_DOES_NOT_EXIST; static final int TARGET_URI_INVALID; }
@Test public void testAddPingbackWhenBlogEntryDisablesPingbacks() throws Exception { Mockito.when( _blogsEntry.isAllowPingbacks() ).thenReturn( false ); execute(); verifyFault( XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND, "Pingbacks are disabled"); }
@Test public void testAddPingbackWithFriendlyURL() throws Exception { long plid = RandomTestUtil.randomLong(); String friendlyURL = RandomTestUtil.randomString(); Mockito.when( _portal.getPlidFromFriendlyURL(_COMPANY_ID, "/" + friendlyURL) ).thenReturn( plid ); long groupId = RandomTestUtil.randomLong(); Mockito.when( _portal.getScopeGroupId(plid) ).thenReturn( groupId ); String friendlyURLPath = RandomTestUtil.randomString(); whenFriendlyURLMapperPopulateParams( "/" + friendlyURLPath, "urlTitle", _URL_TITLE); String targetURI = StringBundler.concat( "http: friendlyURLPath); whenHttpURLToString( StringBundler.concat( "<body><a href='", targetURI, "'>", RandomTestUtil.randomString(), "</a></body>")); execute(targetURI); verifySuccess(); Mockito.verify( _blogsEntryLocalService ).getEntry( groupId, _URL_TITLE ); }
@Test public void testConvertDuplicateCommentExceptionToXmlRpcFault() throws Exception { Mockito.doThrow( DuplicateCommentException.class ).when( _commentManager ).addComment( Mockito.anyLong(), Mockito.anyLong(), Mockito.anyString(), Mockito.anyLong(), Mockito.anyString(), Mockito.<ServiceContextFunction>any() ); execute(); verifyFault( PingbackMethodImpl.PINGBACK_ALREADY_REGISTERED, "Pingback is already registered: null"); }
@Test public void testExecuteWithSuccess() throws Exception { execute(); verifySuccess(); Mockito.verify( _commentManager ).addComment( Matchers.eq(_USER_ID), Matchers.eq(_GROUP_ID), Matchers.eq(BlogsEntry.class.getName()), Matchers.eq(_ENTRY_ID), Matchers.eq( StringBundler.concat( "[...] ", _EXCERPT_BODY, " [...] <a href=", _SOURCE_URI, ">", _READ_MORE, "</a>")), Mockito.<ServiceContextFunction>any() ); }
@Test public void testGetExcerptWhenAnchorHasParent() throws Exception { whenHttpURLToString( StringBundler.concat( "<body><p>Visit <a href='http: "'>Liferay</a> to learn more</p></body>")); execute(); verifyExcerpt("Visit Liferay to learn more"); }
@Test public void testGetExcerptWhenAnchorIsMalformed() throws Exception { whenHttpURLToString("<a href='MALFORMED' />"); execute("MALFORMED"); verifyFault( PingbackMethodImpl.TARGET_URI_INVALID, "Unable to parse target URI"); }
@Test public void testGetExcerptWhenAnchorIsMissing() throws Exception { whenHttpURLToString(""); execute(); verifyFault( PingbackMethodImpl.SOURCE_URI_INVALID, "Unable to find target URI in source"); }
@Test public void testGetExcerptWhenReferrerIsUnavailable() throws Exception { Mockito.when( _http.URLtoString(_SOURCE_URI) ).thenThrow( IOException.class ); execute(); verifyFault( PingbackMethodImpl.SOURCE_URI_DOES_NOT_EXIST, "Error accessing source URI"); }