Skip to content

Commit

Permalink
update images in README, add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-baiborodine committed Nov 13, 2023
1 parent 21987e2 commit 6e06226
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.kiroule.campsitebooking.controller;

import static java.util.Objects.isNull;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.http.HttpStatus.*;

import com.kiroule.campsitebooking.api.v2.BookingApiDelegate;
import com.kiroule.campsitebooking.api.v2.dto.BookingDto;
import com.kiroule.campsitebooking.mapper.BookingMapper;
import com.kiroule.campsitebooking.service.BookingService;
import java.net.URI;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;
import lombok.AllArgsConstructor;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand All @@ -30,7 +29,7 @@ public class BookingApiControllerImpl implements BookingApiDelegate {

@Override
public ResponseEntity<List<LocalDate>> getVacantDates(
Long campsiteId, LocalDate startDate, LocalDate endDate) {
Long campsiteId, LocalDate startDate, LocalDate endDate) {
if (isNull(startDate)) {
startDate = LocalDate.now().plusDays(1);
}
Expand All @@ -50,10 +49,12 @@ public ResponseEntity<BookingDto> getBooking(UUID uuid) {
@Override
public ResponseEntity<BookingDto> addBooking(BookingDto bookingDto) {
var booking = bookingService.createBooking(bookingMapper.toBooking(bookingDto));
var selfLink = WebMvcLinkBuilder.linkTo(this.getClass()).slash(booking.getUuid()).withSelfRel();
var headers = new HttpHeaders();
headers.setLocation(URI.create(selfLink.getHref()));

headers.setLocation(
linkTo(BookingApiControllerImpl.class)
.slash("api/v2/booking")
.slash(booking.getUuid())
.toUri());
return new ResponseEntity<>(bookingMapper.toBookingDto(booking), headers, CREATED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.kiroule.campsitebooking.controller;

import static com.kiroule.campsitebooking.TestDataHelper.*;
import static java.time.LocalDate.now;
import static java.util.Collections.singletonList;
import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.HttpStatus.*;

import com.kiroule.campsitebooking.api.v2.dto.BookingDto;
import com.kiroule.campsitebooking.mapper.BookingMapper;
import com.kiroule.campsitebooking.model.Booking;
import com.kiroule.campsitebooking.service.BookingService;
import java.time.LocalDate;
import java.util.List;
Expand All @@ -29,19 +32,62 @@
@ExtendWith(MockitoExtension.class)
public class BookingApiControllerTest {

@Mock BookingService bookingService;

@Mock
BookingService bookingService;
BookingMapper bookingMapper;

@InjectMocks BookingApiControllerImpl classUnderTest;

@Nested
class AddBooking {

@Test
void happy_path() {
// given
BookingDto bookingDto = nextBookingDto();
Booking booking = nextBooking();
when(bookingMapper.toBooking(any(BookingDto.class))).thenReturn(booking);
when(bookingService.createBooking(any(Booking.class))).thenReturn(booking);
when(bookingMapper.toBookingDto(any(Booking.class))).thenReturn(bookingDto);
// when
ResponseEntity<BookingDto> result = classUnderTest.addBooking(bookingDto);
// then
assertThat(result.getStatusCode()).isEqualTo(CREATED);
assertThat(result.getBody()).isEqualTo(bookingDto);
assertThat(result.getHeaders().getLocation().getPath())
.isEqualTo("/api/v2/booking/" + booking.getUuid());

@InjectMocks
BookingApiControllerImpl classUnderTest;
verify(bookingMapper).toBooking(bookingDto);
verify(bookingService).createBooking(booking);
verify(bookingMapper).toBookingDto(booking);
}
}

@Nested
class GetVacantDates {

private static final Long CAMPSITE_ID = 1L;

@Test
void when_start_date_is_null__then_start_date_is_now_plus_one_day() {
void happy_path() {
// given
LocalDate now = now();
LocalDate startDate = now.plusDays(10);
LocalDate endDate = now.plusDays(15);
doReturn(singletonList(now.plusDays(12)))
.when(bookingService)
.findVacantDates(any(), any(), any());
// when
List<LocalDate> result =
classUnderTest.getVacantDates(CAMPSITE_ID, startDate, endDate).getBody();
// then
assertThat(result).isEqualTo(singletonList(now.plusDays(12)));
verify(bookingService).findVacantDates(startDate, endDate, CAMPSITE_ID);
}

@Test
void given_start_date_is_null__then_start_date_is_now_plus_one_day() {
// given
LocalDate now = now();
LocalDate startDate = null;
Expand All @@ -58,7 +104,7 @@ void when_start_date_is_null__then_start_date_is_now_plus_one_day() {
}

@Test
void when_end_date_is_null__then_end_date_is_start_date_plus_one_month() {
void given_end_date_is_null__then_end_date_is_start_date_plus_one_month() {
// given
LocalDate now = now();
LocalDate startDate = now.plusDays(15);
Expand All @@ -75,12 +121,11 @@ void when_end_date_is_null__then_end_date_is_start_date_plus_one_month() {
}
}


@Nested
class CancelBooking {

@Test
void happyPath() {
void happy_path() {
// given
UUID uuid = randomUUID();
when(bookingService.cancelBooking(any())).thenReturn(true);
Expand All @@ -92,7 +137,7 @@ void happyPath() {
}

@Test
void givenBookingIsNotCancelled_thenBadRequestIsReturned() {
void given_booking_is_not_cancelled__then_bad_request_is_returned() {
UUID uuid = randomUUID();
when(bookingService.cancelBooking(any())).thenReturn(false);
// when
Expand Down
Binary file modified readme/swagger-add-booking-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified readme/swagger-add-booking-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e06226

Please sign in to comment.