Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
spring-boot-data-mongodb-curd
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
Syed Javed Ali
spring-boot-data-mongodb-curd
Commits
7036f8c9
Commit
7036f8c9
authored
Mar 01, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added http response status and Stepverifier test
parent
a0b5d0b7
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
39 additions
and
11 deletions
+39
-11
EmployeeController.java
.../nisum/example/mongodb/controller/EmployeeController.java
+10
-8
EmployeeServiceImpl.java
...om/nisum/example/mongodb/service/EmployeeServiceImpl.java
+1
-0
EmployeeControllerTest.java
...um/example/mongodb/controller/EmployeeControllerTest.java
+3
-3
EmployeeServiceTest.java
...om/nisum/example/mongodb/service/EmployeeServiceTest.java
+25
-0
No files found.
src/main/java/com/nisum/example/mongodb/controller/EmployeeController.java
View file @
7036f8c9
...
@@ -27,7 +27,7 @@ public class EmployeeController {
...
@@ -27,7 +27,7 @@ public class EmployeeController {
@PostMapping
(
path
=
"/create"
)
@PostMapping
(
path
=
"/create"
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
)
{
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
)
{
return
employeeService
.
insert
(
e
);
return
employeeService
.
insert
(
e
)
.
log
()
;
}
}
/**
/**
...
@@ -37,7 +37,7 @@ public class EmployeeController {
...
@@ -37,7 +37,7 @@ public class EmployeeController {
*/
*/
@GetMapping
(
"/{id}"
)
@GetMapping
(
"/{id}"
)
public
ResponseEntity
<
Mono
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
public
ResponseEntity
<
Mono
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
Mono
<
Employee
>
emp
=
employeeService
.
fetchById
(
id
);
Mono
<
Employee
>
emp
=
employeeService
.
fetchById
(
id
)
.
log
()
;
HttpStatus
status
=
emp
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
HttpStatus
status
=
emp
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
return
new
ResponseEntity
<
Mono
<
Employee
>>(
emp
,
status
);
return
new
ResponseEntity
<
Mono
<
Employee
>>(
emp
,
status
);
}
}
...
@@ -48,8 +48,11 @@ public class EmployeeController {
...
@@ -48,8 +48,11 @@ public class EmployeeController {
* @return Flux
* @return Flux
*/
*/
@GetMapping
(
"/name/{name}"
)
@GetMapping
(
"/name/{name}"
)
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
public
ResponseEntity
<
Flux
<
Employee
>>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
return
employeeService
.
fetchByName
(
name
);
Flux
<
Employee
>
employeeFlux
=
employeeService
.
fetchByName
(
name
).
log
();
HttpStatus
status
=
employeeFlux
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
return
new
ResponseEntity
<>(
employeeFlux
,
status
);
}
}
/**
/**
...
@@ -58,7 +61,7 @@ public class EmployeeController {
...
@@ -58,7 +61,7 @@ public class EmployeeController {
*/
*/
@GetMapping
(
value
=
""
,
produces
=
MediaType
.
TEXT_EVENT_STREAM_VALUE
)
@GetMapping
(
value
=
""
,
produces
=
MediaType
.
TEXT_EVENT_STREAM_VALUE
)
public
Flux
<
Employee
>
findAll
()
{
public
Flux
<
Employee
>
findAll
()
{
Flux
<
Employee
>
emps
=
employeeService
.
fetchAll
();
Flux
<
Employee
>
emps
=
employeeService
.
fetchAll
()
.
log
()
;
return
emps
;
return
emps
;
}
}
...
@@ -68,9 +71,8 @@ public class EmployeeController {
...
@@ -68,9 +71,8 @@ public class EmployeeController {
* @return Mono
* @return Mono
*/
*/
@PutMapping
(
"/update"
)
@PutMapping
(
"/update"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
emp
)
{
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
emp
)
{
return
employeeService
.
modify
(
emp
);
return
employeeService
.
modify
(
emp
)
.
log
()
;
}
}
/**
/**
...
@@ -79,7 +81,7 @@ public class EmployeeController {
...
@@ -79,7 +81,7 @@ public class EmployeeController {
* @return Void
* @return Void
*/
*/
@DeleteMapping
(
"/delete/{id}"
)
@DeleteMapping
(
"/delete/{id}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseStatus
(
HttpStatus
.
NO_CONTENT
)
public
void
deleteById
(
@PathVariable
(
"id"
)
Integer
id
)
{
public
void
deleteById
(
@PathVariable
(
"id"
)
Integer
id
)
{
employeeService
.
removeById
(
id
);
employeeService
.
removeById
(
id
);
}
}
...
...
src/main/java/com/nisum/example/mongodb/service/EmployeeServiceImpl.java
View file @
7036f8c9
...
@@ -24,6 +24,7 @@ public class EmployeeServiceImpl implements IEmployeeService {
...
@@ -24,6 +24,7 @@ public class EmployeeServiceImpl implements IEmployeeService {
*/
*/
@Override
@Override
public
Mono
<
Employee
>
insert
(
Employee
emp
)
{
public
Mono
<
Employee
>
insert
(
Employee
emp
)
{
return
employeeRepository
.
save
(
emp
);
return
employeeRepository
.
save
(
emp
);
}
}
...
...
src/test/java/com/nisum/example/mongodb/controller/EmployeeControllerTest.java
View file @
7036f8c9
...
@@ -156,11 +156,11 @@ public class EmployeeControllerTest {
...
@@ -156,11 +156,11 @@ public class EmployeeControllerTest {
{
{
Mono
<
Void
>
voidReturn
=
Mono
.
empty
();
Mono
<
Void
>
voidReturn
=
Mono
.
empty
();
Mockito
Mockito
.
when
(
repository
.
deleteById
(
1
))
.
when
(
repository
.
deleteById
(
20
1
))
.
thenReturn
(
voidReturn
);
.
thenReturn
(
voidReturn
);
webTestClient
.
delete
().
uri
(
"/delete/{id}"
,
1
)
webTestClient
.
delete
().
uri
(
"/delete/{id}"
,
20
1
)
.
exchange
()
.
exchange
()
.
expectStatus
().
is
Ok
();
.
expectStatus
().
is
NoContent
();
}
}
}
}
src/test/java/com/nisum/example/mongodb/service/EmployeeServiceTest.java
View file @
7036f8c9
...
@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
...
@@ -9,6 +9,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
reactor.test.StepVerifier
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
...
@@ -27,6 +28,10 @@ public class EmployeeServiceTest {
...
@@ -27,6 +28,10 @@ public class EmployeeServiceTest {
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
insert
(
employee
));
assertEquals
(
employeeMono
,
employeeService
.
insert
(
employee
));
StepVerifier
.
create
(
employeeService
.
insert
(
employee
).
log
())
.
expectNext
(
employee
)
.
expectComplete
()
.
verify
();
}
}
...
@@ -36,15 +41,24 @@ public class EmployeeServiceTest {
...
@@ -36,15 +41,24 @@ public class EmployeeServiceTest {
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findById
(
101
)).
thenReturn
(
employeeMono
);
Mockito
.
when
(
employeeRepository
.
findById
(
101
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
fetchById
(
101
));
assertEquals
(
employeeMono
,
employeeService
.
fetchById
(
101
));
StepVerifier
.
create
(
employeeService
.
fetchById
(
101
).
log
())
.
expectNext
(
employee
)
.
expectComplete
()
.
verify
();
}
}
@Test
@Test
void
testEmployeeFetchByName
(){
void
testEmployeeFetchByName
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findByName
(
"SYED"
)).
thenReturn
(
employeeFlux
);
Mockito
.
when
(
employeeRepository
.
findByName
(
"SYED"
)).
thenReturn
(
employeeFlux
);
assertEquals
(
employeeFlux
,
employeeService
.
fetchByName
(
"SYED"
));
assertEquals
(
employeeFlux
,
employeeService
.
fetchByName
(
"SYED"
));
StepVerifier
.
create
(
employeeService
.
fetchByName
(
"SYED"
).
log
())
.
expectNext
(
employee
)
.
expectComplete
()
.
verify
();
}
}
...
@@ -54,6 +68,10 @@ public class EmployeeServiceTest {
...
@@ -54,6 +68,10 @@ public class EmployeeServiceTest {
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findAll
()).
thenReturn
(
employeeFlux
);
Mockito
.
when
(
employeeRepository
.
findAll
()).
thenReturn
(
employeeFlux
);
assertEquals
(
employeeFlux
,
employeeService
.
fetchAll
());
assertEquals
(
employeeFlux
,
employeeService
.
fetchAll
());
StepVerifier
.
create
(
employeeService
.
fetchAll
())
.
expectNext
(
employee
)
.
expectComplete
()
.
verify
();
}
}
@Test
@Test
...
@@ -62,6 +80,10 @@ public class EmployeeServiceTest {
...
@@ -62,6 +80,10 @@ public class EmployeeServiceTest {
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
modify
(
employee
));
assertEquals
(
employeeMono
,
employeeService
.
modify
(
employee
));
StepVerifier
.
create
(
employeeService
.
modify
(
employee
))
.
expectNext
(
employee
)
.
expectComplete
()
.
verify
();
}
}
@Test
@Test
...
@@ -69,6 +91,9 @@ public class EmployeeServiceTest {
...
@@ -69,6 +91,9 @@ public class EmployeeServiceTest {
Mono
<
Void
>
voidMono
=
Mono
.
empty
();
Mono
<
Void
>
voidMono
=
Mono
.
empty
();
Mockito
.
when
(
employeeRepository
.
deleteById
(
1
)).
thenReturn
(
voidMono
);
Mockito
.
when
(
employeeRepository
.
deleteById
(
1
)).
thenReturn
(
voidMono
);
assertEquals
(
voidMono
,
employeeService
.
removeById
(
1
));
assertEquals
(
voidMono
,
employeeService
.
removeById
(
1
));
StepVerifier
.
create
(
employeeService
.
removeById
(
1
))
.
expectComplete
()
.
verify
();
}
}
...
...
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