Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
FreshPassPoc2_BookService
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Prangshu Maity
FreshPassPoc2_BookService
Commits
1d86a999
Commit
1d86a999
authored
Dec 12, 2024
by
Prangshu Maity
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update dao layer exception to webflux exception
parent
72216b67
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
13 deletions
+88
-13
BookDao.java
src/main/java/com/nisum/BookInfo/dao/BookDao.java
+34
-8
BookDaoTest.java
src/test/java/com/nisum/BookInfo/dao/BookDaoTest.java
+54
-5
No files found.
src/main/java/com/nisum/BookInfo/dao/BookDao.java
View file @
1d86a999
...
...
@@ -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 {
}
});
});
*/
}
}
src/test/java/com/nisum/BookInfo/dao/BookDaoTest.java
View file @
1d86a999
...
...
@@ -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
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment