Skip to content

Commit

Permalink
#26633 Refactor site testing and enhance site copying command
Browse files Browse the repository at this point in the history
Adjusted SiteCommandIT to ensure a site is created before invoking the copy command. The SiteCopy class's output now includes a note that the copy operation happens in the background.
  • Loading branch information
jgambarios committed Jan 30, 2024
1 parent 9a32d8e commit 9cb9f91
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,26 @@ public class SiteTestHelper {
* @return true if the site exists, false otherwise
*/
protected Boolean siteExist(final String siteName) {
final var siteAPI = clientFactory.getClient(SiteAPI.class);
return siteExist(siteName, siteAPI);
}

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

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());
var response = siteAPI.findByName(
GetSiteByNameRequest.builder().siteName(siteName).build()
);
if ((response != null && response.entity() != null) &&
((response.entity().isLive() != null &&
response.entity().isLive()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.dotcms.model.site.CopySiteRequest;
import com.dotcms.model.site.CreateUpdateSiteRequest;
import com.dotcms.model.site.SiteView;
import java.util.Optional;
import java.util.concurrent.Callable;
import javax.enterprise.context.control.ActivateRequestContext;
import picocli.CommandLine;
Expand Down Expand Up @@ -83,10 +82,19 @@ public Integer call() {
}

private int copy() {

final SiteView site = findSite(siteNameOrId);

final SiteAPI siteAPI = clientFactory.getClient(SiteAPI.class);
ResponseEntityView<SiteView> copy = siteAPI.copy(fromSite(site, copySiteName, copyAll));
output.info(String.format("New Copy Site is [%s].", copy.entity().hostName()));

output.info(String.format(
"New Copy Site is [%s]. Please note that the full replication of all site elements "
+ "is executed as a background job. To confirm the success of the copy "
+ "operation, please check the dotCMS server.",
copy.entity().hostName()
));

return CommandLine.ExitCode.OK;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dotcms.api.client.MapperService;
import com.dotcms.api.client.model.RestClientFactory;
import com.dotcms.cli.command.CommandTest;
import com.dotcms.cli.common.FilesTestHelper;
import com.dotcms.cli.common.InputOutputFormat;
import com.dotcms.common.WorkspaceManager;
import com.dotcms.model.ResponseEntityView;
Expand All @@ -30,7 +31,6 @@
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.Order;
import org.junit.jupiter.api.Test;
import org.wildfly.common.Assert;
Expand Down Expand Up @@ -145,19 +145,23 @@ void Test_Command_Site_Push_Publish_UnPublish_Then_Archive() throws IOException
* Given scenario: Simply call create command followed by copy Expected Result: We simply verify
* the command completes successfully
*/
@Disabled("Test is intermittently failing.")
@Test
@Order(4)
void Test_Command_Copy() {

final SiteAPI siteAPI = clientFactory.getClient(SiteAPI.class);
final var siteName = new FilesTestHelper().createSite(siteAPI);

final CommandLine commandLine = createCommand();
final StringWriter writer = new StringWriter();
try (PrintWriter out = new PrintWriter(writer)) {

commandLine.setOut(out);
final int status = commandLine.execute(SiteCommand.NAME, SiteCopy.NAME, "--idOrName",
siteName);
commandLine.setErr(out);

final int status = commandLine.execute(SiteCommand.NAME, SiteCopy.NAME,
"--idOrName", siteName);
Assertions.assertEquals(CommandLine.ExitCode.OK, status);
final String output = writer.toString();
Assertions.assertTrue(output.startsWith("New Copy Site is"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,17 @@ protected String prepareData(final boolean includeAssets) throws IOException {
* @return The name of the newly created test site.
*/
protected String createSite() {

final SiteAPI siteAPI = clientFactory.getClient(SiteAPI.class);
return createSite(siteAPI);
}

/**
* Creates a new site.
*
* @param siteAPI The site API to use to create the site.
* @return The name of the newly created test site.
*/
public String createSite(final SiteAPI siteAPI) {

// Creating a new test site
final String newSiteName = String.format("site-%d", System.currentTimeMillis());
Expand All @@ -149,7 +158,7 @@ protected String createSite() {
Assertions.assertNotNull(createSiteResponse);
// Publish the new site
siteAPI.publish(createSiteResponse.entity().identifier());
Assertions.assertTrue(siteExist(newSiteName),
Assertions.assertTrue(siteExist(newSiteName, siteAPI),
String.format("Site %s was not created", newSiteName));

return newSiteName;
Expand Down

0 comments on commit 9cb9f91

Please sign in to comment.