/
LanguageApiControllerTest.java
172 lines (144 loc) · 5.83 KB
/
LanguageApiControllerTest.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
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
/*
* SpringMockMvcApiControllerTest.java
*
* Created on 2018-05-15, 7:39
*/
package com.marcnuri.demo.springmockmvc.language;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.marcnuri.demo.springmockmvc.SpringMockMvcException;
import com.marcnuri.demo.springmockmvc.language.LanguageApiControllerTest.Config;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.MimeTypeUtils;
/**
* Created by Marc Nuri <marc@marcnuri.com> on 2018-05-15.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {
LanguageApiController.class
})
@Import(Config.class)
public class LanguageApiControllerTest {
//**************************************************************************************************
// Fields
//**************************************************************************************************
@Autowired
private LanguageService languageService;
@Autowired
private LanguageApiController languageApiController;
private MockMvc mockMvc;
//**************************************************************************************************
// Test preparation
//**************************************************************************************************
@Before
public void setUp() {
mockMvc = MockMvcBuilders
.standaloneSetup(languageApiController)
.build();
}
@After
public void tearDown() {
mockMvc = null;
}
//**************************************************************************************************
// Tests
//**************************************************************************************************
@Test
public void getLanguages_null_shouldReturnOk() throws Exception {
// Given
final Language mockedEsoteric = new Language("Arnoldc", "Lauri Hartikka");
final List<Language> mockedLanguages = Stream.concat(
LanguageRepository.LANGUAGES.stream(),
Stream.of(mockedEsoteric)).collect(Collectors.toList());
doReturn(mockedLanguages).when(languageService).getLanguages(null);
// When
final ResultActions result = mockMvc.perform(
get("/api/languages")
.accept(MimeTypeUtils.APPLICATION_JSON_VALUE));
// Then
result.andExpect(status().isOk());
result.andExpect(jsonPath("$.length()").value(mockedLanguages.size()));
result.andExpect(jsonPath("$[?(@.name === 'Arnoldc')]").exists());
}
@Test
public void getLanguages_invalidInjectParam_shouldReturnBadRequest() throws Exception {
// Given
final String containsParam = "' or 1=1;/*";
final HttpStatus mockedStatus = HttpStatus.BAD_REQUEST;
doThrow(new SpringMockMvcException(mockedStatus, "Welcome to the new century"))
.when(languageService).getLanguages(containsParam);
// When
final ResultActions result = mockMvc.perform(
get("/api/languages").param("contains", containsParam)
.accept(MimeTypeUtils.APPLICATION_JSON_VALUE));
// Then
result.andExpect(status().is(mockedStatus.value()));
}
@Test
public void getLanguages_invalidAcceptHeader_shouldReturnNotAcceptable() throws Exception {
// Given
final String invalidAcceptMimeType = MimeTypeUtils.APPLICATION_XML_VALUE;
doReturn(LanguageRepository.LANGUAGES).when(languageService).getLanguages(null);
// When
final ResultActions result = mockMvc.perform(
get("/api/languages")
.accept(invalidAcceptMimeType));
// Then
result.andExpect(status().isNotAcceptable());
}
@Test
public void getLanguage_existingLanguage_shouldReturnOk() throws Exception {
// Given
final String mockedLanguageName = "Arnoldc";
final Language mockedEsoteric = new Language(mockedLanguageName, "Lauri Hartikka");
doReturn(Optional.of(mockedEsoteric)).when(languageService).getLanguage(mockedLanguageName);
// When
final ResultActions result = mockMvc.perform(
get("/api/languages/".concat(mockedLanguageName))
.accept(MimeTypeUtils.APPLICATION_JSON_VALUE));
// Then
result.andExpect(status().isOk());
result.andExpect(jsonPath("$.name", is(mockedLanguageName)));
}
@Test
public void getLanguage_nonExistingLanguage_shouldReturnNotFound() throws Exception {
//
final String mockedLanguageName = "Arnoldc";
doReturn(Optional.empty()).when(languageService).getLanguage(mockedLanguageName);
// When
final ResultActions result = mockMvc.perform(
get("/api/languages/".concat(mockedLanguageName))
.accept(MimeTypeUtils.APPLICATION_JSON_VALUE));
// Then
result.andExpect(status().isNotFound());
}
@TestConfiguration
protected static class Config {
@Bean
public LanguageService languageService() {
return Mockito.mock(LanguageService.class);
}
}
}