-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathBeerOrderTest.java
More file actions
103 lines (84 loc) · 3.21 KB
/
BeerOrderTest.java
File metadata and controls
103 lines (84 loc) · 3.21 KB
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
100
101
102
103
package com.example;
/**
* @author Olga Maciaszek-Sharma
*/
import java.math.BigDecimal;
import java.util.Arrays;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.contract.stubrunner.junit.StubRunnerExtension;
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.util.StringUtils;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* @author Olga Maciaszek-Sharma
*/
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@AutoConfigureMockMvc
@AutoConfigureJsonTesters
@DirtiesContext
public class BeerOrderTest extends AbstractTest {
@RegisterExtension
static StubRunnerExtension rule = new StubRunnerExtension()
.downloadStub("com.example", "beer-api-producer-xml")
.stubsMode(StubRunnerProperties.StubsMode.LOCAL);
@BeforeAll
public static void beforeClass() {
Assumptions.assumeTrue(atLeast210(), "Spring Cloud Contract must be in version at least 2.1.0");
Assumptions.assumeTrue(StringUtils.isEmpty(System.getenv("OLD_PRODUCER_TRAIN")),
"Env var OLD_PRODUCER_TRAIN must not be set");
}
@Autowired
MockMvc mockMvc;
@Autowired
BeerController beerController;
@BeforeEach
public void setupPort() {
this.beerController.port = rule.findStubUrl("beer-api-producer-xml").getPort();
}
@Test
@Disabled("TODO: Issues with RestAssured")
public void shouldProcessBeerOrder() throws Exception {
XmlMapper xmlMapper = new XmlMapper();
this.mockMvc.perform(MockMvcRequestBuilders.post("/order")
.contentType(MediaType.APPLICATION_XML)
.content(xmlMapper
.writeValueAsString(new BeerOrder(new BigDecimal("123"), Arrays
.asList("abc", "def", "ghi")))))
.andExpect(status().isOk());
}
@Test
@Disabled("TODO: Issues with RestAssured")
public void shouldCancelBeerOrder() throws Exception {
XmlMapper xmlMapper = new XmlMapper();
this.mockMvc.perform(MockMvcRequestBuilders.post("/cancelOrder")
.contentType(MediaType.APPLICATION_XML)
.content(xmlMapper
.writeValueAsString(new BeerOrder(new BigDecimal("123"), Arrays
.asList("abc", "def", "ghi")))))
.andExpect(status().isOk());
}
private static boolean atLeast210() {
try {
Class.forName("org.springframework.cloud.contract.verifier.util.ContractVerifierUtil");
}
catch (Exception ex) {
return false;
}
return true;
}
}