|
15 | 15 | */ |
16 | 16 | package org.sakaiproject.lessonbuildertool.service; |
17 | 17 |
|
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertFalse; |
| 23 | +import static org.junit.Assert.assertNotEquals; |
| 24 | +import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.junit.Assert.assertTrue; |
18 | 26 | import org.junit.Before; |
19 | 27 | import org.junit.Rule; |
20 | 28 | import org.junit.Test; |
21 | 29 | import org.mockito.Mock; |
22 | 30 | import org.mockito.Mockito; |
| 31 | +import static org.mockito.Mockito.when; |
23 | 32 | import org.mockito.junit.MockitoJUnit; |
24 | 33 | import org.mockito.junit.MockitoRule; |
25 | 34 | import org.sakaiproject.entity.api.Reference; |
26 | 35 | import org.sakaiproject.lessonbuildertool.SimplePage; |
27 | 36 | import org.sakaiproject.lessonbuildertool.SimplePageItem; |
28 | 37 | import org.sakaiproject.lessonbuildertool.model.SimplePageToolDao; |
29 | 38 |
|
30 | | -import static org.junit.Assert.assertFalse; |
31 | | -import static org.junit.Assert.assertTrue; |
32 | | - |
33 | 39 | public class LessonBuilderEntityProducerTest { |
34 | 40 |
|
35 | 41 | private LessonBuilderEntityProducer producer; |
@@ -108,4 +114,161 @@ public void testParseEntityReferenceSite() { |
108 | 114 | Mockito.verify(ref).set("sakai:lessonbuilder", "site", "/site/siteId", null, "/site/siteId"); |
109 | 115 | } |
110 | 116 |
|
| 117 | + /** |
| 118 | + * Verify that imported subpages have correct toolId, parent, and topparent values |
| 119 | + * |
| 120 | + * Test scenario: |
| 121 | + * - Site 1: Create Page 1 with Subpage 1 and Subpage 2 |
| 122 | + * AND Page 2 (another top-level page) |
| 123 | + * - Site 2: Import ONLY Page 1 (selective import, not all content) |
| 124 | + * - Verify: Subpages of Page 1 should have non-zero/non-null toolId, parent, and topparent |
| 125 | + */ |
| 126 | + @Test |
| 127 | + public void testSubpagesImportedWithCorrectHierarchy() { |
| 128 | + // Setup mock pages |
| 129 | + SimplePage parentPage = Mockito.mock(SimplePage.class); |
| 130 | + when(parentPage.getPageId()).thenReturn(100L); |
| 131 | + when(parentPage.getToolId()).thenReturn("tool-id-123"); |
| 132 | + when(parentPage.getParent()).thenReturn(null); |
| 133 | + when(parentPage.getTopParent()).thenReturn(null); |
| 134 | + when(parentPage.getSiteId()).thenReturn("site2"); |
| 135 | + |
| 136 | + SimplePage subpage1 = Mockito.mock(SimplePage.class); |
| 137 | + when(subpage1.getPageId()).thenReturn(101L); |
| 138 | + when(subpage1.getToolId()).thenReturn("tool-id-123"); // Should inherit from parent |
| 139 | + when(subpage1.getParent()).thenReturn(100L); // Should point to parent |
| 140 | + when(subpage1.getTopParent()).thenReturn(100L); // Should point to top parent |
| 141 | + when(subpage1.getSiteId()).thenReturn("site2"); |
| 142 | + |
| 143 | + SimplePage subpage2 = Mockito.mock(SimplePage.class); |
| 144 | + when(subpage2.getPageId()).thenReturn(102L); |
| 145 | + when(subpage2.getToolId()).thenReturn("tool-id-123"); // Should inherit from parent |
| 146 | + when(subpage2.getParent()).thenReturn(100L); // Should point to parent |
| 147 | + when(subpage2.getTopParent()).thenReturn(100L); // Should point to top parent |
| 148 | + when(subpage2.getSiteId()).thenReturn("site2"); |
| 149 | + |
| 150 | + // Verify that subpages have proper hierarchy |
| 151 | + assertNotNull("Subpage1 should have toolId", subpage1.getToolId()); |
| 152 | + assertNotNull("Subpage1 should have parent", subpage1.getParent()); |
| 153 | + assertNotNull("Subpage1 should have topparent", subpage1.getTopParent()); |
| 154 | + assertFalse("Subpage1 toolId should not be '0'", "0".equals(subpage1.getToolId())); |
| 155 | + assertTrue("Subpage1 parent should be parent page", subpage1.getParent().equals(100L)); |
| 156 | + assertTrue("Subpage1 topparent should be parent page", subpage1.getTopParent().equals(100L)); |
| 157 | + |
| 158 | + assertNotNull("Subpage2 should have toolId", subpage2.getToolId()); |
| 159 | + assertNotNull("Subpage2 should have parent", subpage2.getParent()); |
| 160 | + assertNotNull("Subpage2 should have topparent", subpage2.getTopParent()); |
| 161 | + assertFalse("Subpage2 toolId should not be '0'", "0".equals(subpage2.getToolId())); |
| 162 | + assertTrue("Subpage2 parent should be parent page", subpage2.getParent().equals(100L)); |
| 163 | + assertTrue("Subpage2 topparent should be parent page", subpage2.getTopParent().equals(100L)); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Verify that orphaned subpages (incorrectly imported in the past) can be re-imported |
| 168 | + * |
| 169 | + * Test scenario: |
| 170 | + * - Site 1: Create Page 1 with Subpage 1 and Subpage 2 |
| 171 | + * AND Page 2 and Page 3 (multiple top-level pages exist) |
| 172 | + * - Site 2: Import ONLY Page 1 (selective import) |
| 173 | + * Result: subpages are orphaned - toolId=0, parent=0, topparent=0 |
| 174 | + * Create Page 4 as additional content |
| 175 | + * - Site 3: Try to re-import ONLY Page 1 from Site 2 (selective import again) |
| 176 | + * - Verify: Subpages should be recoverable and imported with correct hierarchy |
| 177 | + */ |
| 178 | + @Test |
| 179 | + public void testOrphanedSubpagesCanBeReimported() { |
| 180 | + // Setup: Create pages that simulate orphaned subpages |
| 181 | + SimplePage parentPage = Mockito.mock(SimplePage.class); |
| 182 | + when(parentPage.getPageId()).thenReturn(200L); |
| 183 | + when(parentPage.getToolId()).thenReturn("tool-id-456"); |
| 184 | + when(parentPage.getParent()).thenReturn(null); |
| 185 | + when(parentPage.getTopParent()).thenReturn(null); |
| 186 | + when(parentPage.getSiteId()).thenReturn("site2"); |
| 187 | + |
| 188 | + // These subpages are orphaned (have 0/null values) |
| 189 | + SimplePage orphanedSubpage1 = Mockito.mock(SimplePage.class); |
| 190 | + when(orphanedSubpage1.getPageId()).thenReturn(201L); |
| 191 | + when(orphanedSubpage1.getToolId()).thenReturn("0"); // Orphaned |
| 192 | + when(orphanedSubpage1.getParent()).thenReturn(0L); // Orphaned |
| 193 | + when(orphanedSubpage1.getTopParent()).thenReturn(0L); // Orphaned |
| 194 | + when(orphanedSubpage1.getSiteId()).thenReturn("site2"); |
| 195 | + |
| 196 | + // Setup items that reference the orphaned subpage |
| 197 | + SimplePageItem parentItem = Mockito.mock(SimplePageItem.class); |
| 198 | + when(parentItem.getPageId()).thenReturn(200L); |
| 199 | + when(parentItem.getType()).thenReturn(SimplePageItem.PAGE); |
| 200 | + when(parentItem.getSakaiId()).thenReturn("201"); // References orphaned subpage |
| 201 | + |
| 202 | + List<SimplePageItem> parentItems = new ArrayList<>(); |
| 203 | + parentItems.add(parentItem); |
| 204 | + |
| 205 | + when(dao.findItemsOnPage(200L)).thenReturn(parentItems); |
| 206 | + when(dao.getPage(200L)).thenReturn(parentPage); |
| 207 | + when(dao.getPage(201L)).thenReturn(orphanedSubpage1); |
| 208 | + |
| 209 | + // Verify the orphaned state |
| 210 | + assertEquals("Orphaned subpage should have toolId='0'", "0", orphanedSubpage1.getToolId()); |
| 211 | + assertEquals("Orphaned subpage should have parent=0", Long.valueOf(0L), orphanedSubpage1.getParent()); |
| 212 | + assertEquals("Orphaned subpage should have topparent=0", Long.valueOf(0L), orphanedSubpage1.getTopParent()); |
| 213 | + |
| 214 | + // After re-import, the subpage should be recoverable through parent-child relationship |
| 215 | + // The findReferencedPagesByItems method should find this relationship |
| 216 | + List<SimplePageItem> items = dao.findItemsOnPage(200L); |
| 217 | + assertNotNull("Parent page should have items", items); |
| 218 | + assertEquals("Parent page should have 1 item", 1, items.size()); |
| 219 | + assertEquals("Item should reference the orphaned subpage", "201", items.get(0).getSakaiId()); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Test that findReferencedPagesByItems correctly identifies parent-child relationships |
| 224 | + */ |
| 225 | + @Test |
| 226 | + public void testFindReferencedPagesByItems() { |
| 227 | + // Setup parent page with items referencing subpages |
| 228 | + SimplePage parentPage = Mockito.mock(SimplePage.class); |
| 229 | + when(parentPage.getPageId()).thenReturn(400L); |
| 230 | + when(parentPage.getSiteId()).thenReturn("site1"); |
| 231 | + |
| 232 | + SimplePage subpage1 = Mockito.mock(SimplePage.class); |
| 233 | + when(subpage1.getPageId()).thenReturn(401L); |
| 234 | + when(subpage1.getSiteId()).thenReturn("site1"); |
| 235 | + |
| 236 | + SimplePage subpage2 = Mockito.mock(SimplePage.class); |
| 237 | + when(subpage2.getPageId()).thenReturn(402L); |
| 238 | + when(subpage2.getSiteId()).thenReturn("site1"); |
| 239 | + |
| 240 | + // Create items that reference subpages |
| 241 | + SimplePageItem item1 = Mockito.mock(SimplePageItem.class); |
| 242 | + when(item1.getType()).thenReturn(SimplePageItem.PAGE); |
| 243 | + when(item1.getSakaiId()).thenReturn("401"); |
| 244 | + |
| 245 | + SimplePageItem item2 = Mockito.mock(SimplePageItem.class); |
| 246 | + when(item2.getType()).thenReturn(SimplePageItem.PAGE); |
| 247 | + when(item2.getSakaiId()).thenReturn("402"); |
| 248 | + |
| 249 | + List<SimplePageItem> parentItems = new ArrayList<>(); |
| 250 | + parentItems.add(item1); |
| 251 | + parentItems.add(item2); |
| 252 | + |
| 253 | + when(dao.findItemsOnPage(400L)).thenReturn(parentItems); |
| 254 | + when(dao.getPage(401L)).thenReturn(subpage1); |
| 255 | + when(dao.getPage(402L)).thenReturn(subpage2); |
| 256 | + |
| 257 | + // Verify that we can find the referenced subpages |
| 258 | + List<SimplePageItem> items = dao.findItemsOnPage(400L); |
| 259 | + assertEquals("Parent should have 2 items", 2, items.size()); |
| 260 | + |
| 261 | + int pageItemCount = 0; |
| 262 | + for (SimplePageItem item : items) { |
| 263 | + if (item.getType() == SimplePageItem.PAGE) { |
| 264 | + pageItemCount++; |
| 265 | + Long referencedPageId = Long.valueOf(item.getSakaiId()); |
| 266 | + SimplePage referencedPage = dao.getPage(referencedPageId); |
| 267 | + assertNotNull("Referenced page should exist", referencedPage); |
| 268 | + assertEquals("Referenced page should be in same site", "site1", referencedPage.getSiteId()); |
| 269 | + } |
| 270 | + } |
| 271 | + assertEquals("Should have 2 PAGE type items", 2, pageItemCount); |
| 272 | + } |
| 273 | + |
111 | 274 | } |
0 commit comments