Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove all references to a specific element type. #94

Merged
merged 5 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/main/java/org/gridsuite/explore/server/ExploreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,32 @@ public ResponseEntity<Void> duplicateNetworkModifications(@RequestParam("duplica
exploreService.duplicateNetworkModifications(networkModificationId, targetDirectoryId, userId);
return ResponseEntity.ok().build();
}

@PutMapping(value = "/explore/elements/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Modify an element")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The element has been modified successfully")})
public ResponseEntity<Void> updateElement(
@PathVariable UUID id,
@RequestBody ElementAttributes elementAttributes,
@RequestHeader("userId") String userId) {

exploreService.updateElement(id, elementAttributes, userId);
return ResponseEntity.ok().build();
}

@PutMapping(value = "/explore/elements", params = "targetDirectoryUuid")
@Operation(summary = "Move elements within directory tree")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Elements was successfully updated"),
@ApiResponse(responseCode = "404", description = "The elements or the targeted directory was not found"),
@ApiResponse(responseCode = "403", description = "Not authorized execute this update")
})
public ResponseEntity<Void> moveElementsDirectory(
@RequestParam UUID targetDirectoryUuid,
@RequestBody List<UUID> elementsUuids,
@RequestHeader("userId") String userId) {
exploreService.moveElementsDirectory(elementsUuids, targetDirectoryUuid, userId);
return ResponseEntity.ok().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class DirectoryService implements IDirectoryElementsService {

private static final String PARAM_IDS = "ids";
private static final String PARAM_FOR_DELETION = "forDeletion";
private static final String PARAM_TARGET_DIRECTORY_UUID = "targetDirectoryUuid";

private final Map<String, IDirectoryElementsService> genericServices;
private final RestTemplate restTemplate;
Expand Down Expand Up @@ -174,7 +175,7 @@ public ElementAttributes getElementInfos(UUID elementUuid) {
.getBody();
}

private List<ElementAttributes> getElementsInfos(List<UUID> elementsUuids, List<String> elementTypes) {
public List<ElementAttributes> getElementsInfos(List<UUID> elementsUuids, List<String> elementTypes) {
var ids = elementsUuids.stream().map(UUID::toString).collect(Collectors.joining(","));
String path = UriComponentsBuilder.fromPath(ELEMENTS_SERVER_ROOT_PATH).toUriString() + "?ids=" + ids;

Expand Down Expand Up @@ -281,4 +282,17 @@ public void delete(UUID id, String userId) {
List<ElementAttributes> elementAttributesList = getDirectoryElements(id, userId);
elementAttributesList.forEach(elementAttributes -> deleteElement(elementAttributes.getElementUuid(), userId));
}

public void moveElementsDirectory(List<UUID> elementsUuids, UUID targetDirectoryUuid, String userId) {
String path = UriComponentsBuilder
.fromPath(ELEMENTS_SERVER_ROOT_PATH)
.queryParam(PARAM_TARGET_DIRECTORY_UUID, targetDirectoryUuid)
.toUriString();
HttpHeaders headers = new HttpHeaders();
headers.set(HEADER_USER_ID, userId);
headers.setContentType(MediaType.APPLICATION_JSON);

HttpEntity<List<UUID>> httpEntity = new HttpEntity<>(elementsUuids, headers);
restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.PUT, httpEntity, Void.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,26 @@ public void assertCanCreateCase(String userId) {
}
}
}

public void updateElement(UUID id, ElementAttributes elementAttributes, String userId) {
directoryService.updateElement(id, elementAttributes, userId);
ElementAttributes elementsInfos = directoryService.getElementInfos(id);
// send notification if the study name was updated
notifyStudyUpdate(elementsInfos, userId);
}

public void moveElementsDirectory(List<UUID> elementsUuids, UUID targetDirectoryUuid, String userId) {
directoryService.moveElementsDirectory(elementsUuids, targetDirectoryUuid, userId);
//send notification to all studies
List<ElementAttributes> elementsAttributes = directoryService.getElementsInfos(elementsUuids, null);
elementsAttributes.forEach(elementAttributes -> notifyStudyUpdate(elementAttributes, userId));

}

private void notifyStudyUpdate(ElementAttributes element, String userId) {
if (STUDY.equals(element.getType())) {
studyService.notifyStudyUpdate(element.getElementUuid(), userId);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class StudyService implements IDirectoryElementsService {
private static final String STUDY_SERVER_API_VERSION = "v1";

private static final String DELIMITER = "/";
private static final String NOTIFICATION_TYPE_METADATA_UPDATED = "metadata_updated";
private final RestTemplate restTemplate;
private String studyServerBaseUri;

Expand Down Expand Up @@ -91,4 +92,15 @@ private HttpHeaders getHeaders(String userId) {
headers.add(HEADER_USER_ID, userId);
return headers;
}

public ResponseEntity<Void> notifyStudyUpdate(UUID studyUuid, String userId) {
String path = UriComponentsBuilder.fromPath(DELIMITER + STUDY_SERVER_API_VERSION +
"/studies/{studyUuid}/notification?type={metadata_updated}")
.buildAndExpand(studyUuid, NOTIFICATION_TYPE_METADATA_UPDATED)
.toUriString();

HttpHeaders headers = new HttpHeaders();
headers.set(HEADER_USER_ID, userId);
return restTemplate.exchange(studyServerBaseUri + path, HttpMethod.POST, new HttpEntity<>(headers), Void.class);
}
}
43 changes: 43 additions & 0 deletions src/test/java/org/gridsuite/explore/server/ExploreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class ExploreTest {
private final Map<String, Object> modificationSpecificMetadata = Map.of("id", MODIFICATION_UUID, "type", "LOAD_MODIFICATION");

private static final UUID SCRIPT_ID_BASE_FORM_CONTINGENCY_LIST_UUID = UUID.randomUUID();
private static final UUID ELEMENT_UUID = UUID.randomUUID();

@Autowired
private MockMvc mockMvc;
Expand Down Expand Up @@ -159,6 +160,9 @@ public void setup() throws IOException {
String newFilterUuidAsString = mapper.writeValueAsString(FILTER_COPY_UUID);
String newParametersUuidAsString = mapper.writeValueAsString(PARAMETER_COPY_UUID);
String newElementUuidAsString = mapper.writeValueAsString(ELEMENT_COPY_UUID);
String newElementAttributesAsString = mapper.writeValueAsString(new ElementAttributes(ELEMENT_UUID, STUDY1, "STUDY", USER1, 0, null));
String listElementsAsString = "[" + newElementAttributesAsString + "," + publicStudyAttributesAsString + "]";

final Dispatcher dispatcher = new Dispatcher() {
@SneakyThrows
@Override
Expand All @@ -168,6 +172,8 @@ public MockResponse dispatch(RecordedRequest request) {

if (path.matches("/v1/studies/cases/" + NON_EXISTING_CASE_UUID + ".*") && "POST".equals(request.getMethod())) {
return new MockResponse().setResponseCode(404);
} else if (path.matches("/v1/studies/.*/notification?type=metadata_updated") && "POST".equals(request.getMethod())) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/studies\\?duplicateFrom=" + PUBLIC_STUDY_UUID + ".*") && "POST".equals(request.getMethod())) {
return new MockResponse().setBody(newStudyUuidAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
Expand Down Expand Up @@ -241,6 +247,15 @@ public MockResponse dispatch(RecordedRequest request) {
} else if (path.matches("/v1/elements\\?ids=" + FILTER_UUID + "," + PRIVATE_STUDY_UUID + "," + CONTINGENCY_LIST_UUID) && "GET".equals(request.getMethod())) {
return new MockResponse().setBody(listElementsAttributesAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements\\?ids=" + ELEMENT_UUID + "," + PUBLIC_STUDY_UUID) && "GET".equals(request.getMethod())) {
return new MockResponse().setBody(listElementsAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/" + ELEMENT_UUID) && "PUT".equals(request.getMethod())) {
return new MockResponse().setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements\\?targetDirectoryUuid=" + PARENT_DIRECTORY_UUID) && "PUT".equals(request.getMethod())) {
return new MockResponse().setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/.*") && "PUT".equals(request.getMethod())) {
return new MockResponse().setBody(newElementUuidAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
Expand Down Expand Up @@ -292,6 +307,8 @@ public MockResponse dispatch(RecordedRequest request) {
} else if ("GET".equals(request.getMethod())) {
if (path.matches("/v1/elements/" + INVALID_ELEMENT_UUID)) {
return new MockResponse().setBody(invalidElementAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/" + ELEMENT_UUID)) {
return new MockResponse().setBody(newElementAttributesAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_UUID + "/elements")) {
return new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/" + PARENT_DIRECTORY_UUID)) {
Expand Down Expand Up @@ -329,6 +346,8 @@ public MockResponse dispatch(RecordedRequest request) {
} else if (path.matches("/v1/users/.*/cases/count")) {
return new MockResponse().setBody("0").setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/" + ELEMENT_UUID)) {
return new MockResponse().setBody(invalidElementAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
}
} else if ("DELETE".equals(request.getMethod())) {
if (path.matches("/v1/filters/" + FILTER_UUID)) {
Expand Down Expand Up @@ -930,4 +949,28 @@ public void testMaxCaseCreationWithRemoteException() throws Exception {
.andExpect(status().isBadRequest());
}
}

@Test
public void testUpdateElement() throws Exception {
ElementAttributes elementAttributes = new ElementAttributes();
elementAttributes.setElementName(STUDY1);
mockMvc.perform(put("/v1/explore/elements/{id}",
ELEMENT_UUID)
.header("userId", USER1)
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(elementAttributes))
).andExpect(status().isOk());
}

@Test
public void testMoveElementsDirectory() throws Exception {
ElementAttributes elementAttributes = new ElementAttributes();
elementAttributes.setElementName(STUDY1);
mockMvc.perform(put("/v1/explore/elements?targetDirectoryUuid={parentDirectoryUuid}",
PARENT_DIRECTORY_UUID)
.header("userId", USER1)
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsString(List.of(ELEMENT_UUID, PUBLIC_STUDY_UUID)))
).andExpect(status().isOk());
}
}
Loading