Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Completed training module 02-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-hellen committed Apr 10, 2024
1 parent b125cc2 commit d0af7d7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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<BookDTO> bookList = response
.getBody()
.as(new TypeRef<List<BookDTO>>() {});
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<BookDTO> bookList = response
.body()
.as(new TypeRef<List<BookDTO>>() {});

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))));
}
}
31 changes: 18 additions & 13 deletions src/test/java/org/folio/sample/unit/domain/entity/BookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arguments> datesStream() {
return Stream.of(
Arguments.of(2020, true),
Arguments.of(2001, false),
Arguments.of(2100, false),
Arguments.of(2400, true)
);
}
}

0 comments on commit d0af7d7

Please sign in to comment.