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
9f0fa459
Commit
9f0fa459
authored
4 years ago
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test cases for EmployeeController class methods
parent
d597a94b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
227 additions
and
9 deletions
+227
-9
build.gradle
build.gradle
+0
-1
EmployeeController.java
.../nisum/example/mongodb/controller/EmployeeController.java
+35
-2
Employee.java
src/main/java/com/nisum/example/mongodb/model/Employee.java
+7
-2
EmployeeServiceImpl.java
...om/nisum/example/mongodb/service/EmployeeServiceImpl.java
+38
-3
IEmployeeService.java
...a/com/nisum/example/mongodb/service/IEmployeeService.java
+1
-1
EmployeeControllerTest.java
...um/example/mongodb/controller/EmployeeControllerTest.java
+146
-0
No files found.
build.gradle
View file @
9f0fa459
...
...
@@ -25,7 +25,6 @@ dependencies {
compileOnly
'org.projectlombok:lombok'
annotationProcessor
'org.projectlombok:lombok'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
//testImplementation 'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
testImplementation
'io.projectreactor:reactor-test'
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/com/nisum/example/mongodb/controller/EmployeeController.java
View file @
9f0fa459
...
...
@@ -10,18 +10,31 @@ import org.springframework.web.bind.annotation.*;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
/**
* Controller class having http POST, GET, UPDATE and DELETE methods
*/
@RestController
public
class
EmployeeController
{
@Autowired
private
IEmployeeService
employeeService
;
/**
* POST method to perform record creation
* @param e
* @return Mono
*/
@PostMapping
(
path
=
"/create"
)
@ResponseStatus
(
HttpStatus
.
CREATED
)
public
void
create
(
@RequestBody
Employee
e
)
{
employeeService
.
create
(
e
);
public
Mono
<
Employee
>
create
(
@RequestBody
Employee
e
)
{
return
employeeService
.
create
(
e
);
}
/**
* GET method to fetch record based on Id
* @param id
* @return Mono
*/
@GetMapping
(
"/{id}"
)
public
ResponseEntity
<
Mono
<
Employee
>>
findById
(
@PathVariable
(
"id"
)
Integer
id
)
{
Mono
<
Employee
>
e
=
employeeService
.
findById
(
id
);
...
...
@@ -29,23 +42,43 @@ public class EmployeeController {
return
new
ResponseEntity
<
Mono
<
Employee
>>(
e
,
status
);
}
/**
* GET method to fetch record based on name
* @param name
* @return Flux
*/
@GetMapping
(
"/name/{name}"
)
public
Flux
<
Employee
>
findByName
(
@PathVariable
(
"name"
)
String
name
)
{
return
employeeService
.
findByName
(
name
);
}
/**
* GET method to fetch all records
* @return Flux
*/
@GetMapping
(
value
=
""
,
produces
=
MediaType
.
TEXT_EVENT_STREAM_VALUE
)
public
Flux
<
Employee
>
findAll
()
{
Flux
<
Employee
>
emps
=
employeeService
.
findAll
();
return
emps
;
}
/**
* PUT method to do update of record
* @param e
* @return Mono
*/
@PutMapping
(
"/update"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
Mono
<
Employee
>
update
(
@RequestBody
Employee
e
)
{
return
employeeService
.
update
(
e
);
}
/**
* DELETE method to remove record based on Id
* @param id
* @return Void
*/
@DeleteMapping
(
"/delete/{id}"
)
@ResponseStatus
(
HttpStatus
.
OK
)
public
void
delete
(
@PathVariable
(
"id"
)
Integer
id
)
{
...
...
This diff is collapsed.
Click to expand it.
src/main/java/com/nisum/example/mongodb/model/Employee.java
View file @
9f0fa459
...
...
@@ -9,6 +9,11 @@ import org.springframework.context.annotation.ScopedProxyMode;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
/**
* Model class for Employee
* Fields id, name and salary
*/
@Scope
(
scopeName
=
"request"
,
proxyMode
=
ScopedProxyMode
.
TARGET_CLASS
)
@Document
@Data
...
...
@@ -16,12 +21,12 @@ import org.springframework.data.mongodb.core.mapping.Document;
@RequiredArgsConstructor
public
class
Employee
{
@Id
private
int
id
;
private
Integer
id
;
@NonNull
private
String
name
;
@NonNull
private
l
ong
salary
;
private
L
ong
salary
;
}
This diff is collapsed.
Click to expand it.
src/main/java/com/nisum/example/mongodb/service/EmployeeServiceImpl.java
View file @
9f0fa459
...
...
@@ -7,37 +7,72 @@ import org.springframework.stereotype.Service;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.time.Duration
;
/**
*Service class having methods for CURD operations
* Methods having Mono<T> and Flux<T> as return types
*/
@Service
public
class
EmployeeServiceImpl
implements
IEmployeeService
{
@Autowired
private
EmployeeRepository
employeeRepository
;
/**
* perform create operation
* @param e
* @return Mono
*/
@Override
public
void
create
(
Employee
e
)
{
employeeRepository
.
save
(
e
).
subscribe
(
);
public
Mono
<
Employee
>
create
(
Employee
e
)
{
return
employeeRepository
.
save
(
e
);
}
/**
* perform fetch operation based on ID
* @param id
* @return Mono
*/
@Override
public
Mono
<
Employee
>
findById
(
Integer
id
)
{
return
employeeRepository
.
findById
(
id
);
}
/**
* perform fetch operation based on name
* @param name
* @return Flux
*/
@Override
public
Flux
<
Employee
>
findByName
(
String
name
)
{
return
employeeRepository
.
findByName
(
name
);
}
/**
* perform fetch all records
* @return Flux
*/
@Override
public
Flux
<
Employee
>
findAll
()
{
return
employeeRepository
.
findAll
();
return
employeeRepository
.
findAll
()
.
delayElements
(
Duration
.
ofSeconds
(
1
))
;
}
/**
* perform update of object
* @param e
* @return updated object of type Mono
*/
@Override
public
Mono
<
Employee
>
update
(
Employee
e
)
{
return
employeeRepository
.
save
(
e
);
}
/**
* perform delete based on id
* @param id
* @return Mono
*/
@Override
public
Mono
<
Void
>
delete
(
Integer
id
)
{
return
employeeRepository
.
deleteById
(
id
);
...
...
This diff is collapsed.
Click to expand it.
src/main/java/com/nisum/example/mongodb/service/IEmployeeService.java
View file @
9f0fa459
...
...
@@ -6,7 +6,7 @@ import reactor.core.publisher.Mono;
public
interface
IEmployeeService
{
void
create
(
Employee
e
);
Mono
<
Employee
>
create
(
Employee
e
);
Mono
<
Employee
>
findById
(
Integer
id
);
Flux
<
Employee
>
findByName
(
String
name
);
Flux
<
Employee
>
findAll
();
...
...
This diff is collapsed.
Click to expand it.
src/test/java/com/nisum/example/mongodb/controller/EmployeeControllerTest.java
0 → 100644
View file @
9f0fa459
package
com
.
nisum
.
example
.
mongodb
.
controller
;
import
com.nisum.example.mongodb.model.Employee
;
import
com.nisum.example.mongodb.repository.EmployeeRepository
;
import
com.nisum.example.mongodb.service.EmployeeServiceImpl
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
org.springframework.test.web.reactive.server.WebTestClient
;
import
org.springframework.web.reactive.function.BodyInserters
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
mockito
.
Mockito
.
times
;
@ExtendWith
(
SpringExtension
.
class
)
@WebFluxTest
(
controllers
=
EmployeeController
.
class
)
@Import
(
EmployeeServiceImpl
.
class
)
public
class
EmployeeControllerTest
{
@MockBean
private
EmployeeRepository
repository
;
@Autowired
private
WebTestClient
webTestClient
;
@Test
void
testCreateEmployee
(){
Employee
employee
=
new
Employee
();
employee
.
setId
(
1
);
employee
.
setName
(
"SAM"
);
employee
.
setSalary
(
1500L
);
Mockito
.
when
(
repository
.
save
(
employee
)).
thenReturn
(
Mono
.
just
(
employee
));
webTestClient
.
post
()
.
uri
(
"/create"
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)
.
body
(
BodyInserters
.
fromObject
(
employee
))
.
exchange
()
.
expectStatus
().
isCreated
();
Mockito
.
verify
(
repository
,
times
(
1
)).
save
(
employee
);
}
@Test
void
testGetEmployeeById
(){
Employee
employee
=
new
Employee
();
employee
.
setId
(
1
);
employee
.
setName
(
"SAM"
);
employee
.
setSalary
(
1500L
);
Mockito
.
when
(
repository
.
findById
(
employee
.
getId
()))
.
thenReturn
(
Mono
.
just
(
employee
));
webTestClient
.
get
()
.
uri
(
"/{id}"
,
employee
.
getId
())
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBody
()
.
jsonPath
(
"$.name"
).
isNotEmpty
()
.
jsonPath
(
"$.id"
).
isEqualTo
(
employee
.
getId
())
.
jsonPath
(
"$.name"
).
isEqualTo
(
"SAM"
)
.
jsonPath
(
"$.salary"
).
isEqualTo
(
1500
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findById
(
employee
.
getId
());
}
@Test
void
testGetEmployeeByName
(){
Employee
employee1
=
new
Employee
();
employee1
.
setId
(
1
);
employee1
.
setName
(
"SAM"
);
employee1
.
setSalary
(
1500L
);
Employee
employee2
=
new
Employee
();
employee2
.
setId
(
2
);
employee2
.
setName
(
"SAM"
);
employee2
.
setSalary
(
2000L
);
List
<
Employee
>
employeeList
=
Arrays
.
asList
(
employee1
,
employee2
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
fromIterable
(
employeeList
);
Mockito
.
when
(
repository
.
findByName
(
"SAM"
))
.
thenReturn
(
employeeFlux
);
webTestClient
.
get
()
.
uri
(
"/name/{name}"
,
"SAM"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBodyList
(
Employee
.
class
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findByName
(
"SAM"
);
}
@Test
void
testGetAllEmployees
(){
Employee
employee1
=
new
Employee
();
employee1
.
setId
(
1
);
employee1
.
setName
(
"SAM"
);
employee1
.
setSalary
(
1500L
);
Employee
employee2
=
new
Employee
();
employee2
.
setId
(
2
);
employee2
.
setName
(
"JAMES"
);
employee2
.
setSalary
(
2000L
);
List
<
Employee
>
employeeList
=
Arrays
.
asList
(
employee1
,
employee2
);
Flux
<
Employee
>
employeeFlux
=
Flux
.
fromIterable
(
employeeList
);
Mockito
.
when
(
repository
.
findAll
()).
thenReturn
(
employeeFlux
);
webTestClient
.
get
()
.
uri
(
""
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectBodyList
(
Employee
.
class
)
.
hasSize
(
2
);
Mockito
.
verify
(
repository
,
times
(
1
)).
findAll
();
}
@Test
void
testDeleteEmployee
()
{
Mono
<
Void
>
voidReturn
=
Mono
.
empty
();
Mockito
.
when
(
repository
.
deleteById
(
1
))
.
thenReturn
(
voidReturn
);
webTestClient
.
delete
().
uri
(
"/delete/{id}"
,
1
)
.
exchange
()
.
expectStatus
().
isOk
();
}
}
This diff is collapsed.
Click to expand it.
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