Skip to content

Commit

Permalink
Add endpoint to get directory uuid from directory path. (#155)
Browse files Browse the repository at this point in the history
* Add endpoint to get directory uuid from directory path.
* Handle the case of directory name containing the '/' character.
* Add test for directory name containing the ',' character.

Signed-off-by: Franck LECUYER <franck.lecuyer@rte-france.com>
  • Loading branch information
FranckLecuyer authored Aug 2, 2024
1 parent f301a99 commit 8c015bb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -260,4 +262,13 @@ public ResponseEntity<Page<DirectoryElementInfos>> 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<UUID> getDirectoryUuidFromPath(@RequestParam("directoryPath") List<String> directoryPath) {
List<String> decodedDirectoryPath = directoryPath.stream().map(s -> URLDecoder.decode(s, StandardCharsets.UTF_8)).toList();
return ResponseEntity.ok().body(service.getDirectoryUuidFromPath(decodedDirectoryPath));
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/gridsuite/directory/server/DirectoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,18 @@ public Page<DirectoryElementInfos> searchElements(@NonNull String userInput, Str
public boolean areDirectoryElementsDeletable(List<UUID> elementsUuid, String userId) {
return getElements(elementsUuid, true, List.of()).stream().allMatch(e -> isDirectoryElementDeletable(e, userId));
}

public UUID getDirectoryUuidFromPath(List<String> 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;
}
}
109 changes: 109 additions & 0 deletions src/test/java/org/gridsuite/directory/server/DirectoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T> List<T> mvcResultToList(MvcResult mvcResult) throws Exception {
JsonNode resultJson = objectMapper.readTree(mvcResult.getResponse().getContentAsString());
ObjectReader resultReader = objectMapper.readerFor(new TypeReference<>() { });
Expand Down

0 comments on commit 8c015bb

Please sign in to comment.