Skip to content

Commit 5e3978a

Browse files
committed
Fix flaky test
1 parent edba0c0 commit 5e3978a

5 files changed

Lines changed: 20 additions & 15 deletions

File tree

server/src/main/java/dev/findfirst/core/controller/BookmarkController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,13 @@ public ResponseEntity<BookmarkTag> deleteTagFromBookmark(
162162
@RequestParam("tag") @NotBlank String title)
163163
throws TagNotFoundException, BookmarkNotFoundException {
164164

165+
log.debug("Attempt Delete");
165166
// verify that the bookmark exists.
166167
var b = bookmarkService.findByIdJDBC(bookmarkID).orElseThrow(BookmarkNotFoundException::new);
168+
log.debug("Found bookmark");
167169

168170
var t = tagService.findIdByTagTitleJDBC(title).orElseThrow(TagNotFoundException::new);
171+
log.debug("Found tag");
169172

170173
return new ResponseEntity<>(bookmarkService.deleteTag(new BookmarkTag(b.getId(), t)),
171174
HttpStatus.OK);

server/src/main/java/dev/findfirst/core/service/BookmarkService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public PaginatedBookmarkRes listPaginatedJDBC(PaginatedBookmarkReq reqBkmk)
8686

8787
return new PaginatedBookmarkRes(bookmarks, totalPages, reqBkmk.page());
8888

89-
9089
}
9190

9291
public Optional<BookmarkDTO> getBookmarkById(long id) {
@@ -307,8 +306,12 @@ public void addTagToBookmarkJDBC(BookmarkJDBC bookmark, TagJDBC tag) {
307306
bookmarkTagRepository.saveBookmarkTag(new BookmarkTag(bookmark.getId(), tag.getId()));
308307
}
309308

310-
public BookmarkTag deleteTag(BookmarkTag bt) {
311-
bookmarkTagRepository.deleteBookmarkTag(bt);
309+
public BookmarkTag deleteTag(BookmarkTag bt) throws TagNotFoundException {
310+
var deletedCount = bookmarkTagRepository.deleteBookmarkTag(bt);
311+
log.debug("DELETED: {} BookmarkTags", deletedCount);
312+
if (deletedCount == 0) {
313+
throw new TagNotFoundException();
314+
}
312315
return bt;
313316
}
314317

server/src/main/java/dev/findfirst/core/service/TagService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public Optional<TagJDBC> findByTagTitleJDBC(@NonNull String title) {
165165
public Optional<Long> findIdByTagTitleJDBC(@NotBlank String title) {
166166
var tag = tagRepositoryJDBC.findIdByTitle(title, userContext.getUserId());
167167
if (tag.isPresent()) {
168+
log.debug("found tag {}", tag.get());
168169
return Optional.of(tag.get());
169170
} else {
170171
return Optional.ofNullable(null);

server/src/main/resources/application-dev.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# General logging
22
# logging.level.root=INFO
33
logging.level.dev.findfirst=Debug
4-
# logging.level.org.springframework.jdbc=DEBUG
4+
logging.level.org.springframework.jdbc=DEBUG
55

66
# Datasource properties, i.e. postgres.
77
spring.datasource.url=jdbc:postgresql://localhost:5432/findfirst

server/src/test/java/dev/findfirst/core/controller/BookmarkControllerTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import lombok.extern.slf4j.Slf4j;
2323
import org.junit.jupiter.api.BeforeEach;
24-
import org.junit.jupiter.api.Disabled;
2524
import org.junit.jupiter.api.Test;
2625
import org.mockito.Mockito;
2726
import org.springframework.beans.factory.annotation.Autowired;
@@ -82,7 +81,6 @@ void setUp() {
8281
"Effective Java Programming", "Healthy Meal Plans", "Best Running Shoes 2024",
8382
"Beginner’s Guide to Investing", "How to Brew the Perfect Coffee");
8483

85-
8684
@Test
8785
void containerStarupTest() {
8886
assertEquals(true, postgres.isRunning());
@@ -140,7 +138,6 @@ void pageGreaterThanTotalPage() {
140138
Integer page = 6;
141139
Integer size = 6;
142140

143-
144141
var response = restTemplate.exchange(bookmarksPaginationURI + "?page={page}&size={size}",
145142
HttpMethod.GET, getHttpEntity(restTemplate), PaginatedBookmarkRes.class, page, size);
146143

@@ -394,22 +391,21 @@ void deleteBookmarkById() {
394391
}
395392

396393
@Test
397-
// flaky test need to investigate why.
398-
@Disabled
399394
void attemptToDeleteBookmarkTagThatDoesNotExist() {
400395

396+
var header = getHttpEntity(restTemplate);
401397
var delResp = restTemplate.exchange(bookmarkURI + "/100" + "/tag?tag={tag}", HttpMethod.DELETE,
402-
getHttpEntity(restTemplate), String.class, "random");
398+
header, String.class, "random");
403399

404400
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, delResp.getStatusCode());
405401
assertEquals(new BookmarkNotFoundException().getMessage(), delResp.getBody());
406402

407403
// add the tag.
408404
restTemplate.exchange("/api/tags", HttpMethod.POST,
409-
getHttpEntity(restTemplate, List.of("buildings")), TagDTO[].class);
405+
new HttpEntity<>(List.of("buildings"), header.getHeaders()), TagDTO[].class);
410406

411-
delResp = restTemplate.exchange(bookmarkURI + "/5" + "/tag?tag={tag}", HttpMethod.DELETE,
412-
getHttpEntity(restTemplate), String.class, "buildings");
407+
delResp = restTemplate.exchange(bookmarkURI + "/2" + "/tag?tag={tag}", HttpMethod.DELETE,
408+
header, String.class, "buildings");
413409

414410
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, delResp.getStatusCode());
415411
assertEquals(new TagNotFoundException().getMessage(), delResp.getBody());
@@ -521,7 +517,8 @@ void importBookmarksWithUnsupportedContentType() throws IOException {
521517
// Create a byte array with application/json content
522518
byte[] fileContent = "{\"bookmarks\": []}".getBytes(StandardCharsets.UTF_8);
523519

524-
// Build the multipart request with a .html extension but unsupported content type
520+
// Build the multipart request with a .html extension but unsupported content
521+
// type
525522
var bodyBuilder = new MultipartBodyBuilder();
526523
bodyBuilder.part("file", fileContent).filename("invalid_content.html")
527524
.contentType(MediaType.APPLICATION_JSON); // Unsupported content type
@@ -586,7 +583,8 @@ void importBookmarksWithTextPlainContentType() throws IOException {
586583
byte[] fileContent =
587584
"<html><body>Plain Text Content</body></html>".getBytes(StandardCharsets.UTF_8);
588585

589-
// Build the multipart request with a .html extension and text/plain content type
586+
// Build the multipart request with a .html extension and text/plain content
587+
// type
590588
var bodyBuilder = new MultipartBodyBuilder();
591589
bodyBuilder.part("file", fileContent).filename("plainText.html")
592590
.contentType(MediaType.TEXT_PLAIN); // Supported content type

0 commit comments

Comments
 (0)