Skip to content

Commit

Permalink
Filter out orphan elements when searching (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebhs authored Jun 19, 2024
1 parent 9c492ef commit ff1be4d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/main/java/org/gridsuite/directory/server/DirectoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,14 +587,25 @@ public String getDuplicateNameCandidate(UUID directoryUuid, String elementName,

public List<DirectoryElementInfos> searchElements(@NonNull String userInput) {
return directoryElementInfosService.searchElements(userInput)
.stream()
.map(e -> {
List<ElementAttributes> path = getPath(e.getParentId());
e.setPathUuid(path.stream().map(ElementAttributes::getElementUuid).toList());
e.setPathName(path.stream().map(ElementAttributes::getElementName).toList());
return e;
})
.toList();
.stream()
.map(this::populatePathInfo)
.filter(Objects::nonNull)
.toList();
}

private DirectoryElementInfos populatePathInfo(DirectoryElementInfos elementInfos) {
try {
List<ElementAttributes> path = getPath(elementInfos.getParentId());
elementInfos.setPathUuid(path.stream().map(ElementAttributes::getElementUuid).toList());
elementInfos.setPathName(path.stream().map(ElementAttributes::getElementName).toList());
return elementInfos;
} catch (DirectoryException ex) {
if (ex.getType() == DirectoryException.Type.NOT_FOUND) {
LOGGER.error("Error retrieving path for element: '{}' : {}", elementInfos, ex.getMessage());
return null;
}
throw ex;
}
}

public boolean areDirectoryElementsDeletable(List<UUID> elementsUuid, String userId) {
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/org/gridsuite/directory/server/DirectoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1768,4 +1768,42 @@ private void assertQueuesEmptyThenClear(List<String> destinations) {
output.clear(); // purge in order to not fail the other tests
}
}

@Test
public void testSearchWithOrphans() throws Exception {
// root
// / \
// dir1 dir2
ElementAttributes rootDirectory = retrieveInsertAndCheckRootDirectory("directory", USERID_1);
UUID rootDirectoryUuid = rootDirectory.getElementUuid();
// dir1
UUID subDirUuid1 = UUID.randomUUID();
ElementAttributes subDirAttributes1 = toElementAttributes(subDirUuid1, "newSubDir1", DIRECTORY, USERID_1);
insertAndCheckSubElement(rootDirectoryUuid, subDirAttributes1);
insertAndCheckSubElement(subDirUuid1, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, STUDY, USERID_1, ""));
// dir2
UUID subDirUuid2 = UUID.randomUUID();
ElementAttributes subDirAttributes2 = toElementAttributes(subDirUuid2, "newSubDir2", DIRECTORY, USERID_1);
insertAndCheckSubElement(rootDirectoryUuid, subDirAttributes2);
insertAndCheckSubElement(subDirUuid2, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, STUDY, USERID_1, ""));

MvcResult mvcResult = mockMvc
.perform(get("/v1/elements/indexation-infos?userInput={request}", "r").header(USER_ID, USERID_1))
.andExpectAll(status().isOk()).andReturn();
List<Object> result = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>() { });
assertEquals(2, result.size());
output.clear();

// root
// / \
// dir1 (deleted but keeping its sub-elements) dir2
directoryElementRepository.deleteById(subDirUuid1);

mvcResult = mockMvc
.perform(get("/v1/elements/indexation-infos?userInput={request}", "r").header(USER_ID, USERID_1))
.andExpectAll(status().isOk()).andReturn();
result = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), new TypeReference<>() { });
assertEquals(1, result.size());
output.clear();
}
}

0 comments on commit ff1be4d

Please sign in to comment.