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

delete project #19

Merged
merged 1 commit into from
Dec 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public ResponseEntity<ProjectDto> findProjectById(@PathVariable Integer id) {
return new ResponseEntity<>(toProjectDto(projectService.findProjectById(id)), HttpStatus.OK);
}

@Operation(summary = "Delete project if it contains no studies")
@DeleteMapping("/{id}")
public void deleteProject(@PathVariable Integer id) {
projectService.deleteProjectById(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import lombok.*;

import java.time.LocalDateTime;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Getter
@Setter
Expand Down Expand Up @@ -42,4 +44,8 @@ public class ProjectEntity {
@CollectionTable(name = "project_tags", joinColumns = @JoinColumn(name = "project_id"))
@Column(name = "tag")
private List<String> tags;

@OneToMany(mappedBy = "project", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<PinnedProjectEntity> pinnedProjectEntities = new LinkedHashSet<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public interface ProjectService {

ProjectEntity pinProjectForUser(String userId, Integer projectId);

void deleteProjectById(Integer projectId);


}

Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ public ProjectEntity pinProjectForUser(String userId, Integer projectId) {
return project;
}

@Override
public void deleteProjectById(Integer projectId) {
ProjectEntity project = projectRepository.findById(projectId)
.orElseThrow(() -> new ResourceNotFoundException("Project not found with ID: " + projectId));
if (project.getStudies() != null && !project.getStudies().isEmpty()) {
throw new BadRequestException("Project contains studies and cannot be deleted");
}
projectRepository.deleteById(projectId);
}

private void checkIfUserHasALreadyMaxPinnedProjects(String userId) {
List<PinnedProjectEntity> pinnedProjects = pinnedProjectRepository.findById_Nni(userId);
if (pinnedProjects.size() >= 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rte_france.antares.datamanager_back.dto.ProjectDto;
import com.rte_france.antares.datamanager_back.exception.BadRequestException;
import com.rte_france.antares.datamanager_back.exception.ResourceNotFoundException;
import com.rte_france.antares.datamanager_back.repository.model.ProjectEntity;
import com.rte_france.antares.datamanager_back.service.ProjectService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -155,5 +156,36 @@ void findProjectById_returnsProjectDtoWhenProjectExists() throws Exception {
.andExpect(jsonPath("$.name").value("name2050"))
.andExpect(jsonPath("$.description").value("project2050"));
}
@Test
void deleteProject_returnsNoContentWhenProjectDeleted() throws Exception {
Integer projectId = 1;

doNothing().when(projectService).deleteProjectById(projectId);

mockMvc.perform(delete("/v1/project/{id}", projectId)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}

@Test
void deleteProject_returnsNotFoundWhenProjectDoesNotExist() throws Exception {
Integer projectId = 1;

doThrow(new ResourceNotFoundException("Project not found")).when(projectService).deleteProjectById(projectId);

mockMvc.perform(delete("/v1/project/{id}", projectId)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}

@Test
void deleteProject_returnsBadRequestWhenProjectContainsStudies() throws Exception {
Integer projectId = 1;

doThrow(new BadRequestException("Project contains studies and cannot be deleted")).when(projectService).deleteProjectById(projectId);

mockMvc.perform(delete("/v1/project/{id}", projectId)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isInternalServerError());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,50 @@ void pinProjectForUser_throwsExceptionWhenUserHasMaxPinnedProjects() {
assertEquals("You have already 3 pinned projects , please unpin one before pinning another one.", exception.getMessage());
verify(pinnedProjectRepository, never()).save(any(PinnedProjectEntity.class));
}

@Test
void deleteProjectById_deletesProjectWhenNoStudies() {
Integer projectId = 1;
ProjectEntity project = new ProjectEntity();
project.setId(projectId);

when(projectRepository.findById(projectId)).thenReturn(Optional.of(project));

projectService.deleteProjectById(projectId);

verify(projectRepository, times(1)).deleteById(projectId);
}

@Test
void deleteProjectById_throwsExceptionWhenProjectNotFound() {
Integer projectId = 1;

when(projectRepository.findById(projectId)).thenReturn(Optional.empty());

ResourceNotFoundException exception = assertThrows(
ResourceNotFoundException.class,
() -> projectService.deleteProjectById(projectId)
);

assertEquals("Project not found with ID: 1", exception.getMessage());
verify(projectRepository, never()).deleteById(projectId);
}

@Test
void deleteProjectById_throwsExceptionWhenProjectContainsStudies() {
Integer projectId = 1;
ProjectEntity project = new ProjectEntity();
project.setId(projectId);
project.setStudies(List.of(new StudyEntity()));

when(projectRepository.findById(projectId)).thenReturn(Optional.of(project));

BadRequestException exception = assertThrows(
BadRequestException.class,
() -> projectService.deleteProjectById(projectId)
);

assertEquals("Project contains studies and cannot be deleted", exception.getMessage());
verify(projectRepository, never()).deleteById(projectId);
}
}
Loading