diff --git a/src/test/java/org/folio/sample/integration/api/books/GetLeapYearBookTest.java b/src/test/java/org/folio/sample/integration/api/books/GetLeapYearBookTest.java new file mode 100644 index 0000000..bd94055 --- /dev/null +++ b/src/test/java/org/folio/sample/integration/api/books/GetLeapYearBookTest.java @@ -0,0 +1,59 @@ +package org.folio.sample.integration.api.books; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +import io.restassured.common.mapper.TypeRef; +import io.restassured.response.Response; +import java.time.LocalDate; +import java.util.List; +import org.folio.sample.domain.dto.BookDTO; +import org.folio.sample.domain.dto.BookForCreationDTO; +import org.folio.sample.integration.AbstractBaseApiTest; +import org.junit.jupiter.api.Test; +import org.springframework.http.HttpStatus; + +public class GetLeapYearBookTest extends AbstractBaseApiTest { + @Test + void testEmptyGet(){ + Response response = ra() + .queryParam("onlyLeapYears", true) + .get(getRequestUrl("books")); + response.then().statusCode(is(HttpStatus.OK.value())); + + List bookList = response + .getBody() + .as(new TypeRef>() {}); + assertThat(bookList, hasSize(0)); + } + + @Test + void testGetWithBook() { + ra() + .body( + BookForCreationDTO + .builder() + .name("test book") + .publishedDate(LocalDate.of(2020, 1, 1)) + .build() + ) + .post(getRequestUrl("books")) + .then() + .statusCode(is(HttpStatus.CREATED.value())); + + Response response = ra() + .queryParam("onlyLeap", true) + .get(getRequestUrl("books")); + response.then().statusCode(is(HttpStatus.OK.value())); + + List bookList = response + .body() + .as(new TypeRef>() {}); + + assertThat(bookList, hasSize(1)); + assertThat(bookList.get(0).getName(), is(equalTo("test book"))); + assertThat(bookList.get(0).getPublishedDate(), is(equalTo(LocalDate.of(2020, 1, 1)))); + } +} diff --git a/src/test/java/org/folio/sample/unit/domain/entity/BookTest.java b/src/test/java/org/folio/sample/unit/domain/entity/BookTest.java index 8e5e0cf..642d974 100644 --- a/src/test/java/org/folio/sample/unit/domain/entity/BookTest.java +++ b/src/test/java/org/folio/sample/unit/domain/entity/BookTest.java @@ -4,28 +4,33 @@ import static org.hamcrest.Matchers.is; import java.time.LocalDate; +import java.util.stream.Stream; + import org.folio.sample.domain.entity.Book; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.Arguments; + class BookTest { - @Test - void testLeapYear() { + @ParameterizedTest + @MethodSource("datesStream") + void testIsLeapYear (int year, Boolean result) { Book testBook = Book .builder() .name("Sample book") - .publishedDate(LocalDate.of(2020, 1, 1)) + .publishedDate(LocalDate.of(year, 1, 1)) .build(); - assertThat(testBook.isPublishedInLeapYear(), is(true)); + assertThat(testBook.isPublishedInLeapYear(), is(result)); } - @Test - void testNotLeapYear() { - Book testBook = Book - .builder() - .name("Sample book") - .publishedDate(LocalDate.of(2001, 1, 1)) - .build(); - assertThat(testBook.isPublishedInLeapYear(), is(false)); + private static Stream datesStream() { + return Stream.of( + Arguments.of(2020, true), + Arguments.of(2001, false), + Arguments.of(2100, false), + Arguments.of(2400, true) + ); } }