Skip to content

Commit 4e5720a

Browse files
committed
Unit test, Integration test, Cucumber test added
1 parent 3d31053 commit 4e5720a

File tree

15 files changed

+443
-36
lines changed

15 files changed

+443
-36
lines changed

reactive-micro-service/student-service/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
<attribute name="m2e-apt" value="true"/>
3737
</attributes>
3838
</classpathentry>
39+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
40+
<attributes>
41+
<attribute name="maven.pomderived" value="true"/>
42+
<attribute name="test" value="true"/>
43+
</attributes>
44+
</classpathentry>
3945
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
4046
<attributes>
4147
<attribute name="optional" value="true"/>

reactive-micro-service/student-service/pom.xml

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<version>1</version>
1414
<name>student-service</name>
1515
<description>Student service</description>
16-
16+
<properties>
17+
<cucumber.version>7.2.3</cucumber.version>
18+
</properties>
1719
<dependencyManagement>
1820
<dependencies>
1921
<dependency>
@@ -103,6 +105,31 @@
103105
</dependency>
104106

105107
<!-- testing dependencies -->
108+
<dependency>
109+
<groupId>io.cucumber</groupId>
110+
<artifactId>cucumber-java</artifactId>
111+
<version>${cucumber.version}</version>
112+
<scope>test</scope>
113+
</dependency>
114+
<dependency>
115+
<groupId>io.cucumber</groupId>
116+
<artifactId>cucumber-junit</artifactId>
117+
<version>${cucumber.version}</version>
118+
<scope>test</scope>
119+
</dependency>
120+
<dependency>
121+
<groupId>io.cucumber</groupId>
122+
<artifactId>cucumber-junit-platform-engine</artifactId>
123+
<version>${cucumber.version}</version>
124+
<scope>test</scope>
125+
</dependency>
126+
<dependency>
127+
<groupId>io.cucumber</groupId>
128+
<artifactId>cucumber-spring</artifactId>
129+
<version>${cucumber.version}</version>
130+
<scope>test</scope>
131+
</dependency>
132+
106133
<dependency>
107134
<groupId>org.springframework.boot</groupId>
108135
<artifactId>spring-boot-starter-test</artifactId>

