-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove code used for stashing - add supervision classes to handle exi…
…sting stashed elements deletion (#84) Signed-off-by: LE SAULNIER Kevin <[email protected]>
- Loading branch information
1 parent
6f16462
commit 3cbc513
Showing
7 changed files
with
179 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/org/gridsuite/explore/server/SupervisionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.gridsuite.explore.server; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.gridsuite.explore.server.services.SupervisionService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@RestController | ||
@RequestMapping(value = "/" + ExploreApi.API_VERSION + "/supervision") | ||
@Tag(name = "explore-server - Supervision") | ||
public class SupervisionController { | ||
private final SupervisionService supervisionService; | ||
|
||
public SupervisionController(SupervisionService supervisionService) { | ||
this.supervisionService = supervisionService; | ||
} | ||
|
||
@DeleteMapping(value = "/explore/elements", params = "ids") | ||
@Operation(summary = "Remove directories/elements") | ||
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "directories/elements was successfully removed")}) | ||
public ResponseEntity<Void> deleteElements(@RequestParam("ids") List<UUID> elementsUuid, | ||
@RequestHeader("userId") String userId) { | ||
supervisionService.deleteElements(elementsUuid, userId); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/gridsuite/explore/server/services/SupervisionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package org.gridsuite.explore.server.services; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com> | ||
*/ | ||
@Service | ||
public class SupervisionService { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(SupervisionService.class); | ||
private final DirectoryService directoryService; | ||
private final String directoryServerBaseUri; | ||
private final RestTemplate restTemplate; | ||
|
||
private static final String DIRECTORY_SERVER_API_VERSION = "v1"; | ||
private static final String DELIMITER = "/"; | ||
private static final String SUPERVISION_PATH = DELIMITER + "supervision"; | ||
private static final String ELEMENTS_SERVER_ROOT_PATH = DELIMITER + DIRECTORY_SERVER_API_VERSION + DELIMITER + SUPERVISION_PATH + DELIMITER | ||
+ "elements"; | ||
|
||
public SupervisionService(DirectoryService directoryService, RestTemplate restTemplate, RemoteServicesProperties remoteServicesProperties) { | ||
this.directoryServerBaseUri = remoteServicesProperties.getServiceUri("directory-server"); | ||
this.directoryService = directoryService; | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
public void deleteElements(List<UUID> uuids, String userId) { | ||
uuids.forEach(id -> { | ||
try { | ||
directoryService.deleteElement(id, userId); | ||
} catch (Exception e) { | ||
// if deletion fails (element does not exist, server is down...), the process keeps proceeding to at least delete references in directory-server | ||
// orphan elements will be deleted in a dedicated script | ||
LOGGER.error(e.toString(), e); | ||
} | ||
}); | ||
deleteDirectoryElements(uuids); | ||
} | ||
|
||
// DOES NOT CHECK OWNER BEFORE DELETING | ||
private void deleteDirectoryElements(List<UUID> elementUuids) { | ||
var ids = elementUuids.stream().map(UUID::toString).collect(Collectors.joining(",")); | ||
String path = UriComponentsBuilder | ||
.fromPath(ELEMENTS_SERVER_ROOT_PATH) | ||
.queryParam("ids", ids) | ||
.buildAndExpand() | ||
.toUriString(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
restTemplate.exchange(directoryServerBaseUri + path, HttpMethod.DELETE, new HttpEntity<>(headers), Void.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/test/java/org/gridsuite/explore/server/SupervisionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package org.gridsuite.explore.server; | ||
|
||
import org.gridsuite.explore.server.dto.ElementAttributes; | ||
import org.gridsuite.explore.server.services.DirectoryService; | ||
import org.gridsuite.explore.server.services.SupervisionService; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.Mockito.*; | ||
|
||
/** | ||
* @author Kevin Le Saulnier <kevin.lesaulnier at rte-france.com> | ||
*/ | ||
@SpringBootTest | ||
class SupervisionTest { | ||
@Autowired | ||
SupervisionService supervisionService; | ||
|
||
@MockBean | ||
DirectoryService directoryService; | ||
|
||
@MockBean | ||
RestTemplate restTemplate; | ||
|
||
ElementAttributes filter = new ElementAttributes(UUID.randomUUID(), "filter", "FILTER", null, "userId", 0L, null, null); | ||
|
||
ElementAttributes study = new ElementAttributes(UUID.randomUUID(), "study", "STUDY", null, "userId", 0L, null, null); | ||
|
||
@Test | ||
void testDeleteElements() { | ||
List<UUID> uuidsToDelete = List.of(filter.getElementUuid(), study.getElementUuid()); | ||
supervisionService.deleteElements(uuidsToDelete, "userId"); | ||
// deletions of both elements with foreach towards respective microservice | ||
verify(directoryService, times(1)).deleteElement(filter.getElementUuid(), "userId"); | ||
verify(directoryService, times(1)).deleteElement(study.getElementUuid(), "userId"); | ||
// deletions of both elements in directory server | ||
verify(restTemplate, times(1)).exchange(matches(".*/supervision/.*"), eq(HttpMethod.DELETE), any(HttpEntity.class), eq(Void.class)); | ||
} | ||
|
||
@Test | ||
void testDeleteElementsWithErrors() { | ||
List<UUID> uuidsToDelete = List.of(filter.getElementUuid(), study.getElementUuid()); | ||
|
||
// one deletion will fail, this test checks deletions does not stop even when one of them is throwing an exception | ||
doThrow(new RuntimeException("An error occured when deleting filter")).when(directoryService).deleteElement(filter.getElementUuid(), "userId"); | ||
|
||
supervisionService.deleteElements(uuidsToDelete, "userId"); | ||
// deletions of both elements with foreach towards respective microservice | ||
verify(directoryService, times(1)).deleteElement(filter.getElementUuid(), "userId"); | ||
verify(directoryService, times(1)).deleteElement(study.getElementUuid(), "userId"); | ||
// deletions of both elements in directory server | ||
verify(restTemplate, times(1)).exchange(matches(".*/supervision/.*"), eq(HttpMethod.DELETE), any(HttpEntity.class), eq(Void.class)); | ||
} | ||
|
||
@AfterEach | ||
public void verifyNoMoreInteractionsMocks() { | ||
verifyNoMoreInteractions(restTemplate); | ||
verifyNoMoreInteractions(directoryService); | ||
} | ||
} |