Skip to content

Commit

Permalink
Added shift_length_should_equal_to_8_hours unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
masoudarvishian committed May 9, 2024
1 parent 228211a commit 5deb7bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/zenjob/challenge/service/JobService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public Job createJob(UUID companyId, LocalDate startDate, LocalDate endDate) {
Job job = Job.builder()
.id(UUID.randomUUID())
.companyId(companyId)
.startTime(startDate.atTime(8, 0, 0).toInstant(ZoneOffset.UTC))
.startTime(startDate.atTime(9, 0, 0).toInstant(ZoneOffset.UTC))
.endTime(endDate.atTime(17, 0, 0).toInstant(ZoneOffset.UTC))
.build();
job.setShifts(LongStream.range(0, ChronoUnit.DAYS.between(startDate, endDate))
.mapToObj(idx -> startDate.plus(idx, ChronoUnit.DAYS))
.map(date -> Shift.builder()
.id(UUID.randomUUID())
.job(job)
.startTime(date.atTime(8, 0, 0).toInstant(ZoneOffset.UTC))
.startTime(date.atTime(9, 0, 0).toInstant(ZoneOffset.UTC))
.endTime(date.atTime(17, 0, 0).toInstant(ZoneOffset.UTC))
.build())
.collect(Collectors.toList()));
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/zenjob/challenge/JobServiceTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.zenjob.challenge.customexception.InvalidEndDateException;
import com.zenjob.challenge.customexception.InvalidStartDateException;
import com.zenjob.challenge.entity.Job;
import com.zenjob.challenge.entity.Shift;
import com.zenjob.challenge.service.IJobService;
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.Duration;
import java.time.LocalDate;
import java.util.UUID;

Expand All @@ -20,30 +22,51 @@ public class JobServiceTests {

@Test
public void job_should_have_at_least_one_shift() {
// given
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(1);

// when
Job job = jobService.createJob(UUID.randomUUID(), startDate, endDate);

// then
Assertions.assertEquals(1, job.getShifts().size());
}

@Test
public void start_date_cannot_be_in_the_past() {
// given
LocalDate startDate = LocalDate.now().minusDays(10);
LocalDate endDate = LocalDate.now().plusDays(2);

// when - then
Assertions.assertThrows(InvalidStartDateException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
}

@Test
public void the_end_date_should_be_after_the_start_date() {
// given
LocalDate startDate = LocalDate.now().plusDays(1);
LocalDate endDate = LocalDate.now();

// when - then
Assertions.assertThrows(InvalidEndDateException.class, () ->
jobService.createJob(UUID.randomUUID(), startDate, endDate));
}

@Test
public void shift_length_should_equal_to_8_hours() {
// given
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.now().plusDays(1);

// when
Job job = jobService.createJob(UUID.randomUUID(), startDate, endDate);

// then
Shift shift = job.getShifts().get(0);
Duration shiftDuration = Duration.between(shift.getStartTime(), shift.getEndTime());
Assertions.assertEquals(8, shiftDuration.toHours());
}
}

0 comments on commit 5deb7bb

Please sign in to comment.