Skip to content

Commit

Permalink
RI-136! Archivage d'un job + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nekorpeche committed Nov 14, 2021
1 parent 74d617a commit 76ee475
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public ResponseEntity<Void> delete(@PathVariable Long jobId) {
}

@PatchMapping(path = ARCHIVE_JOB_URI)
public ResponseEntity<JobPostingDto> archive(@PathVariable Long jobId) {
JobPostingDto updated = jobPostingService.archiveOrUnarchiveJobPosting(jobId);
public ResponseEntity<JobPostingDto> toggleArchive(@PathVariable Long jobId) {
JobPostingDto updated = jobPostingService.toggleArchiveJobPosting(jobId);
return ResponseEntity.ok(updated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public class JobPostingDto {
private ContractTypeDto contractType;
private StatusDto status;
private Instant createdAt;
private boolean isArchive;

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ public class JobPosting {

@Column(name = "created_at")
private Instant createdAt;

@Column(name = "archive")
private boolean isArchive;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

import com.dynonuggets.refonteimplicaction.model.Status;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface StatusRepository extends JpaRepository<Status, Long> {

@Query("select s from Status s " +
"where (s.id= 2 and s.type = 'job_posting')")
Status getArchivedStatus();

@Query("select s from Status s " +
"where (s.id= 1 and s.type = 'job_posting')")
Status getUnarchivedStatus();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,12 @@ public void deleteJobPosting(Long jobPostingId) {
}

@Transactional
public JobPostingDto archiveOrUnarchiveJobPosting(Long jobPostingId) {
public JobPostingDto toggleArchiveJobPosting(Long jobPostingId) {
JobPosting jobPosting = jobPostingRepository.findById(jobPostingId)
.orElseThrow(() -> new NotFoundException("Impossible de supprimer l'offre, " + jobPostingId + " n'existe pas."));
if (jobPosting.getStatus() == statusRepository.getUnarchivedStatus()) {
jobPosting.setStatus(statusRepository.getArchivedStatus());
} else {
jobPosting.setStatus(statusRepository.getUnarchivedStatus());
}
.orElseThrow(() -> new NotFoundException(String.format(Message.JOB_NOT_FOUND_MESSAGE, jobPostingId)));
jobPosting.setArchive(jobPosting.isArchive());
final JobPosting save = jobPostingRepository.save(jobPosting);
return jobPostingAdapter.toDto(save);

}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dynonuggets.refonteimplicaction.controller;

import com.dynonuggets.refonteimplicaction.dto.JobPostingDto;
import com.dynonuggets.refonteimplicaction.dto.StatusDto;
import com.dynonuggets.refonteimplicaction.exception.NotFoundException;
import com.dynonuggets.refonteimplicaction.service.JobPostingService;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -36,13 +35,9 @@ class JobsPostingControllerTest extends ControllerIntegrationTestBase {
JobPostingService jobPostingService;

List<JobPostingDto> jobPostings;
StatusDto statusAvailable;
StatusDto statusArchived;

@BeforeEach
void setUp() {
statusAvailable = StatusDto.builder().id(1L).label("jobAvailable").type("job_posting").build();
statusArchived = StatusDto.builder().id(2L).label("jobArchived").type("job_posting").build();
jobPostings = Arrays.asList(
JobPostingDto.builder().id(1L).createdAt(Instant.now()).description("description").location("Paris").keywords("toto,yoyo").salary("1111$").build(),
JobPostingDto.builder().id(2L).createdAt(Instant.now()).description("description2").location("New York").keywords("toto2,yoyo2").salary("2222$").build());
Expand Down Expand Up @@ -135,59 +130,57 @@ void getJobByIdWithoutJwtShouldBeForbidden() throws Exception {
@Test
@WithMockUser
void archiveJobShouldChangeStatus() throws Exception {
JobPostingDto jobPostingDtoAvailable = JobPostingDto.builder()
//given
JobPostingDto givenDto = JobPostingDto.builder()
.id(1L)
.createdAt(Instant.now())
.description("description")
.location("Paris")
.keywords("toto,yoyo")
.salary("1111$")
.status(statusAvailable)
.isArchive(false)
.build();

JobPostingDto jobPostingDtoArchived = JobPostingDto.builder()
JobPostingDto expectedDto = JobPostingDto.builder()
.id(1L)
.createdAt(Instant.now())
.description("description")
.location("Paris")
.keywords("toto,yoyo")
.salary("1111$")
.status(statusArchived)
.isArchive(true)
.build();

given(jobPostingService.archiveOrUnarchiveJobPosting(jobPostingDtoAvailable.getId())).willReturn(jobPostingDtoArchived);
given(jobPostingService.toggleArchiveJobPosting(anyLong())).willReturn(expectedDto);

//when
final ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, givenDto.getId()).contentType(APPLICATION_JSON));

mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobPostingDtoAvailable.getId()).contentType(APPLICATION_JSON)).andDo(print())
//then
resultActions.andDo(print())
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(Math.toIntExact(jobPostingDtoAvailable.getId()))))
.andExpect(jsonPath("$.description", is(jobPostingDtoAvailable.getDescription())))
.andExpect(jsonPath("$.location", is(jobPostingDtoAvailable.getLocation())))
.andExpect(jsonPath("$.keywords", is(jobPostingDtoAvailable.getKeywords())))
.andExpect(jsonPath("$.salary", is(jobPostingDtoAvailable.getSalary())))
.andExpect(jsonPath("$.status.label", is(jobPostingDtoArchived.getStatus().getLabel())));

verify(jobPostingService, times(1)).archiveOrUnarchiveJobPosting(anyLong());
.andExpect(jsonPath("$.id", is((givenDto.getId().intValue()))))
.andExpect(jsonPath("$.archive", is(expectedDto.isArchive())));
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
@WithMockUser
void archiveJobPostingWhithUnexistingIdShouldThrowException() throws Exception {
//given
final long jobId = 123L;
final NotFoundException exception = new NotFoundException(String.format(JOB_NOT_FOUND_MESSAGE, jobId));
given(jobPostingService.archiveOrUnarchiveJobPosting(anyLong())).willThrow(exception);
given(jobPostingService.toggleArchiveJobPosting(anyLong())).willThrow(exception);

mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId)).andExpect(status().isNotFound());
//when
ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId));

verify(jobPostingService, times(1)).archiveOrUnarchiveJobPosting(anyLong());
//then
resultActions.andExpect(status().isNotFound());
verify(jobPostingService, times(1)).toggleArchiveJobPosting(anyLong());
}

@Test
void archiveJobByIdWithoutJwtShouldBeForbidden() throws Exception {
//given
final long jobId = 123L;
mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId).contentType(APPLICATION_JSON))
.andDo(print())
.andExpect(status().isForbidden());

verify(jobPostingService, times(0)).archiveOrUnarchiveJobPosting(anyLong());
//when
ResultActions resultActions = mvc.perform(patch(JOBS_BASE_URI + ARCHIVE_JOB_URI, jobId).contentType(APPLICATION_JSON));

//then
resultActions.andDo(print());
resultActions.andExpect(status().isForbidden());
verify(jobPostingService, never()).toggleArchiveJobPosting(anyLong());
}
}
2 changes: 2 additions & 0 deletions data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,5 @@ CREATE TABLE `work_experience`
ALTER TABLE `job_posting`
ADD `short_description` longtext NULL AFTER `description`;
-- 2021-09-25 14:15:20
ALTER TABLE `job_posting`
ADD COLUMN `archive` tinyint(1) NULL DEFAULT '0' AFTER `created_at`;

0 comments on commit 76ee475

Please sign in to comment.