-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunServiceTest.java
72 lines (61 loc) · 2.55 KB
/
RunServiceTest.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
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.testcontainers.containers.MockServerContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockserver.model.HttpError.error;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.verify.VerificationTimes.exactly;
import static org.mockserver.verify.VerificationTimes.once;
@Testcontainers
public class RunServiceTest {
@Container
public static MockServerContainer mockServer = new MockServerContainer("5.9.0");
private static RunService runService;
private static MockServerClient mockServerClient;
@BeforeAll
public static void beforeAll() {
var externalService = ExternalService.create(mockServer.getEndpoint());
runService = new RunService(externalService);
mockServerClient = new MockServerClient(mockServer.getContainerIpAddress(), mockServer.getServerPort());
}
@AfterEach
public void afterEach() {
mockServerClient.reset();
}
@Test
@DisplayName("A run results in a successful request")
public void t1() {
mockServerClient.when(request()).respond(response().withStatusCode(200));
var isSuccess = runService.executeRun(1);
assertThat(isSuccess).isTrue();
mockServerClient.verify(request().withMethod("POST").withPath("/item"), once());
}
@Test
@DisplayName("Multiple runs result successful requests")
public void t2() {
mockServerClient.when(request()).respond(response().withStatusCode(200));
var isSuccess = runService.executeRun(5);
assertThat(isSuccess).isTrue();
mockServerClient.verify(request().withMethod("POST").withPath("/item"), exactly(5));
}
@Test
@DisplayName("A run fails on a server error")
public void t3() {
mockServerClient.when(request()).respond(response().withStatusCode(500));
var isSuccess = runService.executeRun(1);
assertThat(isSuccess).isFalse();
}
@Test
@DisplayName("A run fails on a connection error")
public void t4() {
mockServerClient.when(request()).error(error().withDropConnection(true));
var isSuccess = runService.executeRun(2);
assertThat(isSuccess).isFalse();
}
}