-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
ApiGatewayControllerTest.java
99 lines (85 loc) · 3.61 KB
/
ApiGatewayControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package org.springframework.samples.petclinic.api.boundary.web;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
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.cloud.circuitbreaker.resilience4j.ReactiveResilience4JAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.samples.petclinic.api.application.CustomersServiceClient;
import org.springframework.samples.petclinic.api.application.VisitsServiceClient;
import org.springframework.samples.petclinic.api.dto.OwnerDetails;
import org.springframework.samples.petclinic.api.dto.PetDetails;
import org.springframework.samples.petclinic.api.dto.VisitDetails;
import org.springframework.samples.petclinic.api.dto.Visits;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import java.net.ConnectException;
import java.util.Collections;
@ExtendWith(SpringExtension.class)
@WebFluxTest(controllers = ApiGatewayController.class)
@Import({ReactiveResilience4JAutoConfiguration.class, CircuitBreakerConfiguration.class})
class ApiGatewayControllerTest {
@MockBean
private CustomersServiceClient customersServiceClient;
@MockBean
private VisitsServiceClient visitsServiceClient;
@Autowired
private WebTestClient client;
@Test
void getOwnerDetails_withAvailableVisitsService() {
OwnerDetails owner = new OwnerDetails();
PetDetails cat = new PetDetails();
cat.setId(20);
cat.setName("Garfield");
owner.getPets().add(cat);
Mockito
.when(customersServiceClient.getOwner(1))
.thenReturn(Mono.just(owner));
Visits visits = new Visits();
VisitDetails visit = new VisitDetails();
visit.setId(300);
visit.setDescription("First visit");
visit.setPetId(cat.getId());
visits.getItems().add(visit);
Mockito
.when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.getId())))
.thenReturn(Mono.just(visits));
client.get()
.uri("/api/gateway/owners/1")
.exchange()
.expectStatus().isOk()
//.expectBody(String.class)
//.consumeWith(response ->
// Assertions.assertThat(response.getResponseBody()).isEqualTo("Garfield"));
.expectBody()
.jsonPath("$.pets[0].name").isEqualTo("Garfield")
.jsonPath("$.pets[0].visits[0].description").isEqualTo("First visit");
}
/**
* Test Resilience4j fallback method
*/
@Test
void getOwnerDetails_withServiceError() {
OwnerDetails owner = new OwnerDetails();
PetDetails cat = new PetDetails();
cat.setId(20);
cat.setName("Garfield");
owner.getPets().add(cat);
Mockito
.when(customersServiceClient.getOwner(1))
.thenReturn(Mono.just(owner));
Mockito
.when(visitsServiceClient.getVisitsForPets(Collections.singletonList(cat.getId())))
.thenReturn(Mono.error(new ConnectException("Simulate error")));
client.get()
.uri("/api/gateway/owners/1")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.pets[0].name").isEqualTo("Garfield")
.jsonPath("$.pets[0].visits").isEmpty();
}
}