Commit 4e161a31 authored by Bhanuchander Pathuri's avatar Bhanuchander Pathuri

project structured POC with WEbflux and mongoDB with mapper and exception handling

parent 8c5c9d79
# WebfluxMongoDB
WebfluxMongoDB CRUD Operations
\ No newline at end of file
...@@ -15,12 +15,18 @@ ...@@ -15,12 +15,18 @@
<description>Demo project for Spring Boot webFlux</description> <description>Demo project for Spring Boot webFlux</description>
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId> <artifactId>spring-boot-starter-webflux</artifactId>
...@@ -41,6 +47,11 @@ ...@@ -41,6 +47,11 @@
<artifactId>reactor-test</artifactId> <artifactId>reactor-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
...@@ -57,6 +68,34 @@ ...@@ -57,6 +68,34 @@
</excludes> </excludes>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source> <!-- depending on your project -->
<target>17</target> <!-- depending on your project -->
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins> </plugins>
</build> </build>
......
...@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication; ...@@ -4,6 +4,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories; import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
......
...@@ -3,7 +3,7 @@ package com.nisum.poc.Webflux.controller; ...@@ -3,7 +3,7 @@ package com.nisum.poc.Webflux.controller;
import com.nisum.poc.Webflux.entity.Address; import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.model.AddressPayload; import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.service.IAddressService; import com.nisum.poc.Webflux.service.IAddressService;
import lombok.extern.slf4j.Slf4j; import com.nisum.poc.Webflux.utils.ConstantUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -17,16 +17,19 @@ public class AddressController { ...@@ -17,16 +17,19 @@ public class AddressController {
@Autowired @Autowired
IAddressService addressService; IAddressService addressService;
@Autowired
ConstantUtils constantUtils;
@PostMapping("/saveaddress") @PostMapping("/saveaddress")
public Mono<Address> createAddress(@RequestBody Address address){ public Mono<Address> createAddress(@RequestBody AddressPayload addressPayload){
return addressService.createAddress(address); return addressService.createAddress(addressPayload);
} }
@GetMapping("/alladdress") @GetMapping("/alladdress")
public Flux<Address> getAllAddress(){ public Flux<Address> getAllAddress(){
return addressService.getAllAddress(); return addressService.getAllAddress();
} }
} }
...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.controller; ...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.controller;
import com.nisum.poc.Webflux.entity.Student; import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject; import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.model.StudentPayload; import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.StudentResponsePayload;
import com.nisum.poc.Webflux.service.IStudentService; import com.nisum.poc.Webflux.service.IStudentService;
import com.nisum.poc.Webflux.service.ISubjectsService; import com.nisum.poc.Webflux.service.ISubjectsService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -24,13 +25,13 @@ public class StudentController { ...@@ -24,13 +25,13 @@ public class StudentController {
IStudentService studentService; IStudentService studentService;
@PostMapping("/saveStudent") @PostMapping("/saveStudent")
public Mono<Student> creatStudent(@RequestBody Student student){ public Mono<Student> creatStudent(@RequestBody StudentPayload studentPayload){
return studentService.createStudent(student); return studentService.createStudent(studentPayload);
} }
@GetMapping("/allStudents") @GetMapping("/allStudents")
public Flux<StudentPayload> getAllStudents(){ public Flux<StudentResponsePayload> getAllStudents(){
return studentService.getAllStudents(); return studentService.getAllStudents();
} }
......
...@@ -16,10 +16,9 @@ import org.springframework.data.mongodb.core.mapping.Document; ...@@ -16,10 +16,9 @@ import org.springframework.data.mongodb.core.mapping.Document;
//@Scope(scopeName="request",proxyMode = ScopedProxyMode.TARGET_CLASS) //@Scope(scopeName="request",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Address { public class Address {
@Transient
public static final String SEQUENCE_NAME = "users_sequence";
@Id @Id
private Long id; private String id;
private String address; private String address;
private String city; private String city;
private String pinCode; private String pinCode;
......
package com.nisum.poc.Webflux.entity; package com.nisum.poc.Webflux.entity;
import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.model.SubjectPayload;
import lombok.*; import lombok.*;
import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.context.annotation.ScopedProxyMode;
...@@ -26,7 +28,7 @@ public class Student { ...@@ -26,7 +28,7 @@ public class Student {
private String lastName; private String lastName;
private Integer age; private Integer age;
private List<String> phoneNumbers; private List<String> phoneNumbers;
private List<Address> address; private List<AddressPayload> address;
private List<Subject> subjects; private List<SubjectPayload> subjects;
} }
...@@ -18,8 +18,6 @@ import org.springframework.data.mongodb.core.mapping.Document; ...@@ -18,8 +18,6 @@ import org.springframework.data.mongodb.core.mapping.Document;
//@Scope(scopeName="request",proxyMode = ScopedProxyMode.TARGET_CLASS) //@Scope(scopeName="request",proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Subject { public class Subject {
@Transient
public static final String SEQUENCE_NAME = "users_sequence";
@Id @Id
private Long id; private Long id;
private String subjectName; private String subjectName;
......
package com.nisum.poc.Webflux.exceptions;
public class GlobalException extends RuntimeException{
public GlobalException( String message) {
super(message);
}
}
package com.nisum.poc.Webflux.exceptions;
import com.nisum.poc.Webflux.utils.ApiResponse;
import com.nisum.poc.Webflux.utils.IApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
public class MainExceptionHandler{
@ExceptionHandler(GlobalException.class)
public ResponseEntity<String> globalException(GlobalException globalException){
return ResponseEntity.status(HttpStatus.NOT_ACCEPTABLE).body(globalException.getMessage());
}
}
package com.nisum.poc.Webflux.mapper;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.SubjectPayload;
import org.mapstruct.factory.Mappers;
import java.util.List;
@org.mapstruct.Mapper(componentModel = "spring")
public interface AllMapper {
AllMapper Instance = Mappers.getMapper(AllMapper.class);
Address modelToEntity(AddressPayload addressPayload);
AddressPayload entityToModel(Address address);
List<AddressPayload> entitysToModels(List<Address> address);
Student modelToEntity(StudentPayload studentPayload);
StudentPayload entityToModel(Student student);
Subject modelToEntity(SubjectPayload subjectPayload);
SubjectPayload entityToModel(Subject subject);
}
...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.model; ...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.model;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
......
...@@ -19,6 +19,6 @@ public class StudentPayload { ...@@ -19,6 +19,6 @@ public class StudentPayload {
private String lastName; private String lastName;
private Integer age; private Integer age;
private List<String> phoneNumbers; private List<String> phoneNumbers;
private List<Address> addresses; private List<AddressPayload> addresses;
private List<Subject> subjects; private List<SubjectPayload> subjects;
} }
package com.nisum.poc.Webflux.model;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.entity.Subject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentResponsePayload {
private Long id;
private String firstName;
private String lastName;
private Integer age;
private List<String> phoneNumbers;
private List<Address> addresses;
private List<Subject> subjects;
}
...@@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository; ...@@ -6,7 +6,7 @@ import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
@Repository @Repository
public interface AddressRepository extends ReactiveMongoRepository<Address, Long> { public interface AddressRepository extends ReactiveMongoRepository<Address, String> {
Flux<Address> findByStudentId(Long studentId); Flux<Address> findByStudentId(Long studentId);
} }
...@@ -8,7 +8,7 @@ import reactor.core.publisher.Mono; ...@@ -8,7 +8,7 @@ import reactor.core.publisher.Mono;
@Service @Service
public interface IAddressService { public interface IAddressService {
Mono<Address> createAddress(Address address); Mono<Address> createAddress(AddressPayload addressPayload);
Flux<Address> getAllAddress(); Flux<Address> getAllAddress();
} }
...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.service; ...@@ -3,6 +3,7 @@ package com.nisum.poc.Webflux.service;
import com.nisum.poc.Webflux.entity.Student; import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject; import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.model.StudentPayload; import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.StudentResponsePayload;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
...@@ -11,9 +12,9 @@ import reactor.core.publisher.Mono; ...@@ -11,9 +12,9 @@ import reactor.core.publisher.Mono;
@Service @Service
public interface IStudentService { public interface IStudentService {
Mono<Student> createStudent(Student student); Mono<Student> createStudent(StudentPayload studentPayload);
Flux<StudentPayload> getAllStudents(); Flux<StudentResponsePayload> getAllStudents();
Flux<String> concat(); Flux<String> concat();
Flux<String> merge(); Flux<String> merge();
......
package com.nisum.poc.Webflux.service.impl; package com.nisum.poc.Webflux.service.impl;
import com.nisum.poc.Webflux.entity.Address; import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.exceptions.GlobalException;
import com.nisum.poc.Webflux.mapper.AllMapper;
import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.repository.AddressRepository; import com.nisum.poc.Webflux.repository.AddressRepository;
import com.nisum.poc.Webflux.service.IAddressService; import com.nisum.poc.Webflux.service.IAddressService;
import com.nisum.poc.Webflux.utils.ConstantUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -15,9 +19,15 @@ public class AddressServiceImpl implements IAddressService { ...@@ -15,9 +19,15 @@ public class AddressServiceImpl implements IAddressService {
@Autowired @Autowired
AddressRepository addressRepository; AddressRepository addressRepository;
@Autowired
private AllMapper allMapper;
@Override @Override
public Mono<Address> createAddress(Address address) { public Mono<Address> createAddress(AddressPayload addressPayload) {
return addressRepository.save(address); if (addressPayload.getStudentId() <=0) {
throw new GlobalException(ConstantUtils.SAVE_FAILED);
}
return addressRepository.save(allMapper.modelToEntity(addressPayload));
} }
......
...@@ -3,7 +3,10 @@ package com.nisum.poc.Webflux.service.impl; ...@@ -3,7 +3,10 @@ package com.nisum.poc.Webflux.service.impl;
import com.nisum.poc.Webflux.entity.Address; import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.entity.Student; import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject; import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.mapper.AllMapper;
import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.model.StudentPayload; import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.StudentResponsePayload;
import com.nisum.poc.Webflux.repository.AddressRepository; import com.nisum.poc.Webflux.repository.AddressRepository;
import com.nisum.poc.Webflux.repository.StudentRepository; import com.nisum.poc.Webflux.repository.StudentRepository;
import com.nisum.poc.Webflux.repository.SubjectRepository; import com.nisum.poc.Webflux.repository.SubjectRepository;
...@@ -17,6 +20,7 @@ import reactor.util.function.Tuple2; ...@@ -17,6 +20,7 @@ import reactor.util.function.Tuple2;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
...@@ -30,36 +34,28 @@ public class StudentServiceImpl implements IStudentService { ...@@ -30,36 +34,28 @@ public class StudentServiceImpl implements IStudentService {
AddressRepository addressRepository; AddressRepository addressRepository;
@Autowired @Autowired
SubjectRepository subjectRepository; SubjectRepository subjectRepository;
@Autowired
AllMapper allMapper;
@Override @Override
public Mono<Student> createStudent(Student student) { public Mono<Student> createStudent(StudentPayload studentPayload) {
Student student1 = new Student(); Student student1 = new Student();
student1.setId(student.getId()); student1.setId(studentPayload.getId());
student1.setAge(student.getAge()); student1.setAge(studentPayload.getAge());
student1.setFirstName(student.getFirstName()); student1.setFirstName(studentPayload.getFirstName());
student1.setLastName(student.getLastName()); student1.setLastName(studentPayload.getLastName());
student1.setPhoneNumbers(student.getPhoneNumbers()); student1.setPhoneNumbers(studentPayload.getPhoneNumbers());
Mono<Student> studentMono = studentRepository.save(student1); Mono<Student> studentMono = studentRepository.save(allMapper.modelToEntity(studentPayload));
List<Address> addressList = new ArrayList<>(); List<Address> addressList = new ArrayList<>();
List<Subject> subjectList = new ArrayList<>(); List<Subject> subjectList = new ArrayList<>();
studentMono.subscribe(studentmono -> { studentMono.subscribe(studentmono -> {
student.getAddress().forEach(address -> { studentPayload.getAddresses().forEach(address -> {
Address address1 = new Address(); addressList.add(allMapper.modelToEntity(address));
address1.setId(address.getId());
address1.setAddress(address.getAddress());
address1.setStudentId(studentmono.getId());
address1.setCity(address.getCity());
address1.setAddressType(address.getAddressType());
address1.setPinCode(address.getPinCode());
addressList.add(address1);
}); });
student.getSubjects().forEach(sub -> { studentPayload.getSubjects().forEach(sub -> {
Subject subject = new Subject(); subjectList.add(allMapper.modelToEntity(sub));
subject.setId(sub.getId());
subject.setSubjectName(sub.getSubjectName());
subject.setStudentId(studentmono.getId());
subjectList.add(subject);
}); });
Flux<Address> addressFlux = addressRepository.saveAll(addressList); Flux<Address> addressFlux = addressRepository.saveAll(addressList);
Flux<Subject> subjectFlux = subjectRepository.saveAll(subjectList); Flux<Subject> subjectFlux = subjectRepository.saveAll(subjectList);
...@@ -67,11 +63,11 @@ public class StudentServiceImpl implements IStudentService { ...@@ -67,11 +63,11 @@ public class StudentServiceImpl implements IStudentService {
return studentMono; return studentMono;
} }
@Override @Override
public Flux<StudentPayload> getAllStudents() { public Flux<StudentResponsePayload> getAllStudents() {
return studentRepository.findAll().flatMap(student -> { return studentRepository.findAll().flatMap(student -> {
Flux<Address> addressFlux = addressRepository.findByStudentId(student.getId()).switchIfEmpty(Flux.just(new Address())); Flux<Address> addressFlux = addressRepository.findByStudentId(student.getId()).switchIfEmpty(Flux.just(new Address()));
Flux<Subject> subjectFlux = subjectRepository.findByStudentId(student.getId()).switchIfEmpty(Flux.just(new Subject())); Flux<Subject> subjectFlux = subjectRepository.findByStudentId(student.getId()).switchIfEmpty(Flux.just(new Subject()));
StudentPayload studentPayload = new StudentPayload(); StudentResponsePayload studentPayload = new StudentResponsePayload();
studentPayload.setId(student.getId()); studentPayload.setId(student.getId());
studentPayload.setAge(student.getAge()); studentPayload.setAge(student.getAge());
studentPayload.setPhoneNumbers(student.getPhoneNumbers()); studentPayload.setPhoneNumbers(student.getPhoneNumbers());
...@@ -79,7 +75,7 @@ public class StudentServiceImpl implements IStudentService { ...@@ -79,7 +75,7 @@ public class StudentServiceImpl implements IStudentService {
studentPayload.setLastName(student.getLastName()); studentPayload.setLastName(student.getLastName());
studentPayload.setAddresses(addressFlux.collectList().block()); studentPayload.setAddresses(addressFlux.collectList().block());
studentPayload.setSubjects(subjectFlux.collectList().block()); studentPayload.setSubjects(subjectFlux.collectList().block());
Flux<StudentPayload> studentPayloadFlux = Flux.just(studentPayload); Flux<StudentResponsePayload> studentPayloadFlux = Flux.just(studentPayload);
return studentPayloadFlux; return studentPayloadFlux;
}); });
// return studentFlux; // return studentFlux;
...@@ -108,13 +104,11 @@ public class StudentServiceImpl implements IStudentService { ...@@ -108,13 +104,11 @@ public class StudentServiceImpl implements IStudentService {
}); });
return name3; return name3;
} }
public Mono<String> combineWithZipChara(){ public Mono<String> combineWithZipChara() {
Mono<String> name1 = Mono.just("Hi"); Mono<String> name1 = Mono.just("Hi");
Mono<String> name2 = Mono.just("are"); Mono<String> name2 = Mono.just("are");
Flux<String> name3 = Mono.zip(name1,name2).flatMap(a ->{ return Mono.just("sample");
a.getT1().toString();
});
} }
......
package com.nisum.poc.Webflux.utils;
public class ApiResponse implements IApiResponse{
private Object object;
private boolean status;
private String message;
public ApiResponse(Object object, boolean status, String message) {
this.object = object;
this.status = status;
this.message = message;
}
}
package com.nisum.poc.Webflux.utils;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConstantUtils {
public static final String SAVE_SUCCESSFULL = "Saved Successfully";
public static final String SAVE_FAILED = "Save Failed";
}
package com.nisum.poc.Webflux.utils;
public interface IApiResponse {
}
db.name=StudentDatabase
db.host=mongodb://localhost:27017
server.port=6666
info.app.name=Demo
#logging.config=classpath:log/logback.xml
db.name=StudentDatabase
db.host=mongodb://localhost:27017
server.port=5555
info.app.name=Demo
#logging.config=classpath:log/logback.xml
db.name=StudentDatabase db.name=StudentDatabase
db.host=mongodb://localhost:27017 db.host=mongodb://localhost:27017
server.port=4042 server.port=4042
spring.profiles.active=local
\ No newline at end of file
package com.nisum.poc.Webflux.controller;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.service.IAddressService;
import com.nisum.poc.Webflux.utils.IApiResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
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.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import static org.mockito.Mockito.*;
@RunWith(SpringRunner.class)
@WebFluxTest()
class AddressControllerTest {
@Mock
IAddressService addressService;
@Autowired
private WebTestClient webTestClient;
@MockBean
AddressController addressController;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testCreateAddress() {
Address address = new Address("123l","hyd","hyd","12342","home",1232l);
//when(addressService.createAddress(address)).thenReturn(Mono.just(address));
Mono<Address> addressMono = webTestClient.post().uri("/saveaddress")
.body(Mono.just(address),Address.class)
.exchange()
.expectStatus().isOk().returnResult(Address.class).getResponseBody().next();
StepVerifier.create(addressMono)
.expectSubscription()
.expectNext(new Address("123l","hyd","hyd","12342","home",1232l))
.verifyComplete();
/* Mono<Address> result = addressController.createAddress(new Address(Long.valueOf(1), "address", "city", "pinCode", "addressType", Long.valueOf(1)));
Assertions.assertEquals(null, result);*/
}
@Test
void testGetAllAddress() {
when(addressService.getAllAddress()).thenReturn(null);
Flux<Address> result = addressController.getAllAddress();
Assertions.assertEquals(null, result);
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file
package com.nisum.poc.Webflux.controller;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.model.AddressPayload;
import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.StudentResponsePayload;
import com.nisum.poc.Webflux.model.SubjectPayload;
import com.nisum.poc.Webflux.service.IStudentService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
import static org.mockito.Mockito.*;
class StudentControllerTest {
@Mock
IStudentService studentService;
@InjectMocks
StudentController studentController;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
/*@Test
void testCreatStudent() {
when(studentService.createStudent(any())).thenReturn(null);
Mono<Student> result = studentController.creatStudent(new Student(Long.valueOf(1), "firstName", "lastName", Integer.valueOf(0), List.of("String"), List.of(new AddressPayload(1, "address", "city", "pinCode", "addressType", Long.valueOf(1))), List.of(new SubjectPayload(1, "subjectName", Long.valueOf(1)))));
Assertions.assertEquals(null, result);
}*/
@Test
void testGetAllStudents() {
when(studentService.getAllStudents()).thenReturn(null);
Flux<StudentResponsePayload> result = studentController.getAllStudents();
Assertions.assertEquals(null, result);
}
@Test
void testConcatFlux() {
when(studentService.concat()).thenReturn(null);
Flux<String> result = studentController.concatFlux();
Assertions.assertEquals(null, result);
}
@Test
void testMergeFlux() {
when(studentService.merge()).thenReturn(null);
Flux<String> result = studentController.mergeFlux();
Assertions.assertEquals(null, result);
}
@Test
void testZip() {
when(studentService.combineWithZip()).thenReturn(null);
Flux<Integer> result = studentController.zip();
Assertions.assertEquals(null, result);
}
@Test
void testZipWith() {
Flux<Integer> result = studentController.zipWith();
Assertions.assertEquals(null, result);
}
@Test
void testTransformUsingFlatMap() {
Flux<String> result = studentController.transformUsingFlatMap();
Assertions.assertEquals(null, result);
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file
package com.nisum.poc.Webflux.service.impl;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.repository.AddressRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static org.mockito.Mockito.*;
class AddressServiceImplTest {
@Mock
AddressRepository addressRepository;
@Mock
Logger log;
@InjectMocks
AddressServiceImpl addressServiceImpl;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
/* @Test
void testCreateAddress() {
Mono<Address> result = addressServiceImpl.createAddress(new Address(Long.valueOf(1), "address", "city", "pinCode", "addressType", Long.valueOf(1)));
Assertions.assertEquals(null, result);
}*/
@Test
void testGetAllAddress() {
Flux<Address> result = addressServiceImpl.getAllAddress();
Assertions.assertEquals(null, result);
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file
package com.nisum.poc.Webflux.service.impl;
import com.nisum.poc.Webflux.entity.Address;
import com.nisum.poc.Webflux.entity.Student;
import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.model.StudentPayload;
import com.nisum.poc.Webflux.model.StudentResponsePayload;
import com.nisum.poc.Webflux.repository.AddressRepository;
import com.nisum.poc.Webflux.repository.StudentRepository;
import com.nisum.poc.Webflux.repository.SubjectRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.List;
import static org.mockito.Mockito.*;
class StudentServiceImplTest {
@Mock
StudentRepository studentRepository;
@Mock
AddressRepository addressRepository;
@Mock
SubjectRepository subjectRepository;
@Mock
Logger log;
@InjectMocks
StudentServiceImpl studentServiceImpl;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
/*
@Test
void testCreateStudent() {
Mono<Student> result = studentServiceImpl.createStudent(new Student(Long.valueOf(1), "firstName", "lastName", Integer.valueOf(0), List.of("String"), List.of(new Address(Long.valueOf(1), "address", "city", "pinCode", "addressType", Long.valueOf(1))), List.of(new Subject(Long.valueOf(1), "subjectName", Long.valueOf(1)))));
Assertions.assertEquals(null, result);
}
*/
@Test
void testGetAllStudents() {
when(addressRepository.findByStudentId(anyLong())).thenReturn(null);
when(subjectRepository.findByStudentId(anyLong())).thenReturn(null);
Flux<StudentResponsePayload> result = studentServiceImpl.getAllStudents();
Assertions.assertEquals(null, result);
}
@Test
void testConcat() {
Flux<String> result = studentServiceImpl.concat();
Assertions.assertEquals(null, result);
}
@Test
void testMerge() {
Flux<String> result = studentServiceImpl.merge();
Assertions.assertEquals(null, result);
}
@Test
void testCombineWithZip() {
Flux<Integer> result = studentServiceImpl.combineWithZip();
Assertions.assertEquals(null, result);
}
@Test
void testCombineWithZipChara() {
Mono<String> result = studentServiceImpl.combineWithZipChara();
Assertions.assertEquals(null, result);
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ No newline at end of file
package com.nisum.poc.Webflux.service.impl;
import com.nisum.poc.Webflux.entity.Subject;
import com.nisum.poc.Webflux.repository.SubjectRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import static org.mockito.Mockito.*;
class SubjectsServiceImplTest {
@Mock
SubjectRepository subjectRepository;
@Mock
Logger log;
@InjectMocks
SubjectsServiceImpl subjectsServiceImpl;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void testCreateSuject() {
Mono<Subject> result = subjectsServiceImpl.createSuject(new Subject(Long.valueOf(1), "subjectName", Long.valueOf(1)));
Assertions.assertEquals(null, result);
}
@Test
void testGetAllSubjects() {
Flux<Subject> result = subjectsServiceImpl.getAllSubjects();
Assertions.assertEquals(null, result);
}
}
//Generated with love by TestMe :) Please report issues and submit feature requests at: http://weirddev.com/forum#!/testme
\ 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