Commit 058f8d81 authored by Syed Javed Ali's avatar Syed Javed Ali

Added end points for Getting all records, batch insertion, unit testing for get and post end points

parent e54666af
......@@ -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);
}
......
......@@ -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 roomId,
public Mono<Reservation> updatePrice(@PathVariable String id,
@RequestBody Mono<Reservation> reservation) {
return Mono.just("{}");
return reservationService.updateReservation(id,reservation);
}
@DeleteMapping(path = "{roomId}")
public Mono<Boolean> deleteReservation(@PathVariable String roomId){
return Mono.just(true);
@DeleteMapping(path = "{id}")
public Mono<Boolean> deleteReservation(@PathVariable String id){
return reservationService.deleteReservation(id);
}
}
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
@AllArgsConstructor
@RequiredArgsConstructor
@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;
}
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);
}
......@@ -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()));
}
}
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment