|
| 1 | +package com.dotmarketing.startup.runonce; |
| 2 | + |
| 3 | +import com.dotmarketing.common.db.DotConnect; |
| 4 | +import com.dotmarketing.exception.DotDataException; |
| 5 | +import com.dotcms.util.IntegrationTestInitService; |
| 6 | +import org.junit.BeforeClass; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import static org.junit.Assert.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * Integration test for Task251029RemoveContentTypesLegacyPortletFromLayouts |
| 13 | + * Tests that the startup task properly removes legacy content-types portlet from layouts. |
| 14 | + * |
| 15 | + * @author Neeha Kethi |
| 16 | + */ |
| 17 | +public class Task251029RemoveContentTypesLegacyPortletFromLayoutsTest { |
| 18 | + |
| 19 | + @BeforeClass |
| 20 | + public static void prepare() throws Exception { |
| 21 | + // Initialize the H2 DB |
| 22 | + IntegrationTestInitService.getInstance().init(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Test the forceRun method - should return true when legacy portlets exist |
| 27 | + */ |
| 28 | + @Test |
| 29 | + public void test_forceRun_should_return_true_when_legacy_portlets_exist() throws DotDataException { |
| 30 | + // Setup: Insert a test legacy portlet entry |
| 31 | + new DotConnect() |
| 32 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 33 | + .addParam("test-legacy-id-1") |
| 34 | + .addParam("test-layout-id") |
| 35 | + .addParam("content-types") |
| 36 | + .addParam(1) |
| 37 | + .loadResult(); |
| 38 | + |
| 39 | + try { |
| 40 | + // Test: forceRun should return true when legacy portlets exist |
| 41 | + Task251029RemoveContentTypesLegacyPortletFromLayouts task = new Task251029RemoveContentTypesLegacyPortletFromLayouts(); |
| 42 | + assertTrue("forceRun should return true when content-types portlets exist in layouts", |
| 43 | + task.forceRun()); |
| 44 | + } finally { |
| 45 | + // Cleanup: Remove test data |
| 46 | + new DotConnect() |
| 47 | + .setSQL("DELETE FROM cms_layouts_portlets WHERE id = ?") |
| 48 | + .addParam("test-legacy-id-1") |
| 49 | + .loadResult(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Test the forceRun method - should return false when no legacy portlets exist |
| 55 | + */ |
| 56 | + @Test |
| 57 | + public void test_forceRun_should_return_false_when_no_legacy_portlets_exist() throws DotDataException { |
| 58 | + // Ensure no legacy portlets exist |
| 59 | + new DotConnect() |
| 60 | + .setSQL("DELETE FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 61 | + .addParam("content-types") |
| 62 | + .loadResult(); |
| 63 | + |
| 64 | + // Test: forceRun should return false when no legacy portlets exist |
| 65 | + Task251029RemoveContentTypesLegacyPortletFromLayouts task = new Task251029RemoveContentTypesLegacyPortletFromLayouts(); |
| 66 | + assertFalse("forceRun should return false when no content-types portlets exist in layouts", |
| 67 | + task.forceRun()); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test the executeUpgrade method - should remove all legacy portlets |
| 72 | + */ |
| 73 | + @Test |
| 74 | + public void test_executeUpgrade_should_remove_all_legacy_portlets() throws Exception { |
| 75 | + // Setup: Insert multiple test legacy portlet entries |
| 76 | + new DotConnect() |
| 77 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 78 | + .addParam("test-legacy-id-2") |
| 79 | + .addParam("test-layout-id-1") |
| 80 | + .addParam("content-types") |
| 81 | + .addParam(1) |
| 82 | + .loadResult(); |
| 83 | + |
| 84 | + new DotConnect() |
| 85 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 86 | + .addParam("test-legacy-id-3") |
| 87 | + .addParam("test-layout-id-2") |
| 88 | + .addParam("content-types") |
| 89 | + .addParam(2) |
| 90 | + .loadResult(); |
| 91 | + |
| 92 | + try { |
| 93 | + // Verify setup: Should have legacy portlets before execution |
| 94 | + int countBefore = new DotConnect() |
| 95 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 96 | + .addParam("content-types") |
| 97 | + .getInt("count"); |
| 98 | + assertTrue("Should have legacy portlets before executeUpgrade", countBefore >= 2); |
| 99 | + |
| 100 | + // Test: Execute the upgrade task |
| 101 | + Task251029RemoveContentTypesLegacyPortletFromLayouts task = new Task251029RemoveContentTypesLegacyPortletFromLayouts(); |
| 102 | + task.executeUpgrade(); |
| 103 | + |
| 104 | + // Verify: No legacy portlets should exist after execution |
| 105 | + int countAfter = new DotConnect() |
| 106 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 107 | + .addParam("content-types") |
| 108 | + .getInt("count"); |
| 109 | + assertEquals("All content-types portlets should be removed after executeUpgrade", 0, countAfter); |
| 110 | + |
| 111 | + // Verify: forceRun should return false after execution |
| 112 | + assertFalse("forceRun should return false after executeUpgrade removes all legacy portlets", |
| 113 | + task.forceRun()); |
| 114 | + |
| 115 | + } finally { |
| 116 | + // Cleanup: Ensure test data is removed |
| 117 | + new DotConnect() |
| 118 | + .setSQL("DELETE FROM cms_layouts_portlets WHERE id IN (?, ?)") |
| 119 | + .addParam("test-legacy-id-2") |
| 120 | + .addParam("test-legacy-id-3") |
| 121 | + .loadResult(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test the executeUpgrade method is idempotent - can run multiple times safely |
| 127 | + */ |
| 128 | + @Test |
| 129 | + public void test_executeUpgrade_is_idempotent() throws Exception { |
| 130 | + // Setup: Insert a test legacy portlet entry |
| 131 | + new DotConnect() |
| 132 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 133 | + .addParam("test-legacy-id-4") |
| 134 | + .addParam("test-layout-id-3") |
| 135 | + .addParam("content-types") |
| 136 | + .addParam(1) |
| 137 | + .loadResult(); |
| 138 | + |
| 139 | + try { |
| 140 | + Task251029RemoveContentTypesLegacyPortletFromLayouts task = new Task251029RemoveContentTypesLegacyPortletFromLayouts(); |
| 141 | + |
| 142 | + // First execution |
| 143 | + task.executeUpgrade(); |
| 144 | + int countAfterFirst = new DotConnect() |
| 145 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 146 | + .addParam("content-types") |
| 147 | + .getInt("count"); |
| 148 | + |
| 149 | + // Second execution (should not cause errors) |
| 150 | + task.executeUpgrade(); |
| 151 | + int countAfterSecond = new DotConnect() |
| 152 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 153 | + .addParam("content-types") |
| 154 | + .getInt("count"); |
| 155 | + |
| 156 | + // Both should result in 0 legacy portlets |
| 157 | + assertEquals("First execution should remove all legacy portlets", 0, countAfterFirst); |
| 158 | + assertEquals("Second execution should maintain 0 legacy portlets", 0, countAfterSecond); |
| 159 | + assertEquals("Multiple executions should be idempotent", countAfterFirst, countAfterSecond); |
| 160 | + |
| 161 | + } finally { |
| 162 | + // Cleanup: Remove any remaining test data |
| 163 | + new DotConnect() |
| 164 | + .setSQL("DELETE FROM cms_layouts_portlets WHERE id = ?") |
| 165 | + .addParam("test-legacy-id-4") |
| 166 | + .loadResult(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Test that other portlets are not affected by the task |
| 172 | + */ |
| 173 | + @Test |
| 174 | + public void test_executeUpgrade_preserves_other_portlets() throws Exception { |
| 175 | + // Setup: Insert test entries for both legacy and modern portlets |
| 176 | + new DotConnect() |
| 177 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 178 | + .addParam("test-legacy-id-5") |
| 179 | + .addParam("test-layout-id-4") |
| 180 | + .addParam("content-types") |
| 181 | + .addParam(1) |
| 182 | + .loadResult(); |
| 183 | + |
| 184 | + new DotConnect() |
| 185 | + .setSQL("INSERT INTO cms_layouts_portlets (id, layout_id, portlet_id, portlet_order) VALUES (?, ?, ?, ?)") |
| 186 | + .addParam("test-modern-id-1") |
| 187 | + .addParam("test-layout-id-4") |
| 188 | + .addParam("content-types-angular") |
| 189 | + .addParam(2) |
| 190 | + .loadResult(); |
| 191 | + |
| 192 | + try { |
| 193 | + // Verify setup |
| 194 | + int legacyCountBefore = new DotConnect() |
| 195 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 196 | + .addParam("content-types") |
| 197 | + .getInt("count"); |
| 198 | + int modernCountBefore = new DotConnect() |
| 199 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 200 | + .addParam("content-types-angular") |
| 201 | + .getInt("count"); |
| 202 | + |
| 203 | + assertTrue("Should have legacy portlet before execution", legacyCountBefore >= 1); |
| 204 | + assertTrue("Should have modern portlet before execution", modernCountBefore >= 1); |
| 205 | + |
| 206 | + // Test: Execute the upgrade task |
| 207 | + Task251029RemoveContentTypesLegacyPortletFromLayouts task = new Task251029RemoveContentTypesLegacyPortletFromLayouts(); |
| 208 | + task.executeUpgrade(); |
| 209 | + |
| 210 | + // Verify results |
| 211 | + int legacyCountAfter = new DotConnect() |
| 212 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 213 | + .addParam("content-types") |
| 214 | + .getInt("count"); |
| 215 | + int modernCountAfter = new DotConnect() |
| 216 | + .setSQL("SELECT count(*) as count FROM cms_layouts_portlets WHERE portlet_id = ?") |
| 217 | + .addParam("content-types-angular") |
| 218 | + .getInt("count"); |
| 219 | + |
| 220 | + assertEquals("Legacy portlets should be removed", 0, legacyCountAfter); |
| 221 | + assertEquals("Modern portlets should be preserved", modernCountBefore, modernCountAfter); |
| 222 | + |
| 223 | + } finally { |
| 224 | + // Cleanup |
| 225 | + new DotConnect() |
| 226 | + .setSQL("DELETE FROM cms_layouts_portlets WHERE id IN (?, ?)") |
| 227 | + .addParam("test-legacy-id-5") |
| 228 | + .addParam("test-modern-id-1") |
| 229 | + .loadResult(); |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments