Skip to content

Commit

Permalink
RI-139: [admin] archive plusieurs offres
Browse files Browse the repository at this point in the history
* fix d'un oubli au niveau de l'archivage
* tests
  • Loading branch information
nekorpeche committed Nov 18, 2021
1 parent 9c5796c commit df64dde
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import static com.dynonuggets.refonteimplicaction.utils.ApiUrls.*;

@RestController
Expand Down Expand Up @@ -63,4 +66,13 @@ public ResponseEntity<JobPostingDto> toggleArchive(@PathVariable Long jobId) {
JobPostingDto updated = jobPostingService.toggleArchiveJobPosting(jobId);
return ResponseEntity.ok(updated);
}

@PatchMapping(path = ARCHIVE_JOBS_URI)
public ResponseEntity<List<JobPostingDto>> toggleArchiveJobs(@RequestBody final List<JobPostingDto> jobs) {
List<JobPostingDto> updated =
jobs.stream()
.map(job -> jobPostingService.toggleArchiveJobPosting(job.getId()))
.collect(Collectors.toList());
return ResponseEntity.ok(updated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void deleteJobPosting(Long jobPostingId) {
public JobPostingDto toggleArchiveJobPosting(Long jobPostingId) {
JobPosting jobPosting = jobPostingRepository.findById(jobPostingId)
.orElseThrow(() -> new NotFoundException(String.format(Message.JOB_NOT_FOUND_MESSAGE, jobPostingId)));
jobPosting.setArchive(jobPosting.isArchive());
jobPosting.setArchive(!jobPosting.isArchive());
final JobPosting save = jobPostingRepository.save(jobPosting);
return jobPostingAdapter.toDto(save);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ApiUrls {
public static final String GET_JOB_URI = "/{jobId}";
public static final String DELETE_JOB_URI = "/{jobId}";
public static final String ARCHIVE_JOB_URI = "/{jobId}/archive";
public static final String ARCHIVE_JOBS_URI = "/archive";

// POSTS
public static final String POSTS_BASE_URI = "/api/posts";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dynonuggets.refonteimplicaction.dto.JobPostingDto;
import com.dynonuggets.refonteimplicaction.exception.NotFoundException;
import com.dynonuggets.refonteimplicaction.service.JobPostingService;
import com.google.gson.Gson;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
Expand Down Expand Up @@ -154,6 +155,43 @@ void archiveJobShouldChangeStatus() throws Exception {
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
@WithMockUser
void archiveJobShouldChangeListStatus() throws Exception {
// given
List<JobPostingDto> givenDto = Arrays.asList(
JobPostingDto.builder()
.id(1L)
.archive(false)
.build()
);

List<JobPostingDto> expectedDto = Arrays.asList(
JobPostingDto.builder()
.id(1L)
.archive(true)
.build()
);
Gson gson = new Gson();
String json = gson.toJson(givenDto);

given(jobPostingService.toggleArchiveJobPosting(anyLong())).willReturn(expectedDto.get(0));

// when
final ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOBS_URI).contentType(APPLICATION_JSON).content(json));

// then
resultActions.andDo(print())
.andExpect(status().isOk());
for (int i = 0; i < givenDto.size(); i++) {
final String contentPath = String.format("$[%d]", i);
resultActions
.andExpect(jsonPath(contentPath + ".id", is((givenDto.get(i).getId().intValue()))))
.andExpect(jsonPath(contentPath + ".archive", is(!givenDto.get(i).isArchive())));
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}
}

@Test
@WithMockUser
void archiveJobPostingWhithUnexistingIdShouldThrowException() throws Exception {
Expand Down

0 comments on commit df64dde

Please sign in to comment.