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 {
.bind("author", book.getAuthor())
.fetch()
.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) {
return Mono.just(book); //Returning the saved employee
return Mono.just(book).onErrorResume(); //Returning the saved employee
} else {
return Mono.error(new BookNotFoundException(AppErrorCode.BOOK_NOT_FOUND,"Data Invalid") {
});
}
});
});*/
}
......@@ -80,8 +89,17 @@ public class BookDao {
.bind("author", book.getAuthor())
.fetch()
.rowsUpdated().
flatMap(rows ->
.rowsUpdated().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) {
return Mono.just(book); //Returning the saved employee
......@@ -92,15 +110,23 @@ public class BookDao {
}
});
});*/
}
public Mono<Void> deleteBook(Integer id) {
return databaseClient.sql("DELETE FROM book_details WHERE book_id = :id")
.bind("id", id)
.fetch()
.rowsUpdated().
flatMap(rows ->
.rowsUpdated().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) {
......@@ -114,7 +140,7 @@ public class BookDao {
}
});
});*/
}
}
......@@ -20,23 +20,23 @@ import static org.mockito.Mockito.*;
@SpringBootTest
class BookDaoTest {
@Mock
private DatabaseClient databaseClient;
@Mock
// private DatabaseClient databaseClient;
@InjectMocks
private BookServiceImpl bookService;
//private Book book;
private Book book;
@BeforeEach
public void setUp() {
/* public void setUp() {
// 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");
// Book book = new Book(1, "C++", "This is c++", "C publiser", "Test Author");
}
}*/
@Test
public void testSaveBook_Success() {
......@@ -63,9 +63,58 @@ class BookDaoTest {
.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