public void retry_issue5097 () throws DotSecurityException, DotDataException, IOException, JSONException, DotPublisherException, InterruptedException {
EnvironmentAPI environmentAPI = APILocator.getEnvironmentAPI();
ContentletAPI contentletAPI = APILocator.getContentletAPI();
PublishingEndPointAPI publisherEndPointAPI = APILocator.getPublisherEndPointAPI();
PublisherAPI publisherAPI = PublisherAPI.getInstance();
HttpServletRequest req = ServletTestRunner.localRequest.get();
Environment environment = new Environment();
environment.setName( "TestEnvironment_" + String.valueOf( new Date().getTime() ) );
environment.setPushToAll( false );
//Find the roles of the admin user
Role role = APILocator.getRoleAPI().loadRoleByKey( adminUser.getUserId() );
//Create the permissions for the environment
List<Permission> permissions = new ArrayList<Permission>();
Permission p = new Permission( environment.getId(), role.getId(), PermissionAPI.PERMISSION_USE );
permissions.add( p );
//Create a environment
environmentAPI.saveEnvironment( environment, permissions );
//Now we need to create the end point
PublishingEndPoint endpoint = new PublishingEndPoint();
endpoint.setServerName( new StringBuilder( "TestEndPoint" + String.valueOf( new Date().getTime() ) ) );
endpoint.setAddress( "127.0.0.1" );
endpoint.setPort( "999" );
endpoint.setProtocol( "http" );
endpoint.setAuthKey( new StringBuilder( PublicEncryptionFactory.encryptString( "1111" ) ) );
endpoint.setEnabled( true );
endpoint.setSending( false );//TODO: Shouldn't this be true as we are creating this end point to send bundles to another server..?
endpoint.setGroupId( environment.getId() );
//Save the endpoint.
publisherEndPointAPI.saveEndPoint( endpoint );
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++PUBLISH++++++++++++++++++++++++++++
//Getting test data
List<Contentlet> contentlets = contentletAPI.findAllContent( 0, 1 );
//Validations
assertNotNull( contentlets );
assertEquals( contentlets.size(), 1 );
//Preparing the url in order to push content
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd" );
SimpleDateFormat timeFormat = new SimpleDateFormat( "H-m" );
String publishDate = dateFormat.format( new Date() );
String publishTime = timeFormat.format( new Date() );
String baseURL = "http://" + req.getServerName() + ":" + req.getServerPort() + "/DotAjaxDirector/com.dotcms.publisher.ajax.RemotePublishAjaxAction/cmd/publish/u/admin@dotcms.com/p/admin";
String completeURL = baseURL +
"?remotePublishDate=" + UtilMethods.encodeURIComponent( publishDate ) +
"&remotePublishTime=" + UtilMethods.encodeURIComponent( publishTime ) +
"&remotePublishExpireDate=" +
"&remotePublishExpireTime=" +
"&iWantTo=" + RemotePublishAjaxAction.DIALOG_ACTION_PUBLISH +
"&whoToSend=" + UtilMethods.encodeURIComponent( environment.getId() ) +
"&forcePush=false" +
"&assetIdentifier=" + UtilMethods.encodeURIComponent( contentlets.get( 0 ).getIdentifier() );
//Execute the call
URL publishUrl = new URL( completeURL );
String response = IOUtils.toString( publishUrl.openStream(), "UTF-8" );
//Validations
JSONObject jsonResponse = new JSONObject( response );
assertNotNull( contentlets );
assertEquals( jsonResponse.getInt( "errors" ), 0 );
assertEquals( jsonResponse.getInt( "total" ), 1 );
assertNotNull( jsonResponse.get( "bundleId" ) );
//Now that we have a bundle id
String bundleId = jsonResponse.getString( "bundleId" );
//First we need to verify if this bundle is in the queue job
List<PublishQueueElement> foundBundles = publisherAPI.getQueueElementsByBundleId( bundleId );
assertNotNull( foundBundles );
assertTrue( !foundBundles.isEmpty() );
/*
Now lets wait until it finished, by the way, we are expecting it to fail to publish as the end point does not exist.
Keep in mind the queue will try 3 times before to marked as failed to publish, so we have to wait a bit here....
*/
int x = 0;
do {
Thread.sleep( 3000 );
//Verify if it continues in the queue job
foundBundles = publisherAPI.getQueueElementsByBundleId( bundleId );
x++;
} while ( (foundBundles != null && !foundBundles.isEmpty()) && x < 200 );
//At this points should not be here anymore
foundBundles = publisherAPI.getQueueElementsByBundleId( bundleId );
assertTrue( foundBundles == null || foundBundles.isEmpty() );
//Get the audit records related to this bundle
PublishAuditStatus status = PublishAuditAPI.getInstance().getPublishAuditStatus( bundleId );
//We will be able to retry failed and successfully bundles
assertEquals( status.getStatus(), PublishAuditStatus.Status.FAILED_TO_PUBLISH ); //Remember, we are expecting this to fail
//Get current status dates
Date initialCreationDate = status.getCreateDate();
Date initialUpdateDate = status.getStatusUpdated();
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++RETRY++++++++++++++++++++++++++++++
//Now we can try the retry
req = ServletTestRunner.localRequest.get();
baseURL = "http://" + req.getServerName() + ":" + req.getServerPort() + "/DotAjaxDirector/com.dotcms.publisher.ajax.RemotePublishAjaxAction/cmd/retry/u/admin@dotcms.com/p/admin";
completeURL = baseURL + "?bundlesIds=" + UtilMethods.encodeURIComponent( bundleId );
//Execute the call
URL retryUrl = new URL( completeURL );
response = IOUtils.toString( retryUrl.openStream(), "UTF-8" );//We can expect something like "Bundle id: <strong>1193e3eb-3ccd-496e-8995-29a9fcc48cbd</strong> added successfully to Publishing Queue."
//Validations
assertNotNull( response );
assertTrue( response.contains( bundleId ) );
assertTrue( response.contains( "added successfully to" ) );
//And should be back to the queue job
foundBundles = publisherAPI.getQueueElementsByBundleId( bundleId );
assertNotNull( foundBundles );
assertTrue( !foundBundles.isEmpty() );
//Get current status dates
status = PublishAuditAPI.getInstance().getPublishAuditStatus( bundleId );//Get the audit records related to this bundle