Skip to content

Commit

Permalink
feat(cbir): delete storage when project is deleted (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
bathienle authored Aug 27, 2024
1 parent ec0e85d commit 622845f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 145 deletions.
18 changes: 17 additions & 1 deletion src/main/java/be/cytomine/service/project/ProjectService.java
Original file line number Diff line number Diff line change
Expand Up @@ -970,15 +970,31 @@ protected void beforeUpdate(CytomineDomain domain) {
project.setCountImages(imageInstanceRepository.countAllByProject(project));
}

private void deleteStorage(Long projectId) {
String url = this.applicationProperties.getRetrievalServerURL() + "/api/storages/" + projectId;

log.debug("Send DELETE request to {}", url);
ResponseEntity<String> response = restTemplate.exchange(
url,
HttpMethod.DELETE,
null,
String.class
);

if (!response.getStatusCode().equals(HttpStatus.OK)) {
log.error("Failed to delete storage for project {}", projectId);
}
}

protected void beforeDelete(CytomineDomain domain) {
Project project = (Project)domain;
commandHistoryRepository.deleteAllByProject(project);
undoStackItemRepository.deleteAllByCommand_Project(project);
redoStackItemRepository.deleteAllByCommand_Project(project);
commandRepository.deleteAllByProject(project);
}

deleteStorage(project.getId());
}

public List<Object> getStringParamsI18n(CytomineDomain domain) {
return List.of(domain.getId(), ((Project)domain).getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,43 @@ public class ProjectResourceTests {

private static WireMockServer wireMockServer;

private static void setupStub() {
/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(
WireMock.matching(".*\"name\":\"\\d+\".*")
)
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
)
);

wireMockServer.stubFor(WireMock.delete(urlPathMatching("/api/storages/.*"))
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
)
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);
}

@BeforeAll
public static void beforeAll() {
wireMockServer = new WireMockServer(8888);
wireMockServer.start();
WireMock.configureFor("localhost", 8888);

setupStub();
}

@AfterAll
Expand Down Expand Up @@ -165,20 +197,6 @@ public void list_all_projects_with_extension() throws Exception {
builder.addUserToProject(project, builder.given_superadmin().getUsername());
UserAnnotation userAnnotation = builder.given_a_not_persisted_user_annotation(project);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.equalTo(userAnnotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
String id = URLEncoder.encode(userAnnotation.getSlice().getBaseSlice().getPath(), StandardCharsets.UTF_8);
wireMockServer.stubFor(WireMock.post(urlEqualTo("/image/" + id + "/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

userAnnotation
.getSlice()
.getBaseSlice()
Expand Down Expand Up @@ -532,18 +550,6 @@ public void add_valid_project() throws Exception {
project.setOntology(builder.given_an_ontology());
project.setName("add_valid_project");

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(
WireMock.matching(".*\"name\":\"\\d+\".*")
)
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", "application/json")
.withBody("{ \"message\": \"Created storage with name: " + project.getId() + "\" }")
)
);

/* Test project creation */
restProjectControllerMockMvc.perform(post("/api/project.json")
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -573,16 +579,7 @@ public void add_valid_project_without_ontology() throws Exception {
project.setOntology(null);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(
WireMock.matching(".*\"name\":\"\\d+\".*")
)
.willReturn(aResponse()
.withStatus(HttpStatus.OK.value())
.withHeader("Content-Type", "application/json")
.withBody("{ \"message\": \"Created storage with name: " + project.getId() + "\" }")
)
);


/* Test project creation */
restProjectControllerMockMvc.perform(post("/api/project.json")
Expand Down Expand Up @@ -773,6 +770,7 @@ public void fail_when_editing_project_with_annotation_terms_and_ontology_change(
@Test
@Transactional
public void delete_project() throws Exception {

Project project = builder.given_a_project();
restProjectControllerMockMvc.perform(delete("/api/project/{id}.json", project.getId())
.contentType(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -807,21 +805,6 @@ public void list_last_action() throws Exception {
Project project = builder.given_a_project();
builder.addUserToProject(project, builder.given_superadmin().getUsername());
UserAnnotation userAnnotation = builder.given_a_not_persisted_user_annotation(project);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.equalTo(userAnnotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
String id = URLEncoder.encode(userAnnotation.getSlice().getBaseSlice().getPath(), StandardCharsets.UTF_8);
wireMockServer.stubFor(WireMock.post(urlEqualTo("/image/" + id + "/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

userAnnotation
.getSlice()
.getBaseSlice()
Expand Down Expand Up @@ -1126,21 +1109,6 @@ public void list_command_history() throws Exception {
builder.addUserToProject(project, creator.getUsername());

UserAnnotation userAnnotation = builder.given_a_not_persisted_user_annotation(project);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.equalTo(userAnnotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
String id = URLEncoder.encode(userAnnotation.getSlice().getBaseSlice().getPath(), StandardCharsets.UTF_8);
wireMockServer.stubFor(WireMock.post(urlEqualTo("/image/" + id + "/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

userAnnotation
.getSlice()
.getBaseSlice()
Expand Down Expand Up @@ -1175,20 +1143,6 @@ public void list_command_history_with_dates() throws Exception {
Date stop = DateUtils.addSeconds(new Date(), 5);
UserAnnotation userAnnotation = builder.given_a_not_persisted_user_annotation(project);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.equalTo(userAnnotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
String id = URLEncoder.encode(userAnnotation.getSlice().getBaseSlice().getPath(), StandardCharsets.UTF_8);
wireMockServer.stubFor(WireMock.post(urlEqualTo("/image/" + id + "/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

userAnnotation
.getSlice()
.getBaseSlice()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.security.acls.model.Permission;
import org.springframework.security.test.context.support.WithMockUser;
Expand Down Expand Up @@ -121,6 +122,10 @@ private static void setupStub() {
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

wireMockServer.stubFor(WireMock.delete(urlPathMatching("/api/storages/.*"))
.willReturn(aResponse().withStatus(HttpStatus.OK.value()))
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlPathMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
Expand Down
97 changes: 33 additions & 64 deletions src/test/java/be/cytomine/service/project/ProjectServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.security.test.context.support.WithMockUser;

import be.cytomine.BasicInstanceBuilder;
Expand Down Expand Up @@ -100,11 +101,43 @@ public class ProjectServiceTests {

private static WireMockServer wireMockServer;

private static void setupStub() {
/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

wireMockServer.stubFor(WireMock.delete(urlPathMatching("/api/images/.*"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

wireMockServer.stubFor(WireMock.delete(urlPathMatching("/api/storages/.*"))
.willReturn(aResponse().withStatus(HttpStatus.OK.value()))
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlPathMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);
}

@BeforeAll
public static void beforeAll() {
wireMockServer = new WireMockServer(8888);
wireMockServer.start();
WireMock.configureFor("localhost", 8888);

setupStub();
}

@AfterAll
Expand Down Expand Up @@ -266,19 +299,6 @@ void list_user_project_with_many_filters() {
builder.addUserToProject(project1, builder.given_superadmin().getUsername());
builder.addUserToProject(project2, builder.given_superadmin().getUsername());

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlPathMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

UserAnnotation userAnnotation = builder.given_a_not_persisted_user_annotation(project2);
userAnnotation
.getSlice()
Expand Down Expand Up @@ -339,19 +359,6 @@ void list_command_history_for_project() {
.getImageServer()
.setUrl("http://localhost:8888");

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.matching(".*"))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlPathMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

userAnnotationService.add(userAnnotation1.toJsonObject());
userAnnotationService.add(userAnnotation2.toJsonObject());

Expand Down Expand Up @@ -683,19 +690,6 @@ void list_last_action() {
.getImageServer()
.setUrl("http://localhost:8888");

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/images"))
.withQueryParam("storage", WireMock.equalTo(userAnnotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

/* Simulate call to PIMS */
wireMockServer.stubFor(WireMock.post(urlPathMatching("/image/.*/annotation/drawing"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString().getBytes()))
);

assertThat(projectService.lastAction(project1, 10)).hasSize(0);
userAnnotationService.add(userAnnotation.toJsonObject());
assertThat(projectService.lastAction(project1, 10)).hasSize(1);
Expand Down Expand Up @@ -728,12 +722,6 @@ void list_by_roles() {
void add_project() {
Project project = BasicInstanceBuilder.given_a_not_persisted_project();

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

CommandResponse commandResponse = projectService.add(project.toJsonObject());

assertThat(commandResponse).isNotNull();
Expand All @@ -756,12 +744,6 @@ void add_project_with_users_and_admins() {
User user = builder.given_a_user();
User admin = builder.given_a_user();

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

CommandResponse commandResponse = projectService.add(project.toJsonObject()
.withChange("users", List.of(user.getId()))
.withChange("admins", List.of(admin.getId()))
Expand Down Expand Up @@ -970,12 +952,6 @@ void delete_project() {
void delete_project_just_beeing_created() {
Project project = BasicInstanceBuilder.given_a_not_persisted_project();

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.post(urlPathEqualTo("/api/storages"))
.withRequestBody(WireMock.matching(".*"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

CommandResponse commandResponse = projectService.add(project.toJsonObject());

assertThat(commandResponse).isNotNull();
Expand All @@ -1002,13 +978,6 @@ void delete_project_with_dependencies() {
TagDomainAssociation tagDomainAssociation = builder.given_a_tag_association(builder.given_a_tag(), project);
AttachedFile attachedFile = builder.given_a_attached_file(project);

/* Simulate call to CBIR */
wireMockServer.stubFor(WireMock.delete(urlPathEqualTo("/api/images/" + annotation.getId()))
.withQueryParam("storage", WireMock.equalTo(annotation.getProject().getId().toString()))
.withQueryParam("index", WireMock.equalTo("annotation"))
.willReturn(aResponse().withBody(UUID.randomUUID().toString()))
);

AssertionsForClassTypes.assertThat(entityManager.find(Property.class, property.getId())).isNotNull();
AssertionsForClassTypes.assertThat(entityManager.find(Description.class, description.getId())).isNotNull();
AssertionsForClassTypes.assertThat(entityManager.find(TagDomainAssociation.class, tagDomainAssociation.getId())).isNotNull();
Expand Down

0 comments on commit 622845f

Please sign in to comment.