-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetAllTest.java
More file actions
220 lines (176 loc) · 11.2 KB
/
Copy pathGetAllTest.java
File metadata and controls
220 lines (176 loc) · 11.2 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.space.controller;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.space.controller.utils.ShipInfoTest;
import com.space.controller.utils.TestsHelper;
import com.space.model.ShipType;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import java.util.List;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class GetAllTest extends AbstractTest {
private TestsHelper testsHelper = new TestsHelper();
private ObjectMapper mapper = new ObjectMapper();
private TypeReference<List<ShipInfoTest>> typeReference = new TypeReference<List<ShipInfoTest>>() {
};
//test1
@Test
public void getAllWithoutFiltersReturnAllShips() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getAllShips());
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships.", expected, actual);
}
//test2
@Test
public void getAllWithFiltersNamePageNumber() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?name=ra&pageNumber=1")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(1, 3,
testsHelper.getShipInfosByName("ra",
testsHelper.getAllShips()));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами name и pageNumber.", expected, actual);
}
//test3
@Test
public void getAllWithFiltersPlanetPageSize() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?planet=ur&pageSize=4")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 4,
testsHelper.getShipInfosByPlanet("ur", testsHelper.getAllShips()));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами planet и pageSize.", expected, actual);
}
//test4
@Test
public void getAllWithFiltersShipTypeAfterBefore() throws Exception {
//after 00:00 01.01.3000
//before 00:00 01.01.3011
ResultActions resultActions = mockMvc.perform(get("/rest/ships?shipType=MILITARY&after=32503672800000&before=32850741600000")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByShipType(ShipType.MILITARY,
testsHelper.getShipInfosByAfter(32503672800000L,
testsHelper.getShipInfosByBefore(32850741600000L,
testsHelper.getAllShips()))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами shipType, after и before.", expected, actual);
}
//test5
@Test
public void getAllWithFiltersShipTypeMinSpeedMaxSpeed() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?shipType=TRANSPORT&minSpeed=0.3&maxSpeed=0.6")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByShipType(ShipType.TRANSPORT,
testsHelper.getShipInfosByMinSpeed(0.3,
testsHelper.getShipInfosByMaxSpeed(0.6,
testsHelper.getAllShips()))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами shipType, minSpeed и maxSpeed.", expected, actual);
}
//test6
@Test
public void getAllWithFiltersShipTypeMinCrewSizeMaxCrewSize() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?shipType=MERCHANT&minCrewSize=10&maxCrewSize=1000")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByShipType(ShipType.MERCHANT,
testsHelper.getShipInfosByMinCrewSize(10,
testsHelper.getShipInfosByMaxCrewSize(1000,
testsHelper.getAllShips()))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами shipType, minCrewSize и maxCrewSize.", expected, actual);
}
//test7
@Test
public void getAllWithFiltersIsUsedMinMaxRating() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?isUsed=true&minRating=2&maxRating=4")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByIsUsed(true,
testsHelper.getShipInfosByMinRating(2.,
testsHelper.getShipInfosByMaxRating(4.,
testsHelper.getAllShips()))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами isUsed, minRating и maxRating.", expected, actual);
}
//test8
@Test
public void getAllWithFiltersIsUsedMaxSpeedMaxRating() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?isUsed=false&maxSpeed=0.6&maxRating=7")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByIsUsed(false,
testsHelper.getShipInfosByMaxSpeed(0.6,
testsHelper.getShipInfosByMaxRating(7.,
testsHelper.getAllShips()))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами isUsed, maxSpeed и maxRating.", expected, actual);
}
//test9
@Test
public void getAllWithFiltersNameOrderSpeed() throws Exception {
ResultActions resultActions = mockMvc.perform(get("/rest/ships?name=ca&order=SPEED")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> ships = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(0, 3,
testsHelper.getShipInfosByOrder(ShipOrder.SPEED,
testsHelper.getShipInfosByName("ca",
testsHelper.getAllShips())));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами name и order.", expected, ships);
}
//test10
@Test
public void getAllWithFiltersAfterBeforeMinCrewMaxCrew() throws Exception {
//after 00:00 01.01.2996
//before 00:00 01.01.3009
ResultActions resultActions = mockMvc.perform(get("/rest/ships?after=32377442400000&before=32787669600000&minCrewSize=20&maxCrewSize=1500&pageNumber=1")
.accept(MediaType.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
MvcResult result = resultActions.andReturn();
String contentAsString = result.getResponse().getContentAsString();
List<ShipInfoTest> actual = mapper.readValue(contentAsString, typeReference);
List<ShipInfoTest> expected = testsHelper.getShipInfosByPage(1, 3,
testsHelper.getShipInfosByAfter(32377442400000L,
testsHelper.getShipInfosByBefore(32787669600000L,
testsHelper.getShipInfosByMinCrewSize(20,
testsHelper.getShipInfosByMaxCrewSize(1500,
testsHelper.getAllShips())))));
assertEquals("Возвращается не правильный результат при запросе GET /rest/ships с параметрами after, before, minCrewSize и maxCrewSize.", expected, actual);
}
}