-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationControllerTest.java
More file actions
157 lines (134 loc) · 4.86 KB
/
ConfigurationControllerTest.java
File metadata and controls
157 lines (134 loc) · 4.86 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
package de.unistuttgart.bugfinder;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.unistuttgart.bugfinder.code.Code;
import de.unistuttgart.bugfinder.code.CodeDTO;
import de.unistuttgart.bugfinder.code.word.Word;
import de.unistuttgart.bugfinder.code.word.WordDTO;
import de.unistuttgart.bugfinder.configuration.Configuration;
import de.unistuttgart.bugfinder.configuration.ConfigurationDTO;
import de.unistuttgart.bugfinder.configuration.ConfigurationMapper;
import de.unistuttgart.bugfinder.configuration.ConfigurationRepository;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
@AutoConfigureMockMvc
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class ConfigurationControllerTest {
private final String API_URL = "/configurations";
@Autowired
private MockMvc mvc;
@Autowired
private ConfigurationMapper configurationMapper;
@Autowired
private ConfigurationRepository configurationRepository;
private ObjectMapper objectMapper;
private Configuration initialConfig;
private ConfigurationDTO initialConfigDTO;
@BeforeEach
public void createBasicData() {
final Code code1 = new Code();
code1.setWords(
Arrays.asList(
new Word("public"),
new Word(" "),
new Word("void"),
new Word(" "),
new Word("sayHello()"),
new Word(" "),
new Word("{"),
new Word("\n"),
new Word("}")
)
);
final Configuration configuration = new Configuration();
configuration.setCodes(Set.of(code1));
initialConfig = configurationRepository.save(configuration);
initialConfigDTO = configurationMapper.toDTO(initialConfig);
objectMapper = new ObjectMapper();
}
@BeforeAll
@AfterEach
public void deleteBasicData() {
configurationRepository.deleteAll();
}
@Test
void getConfigurations() throws Exception {
final MvcResult result = mvc
.perform(get(API_URL).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
final List<ConfigurationDTO> configurations = Arrays.asList(
objectMapper.readValue(result.getResponse().getContentAsString(), ConfigurationDTO[].class)
);
assertSame(1, configurations.size());
assertEquals(initialConfigDTO, configurations.get(0));
}
@Test
void getConfiguration() throws Exception {
final MvcResult result = mvc
.perform(get(API_URL + "/" + initialConfig.getId()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
final ConfigurationDTO configuration = objectMapper.readValue(
result.getResponse().getContentAsString(),
ConfigurationDTO.class
);
assertEquals(initialConfigDTO, configuration);
}
@Test
void getConfiguration_DoesNotExist_ThrowsNotFound() throws Exception {
mvc
.perform(get(API_URL + "/" + UUID.randomUUID()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound());
}
@Test
void createConfiguration() throws Exception {
CodeDTO newCode = new CodeDTO();
newCode.setWords(
Arrays.asList(
new WordDTO("public"),
new WordDTO(" "),
new WordDTO("void"),
new WordDTO(" "),
new WordDTO("sayGoodbye()"),
new WordDTO(" "),
new WordDTO("{"),
new WordDTO("\n"),
new WordDTO("}")
)
);
final ConfigurationDTO newConfiguration = new ConfigurationDTO();
newConfiguration.setCodes(List.of(newCode));
mvc
.perform(
post(API_URL).content(objectMapper.writeValueAsString(newConfiguration)).contentType(MediaType.APPLICATION_JSON)
)
.andExpect(status().isCreated());
assertSame(2, configurationRepository.findAll().size());
}
@Test
void deleteConfiguration() throws Exception {
final MvcResult result = mvc
.perform(delete(API_URL + "/" + initialConfig.getId()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
final ConfigurationDTO configuration = objectMapper.readValue(
result.getResponse().getContentAsString(),
ConfigurationDTO.class
);
assertEquals(initialConfigDTO, configuration);
assertTrue(configurationRepository.findById(initialConfig.getId()).isEmpty());
}
}