Skip to content

Commit

Permalink
Split JobServiceTests.java and added ShiftServiceTests
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 10, 2024
1 parent 6350dae commit 56937ea
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.UUID;

@SpringBootTest
Expand Down Expand Up @@ -123,55 +122,4 @@ 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() throws NotFoundException {
// 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
shiftService.cancelShift(job.getCompanyId(), firstShift.getId());

// then
Assertions.assertFalse(shiftService.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, () ->
shiftService.cancelShift(companyId, firstShift.getId()));
}

@Test
public void cancel_shift_for_a_talent() throws NotFoundException {
// 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);
Shift secondShift = job.getShifts().get(1);
UUID talentId = UUID.randomUUID();
shiftService.bookTalent(talentId, firstShift.getId());
shiftService.bookTalent(talentId, secondShift.getId());

// when
shiftService.cancelShiftForTalent(job.getCompanyId(), talentId);

// then
Optional<Shift> firstShiftById = shiftService.getShift(firstShift.getId());
Optional<Shift> secondShiftById = shiftService.getShift(secondShift.getId());
Assertions.assertNull(firstShiftById.get().getTalentId());
Assertions.assertNull(secondShiftById.get().getTalentId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.zenjob.challenge.application.services;

import com.zenjob.challenge.application.interfaces.JobService;
import com.zenjob.challenge.application.interfaces.ShiftService;
import com.zenjob.challenge.domain.entity.Job;
import com.zenjob.challenge.domain.entity.Shift;
import com.zenjob.challenge.domain.exceptions.InvalidActionException;
import javassist.NotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.LocalDate;
import java.util.Optional;
import java.util.UUID;

@SpringBootTest
public class ShiftServiceTests {

@Autowired
private JobService jobService;

@Autowired
private ShiftService shiftService;

@Test
public void cancel_a_single_shift_by_company() throws NotFoundException {
// 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
shiftService.cancelShift(job.getCompanyId(), firstShift.getId());

// then
Assertions.assertFalse(shiftService.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, () ->
shiftService.cancelShift(companyId, firstShift.getId()));
}

@Test
public void cancel_shift_for_a_talent() throws NotFoundException {
// 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);
Shift secondShift = job.getShifts().get(1);
UUID talentId = UUID.randomUUID();
shiftService.bookTalent(talentId, firstShift.getId());
shiftService.bookTalent(talentId, secondShift.getId());

// when
shiftService.cancelShiftForTalent(job.getCompanyId(), talentId);

// then
Optional<Shift> firstShiftById = shiftService.getShift(firstShift.getId());
Optional<Shift> secondShiftById = shiftService.getShift(secondShift.getId());
Assertions.assertNull(firstShiftById.get().getTalentId());
Assertions.assertNull(secondShiftById.get().getTalentId());
}
}

0 comments on commit 56937ea

Please sign in to comment.