diff --git a/src/main/java/be/cytomine/service/project/ProjectService.java b/src/main/java/be/cytomine/service/project/ProjectService.java index 45d73df1..a2d59416 100644 --- a/src/main/java/be/cytomine/service/project/ProjectService.java +++ b/src/main/java/be/cytomine/service/project/ProjectService.java @@ -970,6 +970,21 @@ 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 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; @@ -977,8 +992,9 @@ protected void beforeDelete(CytomineDomain domain) { undoStackItemRepository.deleteAllByCommand_Project(project); redoStackItemRepository.deleteAllByCommand_Project(project); commandRepository.deleteAllByProject(project); - } + deleteStorage(project.getId()); + } public List getStringParamsI18n(CytomineDomain domain) { return List.of(domain.getId(), ((Project)domain).getName()); diff --git a/src/test/java/be/cytomine/api/controller/project/ProjectResourceTests.java b/src/test/java/be/cytomine/api/controller/project/ProjectResourceTests.java index 7cacd481..247156e2 100644 --- a/src/test/java/be/cytomine/api/controller/project/ProjectResourceTests.java +++ b/src/test/java/be/cytomine/api/controller/project/ProjectResourceTests.java @@ -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 @@ -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() @@ -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) @@ -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") @@ -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) @@ -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() @@ -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() @@ -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() diff --git a/src/test/java/be/cytomine/authorization/project/ProjectAuthorizationTest.java b/src/test/java/be/cytomine/authorization/project/ProjectAuthorizationTest.java index 8711677b..61a5fe3e 100644 --- a/src/test/java/be/cytomine/authorization/project/ProjectAuthorizationTest.java +++ b/src/test/java/be/cytomine/authorization/project/ProjectAuthorizationTest.java @@ -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; @@ -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(".*")) diff --git a/src/test/java/be/cytomine/service/project/ProjectServiceTests.java b/src/test/java/be/cytomine/service/project/ProjectServiceTests.java index 7f35ba92..d8b3c22c 100644 --- a/src/test/java/be/cytomine/service/project/ProjectServiceTests.java +++ b/src/test/java/be/cytomine/service/project/ProjectServiceTests.java @@ -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; @@ -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 @@ -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() @@ -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()); @@ -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); @@ -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(); @@ -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())) @@ -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(); @@ -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();