reactive-micro-service/student-service/src/main/java/com/prs/services/student/controller/StudentController.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.http.HttpStatus;
1010
import org.springframework.http.MediaType;
11+
import org.springframework.http.ResponseEntity;
1112
import org.springframework.web.bind.annotation.DeleteMapping;
1213
import org.springframework.web.bind.annotation.GetMapping;
1314
import org.springframework.web.bind.annotation.PathVariable;
@@ -37,6 +38,7 @@ public class StudentController {
3738
IStudentService service;
3839

3940
@PostMapping("/add")
41+
@ResponseStatus(HttpStatus.CREATED)
4042
public Mono<Student> add(@RequestBody StudentEntity employee) {
4143
LOGGER.info("Student add: {}", employee);
4244
return service.save(employee);
@@ -55,8 +57,11 @@ public Flux<Student> findAll() {
5557
}
5658

5759
@PutMapping("/update/{id}")
58-
public Mono<Student> update(@PathVariable("id") Long id,@RequestBody StudentEntity employee){
59-
return service.update(id, employee);
60+
@ResponseStatus(HttpStatus.OK)
61+
public Mono<ResponseEntity<Student>> update(@PathVariable("id") Long id,@RequestBody StudentEntity student){
62+
return service.update(id, student).map(s -> ResponseEntity.ok()
63+
.body(s))
64+
.switchIfEmpty(Mono.just(ResponseEntity.notFound().build()));
6065

6166
}
6267

reactive-micro-service/student-service/src/main/java/com/prs/services/student/entity/StudentEntity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import org.springframework.data.relational.core.mapping.Column;
55
import org.springframework.data.relational.core.mapping.Table;
66

7+
import lombok.AllArgsConstructor;
78
import lombok.Data;
9+
import lombok.NoArgsConstructor;
810
import lombok.ToString;
911

1012
@Data
1113
@Table("Student")
1214
@ToString
15+
@AllArgsConstructor
16+
@NoArgsConstructor
1317
public class StudentEntity {
1418

1519
@Id

reactive-micro-service/student-service/src/main/java/com/prs/services/student/model/Student.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import com.fasterxml.jackson.annotation.JsonInclude;
55
import com.fasterxml.jackson.annotation.JsonInclude.Include;
66

7+
import lombok.AllArgsConstructor;
78
import lombok.Data;
9+
import lombok.NoArgsConstructor;
810
import lombok.ToString;
911

1012
@Data
1113
@JsonInclude(Include.NON_NULL)
1214
@JsonIgnoreProperties(ignoreUnknown = true)
1315
@ToString
16+
@AllArgsConstructor
17+
@NoArgsConstructor
1418
public class Student {
1519

1620
private Long id;

reactive-micro-service/student-service/src/main/java/com/prs/services/student/service/StudentServiceImpl.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public class StudentServiceImpl implements IStudentService {
3333

3434
@Override
3535
public Mono<Student> save(StudentEntity entity) {
36-
// return repository.save(entity).map(mapper);Issue in SimpleR2dbcRepository save(e); So used saveAll for time being
37-
return repository.saveAll(Arrays.asList(entity)).last().flatMap(mapper);
36+
// return repository.save(entity).map(mapper); Issue in SimpleR2dbcRepository save(e); So used saveAll for time being
37+
return repository.saveAll(Arrays.asList(entity)).last().map(simpleMapper);
3838
}
3939

4040
@Override
@@ -52,7 +52,7 @@ public Flux<Student> findAll() {
5252

5353
@Override
5454
public Mono<Student> findById(Long id) {
55-
return repository.findById(id).flatMap(mapper);
55+
return repository.findById(id).flatMap(deepMapper);
5656
}
5757

5858
@Override
@@ -63,37 +63,31 @@ public Mono<Void> deleteById(Long id) {
6363
@Override
6464
public Flux<Student> findByCollege(Long collegeId) {
6565
return findAll().filter(a -> a.getCollege().getId().equals(collegeId));
66-
// return Flux.fromIterable(repository.findByCollegeId(collegeId)).map(mapper);
6766
}
6867

6968
@Override
7069
public Flux<Student> findByDepartment(Long departmentId) {
7170
return findAll().filter(a -> a.getDepartment().getId().equals(departmentId));
72-
// return Flux.fromIterable(repository.findByDepartmentId(departmentId)).map(mapper);
7371
}
7472

75-
private Function<StudentEntity, Mono<Student>> mapper = c -> {
73+
private Function<StudentEntity, Student> simpleMapper = c -> {
7674
var student = new Student();
7775
BeanUtils.copyProperties(c, student);
78-
var department = departmentClient.findByDepartment(c.getDepartmentId());
76+
return student;
77+
};
78+
79+
private Function<StudentEntity, Mono<Student>> deepMapper = c -> {
80+
var student = simpleMapper.apply(c);
7981
var college = collegeClient.findByCollege(c.getCollegeId());
80-
var tuple = Mono.zip(department, college);
82+
var department = departmentClient.findByDepartment(c.getDepartmentId());
83+
var tuple = Mono.zip(college,department);
8184
return tuple.map(t-> {
82-
var dept = new Department();
83-
var coll = new College();
84-
BeanUtils.copyProperties(t.getT1(), dept);
85-
BeanUtils.copyProperties(t.getT2(), coll);
86-
student.setCollege(coll);
87-
student.setDepartment(dept);
85+
student.setCollege(new College());
86+
student.setDepartment(new Department());
87+
BeanUtils.copyProperties(t.getT1(), student.getCollege());
88+
BeanUtils.copyProperties(t.getT2(), student.getDepartment());
8889
return student;
8990
});
9091
};
9192

92-
private Function<StudentEntity, Student> simpleMapper = c -> {
93-
var student = new Student();
94-
BeanUtils.copyProperties(c, student);
95-
return student;
96-
};
97-
98-
9993
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.prs.services.student.controller;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.List;
6+
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.ActiveProfiles;
12+
import org.springframework.test.web.reactive.server.WebTestClient;
13+
14+
import com.prs.services.student.entity.StudentEntity;
15+
import com.prs.services.student.model.Student;
16+
import com.prs.services.student.repository.StudentRepository;
17+
18+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
19+
@ActiveProfiles("test")
20+
@AutoConfigureWebTestClient
21+
public class StudentControllerIntegrationTest {
22+
23+
@Autowired
24+
private WebTestClient webTestClient;
25+
26+
@Autowired
27+
private StudentRepository studentRepository;
28+
29+
// @BeforeEach
30+
void setUp() {
31+
var students = List.of(new StudentEntity(null,"Prasath",31,1l,1l),
32+
new StudentEntity(null,"Mani",62,1l,1l),
33+
new StudentEntity(null,"Nishanthi",30,1l,1l));
34+
35+
studentRepository
36+
.deleteAll()
37+
.thenMany(studentRepository.saveAll(students))
38+
.blockLast();
39+
}
40+
41+
@Test
42+
void getAllStudents() {
43+
webTestClient
44+
.get()
45+
.uri("/findAll")
46+
.exchange()
47+
.expectStatus()
48+
.is2xxSuccessful()
49+
.expectBodyList(Student.class)
50+
.consumeWith(l->{
51+
l.getResponseBody().forEach(s->{
52+
System.out.println("id : "+s.getId());
53+
});
54+
})
55+
.hasSize(4);
56+
}
57+
58+
@Test
59+
void getStudentById() {
60+
Long id = 1l;
61+
webTestClient
62+
.get()
63+
.uri("/get-by/{id}", id)
64+
.exchange()
65+
.expectStatus()
66+
.is2xxSuccessful()
67+
.expectBody(Student.class)
68+
.consumeWith(result -> {
69+
var student = result.getResponseBody();
70+
assert student != null;
71+
assertEquals("aa", student.getName());
72+
});
73+
//.expectBody() .jsonPath("$.name").isEqualTo("Prasath");
74+
75+
}
76+
77+
@Test
78+
void updateStudent() {
79+
var id = 2l;
80+
var updatedStudent = new StudentEntity(2l,"Nishanthi M",30,2l,2l);
81+
82+
webTestClient
83+
.put()
84+
.uri("/update/{id}", id)
85+
.bodyValue(updatedStudent)
86+
.exchange()
87+
.expectStatus()
88+
.is2xxSuccessful()
89+
.expectBody(Student.class)
90+
.consumeWith(movieInfoEntityExchangeResult -> {
91+
var student = movieInfoEntityExchangeResult.getResponseBody();
92+
assert student != null;
93+
assertEquals("Nishanthi M", student.getName());
94+
});
95+
}
96+
97+
@Test
98+
void updateStudent_notFound() {
99+
var id = 5l;
100+
var updatedStudent = new StudentEntity(2l,"Nishanthi M",30,2l,2l);
101+
102+
webTestClient
103+
.put()
104+
.uri("/update/{id}", id)
105+
.bodyValue(updatedStudent)
106+
.exchange()
107+
.expectStatus()
108+
.isNotFound();
109+
}
110+
111+
@Test
112+
void deleteStudentById() {
113+
var id = 2l;
114+
115+
webTestClient
116+
.delete()
117+
.uri("/delete/{id}", id)
118+
.exchange()
119+
.expectStatus()
120+
.isNoContent();
121+
}
122+
123+
}

0 commit comments

Comments
 (0)