From 7887d9d8bfb32b685564bdf6caf2ed54f7dbf6ae Mon Sep 17 00:00:00 2001 From: masoudarvishian Date: Fri, 10 May 2024 17:12:33 +0200 Subject: [PATCH] Refactored tests --- .../challenge/application/interfaces/JobService.java | 1 + .../challenge/application/interfaces/ShiftService.java | 1 + .../challenge/application/services/JobServiceImpl.java | 5 +++++ .../challenge/application/services/ShiftServiceImpl.java | 5 +++++ .../challenge/application/services/JobServiceTests.java | 8 ++++++++ .../challenge/application/services/ShiftServiceTests.java | 8 ++++++++ 6 files changed, 28 insertions(+) diff --git a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java index 6176600..1967823 100644 --- a/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java +++ b/src/main/java/com/zenjob/challenge/application/interfaces/JobService.java @@ -11,4 +11,5 @@ public interface JobService { Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate); void cancelJob(UUID companyId, UUID jobId) throws NotFoundException; Optional getJob(UUID id); + void clearAllJobs(); } diff --git a/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java b/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java index 609fa49..c171089 100644 --- a/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java +++ b/src/main/java/com/zenjob/challenge/application/interfaces/ShiftService.java @@ -13,4 +13,5 @@ public interface ShiftService { void cancelShift(UUID companyId, UUID shiftId) throws NotFoundException; Optional getShift(UUID id); void cancelShiftForTalent(UUID companyId, UUID talentId); + void clearAllShifts(); } diff --git a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java index 0f99b10..dcd994c 100644 --- a/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/JobServiceImpl.java @@ -52,6 +52,11 @@ public Optional getJob(UUID id) { return jobRepository.findById(id); } + @Override + public void clearAllJobs() { + jobRepository.deleteAll(); + } + private static Job buildJob(UUID companyId, LocalDate startDate, LocalDate endDate) { return Job.builder() .id(UUID.randomUUID()) diff --git a/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java b/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java index 957d217..a8267bc 100644 --- a/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java +++ b/src/main/java/com/zenjob/challenge/application/services/ShiftServiceImpl.java @@ -60,6 +60,11 @@ public void cancelShiftForTalent(UUID companyId, UUID talentId) { }); } + @Override + public void clearAllShifts() { + shiftRepository.deleteAll(); + } + private List getShiftsByTalentIdAndCompanyId(UUID talentId, UUID companyId) { List shifts = shiftRepository.findAllByTalentId(talentId); return shifts.stream() diff --git a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java index 7db2e36..0894a31 100644 --- a/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java +++ b/src/test/java/com/zenjob/challenge/application/services/JobServiceTests.java @@ -8,9 +8,11 @@ import com.zenjob.challenge.domain.entity.Shift; import com.zenjob.challenge.application.interfaces.JobService; import javassist.NotFoundException; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; import java.time.Duration; @@ -28,6 +30,12 @@ public class JobServiceTests { @Autowired private ShiftService shiftService; + @AfterEach + public void cleanUpDatabase() { + shiftService.clearAllShifts(); + jobService.clearAllJobs(); + } + @Test public void job_should_have_at_least_one_shift() { // given diff --git a/src/test/java/com/zenjob/challenge/application/services/ShiftServiceTests.java b/src/test/java/com/zenjob/challenge/application/services/ShiftServiceTests.java index 6506d30..ef0014d 100644 --- a/src/test/java/com/zenjob/challenge/application/services/ShiftServiceTests.java +++ b/src/test/java/com/zenjob/challenge/application/services/ShiftServiceTests.java @@ -6,9 +6,11 @@ import com.zenjob.challenge.domain.entity.Shift; import com.zenjob.challenge.domain.exceptions.InvalidActionException; import javassist.NotFoundException; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; import java.time.LocalDate; @@ -24,6 +26,12 @@ public class ShiftServiceTests { @Autowired private ShiftService shiftService; + @AfterEach + public void cleanUpDatabase() { + shiftService.clearAllShifts(); + jobService.clearAllJobs(); + } + @Test public void cancel_a_single_shift_by_company() throws NotFoundException { // given