Skip to content

Commit

Permalink
fix integration and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelherr committed Aug 16, 2024
1 parent db3b930 commit 69e84f6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

@Repository
public interface BookRepository extends MongoRepository<Book, String> {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.esgoet.backend.controllers;

import com.github.esgoet.backend.dto.BookDto;
import com.github.esgoet.backend.models.Book;
import com.github.esgoet.backend.models.Genre;
import com.github.esgoet.backend.repositories.BookRepository;
Expand Down Expand Up @@ -129,15 +130,12 @@ void updateBook_Test_When_IdMatches() throws Exception {
// GIVEN
bookRepository.save(new Book("1", "author1", "title1", Genre.FANTASY, "description1", "12345678", "cover1", localDate));

Book updatedBook = new Book("1", "author2", "title2", Genre.HISTORY, "description2", "23456789", "cover2", localDate);

// WHEN
mockMvc.perform(MockMvcRequestBuilders.put("/api/books/1/update")
.contentType("application/json")
.content("""
{
"id": "1",
"author": "author2",
"author": "author2",
"title": "title2",
"genre": "HISTORY",
"description": "description2",
Expand All @@ -149,16 +147,14 @@ void updateBook_Test_When_IdMatches() throws Exception {
// THEN
.andExpect(status().isOk())
.andExpect(content().json("""
{
"id": "1",
"author": "author2",
{ "id": "1",
"author": "author2",
"title": "title2",
"genre": "HISTORY",
"description": "description2",
"isbn": "23456789",
"cover": "cover2",
"publicationDate": "2024-08-14"
}
"""));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,45 +104,41 @@ void testUpdateBook_Success() {
// Given
String id = "1";
Book existingBook = new Book(id, "author1", "title1", Genre.THRILLER, "description1", "12345678", "cover1", localDate);
BookDto updatedBook = new BookDto("author2", "title2", Genre.FICTION, "description2", "23456789", "cover2", localDate);
BookDto updatedBookDto = new BookDto("author2", "title2", Genre.FICTION, "description2", "23456789", "cover2", localDate);
Book updatedBook = new Book("1", "author2", "title2", Genre.FICTION, "description2", "23456789", "cover2", localDate);

// When
when(bookRepo.findById(id)).thenReturn(Optional.of(existingBook));
when(bookRepo.save(any(Book.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
when(bookRepo.save(updatedBook)).thenReturn(updatedBook);

Book result = bookService.updateBook(updatedBook, id);
Book result = bookService.updateBook(updatedBookDto, id);

// Then
assertNotNull(result);
assertEquals(updatedBook.title(), result.title());
assertEquals(updatedBook.author(), result.author());
assertEquals(updatedBook.genre(), result.genre());
assertEquals(updatedBook.description(), result.description());
assertEquals(updatedBook.cover(), result.cover());
assertEquals(updatedBook.isbn(), result.isbn());
assertEquals(updatedBook.publicationDate(), result.publicationDate());
assertEquals(updatedBook, result);
verify(bookRepo).findById(id);
verify(bookRepo).save(any(Book.class));
verify(bookRepo).save(updatedBook);
}

@Test
void testUpdateBook_BookNotFound() {

// Given
String id = "1";
BookDto updatedBook = new BookDto("author", "title", Genre.FICTION, "description", "isbn", "cover", localDate);
BookDto updatedBookDto = new BookDto("author", "title", Genre.FICTION, "description", "isbn", "cover", localDate);
Book updatedBook = new Book("1", "author", "title", Genre.FICTION, "description", "isbn", "cover", localDate);

//When
when(bookRepo.findById(id)).thenReturn(Optional.empty());

//Then
BookNotFoundException thrown = assertThrows(
BookNotFoundException.class,
() -> bookService.updateBook(updatedBook, id)
() -> bookService.updateBook(updatedBookDto, id)
);
assertEquals("No book found with id: " + id, thrown.getMessage());
verify(bookRepo).findById(id);
verify(bookRepo, never()).save(any(Book.class));
verify(bookRepo, never()).save(updatedBook);
}

}

0 comments on commit 69e84f6

Please sign in to comment.