Skip to content

Commit

Permalink
#26633 Refactor SitePushHandler and add SiteTestHelper
Browse files Browse the repository at this point in the history
The SitePushHandler class has been refactored to improve readability and maintainability. Helper methods have been introduced to decide whether a site should be published, archived or unpublished. The push handler now utilizes ScheduledExecutorService to handle potential delays in site status changes. Furthermore, SiteTestHelper has been introduced providing common utility functions for site related tests.
  • Loading branch information
jgambarios committed Jan 30, 2024
1 parent 1f4ae11 commit 9a32d8e
Show file tree
Hide file tree
Showing 4 changed files with 361 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
import com.dotcms.DotCMSITProfile;
import com.dotcms.api.client.model.RestClientFactory;
import com.dotcms.api.client.model.ServiceManager;
import com.dotcms.common.SiteTestHelper;
import com.dotcms.model.ResponseEntityView;
import com.dotcms.model.config.ServiceBean;
import com.dotcms.model.site.*;
import com.dotcms.model.site.CopySiteRequest;
import com.dotcms.model.site.CreateUpdateSiteRequest;
import com.dotcms.model.site.GetSiteByNameRequest;
import com.dotcms.model.site.Site;
import com.dotcms.model.site.SiteView;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import java.io.IOException;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.NotFoundException;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.wildfly.common.Assert;

@QuarkusTest
@TestProfile(DotCMSITProfile.class)
class SiteAPIIT {
class SiteAPIIT extends SiteTestHelper {

@ConfigProperty(name = "com.dotcms.starter.site", defaultValue = "default")
String siteName;
Expand Down Expand Up @@ -116,7 +119,6 @@ void Test_Create_New_Site_Then_Update_Then_Delete() {

}

@Disabled("Test is intermittently failing.")
@Test
void Test_Archive_Unarchive() {

Expand All @@ -131,14 +133,11 @@ void Test_Archive_Unarchive() {

ResponseEntityView<SiteView> archiveSite = clientFactory.getClient(SiteAPI.class).archive(identifier);
Assertions.assertNotNull(archiveSite.entity());
ResponseEntityView<SiteView> byName = clientFactory.getClient(SiteAPI.class).findByName(GetSiteByNameRequest.builder().siteName(newSiteName).build());
Assertions.assertTrue(byName.entity().isArchived());
ResponseEntityView<SiteView> unarchiveSite = clientFactory.getClient(SiteAPI.class).unarchive(identifier);
Assertions.assertFalse(unarchiveSite.entity().isArchived());

byName = clientFactory.getClient(SiteAPI.class).findByName(GetSiteByNameRequest.builder().siteName(newSiteName).build());
Assertions.assertFalse(byName.entity().isArchived());
Assertions.assertTrue(checkValidSiteStatus(newSiteName, false, true));

ResponseEntityView<SiteView> unarchiveSite = clientFactory.getClient(SiteAPI.class).unarchive(identifier);
Assertions.assertNotNull(unarchiveSite.entity());
Assertions.assertTrue(checkValidSiteStatus(newSiteName, false, false));
}

@Test
Expand All @@ -155,11 +154,12 @@ void Test_Publish_UnPublish_Site() {
Assert.assertFalse(createSiteResponse.entity().isLive());

ResponseEntityView<SiteView> publishedSite = clientFactory.getClient(SiteAPI.class).publish(identifier);
Assertions.assertTrue(publishedSite.entity().isLive());
Assertions.assertNotNull(publishedSite.entity());
Assertions.assertTrue(checkValidSiteStatus(newSiteName, true, false));

ResponseEntityView<SiteView> unPublishedSite = clientFactory.getClient(SiteAPI.class).unpublish(identifier);
Assertions.assertFalse(unPublishedSite.entity().isLive());

Assertions.assertNotNull(unPublishedSite.entity());
Assertions.assertTrue(checkValidSiteStatus(newSiteName, false, false));
}

@Test
Expand Down Expand Up @@ -187,5 +187,4 @@ void Test_Copy_Site() {

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.dotcms.common;

import com.dotcms.api.SiteAPI;
import com.dotcms.api.client.model.RestClientFactory;
import com.dotcms.model.site.GetSiteByNameRequest;
import javax.inject.Inject;
import javax.ws.rs.NotFoundException;

public class SiteTestHelper {

@Inject
RestClientFactory clientFactory;

/**
* Checks if a site with the given name exists.
*
* @param siteName the name of the site to check
* @return true if the site exists, false otherwise
*/
protected Boolean siteExist(final String siteName) {

long start = System.currentTimeMillis();
long end = start + 15 * 1000; // 15 seconds * 1000 ms/sec
while (System.currentTimeMillis() < end) {
try {
var response = clientFactory.getClient(SiteAPI.class)
.findByName(GetSiteByNameRequest.builder().siteName(siteName).build());
if ((response != null && response.entity() != null) &&
((response.entity().isLive() != null &&
response.entity().isLive()) &&
(response.entity().isWorking() != null &&
response.entity().isWorking()))) {
return true;
}
} catch (NotFoundException e) {
// Do nothing
}

try {
Thread.sleep(2000); // Sleep for 2 second
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}

return false;
}

/**
* Checks if the site statuses are valid.
*
* @param siteName The name of the site to check.
* @param isLive The expected live status of the site.
* @param archive The expected archive status of the site.
* @return True if the site statuses are valid, false otherwise.
*/
protected Boolean checkValidSiteStatus(final String siteName,
final boolean isLive, final boolean archive) {

long start = System.currentTimeMillis();
long end = start + 15 * 1000; // 15 seconds * 1000 ms/sec
while (System.currentTimeMillis() < end) {
try {
var response = clientFactory.getClient(SiteAPI.class)
.findByName(GetSiteByNameRequest.builder().siteName(siteName).build());
if ((response != null && response.entity() != null) &&
((response.entity().isLive() != null &&
response.entity().isLive().equals(isLive)) &&
(response.entity().isArchived() != null &&
response.entity().isArchived().equals(archive)))) {
return true;
}
} catch (NotFoundException e) {
// Do nothing
}

try {
Thread.sleep(2000); // Sleep for 2 second
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}

return false;
}
}
Loading

0 comments on commit 9a32d8e

Please sign in to comment.