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
a0b5d0b7
Commit
a0b5d0b7
authored
Feb 26, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test cases for EmployeeService class methods
parent
9f0fa459
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
143 additions
and
45 deletions
+143
-45
EmployeeController.java
.../nisum/example/mongodb/controller/EmployeeController.java
+12
-13
Employee.java
src/main/java/com/nisum/example/mongodb/model/Employee.java
+2
-5
EmployeeServiceImpl.java
...om/nisum/example/mongodb/service/EmployeeServiceImpl.java
+21
-15
IEmployeeService.java
...a/com/nisum/example/mongodb/service/IEmployeeService.java
+6
-6
EmployeeControllerTest.java
...um/example/mongodb/controller/EmployeeControllerTest.java
+26
-6
EmployeeServiceTest.java
...om/nisum/example/mongodb/service/EmployeeServiceTest.java
+76
-0
No files found.
src/main/java/com/nisum/example/mongodb/controller/EmployeeController.java
View file @
a0b5d0b7
...
@@ -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
.
create
(
e
);
return
employeeService
.
insert
(
e
);
}
}
/**
/**
...
@@ -37,9 +37,9 @@ public class EmployeeController {
...
@@ -37,9 +37,9 @@ 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
>
e
=
employeeService
.
find
ById
(
id
);
Mono
<
Employee
>
e
mp
=
employeeService
.
fetch
ById
(
id
);
HttpStatus
status
=
e
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
HttpStatus
status
=
e
mp
!=
null
?
HttpStatus
.
OK
:
HttpStatus
.
NOT_FOUND
;
return
new
ResponseEntity
<
Mono
<
Employee
>>(
e
,
status
);
return
new
ResponseEntity
<
Mono
<
Employee
>>(
e
mp
,
status
);
}
}
/**
/**
...
@@ -49,8 +49,7 @@ public class EmployeeController {
...
@@ -49,8 +49,7 @@ public class EmployeeController {
*/
*/
@GetMapping
(
"/name/{name}"
)
@GetMapping
(
"/name/{name}"
)
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
return
employeeService
.
fetchByName
(
name
);
return
employeeService
.
findByName
(
name
);
}
}
/**
/**
...
@@ -59,19 +58,19 @@ public class EmployeeController {
...
@@ -59,19 +58,19 @@ 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
.
f
ind
All
();
Flux
<
Employee
>
emps
=
employeeService
.
f
etch
All
();
return
emps
;
return
emps
;
}
}
/**
/**
* PUT method to do
update
of record
* PUT method to do
modify
of record
* @param e
* @param e
mp
* @return Mono
* @return Mono
*/
*/
@PutMapping
(
"/update"
)
@PutMapping
(
"/update"
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
e
)
{
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
e
mp
)
{
return
employeeService
.
update
(
e
);
return
employeeService
.
modify
(
emp
);
}
}
/**
/**
...
@@ -81,8 +80,8 @@ public class EmployeeController {
...
@@ -81,8 +80,8 @@ public class EmployeeController {
*/
*/
@DeleteMapping
(
"/delete/{id}"
)
@DeleteMapping
(
"/delete/{id}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
delete
(
@PathVariable
(
"id"
)
Integer
id
)
{
public
void
delete
ById
(
@PathVariable
(
"id"
)
Integer
id
)
{
employeeService
.
delete
(
id
).
subscribe
(
);
employeeService
.
removeById
(
id
);
}
}
}
}
src/main/java/com/nisum/example/mongodb/model/Employee.java
View file @
a0b5d0b7
package
com
.
nisum
.
example
.
mongodb
.
model
;
package
com
.
nisum
.
example
.
mongodb
.
model
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
import
lombok.NonNull
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.Scope
;
import
org.springframework.context.annotation.ScopedProxyMode
;
import
org.springframework.context.annotation.ScopedProxyMode
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.annotation.Id
;
...
@@ -18,14 +17,12 @@ import org.springframework.data.mongodb.core.mapping.Document;
...
@@ -18,14 +17,12 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Document
@Document
@Data
@Data
@NoArgsConstructor
@NoArgsConstructor
@
Required
ArgsConstructor
@
All
ArgsConstructor
public
class
Employee
{
public
class
Employee
{
@Id
@Id
private
Integer
id
;
private
Integer
id
;
@NonNull
private
String
name
;
private
String
name
;
@NonNull
private
Long
salary
;
private
Long
salary
;
...
...
src/main/java/com/nisum/example/mongodb/service/EmployeeServiceImpl.java
View file @
a0b5d0b7
...
@@ -7,8 +7,6 @@ import org.springframework.stereotype.Service;
...
@@ -7,8 +7,6 @@ import org.springframework.stereotype.Service;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.core.publisher.Mono
;
import
java.time.Duration
;
/**
/**
*Service class having methods for CURD operations
*Service class having methods for CURD operations
* Methods having Mono<T> and Flux<T> as return types
* Methods having Mono<T> and Flux<T> as return types
...
@@ -21,12 +19,12 @@ public class EmployeeServiceImpl implements IEmployeeService {
...
@@ -21,12 +19,12 @@ public class EmployeeServiceImpl implements IEmployeeService {
/**
/**
* perform create operation
* perform create operation
* @param e
* @param e
mp
* @return Mono
* @return Mono
*/
*/
@Override
@Override
public
Mono
<
Employee
>
create
(
Employee
e
)
{
public
Mono
<
Employee
>
insert
(
Employee
emp
)
{
return
employeeRepository
.
save
(
e
);
return
employeeRepository
.
save
(
e
mp
);
}
}
/**
/**
...
@@ -35,17 +33,19 @@ public class EmployeeServiceImpl implements IEmployeeService {
...
@@ -35,17 +33,19 @@ public class EmployeeServiceImpl implements IEmployeeService {
* @return Mono
* @return Mono
*/
*/
@Override
@Override
public
Mono
<
Employee
>
f
ind
ById
(
Integer
id
)
{
public
Mono
<
Employee
>
f
etch
ById
(
Integer
id
)
{
return
employeeRepository
.
findById
(
id
);
return
employeeRepository
.
findById
(
id
);
}
}
/**
/**
* perform fetch operation based on name
* perform fetch operation based on name
* @param name
* @param name
* @return Flux
* @return Flux
*/
*/
@Override
@Override
public
Flux
<
Employee
>
f
ind
ByName
(
String
name
)
{
public
Flux
<
Employee
>
f
etch
ByName
(
String
name
)
{
return
employeeRepository
.
findByName
(
name
);
return
employeeRepository
.
findByName
(
name
);
}
}
...
@@ -54,27 +54,33 @@ public class EmployeeServiceImpl implements IEmployeeService {
...
@@ -54,27 +54,33 @@ public class EmployeeServiceImpl implements IEmployeeService {
* @return Flux
* @return Flux
*/
*/
@Override
@Override
public
Flux
<
Employee
>
findAll
()
{
public
Flux
<
Employee
>
fetchAll
()
{
return
employeeRepository
.
findAll
().
delayElements
(
Duration
.
ofSeconds
(
1
));
return
employeeRepository
.
findAll
()
/*.delayElements(Duration.ofSeconds(1)).map(
employee -> {
employee.setSalary(4000L);
System.out.println(employee);
return employee;
})*/
;
}
}
/**
/**
* perform
update
of object
* perform
modify
of object
* @param e
* @param e
mp
* @return updated object of type Mono
* @return updated object of type Mono
*/
*/
@Override
@Override
public
Mono
<
Employee
>
update
(
Employee
e
)
{
public
Mono
<
Employee
>
modify
(
Employee
emp
)
{
return
employeeRepository
.
save
(
e
);
return
employeeRepository
.
save
(
e
mp
);
}
}
/**
/**
* perform
delete
based on id
* perform
removeById
based on id
* @param id
* @param id
* @return Mono
* @return Mono
*/
*/
@Override
@Override
public
Mono
<
Void
>
delete
(
Integer
id
)
{
public
Mono
<
Void
>
removeById
(
Integer
id
)
{
return
employeeRepository
.
deleteById
(
id
);
return
employeeRepository
.
deleteById
(
id
);
}
}
}
}
src/main/java/com/nisum/example/mongodb/service/IEmployeeService.java
View file @
a0b5d0b7
...
@@ -6,10 +6,10 @@ import reactor.core.publisher.Mono;
...
@@ -6,10 +6,10 @@ import reactor.core.publisher.Mono;
public
interface
IEmployeeService
{
public
interface
IEmployeeService
{
Mono
<
Employee
>
create
(
Employee
e
);
Mono
<
Employee
>
insert
(
Employee
emp
);
Mono
<
Employee
>
f
ind
ById
(
Integer
id
);
Mono
<
Employee
>
f
etch
ById
(
Integer
id
);
Flux
<
Employee
>
f
ind
ByName
(
String
name
);
Flux
<
Employee
>
f
etch
ByName
(
String
name
);
Flux
<
Employee
>
f
ind
All
();
Flux
<
Employee
>
f
etch
All
();
Mono
<
Employee
>
update
(
Employee
e
);
Mono
<
Employee
>
modify
(
Employee
emp
);
Mono
<
Void
>
delete
(
Integer
id
);
Mono
<
Void
>
removeById
(
Integer
id
);
}
}
src/test/java/com/nisum/example/mongodb/controller/EmployeeControllerTest.java
View file @
a0b5d0b7
...
@@ -45,7 +45,7 @@ public class EmployeeControllerTest {
...
@@ -45,7 +45,7 @@ public class EmployeeControllerTest {
webTestClient
.
post
()
webTestClient
.
post
()
.
uri
(
"/create"
)
.
uri
(
"/create"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
body
(
BodyInserters
.
from
Object
(
employee
))
.
body
(
BodyInserters
.
from
Value
(
employee
))
.
exchange
()
.
exchange
()
.
expectStatus
().
isCreated
();
.
expectStatus
().
isCreated
();
...
@@ -59,20 +59,21 @@ public class EmployeeControllerTest {
...
@@ -59,20 +59,21 @@ public class EmployeeControllerTest {
employee
.
setName
(
"SAM"
);
employee
.
setName
(
"SAM"
);
employee
.
setSalary
(
1500L
);
employee
.
setSalary
(
1500L
);
Mockito
.
when
(
repository
.
findById
(
employee
.
getId
()
))
Mockito
.
when
(
repository
.
findById
(
1
))
.
thenReturn
(
Mono
.
just
(
employee
));
.
thenReturn
(
Mono
.
just
(
employee
));
webTestClient
.
get
()
webTestClient
.
get
()
.
uri
(
"/{id}"
,
employee
.
getId
()
)
.
uri
(
"/{id}"
,
1
)
.
exchange
()
.
exchange
()
.
expectStatus
().
isOk
()
.
expectStatus
().
isOk
()
.
expectBody
()
.
expectBody
()
.
jsonPath
(
"$.name"
).
isNotEmpty
()
.
jsonPath
(
"$.name"
).
isNotEmpty
()
.
jsonPath
(
"$.id"
).
isEqualTo
(
employee
.
getId
()
)
.
jsonPath
(
"$.id"
).
isEqualTo
(
1
)
.
jsonPath
(
"$.name"
).
isEqualTo
(
"SAM"
)
.
jsonPath
(
"$.name"
).
isEqualTo
(
"SAM"
)
.
jsonPath
(
"$.salary"
).
isEqualTo
(
1500
);
.
jsonPath
(
"$.salary"
).
isEqualTo
(
1500
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findById
(
employee
.
getId
());
Mockito
.
verify
(
repository
,
times
(
1
))
.
findById
(
1
);
}
}
@Test
@Test
...
@@ -98,7 +99,8 @@ public class EmployeeControllerTest {
...
@@ -98,7 +99,8 @@ public class EmployeeControllerTest {
.
uri
(
"/name/{name}"
,
"SAM"
)
.
uri
(
"/name/{name}"
,
"SAM"
)
.
exchange
()
.
exchange
()
.
expectStatus
().
isOk
()
.
expectStatus
().
isOk
()
.
expectBodyList
(
Employee
.
class
);
.
expectBodyList
(
Employee
.
class
)
.
hasSize
(
2
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findByName
(
"SAM"
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findByName
(
"SAM"
);
}
}
...
@@ -130,6 +132,24 @@ public class EmployeeControllerTest {
...
@@ -130,6 +132,24 @@ public class EmployeeControllerTest {
Mockito
.
verify
(
repository
,
times
(
1
)).
findAll
();
Mockito
.
verify
(
repository
,
times
(
1
)).
findAll
();
}
}
@Test
void
testUpdateEmployee
(){
Employee
employee
=
new
Employee
();
employee
.
setId
(
3
);
employee
.
setName
(
"SYED"
);
employee
.
setSalary
(
1800L
);
Mockito
.
when
(
repository
.
save
(
employee
)).
thenReturn
(
Mono
.
just
(
employee
));
webTestClient
.
put
()
.
uri
(
"/update"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
body
(
BodyInserters
.
fromValue
(
employee
))
.
exchange
()
.
expectStatus
().
isOk
();
Mockito
.
verify
(
repository
,
times
(
1
)).
save
(
employee
);
}
@Test
@Test
void
testDeleteEmployee
()
void
testDeleteEmployee
()
...
...
src/test/java/com/nisum/example/mongodb/service/EmployeeServiceTest.java
0 → 100644
View file @
a0b5d0b7
package
com
.
nisum
.
example
.
mongodb
.
service
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.repository.EmployeeRepository
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
@SpringBootTest
public
class
EmployeeServiceTest
{
@Autowired
private
IEmployeeService
employeeService
;
@MockBean
private
EmployeeRepository
employeeRepository
;
@Test
void
testInsertEmployee
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
insert
(
employee
));
}
@Test
void
testEmployeeFetchById
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findById
(
101
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
fetchById
(
101
));
}
@Test
void
testEmployeeFetchByName
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findByName
(
"SYED"
)).
thenReturn
(
employeeFlux
);
assertEquals
(
employeeFlux
,
employeeService
.
fetchByName
(
"SYED"
));
}
@Test
void
testFetchAllEmployees
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
findAll
()).
thenReturn
(
employeeFlux
);
assertEquals
(
employeeFlux
,
employeeService
.
fetchAll
());
}
@Test
void
testModifyEmployee
(){
Employee
employee
=
new
Employee
(
101
,
"SYED"
,
1500L
);
Mono
<
Employee
>
employeeMono
=
Mono
.
just
(
employee
);
Mockito
.
when
(
employeeRepository
.
save
(
employee
)).
thenReturn
(
employeeMono
);
assertEquals
(
employeeMono
,
employeeService
.
modify
(
employee
));
}
@Test
void
testRemoveByIdEmployee
(){
Mono
<
Void
>
voidMono
=
Mono
.
empty
();
Mockito
.
when
(
employeeRepository
.
deleteById
(
1
)).
thenReturn
(
voidMono
);
assertEquals
(
voidMono
,
employeeService
.
removeById
(
1
));
}
}
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