Commit 1d86a999 authored by Prangshu Maity's avatar Prangshu Maity

update dao layer exception to webflux exception

parent 72216b67
...@@ -29,15 +29,24 @@ public class BookDao { ...@@ -29,15 +29,24 @@ public class BookDao {
.bind("author", book.getAuthor()) .bind("author", book.getAuthor())
.fetch() .fetch()
.rowsUpdated().flatMap(rows -> .rowsUpdated().flatMap(rows ->
Mono.justOrEmpty(book) // empty if book is null)
.switchIfEmpty(Mono.error(new BookNotFoundException(AppErrorCode.BOOK_NOT_FOUND, "Data Invalid"))) // If book is null, throw error
.onErrorResume(BookNotFoundException.class, ex -> {
// Handle specific error if needed
return Mono.error(ex); // Propagate the original error if required
})
);
/*flatMap(rows ->
{ {
if (rows > 0) { if (rows > 0) {
return Mono.just(book); //Returning the saved employee return Mono.just(book).onErrorResume(); //Returning the saved employee
} else { } else {
return Mono.error(new BookNotFoundException(AppErrorCode.BOOK_NOT_FOUND,"Data Invalid") { return Mono.error(new BookNotFoundException(AppErrorCode.BOOK_NOT_FOUND,"Data Invalid") {
}); });
} }
}); });*/
} }
...@@ -80,8 +89,17 @@ public class BookDao { ...@@ -80,8 +89,17 @@ public class BookDao {
.bind("author", book.getAuthor()) .bind("author", book.getAuthor())
.fetch() .fetch()
.rowsUpdated(). .rowsUpdated().flatMap(rows ->
flatMap(rows -> Mono.justOrEmpty(rows) // Wrap rows in Mono
.filter(r -> r > 0) // Only continue if rows > 0, else it will trigger error handling
.switchIfEmpty(Mono.error(new BookNotFoundException(AppErrorCode.BOOK_UPDATE_FAILED, "UPDATE OF BOOK NOT POSSIBLE"))) // If rows <= 0, trigger error
.flatMap(r -> Mono.just(book)) // If rows > 0, return the book
);
/*flatMap(rows ->
{ {
if (rows > 0) { if (rows > 0) {
return Mono.just(book); //Returning the saved employee return Mono.just(book); //Returning the saved employee
...@@ -92,15 +110,23 @@ public class BookDao { ...@@ -92,15 +110,23 @@ public class BookDao {
} }
}); });*/
} }
public Mono<Void> deleteBook(Integer id) { public Mono<Void> deleteBook(Integer id) {
return databaseClient.sql("DELETE FROM book_details WHERE book_id = :id") return databaseClient.sql("DELETE FROM book_details WHERE book_id = :id")
.bind("id", id) .bind("id", id)
.fetch() .fetch()
.rowsUpdated(). .rowsUpdated().flatMap(rows ->
flatMap(rows -> Mono.justOrEmpty(rows) // Wrap rows in Mono
.filter(r -> r > 0) // Continue if rows > 0, else proceed to error handling
.switchIfEmpty(Mono.error(new BookNotFoundException(AppErrorCode.BOOK_DELETION_FAILED, "Invalid Book data"))) // Trigger error if rows <= 0
.then(Mono.empty()) // If rows > 0, return Mono.empty()
);
/*flatMap(rows ->
{ {
if (rows > 0) { if (rows > 0) {
...@@ -114,7 +140,7 @@ public class BookDao { ...@@ -114,7 +140,7 @@ public class BookDao {
} }
}); });*/
} }
} }
...@@ -21,22 +21,22 @@ import static org.mockito.Mockito.*; ...@@ -21,22 +21,22 @@ import static org.mockito.Mockito.*;
class BookDaoTest { class BookDaoTest {
@Mock @Mock
private DatabaseClient databaseClient; // private DatabaseClient databaseClient;
@InjectMocks @InjectMocks
private BookServiceImpl bookService; private BookServiceImpl bookService;
//private Book book; private Book book;
@BeforeEach @BeforeEach
public void setUp() { /* public void setUp() {
// book = new Book(1, "Java Book", "A test description", "Test Publisher", "Test Author"); // book = new Book(1, "Java Book", "A test description", "Test Publisher", "Test Author");
// BookDto bookDto = new BookDto(1, "C++", "This is c++", "C publiser", "Test Author"); // BookDto bookDto = new BookDto(1, "C++", "This is c++", "C publiser", "Test Author");
// Book book = new Book(1, "C++", "This is c++", "C publiser", "Test Author"); // Book book = new Book(1, "C++", "This is c++", "C publiser", "Test Author");
} }*/
@Test @Test
public void testSaveBook_Success() { public void testSaveBook_Success() {
...@@ -63,9 +63,58 @@ class BookDaoTest { ...@@ -63,9 +63,58 @@ class BookDaoTest {
.verifyComplete(); // The Mono should complete successfully .verifyComplete(); // The Mono should complete successfully
} }
@Mock
private DatabaseClient databaseClient; // Mocked database client
@InjectMocks
private BookDao bookDao; // Class under test
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this); // Initialize mocks
}
@Test
void saveBook_shouldReturnBook_whenInsertionIsSuccessful() {
// Arrange
Book book = new Book(1, "Test Book", "A test description", "Test Publisher", "Test Author");
// Mock the databaseClient behavior
DatabaseClient.GenericExecuteSpec executeSpec = mock(DatabaseClient.GenericExecuteSpec.class);
when(databaseClient.sql(anyString())).thenReturn(executeSpec);
when(executeSpec.bind(anyString(), any())).thenReturn(executeSpec);
// when(executeSpec.fetch()).thenReturn(mock(DatabaseClient.RowsUpdatedSpec.class));
// when(executeSpec.rowsUpdated()).thenReturn(Mono.just(1L)); // Simulate a successful insert (1 row updated)
// Act & Assert
StepVerifier.create(bookDao.saveBook(book))
.expectNext(book) // Expect the saved book to be returned
.verifyComplete(); // Ensure the Mono completes successfully
}
@Test
void saveBook_shouldReturnError_whenInsertionFails() {
// Arrange
Book book = new Book(123, "Test Book", "A test description", "Test Publisher", "Test Author");
// Mock the databaseClient behavior
DatabaseClient.GenericExecuteSpec executeSpec = mock(DatabaseClient.GenericExecuteSpec.class);
when(databaseClient.sql(anyString())).thenReturn(executeSpec);
when(executeSpec.bind(anyString(), any())).thenReturn(executeSpec);
//when(executeSpec.fetch()).thenReturn(mock(DatabaseClient.RowsUpdatedSpec.class));
// when(executeSpec.rowsUpdated()).thenReturn(Mono.just(0L)); // Simulate a failure (0 rows updated)
// Act & Assert
//StepVerifier.create(bookDao.saveBook(book))
// .expectError(BookInsertException.class) // Expect the custom exception
// .verify(); // Ensure the Mono emits an error
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment