diff --git a/src/main/java/org/gridsuite/directory/server/DirectoryController.java b/src/main/java/org/gridsuite/directory/server/DirectoryController.java index 00f7355e..9cf9a960 100644 --- a/src/main/java/org/gridsuite/directory/server/DirectoryController.java +++ b/src/main/java/org/gridsuite/directory/server/DirectoryController.java @@ -21,6 +21,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.List; import java.util.UUID; @@ -260,4 +262,13 @@ public ResponseEntity> searchElements( return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON) .body(service.searchElements(userInput, directoryUuid)); } + + @GetMapping(value = "/directories/uuid") + @Operation(summary = "Get directory uuid from given path") + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The directory uuid"), + @ApiResponse(responseCode = "404", description = "The directory was not found")}) + public ResponseEntity getDirectoryUuidFromPath(@RequestParam("directoryPath") List directoryPath) { + List decodedDirectoryPath = directoryPath.stream().map(s -> URLDecoder.decode(s, StandardCharsets.UTF_8)).toList(); + return ResponseEntity.ok().body(service.getDirectoryUuidFromPath(decodedDirectoryPath)); + } } diff --git a/src/main/java/org/gridsuite/directory/server/DirectoryService.java b/src/main/java/org/gridsuite/directory/server/DirectoryService.java index 778b29a7..09f2f3c0 100644 --- a/src/main/java/org/gridsuite/directory/server/DirectoryService.java +++ b/src/main/java/org/gridsuite/directory/server/DirectoryService.java @@ -576,4 +576,18 @@ public Page searchElements(@NonNull String userInput, Str public boolean areDirectoryElementsDeletable(List elementsUuid, String userId) { return getElements(elementsUuid, true, List.of()).stream().allMatch(e -> isDirectoryElementDeletable(e, userId)); } + + public UUID getDirectoryUuidFromPath(List directoryPath) { + UUID parentDirectoryUuid = null; + + for (String s : directoryPath) { + UUID currentDirectoryUuid = getDirectoryUuid(s, parentDirectoryUuid); + if (currentDirectoryUuid == null) { + throw new DirectoryException(NOT_FOUND); + } else { + parentDirectoryUuid = currentDirectoryUuid; + } + } + return parentDirectoryUuid; + } } diff --git a/src/test/java/org/gridsuite/directory/server/DirectoryTest.java b/src/test/java/org/gridsuite/directory/server/DirectoryTest.java index 93b8a14c..f16ea932 100644 --- a/src/test/java/org/gridsuite/directory/server/DirectoryTest.java +++ b/src/test/java/org/gridsuite/directory/server/DirectoryTest.java @@ -1800,6 +1800,115 @@ public void testCountUserCases() throws Exception { assertEquals("1", result.getResponse().getContentAsString()); } + @Test + public void testGetDirectoryFromPath() throws Exception { + + // root (userId2) + // / | \ + // dir1 (userId1) dir2 (userId2) dir3 (userId3) + // | | + // dir4 (userId1) dir5 (userId3) + // / | \ + // 'a,b,c' 'dir6/dir7/dir8' '&~#{[^repert' + + ElementAttributes rootDirectory = retrieveInsertAndCheckRootDirectory("root", USERID_2); + UUID rootDirectoryUuid = rootDirectory.getElementUuid(); + UUID subDirUuid1 = UUID.randomUUID(); + ElementAttributes subDirAttributes1 = toElementAttributes(subDirUuid1, "dir1", DIRECTORY, USERID_1); + UUID subDirUuid2 = UUID.randomUUID(); + ElementAttributes subDirAttributes2 = toElementAttributes(subDirUuid2, "dir2", DIRECTORY, USERID_2); + UUID subDirUuid3 = UUID.randomUUID(); + ElementAttributes subDirAttributes3 = toElementAttributes(subDirUuid3, "dir3", DIRECTORY, USERID_3); + UUID subDirUuid4 = UUID.randomUUID(); + ElementAttributes subDirAttributes4 = toElementAttributes(subDirUuid4, "dir4", DIRECTORY, USERID_1); + UUID subDirUuid5 = UUID.randomUUID(); + ElementAttributes subDirAttributes5 = toElementAttributes(subDirUuid5, "dir5", DIRECTORY, USERID_3); + UUID subDirUuid6 = UUID.randomUUID(); + String encodedPath = "%26~%23%7B%5B%5Erepert"; + String decodedPath = "&~#{[^repert"; + ElementAttributes subDirAttributes6 = toElementAttributes(subDirUuid6, decodedPath, DIRECTORY, USERID_3); + UUID subDirUuid7 = UUID.randomUUID(); + ElementAttributes subDirAttributes7 = toElementAttributes(subDirUuid7, "dir6/dir7/dir8", DIRECTORY, USERID_1); + UUID subDirUuid8 = UUID.randomUUID(); + ElementAttributes subDirAttributes8 = toElementAttributes(subDirUuid8, "a,b,c", DIRECTORY, USERID_1); + + insertAndCheckSubElement(rootDirectoryUuid, subDirAttributes1); + insertAndCheckSubElement(rootDirectoryUuid, subDirAttributes2); + insertAndCheckSubElement(rootDirectoryUuid, subDirAttributes3); + insertAndCheckSubElement(subDirUuid1, subDirAttributes4); + insertAndCheckSubElement(subDirUuid3, subDirAttributes5); + insertAndCheckSubElement(subDirUuid5, subDirAttributes6); + insertAndCheckSubElement(subDirUuid5, subDirAttributes7); + insertAndCheckSubElement(subDirUuid5, subDirAttributes8); + + insertAndCheckSubElement(subDirUuid1, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, TYPE_01, USERID_1, "")); + insertAndCheckSubElement(subDirUuid2, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, TYPE_01, USERID_2, "")); + insertAndCheckSubElement(subDirUuid3, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, TYPE_01, USERID_3, "")); + insertAndCheckSubElement(subDirUuid4, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, TYPE_01, USERID_1, "")); + insertAndCheckSubElement(subDirUuid5, toElementAttributes(UUID.randomUUID(), RECOLLEMENT, TYPE_01, USERID_3, "")); + + MvcResult mvcResult; + + // existing root directory + mvcResult = mockMvc + .perform(get("/v1/directories/uuid?directoryPath=" + "root") + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isOk()).andReturn(); + UUID resultUuid = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), UUID.class); + assertEquals(rootDirectory.getElementUuid(), resultUuid); + output.clear(); + + // existing non root directory + mvcResult = mockMvc + .perform(get("/v1/directories/uuid?directoryPath=" + "root,dir1,dir4") + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isOk()).andReturn(); + resultUuid = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), UUID.class); + assertEquals(subDirAttributes4.getElementUuid(), resultUuid); + output.clear(); + + // unexisting directory + mockMvc + .perform(get("/v1/directories/uuid?directoryPath=" + "root,dir1,dir5") + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isNotFound()).andReturn(); + output.clear(); + + // path to element (not a directory) + mockMvc + .perform(get("/v1/directories/uuid?directoryPath=" + "root,dir1,dir4," + RECOLLEMENT) + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isNotFound()).andReturn(); + output.clear(); + + // existing directory with special characters in path + mvcResult = mockMvc + .perform(get("/v1/directories/uuid?directoryPath=root,dir3,dir5," + encodedPath) + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isOk()).andReturn(); + resultUuid = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), UUID.class); + assertEquals(subDirAttributes6.getElementUuid(), resultUuid); + output.clear(); + + // existing directory with '/' character in name + mvcResult = mockMvc + .perform(get("/v1/directories/uuid?directoryPath=root,dir3,dir5,dir6/dir7/dir8") + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isOk()).andReturn(); + resultUuid = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), UUID.class); + assertEquals(subDirAttributes7.getElementUuid(), resultUuid); + output.clear(); + + // existing directory with ',' character in name + mvcResult = mockMvc + .perform(get("/v1/directories/uuid?directoryPath=root,dir3,dir5,a%2Cb%2Cc") + .contentType(MediaType.APPLICATION_JSON)) + .andExpectAll(status().isOk()).andReturn(); + resultUuid = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), UUID.class); + assertEquals(subDirAttributes8.getElementUuid(), resultUuid); + output.clear(); + } + private List mvcResultToList(MvcResult mvcResult) throws Exception { JsonNode resultJson = objectMapper.readTree(mvcResult.getResponse().getContentAsString()); ObjectReader resultReader = objectMapper.readerFor(new TypeReference<>() { });