Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
webflux-reactive-mongo
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
Sarika Sama
webflux-reactive-mongo
Commits
c8eed474
Commit
c8eed474
authored
May 24, 2023
by
Sarika Sama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added junit and mockito to controller and service
parent
c367a550
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
159 additions
and
9 deletions
+159
-9
pom.xml
pom.xml
+5
-0
ProductController.java
.../webfluxreactivemongodb/controller/ProductController.java
+2
-2
ProductService.java
.../nisum/webfluxreactivemongodb/service/ProductService.java
+2
-2
ProductServiceImpl.java
...um/webfluxreactivemongodb/service/ProductServiceImpl.java
+3
-3
WebfluxReactiveMongodbApplicationTests.java
...activemongodb/WebfluxReactiveMongodbApplicationTests.java
+106
-2
ProductServiceTest.java
...um/webfluxreactivemongodb/service/ProductServiceTest.java
+41
-0
No files found.
pom.xml
View file @
c8eed474
...
...
@@ -51,6 +51,11 @@
<artifactId>
reactor-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<scope>
test
</scope>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/nisum/webfluxreactivemongodb/controller/ProductController.java
View file @
c8eed474
...
...
@@ -17,7 +17,7 @@ public class ProductController {
private
ProductService
productService
;
@PostMapping
public
Mono
<
ProductDto
>
saveproduct
(
@RequestBody
ProductDto
productDto
){
public
Mono
<
ProductDto
>
saveproduct
(
@RequestBody
Mono
<
ProductDto
>
productDto
){
return
productService
.
saveProduct
(
productDto
);
}
@GetMapping
(
"{id}"
)
...
...
@@ -31,7 +31,7 @@ public class ProductController {
}
@PutMapping
(
"{id}"
)
public
Mono
<
ProductDto
>
updateProduct
(
@RequestBody
ProductDto
productDto
,
@PathVariable
(
"id"
)
int
id
){
public
Mono
<
ProductDto
>
updateProduct
(
@RequestBody
Mono
<
ProductDto
>
productDto
,
@PathVariable
(
"id"
)
int
id
){
return
productService
.
updateProduct
(
productDto
,
id
);
}
...
...
src/main/java/com/nisum/webfluxreactivemongodb/service/ProductService.java
View file @
c8eed474
...
...
@@ -6,13 +6,13 @@ import reactor.core.publisher.Mono;
public
interface
ProductService
{
Mono
<
ProductDto
>
saveProduct
(
ProductDto
productDto
);
Mono
<
ProductDto
>
saveProduct
(
Mono
<
ProductDto
>
productDto
);
Mono
<
ProductDto
>
getProduct
(
int
id
);
Flux
<
ProductDto
>
getAllProducts
();
Mono
<
ProductDto
>
updateProduct
(
ProductDto
productDto
,
int
id
);
Mono
<
ProductDto
>
updateProduct
(
Mono
<
ProductDto
>
productDto
,
int
id
);
Mono
<
Void
>
deleteProduct
(
int
id
);
...
...
src/main/java/com/nisum/webfluxreactivemongodb/service/ProductServiceImpl.java
View file @
c8eed474
...
...
@@ -12,10 +12,10 @@ import reactor.core.publisher.Flux;
import
reactor.core.publisher.Mono
;
@Service
@AllArgsConstructor
public
class
ProductServiceImpl
implements
ProductService
{
public
abstract
class
ProductServiceImpl
implements
ProductService
{
@Autowired
private
ProductRepository
productRepository
;
@Override
//
@Override
public
Mono
<
ProductDto
>
saveProduct
(
ProductDto
productDto
)
{
Product
product
=
ProductMapper
.
mapToProduct
(
productDto
);
Mono
<
Product
>
savedProduct
=
productRepository
.
save
(
product
);
...
...
@@ -38,7 +38,7 @@ private ProductRepository productRepository;
.
switchIfEmpty
(
Flux
.
empty
());
}
@Override
//
@Override
public
Mono
<
ProductDto
>
updateProduct
(
ProductDto
productDto
,
int
id
)
{
Mono
<
Product
>
productMono
=
productRepository
.
findById
(
id
);
return
productMono
.
flatMap
((
existingProduct
)->{
...
...
src/test/java/com/nisum/webfluxreactivemongodb/WebfluxReactiveMongodbApplicationTests.java
View file @
c8eed474
package
com
.
nisum
.
webfluxreactivemongodb
;
import
com.nisum.webfluxreactivemongodb.controller.ProductController
;
import
com.nisum.webfluxreactivemongodb.dto.ProductDto
;
import
com.nisum.webfluxreactivemongodb.service.ProductService
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.web.reactive.server.WebTestClient
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
reactor.test.StepVerifier
;
@SpringBootTest
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@WebFluxTest
({
ProductService
.
class
,
ProductController
.
class
})
class
WebfluxReactiveMongodbApplicationTests
{
@Autowired
WebTestClient
webTestClient
;
@MockBean
ProductService
productService
;
@Test
void
addProductTest
()
{
Mono
<
ProductDto
>
productDtoMono
=
Mono
.
just
(
new
ProductDto
(
1
,
"tv"
,
4
,
15000.00
));
when
(
productService
.
saveProduct
(
productDtoMono
)).
thenReturn
(
productDtoMono
);
webTestClient
.
post
().
uri
(
"/api/products"
)
.
body
(
Mono
.
just
(
productDtoMono
),
ProductDto
.
class
)
.
exchange
()
.
expectStatus
().
isOk
();
//200
}
@Test
void
getProductsTest
(){
Flux
<
ProductDto
>
productDtoFlux
=
Flux
.
just
(
new
ProductDto
(
1
,
"tv"
,
4
,
15000.00
),
new
ProductDto
(
2
,
"mobile"
,
5
,
20000.00
));
when
(
productService
.
getAllProducts
()).
thenReturn
(
productDtoFlux
);
Flux
<
ProductDto
>
responseBody
=
webTestClient
.
get
().
uri
(
"/api/products"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
returnResult
(
ProductDto
.
class
)
.
getResponseBody
();
StepVerifier
.
create
(
responseBody
)
.
expectSubscription
()
.
expectNext
(
new
ProductDto
(
1
,
"tv"
,
4
,
15000.00
))
.
expectNext
(
new
ProductDto
(
2
,
"mobile"
,
5
,
20000.00
))
.
verifyComplete
();
}
@Test
void
contextLoads
()
{
void
getProductByIdTest
(){
Mono
<
ProductDto
>
productDtoMono
=
Mono
.
just
(
new
ProductDto
(
2
,
"mobile"
,
5
,
20000.00
));
when
(
productService
.
getProduct
(
2
)).
thenReturn
(
productDtoMono
);
Flux
<
ProductDto
>
responseBody
=
webTestClient
.
get
().
uri
(
"/api/products/2"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
returnResult
(
ProductDto
.
class
)
.
getResponseBody
();
StepVerifier
.
create
(
responseBody
)
.
expectSubscription
()
.
expectNextMatches
(
productDto
->
productDto
.
getName
().
equals
(
"mobile"
))
.
verifyComplete
();
}
@Test
void
updateProductTest
(){
Mono
<
ProductDto
>
productDtoMono
=
Mono
.
just
(
new
ProductDto
(
3
,
"laptop"
,
2
,
50000.00
));
when
(
productService
.
updateProduct
(
productDtoMono
,
3
)).
thenReturn
(
productDtoMono
);
webTestClient
.
put
().
uri
(
"/api/products/3"
)
.
body
(
Mono
.
just
(
productDtoMono
),
ProductDto
.
class
)
.
exchange
()
.
expectStatus
().
isOk
();
}
@Test
void
deleteProductTest
(){
given
(
productService
.
deleteProduct
(
3
)).
willReturn
(
Mono
.
empty
());
webTestClient
.
delete
().
uri
(
"/api/products/3"
)
.
exchange
()
.
expectStatus
().
isOk
();
}
// @Test
// void productBetweenRangeTest(){
// Flux<ProductDto> productDtoMono = Flux.just(new ProductDto(2,"mobile",5,20000.00));
// when(productService.getProductInRange(0,1000000)).thenReturn(productDtoMono);
//
//
// Flux<ProductDto> responseBody = webTestClient.get().uri("/api/products/product-range")
// .exchange()
// .expectStatus().isOk()
// .returnResult(ProductDto.class)
// .getResponseBody();
//
// StepVerifier.create(responseBody)
// .expectSubscription()
// .verifyComplete();
//
//
// }
}
src/test/java/com/nisum/webfluxreactivemongodb/service/ProductServiceTest.java
0 → 100644
View file @
c8eed474
package
com
.
nisum
.
webfluxreactivemongodb
.
service
;
import
com.nisum.webfluxreactivemongodb.controller.ProductController
;
import
com.nisum.webfluxreactivemongodb.dto.ProductDto
;
import
com.nisum.webfluxreactivemongodb.repository.ProductRepository
;
import
org.junit.jupiter.api.Test
;
import
org.junit.runner.RunWith
;
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.test.context.junit4.SpringRunner
;
import
reactor.core.publisher.Flux
;
import
reactor.test.StepVerifier
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.
when
;
@RunWith
(
SpringRunner
.
class
)
@WebFluxTest
({
ProductService
.
class
,
ProductRepository
.
class
})
public
class
ProductServiceTest
{
@MockBean
ProductRepository
productRepository
;
@Autowired
ProductService
productService
;
@Test
void
getProductsTest
(){
//
StepVerifier
.
create
(
productService
.
findByAll
)
.
expectSubscription
()
.
expectNext
()
.
verifyComplete
();
}
}
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