Skip to content

Commit b6fee4d

Browse files
committed
coderabbitai IV tests
1 parent 6ea643c commit b6fee4d

File tree

2 files changed

+65
-69
lines changed

2 files changed

+65
-69
lines changed

lessonbuilder/tool/src/java/org/sakaiproject/lessonbuildertool/service/LessonBuilderEntityProducer.java

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,55 @@ public Map<Long, List<Long>> findReferencedPagesByItems(String siteId) {
886886
return pageToReferencedPages;
887887
}
888888

889+
/**
890+
* Builds a parent map from subpage references, considering only pages in pageMap (for selective import).
891+
*
892+
* @param subpageRefs Map of parent page IDs to lists of child page IDs
893+
* @param pageMap Map of old page IDs to new page IDs (determines which pages are being imported)
894+
* @param calculatedParentMap Output map to populate with child -> parent relationships
895+
*/
896+
Map<Long, Long> buildParentMapFromReferences(Map<Long, List<Long>> subpageRefs, Map<Long, Long> pageMap, Map<Long, Long> calculatedParentMap) {
897+
for (Map.Entry<Long, List<Long>> entry : subpageRefs.entrySet()) {
898+
Long oldParentPageId = entry.getKey();
899+
List<Long> oldChildPageIds = entry.getValue();
900+
901+
if (!pageMap.containsKey(oldParentPageId)) continue;
902+
903+
for (Long oldChildPageId : oldChildPageIds) {
904+
if (pageMap.containsKey(oldChildPageId)) {
905+
calculatedParentMap.put(oldChildPageId, oldParentPageId);
906+
}
907+
}
908+
}
909+
return calculatedParentMap;
910+
}
911+
912+
/**
913+
* Calculates top parent relationships by walking up the parent chain.
914+
*
915+
* @param calculatedParentMap Map of child -> parent relationships
916+
* @param calculatedTopParentMap Output map to populate with page -> top parent relationships
917+
*/
918+
Map<Long, Long> calculateTopParentMap(Map<Long, Long> calculatedParentMap, Map<Long, Long> calculatedTopParentMap) {
919+
for (Long pageId : calculatedParentMap.keySet()) {
920+
Long currentPageId = pageId;
921+
Long topParent = null;
922+
Set<Long> visited = new HashSet<>();
923+
while (calculatedParentMap.containsKey(currentPageId)) {
924+
if (!visited.add(currentPageId)) {
925+
log.warn("Cycle detected in page hierarchy at page {}", currentPageId);
926+
break;
927+
}
928+
topParent = calculatedParentMap.get(currentPageId);
929+
currentPageId = topParent;
930+
}
931+
if (topParent != null) {
932+
calculatedTopParentMap.put(pageId, topParent);
933+
}
934+
}
935+
return calculatedTopParentMap;
936+
}
937+
889938
/**
890939
* Finds the top-level pages that should become new Lessons from the selected pages
891940
* Only considers pages that were originally selected by the user, not those added automatically as references
@@ -1914,18 +1963,7 @@ public String mergeInternal(String siteId, Element root, String archivePath, Str
19141963
Map<Long, List<Long>> subpageRefs = findReferencedPagesByItems(fromSiteId);
19151964

19161965
// Build parent map from subpage references
1917-
for (Map.Entry<Long, List<Long>> entry : subpageRefs.entrySet()) {
1918-
Long oldParentPageId = entry.getKey();
1919-
List<Long> oldChildPageIds = entry.getValue();
1920-
1921-
if (!pageMap.containsKey(oldParentPageId)) continue;
1922-
1923-
for (Long oldChildPageId : oldChildPageIds) {
1924-
if (pageMap.containsKey(oldChildPageId)) {
1925-
calculatedParentMap.put(oldChildPageId, oldParentPageId);
1926-
}
1927-
}
1928-
}
1966+
buildParentMapFromReferences(subpageRefs, pageMap, calculatedParentMap);
19291967

19301968
// For cross-server imports, fall back to XML attributes if subpageRefs is empty
19311969
if (!isSameServer && subpageRefs.isEmpty()) {
@@ -1945,22 +1983,7 @@ public String mergeInternal(String siteId, Element root, String archivePath, Str
19451983
}
19461984

19471985
// Calculate top parents by walking up the tree
1948-
for (Long pageId : calculatedParentMap.keySet()) {
1949-
Long currentPageId = pageId;
1950-
Long topParent = null;
1951-
Set<Long> visited = new HashSet<>();
1952-
while (calculatedParentMap.containsKey(currentPageId)) {
1953-
if (!visited.add(currentPageId)) {
1954-
log.warn("Cycle detected in page hierarchy at page {}", currentPageId);
1955-
break;
1956-
}
1957-
topParent = calculatedParentMap.get(currentPageId);
1958-
currentPageId = topParent;
1959-
}
1960-
if (topParent != null) {
1961-
calculatedTopParentMap.put(pageId, topParent);
1962-
}
1963-
}
1986+
calculateTopParentMap(calculatedParentMap, calculatedTopParentMap);
19641987

19651988
// Apply calculated relationships to imported pages
19661989
int hierarchyUpdates = 0;

lessonbuilder/tool/src/test/org/sakaiproject/lessonbuildertool/service/LessonBuilderEntityProducerTest.java

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -242,39 +242,24 @@ public void testFindReferencedPagesByItemsSkipsOrphans() {
242242
*/
243243
@Test
244244
public void testHierarchyCalculationFromReferences() {
245-
// Simulate the hierarchy calculation logic
246-
Map<Long, List<Long>> subpageRefs = new HashMap<>();
247-
Map<Long, Long> calculatedParentMap = new HashMap<>();
248-
Map<Long, Long> calculatedTopParentMap = new HashMap<>();
249-
250245
// Setup: Page 100 has subpages 101, 102
251246
// Page 101 has subpage 103
247+
Map<Long, List<Long>> subpageRefs = new HashMap<>();
252248
subpageRefs.put(100L, List.of(101L, 102L));
253249
subpageRefs.put(101L, List.of(103L));
254250

255-
// Build parent map (from subpage references)
256-
for (Map.Entry<Long, List<Long>> entry : subpageRefs.entrySet()) {
257-
Long parentPageId = entry.getKey();
258-
List<Long> childPageIds = entry.getValue();
259-
for (Long childPageId : childPageIds) {
260-
calculatedParentMap.put(childPageId, parentPageId);
261-
}
262-
}
263-
264-
// Calculate top parents (walk up the tree)
265-
for (Long pageId : calculatedParentMap.keySet()) {
266-
Long currentPageId = pageId;
267-
Long topParent = null;
268-
269-
while (calculatedParentMap.containsKey(currentPageId)) {
270-
topParent = calculatedParentMap.get(currentPageId);
271-
currentPageId = topParent;
272-
}
273-
274-
if (topParent != null) {
275-
calculatedTopParentMap.put(pageId, topParent);
276-
}
277-
}
251+
// Simulate pageMap (all pages are being imported)
252+
Map<Long, Long> pageMap = new HashMap<>();
253+
pageMap.put(100L, 100L);
254+
pageMap.put(101L, 101L);
255+
pageMap.put(102L, 102L);
256+
pageMap.put(103L, 103L);
257+
258+
Map<Long, Long> calculatedParentMap = new HashMap<>();
259+
Map<Long, Long> calculatedTopParentMap = new HashMap<>();
260+
261+
producer.buildParentMapFromReferences(subpageRefs, pageMap, calculatedParentMap);
262+
producer.calculateTopParentMap(calculatedParentMap, calculatedTopParentMap);
278263

279264
// Verify results
280265
assertEquals("Page 101 parent should be 100", Long.valueOf(100L), calculatedParentMap.get(101L));
@@ -309,19 +294,7 @@ public void testHierarchyCalculationWithSelectiveImport() {
309294

310295
Map<Long, Long> calculatedParentMap = new HashMap<>();
311296

312-
// Build parent map - ONLY for pages in pageMap
313-
for (Map.Entry<Long, List<Long>> entry : subpageRefs.entrySet()) {
314-
Long oldParentPageId = entry.getKey();
315-
List<Long> oldChildPageIds = entry.getValue();
316-
317-
if (!pageMap.containsKey(oldParentPageId)) continue;
318-
319-
for (Long oldChildPageId : oldChildPageIds) {
320-
if (pageMap.containsKey(oldChildPageId)) {
321-
calculatedParentMap.put(oldChildPageId, oldParentPageId);
322-
}
323-
}
324-
}
297+
producer.buildParentMapFromReferences(subpageRefs, pageMap, calculatedParentMap);
325298

326299
// Verify: only page 101 should have parent (102 not imported)
327300
assertEquals("Page 101 should have parent 100", Long.valueOf(100L), calculatedParentMap.get(101L));

0 commit comments

Comments
 (0)