Skip to content

Commit

Permalink
#30296: Adding integration test for Task241009ReplaceLanguagesWithLo…
Browse files Browse the repository at this point in the history
…calesPortlet
  • Loading branch information
victoralfaro-dotcms committed Oct 16, 2024
1 parent 859002d commit 5490af4
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,35 @@
import java.util.Optional;

/**
* Adds the dotAI portlet to all layouts which have API Playground portlet too, if it does not already exists.
* Adds the dotAI portlet to all layouts which have API Playground portlet too, if it does not already exist.
* @author vico
*/
public class Task241009ReplaceLanguagesWithLocalesPortlet implements StartupTask {
public class Task241015ReplaceLanguagesWithLocalesPortlet implements StartupTask {

private static final String LANGUAGES_PORTLET_ID = "languages";
private static final String LOCALES_PORTLET_ID = "locales";
private static final String WORKFLOW_PORTLET_ID = "workflow-schemes";
public static final String LANGUAGES_PORTLET_ID = "languages";
public static final String LOCALES_PORTLET_ID = "locales";
public static final String WORKFLOW_PORTLET_ID = "workflow-schemes";

/**
* Determines if the task should be forced to run.
*
* @return true if no locales portlet is found in any layout, false otherwise.
*/
@Override
public boolean forceRun() {
// if no locales found then flag it as true
return getLayoutPortletsByPortletId(LOCALES_PORTLET_ID).isEmpty();
}

/**
* Executes the upgrade task.
* Adds the locales portlet to layouts containing the languages portlet or the workflow portlet.
* Replaces the languages portlet with the locales portlet.
* Clears the layout cache.
*
* @throws DotDataException if there is an error accessing the database.
* @throws DotRuntimeException if there is a runtime error during execution.
*/
@Override
public void executeUpgrade() throws DotDataException, DotRuntimeException {
final List<Map<String, Object>> languagesLayoutPortlets = getLayoutPortletsByPortletId(LANGUAGES_PORTLET_ID);
Expand All @@ -38,7 +52,7 @@ public void executeUpgrade() throws DotDataException, DotRuntimeException {
final Map<String, Object> row = workflowLayoutPortlets.get(0);
final String layoutId = (String) row.get("layout_id");
final int portletOrder = Optional.ofNullable((Integer) row.get("portlet_order")).orElse(0) + 1;
insertLocale(layoutId, portletOrder);
insertLocalesPortlet(layoutId, portletOrder);
}
} else {
languagesLayoutPortlets.forEach(this::replaceLanguage);
Expand All @@ -49,32 +63,20 @@ public void executeUpgrade() throws DotDataException, DotRuntimeException {

private void replaceLanguage(final Map<String, Object> row) {
final String layoutId = (String) row.get("layout_id");
final int count = Try.of(
() -> new DotConnect()
.setSQL("SELECT COUNT(portlet_id) AS count" +
" FROM cms_layouts_portlets" +
" WHERE layout_id = ? AND portlet_id = ?")
.addParam(layoutId)
.getInt("count"))
.getOrElse(0);
if (count == 0) {
final int portletOrder = Optional.ofNullable((Integer) row.get("portlet_order")).orElse(1);
insertLocale(layoutId, portletOrder);
final int portletOrder = Optional.ofNullable((Integer) row.get("portlet_order")).orElse(1);
insertLocalesPortlet(layoutId, portletOrder);

final String id = (String) row.get("id");
deleteLanguage(id);
}
final String id = (String) row.get("id");
deleteLanguagesPortlet(id);
}

private void deleteLanguage(final String id) {
private void deleteLanguagesPortlet(final String id) {
Try.run(() -> new DotConnect()
.setSQL("DELETE FROM cms_layouts_portlets WHERE id = ?")
.addParam(id)
.loadResult())
.executeStatement(String.format("DELETE FROM cms_layouts_portlets WHERE id = '%s'", id)))
.getOrElseThrow(ex -> new RuntimeException(ex));
}

private static void insertLocale(final String layoutId, final int portletOrder) {
private static void insertLocalesPortlet(final String layoutId, final int portletOrder) {
Try.run(() -> new DotConnect()
.setSQL("INSERT INTO cms_layouts_portlets(id, layout_id, portlet_id, portlet_order)" +
" VALUES (?, ?, ?, ?)")
Expand All @@ -89,7 +91,7 @@ private static void insertLocale(final String layoutId, final int portletOrder)
private List<Map<String, Object>> getLayoutPortletsByPortletId(final String portletId) {
return Try.of(
() -> new DotConnect()
.setSQL("SELECT layout_id FROM cms_layouts_portlets WHERE portlet_id = ?")
.setSQL("SELECT * FROM cms_layouts_portlets WHERE portlet_id = ?")
.addParam(portletId)
.loadObjectResults())
.getOrElse(List.of());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public static List<Class<?>> getStartupRunOnceTaskClasses() {
.add(Task241009CreatePostgresJobQueueTables.class)
.add(Task241013RemoveFullPathLcColumnFromIdentifier.class)
.add(Task241014AddTemplateValueOnContentletIndex.class)
.add(Task241009ReplaceLanguagesWithLocalesPortlet.class)
.add(Task241015ReplaceLanguagesWithLocalesPortlet.class)
.build();
return ret.stream().sorted(classNameComparator).collect(Collectors.toList());
}
Expand Down
4 changes: 3 additions & 1 deletion dotcms-integration/src/test/java/com/dotcms/MainSuite2b.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
import com.dotmarketing.startup.runonce.Task240530AddDotAIPortletToLayoutTest;
import com.dotmarketing.startup.runonce.Task240606AddVariableColumnToWorkflowTest;
import com.dotmarketing.startup.runonce.Task241009CreatePostgresJobQueueTablesTest;
import com.dotmarketing.startup.runonce.Task241015ReplaceLanguagesWithLocalesPortletTest;
import com.dotmarketing.startup.runonce.Task241013RemoveFullPathLcColumnFromIdentifierTest;
import com.dotmarketing.util.ConfigUtilsTest;
import com.dotmarketing.util.ITConfigTest;
Expand Down Expand Up @@ -386,7 +387,8 @@
LegacyJSONObjectRenderTest.class,
Task241013RemoveFullPathLcColumnFromIdentifierTest.class,
Task241009CreatePostgresJobQueueTablesTest.class,
Task241013RemoveFullPathLcColumnFromIdentifierTest.class
Task241013RemoveFullPathLcColumnFromIdentifierTest.class,
Task241015ReplaceLanguagesWithLocalesPortletTest.class
})

public class MainSuite2b {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.dotmarketing.startup.runonce;

import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.common.db.DotConnect;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.util.UUIDUtil;
import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class Task241015ReplaceLanguagesWithLocalesPortletTest {

@BeforeClass
public static void prepare() throws Exception {
// Setting web app environment
IntegrationTestInitService.getInstance().init();
}

/**
* Given the locales portlets are inserted
* When the upgrade task is executed
* Then the locales portlets should be added to the layout
* And the task should not need to be forced to run
*
* @throws SQLException if there is an SQL error
* @throws DotDataException if there is a data access error
*/
@Test
public void test_upgradeTask_success() throws SQLException, DotDataException {
final DotConnect dotConnect = new DotConnect();
final Task241015ReplaceLanguagesWithLocalesPortlet task = new Task241015ReplaceLanguagesWithLocalesPortlet();

insertLocalesPortlets(dotConnect);
assertFalse(task.forceRun());

deleteAnyLocalesPortlets(dotConnect);
assertTrue(task.forceRun());

task.executeUpgrade();
assertFalse(task.forceRun());
}

/**
* Scenario: Upgrade task execution when no languages portlets are present
*
* Given the locales portlets are not present
* When the upgrade task is executed
* Then the locales portlets should be added to the layout
* And the task should not need to be forced to run
*
* @throws SQLException if there is an SQL error
* @throws DotDataException if there is a data access error
*/
@Test
public void test_upgradeTask_noLanguages_success() throws SQLException, DotDataException {
final DotConnect dotConnect = new DotConnect();
final Task241015ReplaceLanguagesWithLocalesPortlet task = new Task241015ReplaceLanguagesWithLocalesPortlet();

assertFalse(task.forceRun());

deleteAnyLanguagesPortlets(dotConnect);
deleteAnyLocalesPortlets(dotConnect);
assertTrue(task.forceRun());

task.executeUpgrade();
assertFalse(task.forceRun());
}

private void insertLocalesPortlets(final DotConnect dotConnect) throws DotDataException {
final List<Map<String, Object>> rows = dotConnect
.setSQL("SELECT layout_id, portlet_order" +
" FROM cms_layouts_portlets" +
" WHERE portlet_id = ?" +
" ORDER BY portlet_order" +
" DESC LIMIT 1")
.addParam(Task241015ReplaceLanguagesWithLocalesPortlet.LANGUAGES_PORTLET_ID)
.loadObjectResults();
if (rows.isEmpty()) {
return;
}

final Map<String, Object> row = rows.get(0);
dotConnect
.setSQL("INSERT INTO cms_layouts_portlets(id, layout_id, portlet_id, portlet_order)" +
" VALUES(?, ?, ?, ?)")
.addParam(UUIDUtil.uuid())
.addParam(row.get("layout_id"))
.addParam(Task241015ReplaceLanguagesWithLocalesPortlet.LOCALES_PORTLET_ID)
.addParam(((Integer) row.get("portlet_order")) + 1)
.loadResult();
}

private void deletePortlets(final DotConnect dotConnect, final String portletId) throws SQLException {
dotConnect
.executeStatement(String.format(
"DELETE FROM cms_layouts_portlets WHERE portlet_id = '%s'",
portletId));
}

private void deleteAnyLocalesPortlets(final DotConnect dotConnect) throws SQLException {
deletePortlets(dotConnect, Task241015ReplaceLanguagesWithLocalesPortlet.LOCALES_PORTLET_ID);
}

private void deleteAnyLanguagesPortlets(final DotConnect dotConnect) throws SQLException {
deletePortlets(dotConnect, Task241015ReplaceLanguagesWithLocalesPortlet.LANGUAGES_PORTLET_ID);
}

}

0 comments on commit 5490af4

Please sign in to comment.