-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateShipTest.java
More file actions
134 lines (115 loc) · 5.44 KB
/
Copy pathCreateShipTest.java
File metadata and controls
134 lines (115 loc) · 5.44 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
package com.space.controller;
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.Before;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.ResultActions;
import static org.springframework.test.util.AssertionErrors.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
public class CreateShipTest extends AbstractTest {
private ObjectMapper mapper = new ObjectMapper();
private ShipInfoTest expected;
@Before
public void setup() {
super.setup();
expected = new ShipInfoTest(41L, "123456789", "Earth", ShipType.MILITARY, 32998274577071L, true, 0.8, 14, 6.4);
}
//test1
@Test
public void createShipEmptyBodyTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content("{}"))
.andExpect(status().isBadRequest());
}
//test2
@Test
public void createShipNoSpeedTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.NO_SPEED_JSON))
.andExpect(status().isBadRequest());
}
//test3
@Test
public void createShipEmptyNameTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.EMPTY_NAME_JSON))
.andExpect(status().isBadRequest());
}
//test4
@Test
public void createShipProdDateNegativeTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.NEGATIVE_PROD_DATE_JSON))
.andExpect(status().isBadRequest());
}
//test5
@Test
public void createShipCrewSizeTooBigTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.TOO_BIG_CREW_SIZE_JSON))
.andExpect(status().isBadRequest());
}
//test6
@Test
public void createShipPlanetLengthTooBigTest() throws Exception {
mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.TOO_BIG_PLANET_LENGTH_JSON))
.andExpect(status().isBadRequest());
}
//test7
@Test
public void createShipIsUsedAbsentTest() throws Exception {
expected.isUsed = false;
expected.rating = 12.8;
ResultActions resultActions = mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.NO_IS_USED_JSON))
.andExpect(status().isOk());
String contentAsString = resultActions.andReturn().getResponse().getContentAsString();
ShipInfoTest actual = mapper.readValue(contentAsString, ShipInfoTest.class);
assertEquals("Возвращается не правильный результат при запросе создания корабля без параметра isUsed.", expected, actual);
}
//test8
@Test
public void createShipIsUsedTrueTest() throws Exception {
ResultActions resultActions = mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.IS_USED_TRUE_JSON))
.andExpect(status().isOk());
String contentAsString = resultActions.andReturn().getResponse().getContentAsString();
ShipInfoTest actual = mapper.readValue(contentAsString, ShipInfoTest.class);
assertEquals("Возвращается не правильный результат при запросе создания корабля с параметром isUsed.", expected, actual);
}
//test9
@Test
public void createShipIsUsedFalseTest() throws Exception {
expected.isUsed = false;
expected.rating = 12.8;
ResultActions resultActions = mockMvc.perform(post("/rest/ships/")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.accept(MediaType.APPLICATION_JSON_UTF8)
.content(TestsHelper.IS_USED_FALSE_JSON))
.andExpect(status().isOk());
String contentAsString = resultActions.andReturn().getResponse().getContentAsString();
ShipInfoTest actual = mapper.readValue(contentAsString, ShipInfoTest.class);
assertEquals("Возвращается не правильный результат при запросе создания корабля с параметром isUsed.", expected, actual);
}
}