Skip to content

Commit

Permalink
Optimize calls and add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: TOURI ANIS <[email protected]>
  • Loading branch information
anistouri committed Apr 4, 2024
1 parent b552f26 commit c50e625
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ private List<ElementAttributes> getDirectoryElements(UUID directoryUuid, String

public void deleteElement(UUID id, String userId) {
ElementAttributes elementAttribute = getElementInfos(id);
IDirectoryElementsService service = getGenericService(elementAttribute.getType());
service.delete(elementAttribute.getElementUuid(), userId);
deleteElementByElementAttribute(elementAttribute, userId);
}

public void deleteElementByElementAttribute(ElementAttributes elementAttributes, String userId) {
IDirectoryElementsService service = getGenericService(elementAttributes.getType());
service.delete(elementAttributes.getElementUuid(), userId);
}

private IDirectoryElementsService getGenericService(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,33 @@ public void deleteElements(List<UUID> uuids, String userId) {
}
}

public void deleteStashedElements(int daysAgo) {
Map<String, List<UUID>> stashedElementsToDelete = directoryService.getStashedElementInfos(daysAgo).stream()
.collect(Collectors.groupingBy(
ElementAttributes::getOwner,
Collectors.mapping(
ElementAttributes::getElementUuid,
Collectors.toList()
)
));
stashedElementsToDelete.forEach((userId, uuids) -> deleteElements(uuids, userId));
public void deleteElementsByElementAttributes(String userId, List<ElementAttributes> elementsAttributes) {
try {
elementsAttributes.forEach(elementAttributes ->
directoryService.deleteElementByElementAttribute(elementAttributes, userId));
} catch (Exception e) {
LOGGER.error(e.toString(), e);
} finally {
List<UUID> elementsUuid = elementsAttributes.stream()
.map(ElementAttributes::getElementUuid)
.toList();
directoryService.deleteDirectoryElements(
elementsUuid,
userId);
}
}

public void deleteStashedElementsOlderThanDays(int daysAgo) {
Map<String, List<ElementAttributes>> stashedElementsToDelete = directoryService.getStashedElementInfos(daysAgo).stream()
.collect(Collectors.groupingBy(ElementAttributes::getOwner));
if (stashedElementsToDelete.isEmpty()) {
LOGGER.error("No elements found in the trash to clean up.");
return;
}
stashedElementsToDelete.forEach((owner, elementAttributesList) ->
deleteElementsByElementAttributes(owner, elementAttributesList)
);
LOGGER.error("Trash cleanup completed successfully.");
}

public void updateFilter(UUID id, String filter, String userId, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ScheduledCleaner(ExploreService exploreService) {
public void deleteStashedExpired() {
ZonedDateTime startZonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
LOGGER.info("Cleaning cases cron starting execution at {}", startZonedDateTime);
exploreService.deleteStashedElements(30); // delete all stashed elements older than 30 days
exploreService.deleteStashedElementsOlderThanDays(30); // delete all stashed elements older than 30 days
ZonedDateTime endZonedDateTime = ZonedDateTime.now(ZoneOffset.UTC);
LOGGER.info("Cleaning cases cron finished execution at {}", endZonedDateTime);
}
Expand Down
45 changes: 29 additions & 16 deletions src/test/java/org/gridsuite/explore/server/ExploreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -90,11 +91,17 @@ 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();
ElementAttributes contingencyElementAttributes = new ElementAttributes(CONTINGENCY_LIST_UUID, "directory", "CONTINGENCY_LIST", new AccessRightsAttributes(true), USER1, 0, null);

@Autowired
private MockMvc mockMvc;
@Autowired
private DirectoryService directoryService;

@Mock
private DirectoryService directoryServiceMock;
@InjectMocks
private ExploreService exploreServiceMock;
@Autowired
private ContingencyListService contingencyListService;
@Autowired
Expand All @@ -114,7 +121,7 @@ public class ExploreTest {
@InjectMocks
private ScheduledCleaner scheduledCleaner;

@Autowired
@Mock
private ExploreService exploreService;

@Before
Expand Down Expand Up @@ -152,7 +159,7 @@ public void setup() throws IOException {
String modificationInfosAttributesAsString = mapper.writeValueAsString(List.of(modificationSpecificMetadata));
String modificationIdsAsString = mapper.writeValueAsString(Map.of(MODIFICATION_UUID, MODIFICATION_UUID));
String directoryAttributesAsString2 = mapper.writeValueAsString(new ElementAttributes(DIRECTORY_UUID, "directory", "DIRECTORY", new AccessRightsAttributes(true), USER1, 0, null));
String directoryAttributesStashedAsString = mapper.writeValueAsString(List.of(new ElementAttributes(DIRECTORY_UUID, "directory", "CONTINGENCY_LIST", new AccessRightsAttributes(true), USER1, 0, null)));
String contingencyAttributesStashedAsString = mapper.writeValueAsString(List.of(contingencyElementAttributes));

final Dispatcher dispatcher = new Dispatcher() {
@SneakyThrows
Expand Down Expand Up @@ -285,10 +292,7 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setBody(listOfPrivateStudyAttributesAsString.replace("elementUuid", "id")).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements/stashed\\?daysAgo=.*")) {
return new MockResponse().setBody(directoryAttributesStashedAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/directories/3cd7cdc4-5a5d-4970-913e-3e91deeb8c35/elements\\?stashed=true")) {
return new MockResponse().setBody(directoryAttributesStashedAsString).setResponseCode(200)
return new MockResponse().setBody(contingencyAttributesStashedAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
}
} else if ("DELETE".equals(request.getMethod())) {
Expand Down Expand Up @@ -318,7 +322,7 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/parameters/" + PARAMETERS_UUID)) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("\\/v1\\/elements\\?ids=([^,]+,){2,}[^,]+$")) {
} else if (path.matches("\\/v1\\/elements\\?ids=([^,]+,?)+$")) {
return new MockResponse().setResponseCode(200);
}
return new MockResponse().setResponseCode(404);
Expand Down Expand Up @@ -722,16 +726,25 @@ public void testGetModificationMetadata() {
@Test
public void testDeleteStashedExpired() {
scheduledCleaner.deleteStashedExpired();
Mockito.verify(exploreService).deleteStashedElements(30);
Mockito.verify(exploreService).deleteStashedElementsOlderThanDays(30);
}
/* @Test
public void testDeleteStashedElements() {
int daysAgo = 30;
exploreService.deleteStashedElements(daysAgo);

Map<String, List<UUID>> expectedMap = Map.of(USER1, List.of(DIRECTORY_UUID));
expectedMap.forEach((userId, uuids) ->
Mockito.verify(exploreService).deleteElements(uuids, userId));
}*/
@Test
public void testDeleteStashedElementsOlderThanDaysWithNonEmptyList() {
Mockito.when(directoryServiceMock.getStashedElementInfos(Mockito.anyInt())).thenReturn(List.of(contingencyElementAttributes));

exploreServiceMock.deleteStashedElementsOlderThanDays(30);

Mockito.verify(directoryServiceMock, Mockito.atLeastOnce()).deleteElementByElementAttribute(Mockito.any(ElementAttributes.class), Mockito.anyString());
Mockito.verify(directoryServiceMock, Mockito.atLeastOnce()).deleteDirectoryElements(Mockito.anyList(), Mockito.anyString());
}

@Test
public void testGetStashedElementInfos() {
List<ElementAttributes> elementAttributes = directoryService.getStashedElementInfos(30);
assertEquals(1, elementAttributes.size());
assertEquals(CONTINGENCY_LIST_UUID, elementAttributes.get(0).getElementUuid());
assertEquals(USER1, elementAttributes.get(0).getOwner());

}
}

0 comments on commit c50e625

Please sign in to comment.