Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
WebFlux-Spring-Reactive
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
WebFlux-Spring-Reactive
Commits
058f8d81
Commit
058f8d81
authored
Feb 15, 2021
by
Syed Javed Ali
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added end points for Getting all records, batch insertion, unit testing for get and post end points
parent
e54666af
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
171 additions
and
24 deletions
+171
-24
WebfluxSpringReactiveApplication.java
...a/com/nisum/example/WebfluxSpringReactiveApplication.java
+2
-1
ReservationController.java
...a/com/nisum/example/controller/ReservationController.java
+21
-11
Reservation.java
src/main/java/com/nisum/example/model/Reservation.java
+16
-6
ReservationService.java
...in/java/com/nisum/example/service/ReservationService.java
+5
-0
ReservationServiceImpl.java
...ava/com/nisum/example/service/ReservationServiceImpl.java
+51
-6
ReservationControllerTest.java
...m/nisum/example/controller/ReservationControllerTest.java
+76
-0
No files found.
src/main/java/com/nisum/example/WebfluxSpringReactiveApplication.java
View file @
058f8d81
...
...
@@ -6,7 +6,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public
class
WebfluxSpringReactiveApplication
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
SpringApplication
.
run
(
WebfluxSpringReactiveApplication
.
class
,
args
);
}
...
...
src/main/java/com/nisum/example/controller/ReservationController.java
View file @
058f8d81
...
...
@@ -5,8 +5,11 @@ import com.nisum.example.service.ReservationService;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.MediaType
;
import
org.springframework.web.bind.annotation.*
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.util.List
;
@RestController
@RequestMapping
(
ReservationController
.
ROOM_V_1_RESERVATION
)
//base string of url
@CrossOrigin
//to run angular and spring app locally
...
...
@@ -21,32 +24,39 @@ public class ReservationController {
this
.
reservationService
=
reservationService
;
}
@GetMapping
(
path
=
"{id}"
,
produces
=
MediaType
.
APPLICATION_
PROBLEM_
JSON_UTF8_VALUE
)
@GetMapping
(
path
=
"{id}"
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
public
Mono
<
Reservation
>
getReservationById
(
@PathVariable
String
id
){
return
reservationService
.
getReservation
(
id
);
}
//reservationService.getReservation(roomId)
// return Mono.just("{}");
return
reservationService
.
getReservation
(
id
);
@GetMapping
(
path
=
"all"
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
public
Flux
<
Reservation
>
getAllReservations
(){
return
reservationService
.
getAllReservations
();
}
@PostMapping
(
path
=
""
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
,
consumes
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
public
Mono
<
Reservation
>
createReservation
(
@RequestBody
Mono
<
Reservation
>
reservation
){
//return Mono.just("{}")
return
reservationService
.
createReservation
(
reservation
);
}
@PutMapping
(
path
=
"{roomId}"
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
,
@PostMapping
(
path
=
"reservations"
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
,
consumes
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
public
Flux
<
Reservation
>
createReservations
(
@RequestBody
List
<
Reservation
>
reservationList
){
return
reservationService
.
createReservations
(
reservationList
);
}
@PutMapping
(
path
=
"{id}"
,
produces
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
,
consumes
=
MediaType
.
APPLICATION_JSON_UTF8_VALUE
)
public
Mono
<
String
>
updatePrice
(
@PathVariable
String
roomI
d
,
public
Mono
<
Reservation
>
updatePrice
(
@PathVariable
String
i
d
,
@RequestBody
Mono
<
Reservation
>
reservation
)
{
return
Mono
.
just
(
"{}"
);
return
reservationService
.
updateReservation
(
id
,
reservation
);
}
@DeleteMapping
(
path
=
"{
roomI
d}"
)
public
Mono
<
Boolean
>
deleteReservation
(
@PathVariable
String
roomI
d
){
return
Mono
.
just
(
true
);
@DeleteMapping
(
path
=
"{
i
d}"
)
public
Mono
<
Boolean
>
deleteReservation
(
@PathVariable
String
i
d
){
return
reservationService
.
deleteReservation
(
id
);
}
}
src/main/java/com/nisum/example/model/Reservation.java
View file @
058f8d81
package
com
.
nisum
.
example
.
model
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.NonNull
;
import
lombok.RequiredArgsConstructor
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
import
java.time.LocalDate
;
/**
* Model class for Reservation
* having required fields
*/
@Data
@NoArgsConstructor
@
All
ArgsConstructor
@
Required
ArgsConstructor
@Document
public
class
Reservation
{
@Id
private
String
id
;
@NonNull
private
Long
roomNumber
;
private
LocalDate
checkInDate
;
private
LocalDate
checkOutDate
;
@NonNull
private
LocalDate
checkIn
;
@NonNull
private
LocalDate
checkOut
;
@NonNull
private
Integer
price
;
@Id
private
String
id
;
}
src/main/java/com/nisum/example/service/ReservationService.java
View file @
058f8d81
package
com
.
nisum
.
example
.
service
;
import
com.nisum.example.model.Reservation
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.util.List
;
public
interface
ReservationService
{
public
Mono
<
Reservation
>
getReservation
(
String
Id
);
public
Flux
<
Reservation
>
getAllReservations
();
public
Mono
<
Reservation
>
createReservation
(
Mono
<
Reservation
>
reservationMono
);
public
Flux
<
Reservation
>
createReservations
(
List
<
Reservation
>
reservationList
);
public
Mono
<
Reservation
>
updateReservation
(
String
id
,
Mono
<
Reservation
>
reservationMono
);
public
Mono
<
Boolean
>
deleteReservation
(
String
id
);
}
src/main/java/com/nisum/example/service/ReservationServiceImpl.java
View file @
058f8d81
...
...
@@ -5,9 +5,17 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.data.mongodb.core.ReactiveMongoOperations
;
import
org.springframework.data.mongodb.core.query.Criteria
;
import
org.springframework.data.mongodb.core.query.Query
;
import
org.springframework.data.mongodb.core.query.Update
;
import
org.springframework.stereotype.Service
;
import
reactor.core.publisher.Flux
;
import
reactor.core.publisher.Mono
;
import
java.util.List
;
/**
* Responsible to perform get, post, delete and update operations
*/
@Service
public
class
ReservationServiceImpl
implements
ReservationService
{
...
...
@@ -18,35 +26,72 @@ public class ReservationServiceImpl implements ReservationService {
this
.
reactiveMongoOperations
=
reactiveMongoOperations
;
}
/**
* perform fetch operation based on id
* @param Id
* @return One entity -Mono<T>
*/
@Override
public
Mono
<
Reservation
>
getReservation
(
String
Id
)
{
return
reactiveMongoOperations
.
findById
(
Id
,
Reservation
.
class
);
}
@Override
public
Flux
<
Reservation
>
getAllReservations
()
{
return
reactiveMongoOperations
.
findAll
(
Reservation
.
class
);
}
/**
* perform save operation of one entity
* @param reservationMono
* @return same entity as type Mon<T>
*/
@Override
public
Mono
<
Reservation
>
createReservation
(
Mono
<
Reservation
>
reservationMono
)
{
return
reactiveMongoOperations
.
save
(
reservationMono
);
}
/**
* perform save operation of one entities as List collection
* @param reservationList
* @return same entities as type Flux<T>
*/
@Override
public
Flux
<
Reservation
>
createReservations
(
List
<
Reservation
>
reservationList
)
{
return
reactiveMongoOperations
.
insert
(
reservationList
,
List
.
class
);
}
/**
* perform update operation by taking id and entity as an input
* @param id
* @param reservationMono
* @return same entity of type Mono<T>
*/
@Override
public
Mono
<
Reservation
>
updateReservation
(
String
id
,
Mono
<
Reservation
>
reservationMono
)
{
//if data already present do update
return
reactiveMongoOperations
.
save
(
reservationMono
);
//
return reactiveMongoOperations.save(reservationMono);
//update just price
/*return reservationMono.flatMap(reservation -> {
return
reactiveMongoOperations.findAndModify(Query.query(Criteria.where("id").is(id)),
return
reservationMono
.
flatMap
(
reservation
->
reactiveMongoOperations
.
findAndModify
(
Query
.
query
(
Criteria
.
where
(
"id"
).
is
(
id
)),
Update
.
update
(
"price"
,
reservation
.
getPrice
()),
Reservation
.
class
)
.
flatMap
(
result
->
{
result
.
setPrice
(
reservation
.
getPrice
());
return
Mono
.
just
(
result
);
})
;
});*/
})
);
}
/**
* perform delete operation by taking id as input
* @param id
* @return boolean only if record is deleted successfully
*/
@Override
public
Mono
<
Boolean
>
deleteReservation
(
String
id
)
{
return
reactiveMongoOperations
.
remove
(
Query
.
query
(
Criteria
.
where
(
"id"
)),
Reservation
.
class
)
return
reactiveMongoOperations
.
remove
(
Query
.
query
(
Criteria
.
where
(
"id"
)
.
is
(
id
)
),
Reservation
.
class
)
.
flatMap
(
deleteResult
->
Mono
.
just
(
deleteResult
.
wasAcknowledged
()));
}
}
src/test/java/com/nisum/example/controller/ReservationControllerTest.java
0 → 100644
View file @
058f8d81
package
com
.
nisum
.
example
.
controller
;
import
com.nisum.example.model.Reservation
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.context.junit.jupiter.SpringExtension
;
import
org.springframework.test.web.reactive.server.WebTestClient
;
import
reactor.core.publisher.Mono
;
import
java.time.LocalDate
;
import
java.time.temporal.ChronoUnit
;
@ExtendWith
(
SpringExtension
.
class
)
@SpringBootTest
(
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
)
class
ReservationControllerTest
{
@Autowired
private
ApplicationContext
context
;
private
WebTestClient
webTestClient
;
private
Reservation
reservation
;
@BeforeEach
void
setUp
()
{
webTestClient
=
WebTestClient
.
bindToApplicationContext
(
this
.
context
)
.
build
();
reservation
=
new
Reservation
(
123L
,
LocalDate
.
now
(),
LocalDate
.
now
().
plus
(
10
,
ChronoUnit
.
DAYS
),
150
);
}
@Test
void
getReservation
()
{
webTestClient
.
get
().
uri
(
"/room/v1/reservation/123"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectHeader
().
contentType
(
MediaType
.
APPLICATION_JSON_UTF8
)
.
expectBodyList
(
Reservation
.
class
);
}
@Test
void
getAllReservations
()
{
webTestClient
.
get
().
uri
(
"/room/v1/reservation/all"
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectHeader
().
contentType
(
MediaType
.
APPLICATION_JSON_UTF8
)
.
expectBodyList
(
Reservation
.
class
);
}
@Test
void
createReservation
()
{
webTestClient
.
post
()
.
uri
(
"/room/v1/reservation/"
)
.
body
(
Mono
.
just
(
reservation
),
Reservation
.
class
)
.
exchange
()
.
expectStatus
().
isOk
()
.
expectHeader
().
contentType
(
MediaType
.
APPLICATION_JSON_UTF8
)
.
expectBody
(
Reservation
.
class
);
}
/*@Test
void createReservations() {
webTestClient.post()
.uri("/room/v1/reservation/reservations")
.body(Flux.just(reservation),Reservation.class)
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(Reservation.class);
}*/
}
\ No newline at end of file
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