Skip to content

Commit

Permalink
Added cancel_a_single_shift_by_company & a_company_can_only_cancel_it…
Browse files Browse the repository at this point in the history
…s_own_shifts unit tests
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 061bbd0 commit 6b93c79
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/zenjob/challenge/service/IJobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface IJobService {
void bookTalent(UUID talent, UUID shiftId);
void cancelJob(UUID companyId, UUID jobId);
Optional<Job> getJob(UUID id);
void cancelShift(UUID companyId, UUID shiftId);
Optional<Shift> getShift(UUID id);
}
14 changes: 14 additions & 0 deletions src/main/java/com/zenjob/challenge/service/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,18 @@ public void cancelJob(UUID companyId, UUID jobId) {
public Optional<Job> getJob(UUID id) {
return jobRepository.findById(id);
}

@Override
public void cancelShift(UUID companyId, UUID shiftId) {
Optional<Shift> shift = getShift(shiftId);
if (!shift.get().getJob().getCompanyId().equals(companyId))
throw new InvalidActionException();

shiftRepository.deleteById(shiftId);
}

@Override
public Optional<Shift> getShift(UUID id) {
return shiftRepository.findById(id);
}
}
32 changes: 29 additions & 3 deletions src/test/java/com/zenjob/challenge/JobServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.zenjob.challenge.customexception.InvalidStartDateException;
import com.zenjob.challenge.entity.Job;
import com.zenjob.challenge.entity.Shift;
import com.zenjob.challenge.repository.JobRepository;
import com.zenjob.challenge.service.IJobService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -16,8 +15,6 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@SpringBootTest
Expand Down Expand Up @@ -120,4 +117,33 @@ public void a_company_can_only_cancel_its_own_jobs() {
Assertions.assertThrows(InvalidActionException.class, () ->
jobService.cancelJob(companyId, job.getId()));
}

@Test
public void cancel_a_single_shift_by_company() {
// given
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(5);
Job job = jobService.createJob(UUID.randomUUID(), startDate, endDate);
Shift firstShift = job.getShifts().get(0);

// when
jobService.cancelShift(job.getCompanyId(), firstShift.getId());

// then
Assertions.assertFalse(jobService.getShift(firstShift.getId()).isPresent());
}

@Test
public void a_company_can_only_cancel_its_own_shifts() {
// given
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(5);
Job job = jobService.createJob(UUID.randomUUID(), startDate, endDate);
Shift firstShift = job.getShifts().get(0);
UUID companyId = UUID.randomUUID();

// when - then
Assertions.assertThrows(InvalidActionException.class, () ->
jobService.cancelShift(companyId, firstShift.getId()));
}
}

0 comments on commit 6b93c79

Please sign in to comment.