Skip to content

Commit

Permalink
fix(): When restoring a subtree, do not send a nodeCreated notifica…
Browse files Browse the repository at this point in the history
…tion for each nodes in the subtree but a single notification to `insertSubtree` and all its children.

fix(): RestoreNode now use the `InsertMode.CHILD` to add a new branch instead of trying to insert a node between the parent and already existing children.
It causes unexpected behaviors.

Signed-off-by: sBouzols <[email protected]>
  • Loading branch information
sBouzols committed Dec 19, 2024
1 parent 091d064 commit b9ebcac
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* @author Jacques Borsenberger <jacques.borsenberger at rte-france.com>
*/
public interface NodeRepository extends JpaRepository<NodeEntity, UUID> {
long countByParentNodeIdNode(UUID id);

List<NodeEntity> findAllByParentNodeIdNode(UUID id);

List<NodeEntity> findAllByStudyId(UUID id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,13 @@ public void restoreNode(UUID studyId, List<UUID> nodeIds, UUID anchorNodeId) {
nodeToRestore.setStashed(false);
nodeToRestore.setStashDate(null);
nodesRepository.save(nodeToRestore);
notificationService.emitNodeInserted(studyId, anchorNodeId, nodeId, InsertMode.AFTER, anchorNodeId);
restoreNodeChildren(studyId, nodeId);
boolean hasChildren = nodesRepository.countByParentNodeIdNode(nodeId) > 0;
if (hasChildren) {
restoreNodeChildren(studyId, nodeId);
notificationService.emitSubtreeInserted(studyId, nodeId, anchorNodeId);
} else {
notificationService.emitNodeInserted(studyId, anchorNodeId, nodeId, InsertMode.CHILD, anchorNodeId);
}
}
}

Expand Down Expand Up @@ -626,7 +631,6 @@ private void restoreNodeChildren(UUID studyId, UUID parentNodeId) {
nodeEntity.setStashed(false);
nodeEntity.setStashDate(null);
nodesRepository.save(nodeEntity);
notificationService.emitNodeInserted(studyId, parentNodeId, nodeEntity.getIdNode(), InsertMode.AFTER, parentNodeId);
restoreNodeChildren(studyId, nodeEntity.getIdNode());
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ void testNodeStashAndRestore() throws Exception {
assertTrue(result.getResponse().getContentAsString().contains(n4.getId().toString()));

//And now we restore the tree we just stashed
restoreNode(studyId, List.of(stashedNode1.getIdNode(), stashedNode4.getIdNode()), root.getId(), Set.of(n1.getId(), n2.getId(), n3.getId(), n4.getId()), userId);
restoreNode(studyId, List.of(stashedNode1.getIdNode(), stashedNode4.getIdNode()), root.getId(), userId);

var restoredNode1 = nodeRepository.findById(n1.getId()).orElseThrow();
assertFalse(restoredNode1.isStashed());
Expand Down Expand Up @@ -725,15 +725,15 @@ private void stashNode(UUID studyUuid, AbstractNode child, boolean stashChildren
assertTrue(expectedStash.stream().anyMatch(node -> node.getId().equals(id))));
}

private void restoreNode(UUID studyUuid, List<UUID> nodeId, UUID anchorNodeId, Set<UUID> expectedIdRestored, String userId) throws Exception {
private void restoreNode(UUID studyUuid, List<UUID> nodeId, UUID anchorNodeId, String userId) throws Exception {
String param = nodeId.stream().map(UUID::toString).reduce("", (a1, a2) -> a1 + "," + a2).substring(1);
mockMvc.perform(post("/v1/studies/{studyUuid}/tree/nodes/restore?anchorNodeId={anchorNodeId}", studyUuid, anchorNodeId).header(USER_ID_HEADER, "userId")
mockMvc.perform(post("/v1/studies/{studyUuid}/tree/nodes/restore?anchorNodeId={anchorNodeId}", studyUuid, anchorNodeId).header(USER_ID_HEADER, userId)
.queryParam("ids", param))
.andExpect(status().isOk());

for (int i = 0; i < expectedIdRestored.size(); i++) {
for (int i = 0; i < nodeId.size(); i++) {
var message = output.receive(TIMEOUT, STUDY_UPDATE_DESTINATION);
assertTrue(expectedIdRestored.contains(message.getHeaders().get(HEADER_NEW_NODE)));
assertTrue(nodeId.contains(message.getHeaders().get(HEADER_NEW_NODE)) || anchorNodeId.equals(message.getHeaders().get(HEADER_NEW_NODE)));
}
}

Expand Down

0 comments on commit b9ebcac

Please sign in to comment.