Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/edit put book #7

Merged
merged 15 commits into from
Aug 16, 2024
Merged

Feat/edit put book #7

merged 15 commits into from
Aug 16, 2024

Conversation

marcelherr
Copy link
Contributor

added a edit form to update books with integration and unit tests

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

.contentType("application/json")
.content("""
{
"id": "1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to update (or change) the value of id? If the id is not changed after creation, do we need to add it as a payload for the PUT method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes id should not be part of payload

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This variable is not used in the test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint, i change that.

Copy link
Contributor

@esgoet esgoet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can also leave frontend as is and do that later

.contentType("application/json")
.content("""
{
"id": "1",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes id should not be part of payload


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would simplify it and just also have an updatedBook of type Book (and rename updatedBook to updatedBookDto)

Suggested change
when(bookRepo.save(any(Book.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
when(bookRepo.save(updatedBook).thenAnswer(updatedBook);

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment below

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

Book result = bookService.updateBook(updatedBook, id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


// Then
assertNotNull(result);
assertEquals(updatedBook.title(), result.title());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then just here, instead of testing every property

Suggested change
assertEquals(updatedBook.title(), result.title());
assertEquals(updatedBook, result);

//Then
BookNotFoundException thrown = assertThrows(
BookNotFoundException.class,
() -> bookService.updateBook(updatedBook, id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
() -> 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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
verify(bookRepo, never()).save(any(Book.class));
verify(bookRepo, never()).save(updatedBook);

frontend/src/components/editForm/EditForm.tsx Show resolved Hide resolved
<label>Description: </label>
<textarea rows={5} cols={30} placeholder={book.description} name="description" value={formData.description}
onChange={handleChange}/>
<label>Genre: <input placeholder={book.genre} type="text" name="genre" value={formData.genre}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be select with options to match addBook form, but we can also add that later

}

function onCancel() {
console.log("cancel")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add original book info back into the form?

Copy link

Quality Gate Passed Quality Gate passed for 'neuefische_hh-java-24-3-teamproject-team-2-frontend'

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

Quality Gate Passed Quality Gate passed for 'neuefische_hh-java-24-3-teamproject-team-2-backend'

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
100.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link
Contributor

@esgoet esgoet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm :)

@marcelherr marcelherr merged commit 62251b4 into main Aug 16, 2024
5 checks passed
@marcelherr marcelherr deleted the Feat/Edit-PUT-book branch August 16, 2024 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants