spring-boot-2-template
440 строк · 16.8 Кб
1package by.andd3dfx.templateapp.controllers;2
3import static org.hamcrest.CoreMatchers.containsString;4import static org.hamcrest.CoreMatchers.is;5import static org.hamcrest.CoreMatchers.notNullValue;6import static org.hamcrest.MatcherAssert.assertThat;7import static org.hamcrest.Matchers.hasSize;8import static org.springframework.http.MediaType.APPLICATION_JSON;9import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;10import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;11import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;12import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;13import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;14import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;15import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;16
17import by.andd3dfx.templateapp.IntegrationTestInitializer;18import by.andd3dfx.templateapp.dto.ArticleDto;19import by.andd3dfx.templateapp.dto.ArticleUpdateDto;20import com.fasterxml.jackson.databind.ObjectMapper;21import java.io.IOException;22import java.time.LocalDateTime;23import org.junit.jupiter.api.BeforeEach;24import org.junit.jupiter.api.Test;25import org.springframework.beans.factory.annotation.Autowired;26import org.springframework.boot.test.context.SpringBootTest;27import org.springframework.mock.http.MockHttpOutputMessage;28import org.springframework.test.context.ContextConfiguration;29import org.springframework.test.context.web.WebAppConfiguration;30import org.springframework.test.web.servlet.MockMvc;31import org.springframework.web.context.WebApplicationContext;32
33@ContextConfiguration(initializers = IntegrationTestInitializer.class)34@SpringBootTest
35@WebAppConfiguration
36class ArticleControllerTest {37
38private MockMvc mockMvc;39
40@Autowired41private WebApplicationContext webApplicationContext;42@Autowired43private ObjectMapper objectMapper;44
45@BeforeEach46public void setup() {47mockMvc = webAppContextSetup(webApplicationContext)48.build();49}50
51@Test52public void createArticle() throws Exception {53ArticleDto articleDto = ArticleDto.builder()54.title("Some tittle value")55.summary("Some summary value")56.text("Some text")57.author("Some author")58.build();59
60mockMvc.perform(post("/api/v1/articles")61.contentType(APPLICATION_JSON)62.content(json(articleDto))63)64.andExpect(status().isCreated())65.andExpect(jsonPath("$.id", notNullValue()))66.andExpect(jsonPath("$.title", is(articleDto.getTitle())))67.andExpect(jsonPath("$.summary", is(articleDto.getSummary())))68.andExpect(jsonPath("$.text", is(articleDto.getText())))69.andExpect(jsonPath("$.author", is(articleDto.getAuthor())))70.andExpect(jsonPath("$.dateCreated", notNullValue()))71.andExpect(jsonPath("$.dateUpdated", notNullValue()));72}73
74@Test75public void createArticleWithIdPopulated() throws Exception {76ArticleDto articleDto = ArticleDto.builder()77.id(123L)78.title("Some tittle value")79.summary("Some summary value")80.text("Some text")81.author("Some author")82.build();83
84final String message = mockMvc.perform(post("/api/v1/articles")85.contentType(APPLICATION_JSON)86.content(json(articleDto))87)88.andExpect(status().isBadRequest())89.andReturn().getResolvedException().getMessage();90assertThat(message, containsString("Article id shouldn't be present"));91}92
93@Test94public void createArticleWithoutTitle() throws Exception {95ArticleDto articleDto = new ArticleDto();96articleDto.setSummary("Some summary value");97articleDto.setText("Some text");98articleDto.setAuthor("Some author");99
100final String message = mockMvc.perform(post("/api/v1/articles")101.contentType(APPLICATION_JSON)102.content(json(articleDto))103)104.andExpect(status().isBadRequest())105.andReturn().getResolvedException().getMessage();106assertThat(message, containsString("Title should be populated"));107}108
109@Test110public void createArticleWithEmptyTitle() throws Exception {111ArticleDto articleDto = new ArticleDto();112articleDto.setTitle("");113articleDto.setSummary("Some summary value");114articleDto.setText("Some text");115articleDto.setAuthor("Some author");116
117final String message = mockMvc.perform(post("/api/v1/articles")118.contentType(APPLICATION_JSON)119.content(json(articleDto))120)121.andExpect(status().isBadRequest())122.andReturn().getResolvedException().getMessage();123assertThat(message, containsString("Title length must be between 1 and 100"));124}125
126@Test127public void createArticleWithTooLongTitle() throws Exception {128ArticleDto articleDto = new ArticleDto();129articleDto.setTitle(createStringWithLength(101));130articleDto.setSummary("Some summary value");131articleDto.setText("Some text");132articleDto.setAuthor("Some author");133
134String message = mockMvc.perform(post("/api/v1/articles")135.contentType(APPLICATION_JSON)136.content(json(articleDto))137)138.andExpect(status().isBadRequest())139.andReturn().getResolvedException().getMessage();140assertThat(message, containsString("Title length must be between 1 and 100"));141}142
143@Test144public void createArticleWithTooLongSummary() throws Exception {145ArticleDto articleDto = new ArticleDto();146articleDto.setTitle("Some title");147articleDto.setSummary(createStringWithLength(260));148articleDto.setText("Some text");149articleDto.setAuthor("Some author");150
151String message = mockMvc.perform(post("/api/v1/articles")152.contentType(APPLICATION_JSON)153.content(json(articleDto))154)155.andExpect(status().isBadRequest())156.andReturn().getResolvedException().getMessage();157assertThat(message, containsString("Summary length shouldn't be greater than 255"));158}159
160@Test161public void createArticleWithoutText() throws Exception {162ArticleDto articleDto = ArticleDto.builder()163.title("Some title")164.summary("Some summary value")165.author("Some author")166.build();167
168String message = mockMvc.perform(post("/api/v1/articles")169.contentType(APPLICATION_JSON)170.content(json(articleDto))171)172.andExpect(status().isBadRequest())173.andReturn().getResolvedException().getMessage();174assertThat(message, containsString("Text should be populated"));175}176
177@Test178public void createArticleWithEmptyText() throws Exception {179ArticleDto articleDto = new ArticleDto();180articleDto.setTitle("Some title");181articleDto.setSummary("Some summary value");182articleDto.setText("");183articleDto.setAuthor("Some author");184
185String message = mockMvc.perform(post("/api/v1/articles")186.contentType(APPLICATION_JSON)187.content(json(articleDto))188)189.andExpect(status().isBadRequest())190.andReturn().getResolvedException().getMessage();191assertThat(message, containsString("Text length should be 1 at least"));192}193
194@Test195public void createArticleWithoutAuthor() throws Exception {196ArticleDto articleDto = new ArticleDto();197articleDto.setTitle("Some title");198articleDto.setSummary("Some summary value");199articleDto.setText("Some text");200
201String message = mockMvc.perform(post("/api/v1/articles")202.contentType(APPLICATION_JSON)203.content(json(articleDto))204)205.andExpect(status().isBadRequest())206.andReturn().getResolvedException().getMessage();207assertThat(message, containsString("Author should be populated"));208}209
210@Test211public void createArticleWithDateCreatedPopulated() throws Exception {212ArticleDto articleDto = new ArticleDto();213articleDto.setTitle("Some tittle value");214articleDto.setSummary("Some summary value");215articleDto.setText("Some text");216articleDto.setAuthor("Some author");217articleDto.setDateCreated(LocalDateTime.now());218
219String message = mockMvc.perform(post("/api/v1/articles")220.contentType(APPLICATION_JSON)221.content(json(articleDto))222)223.andExpect(status().isBadRequest())224.andReturn().getResolvedException().getMessage();225assertThat(message, containsString("DateCreated shouldn't be populated"));226}227
228@Test229public void createArticleWithDateUpdatedPopulated() throws Exception {230ArticleDto articleDto = new ArticleDto();231articleDto.setTitle("Some tittle value");232articleDto.setSummary("Some summary value");233articleDto.setText("Some text");234articleDto.setAuthor("Some author");235articleDto.setDateUpdated(LocalDateTime.now());236
237String message = mockMvc.perform(post("/api/v1/articles")238.contentType(APPLICATION_JSON)239.content(json(articleDto))240)241.andExpect(status().isBadRequest())242.andReturn().getResolvedException().getMessage();243assertThat(message, containsString("DateUpdated shouldn't be populated"));244}245
246@Test247public void deleteArticle() throws Exception {248mockMvc.perform(delete("/api/v1/articles/1")249.contentType(APPLICATION_JSON)250)251.andExpect(status().isNoContent());252}253
254@Test255public void deleteAbsentArticle() throws Exception {256mockMvc.perform(delete("/api/v1/articles/9999")257.contentType(APPLICATION_JSON))258.andExpect(status().isNotFound());259}260
261@Test262public void readArticle() throws Exception {263mockMvc.perform(get("/api/v1/articles/1")264.contentType(APPLICATION_JSON)265)266.andExpect(status().isOk())267.andExpect(jsonPath("$.id", is(1)));268}269
270@Test271public void readAbsentArticle() throws Exception {272mockMvc.perform(get("/api/v1/articles/345")273.contentType(APPLICATION_JSON))274.andExpect(status().isNotFound());275}276
277@Test278public void readArticles() throws Exception {279mockMvc.perform(get("/api/v1/articles")280.contentType(APPLICATION_JSON)281)282.andExpect(status().isOk())283.andExpect(jsonPath("$.content", hasSize(10)))284.andExpect(jsonPath("$.number", is(0)))285.andExpect(jsonPath("$.size", is(50)))286.andExpect(jsonPath("$.totalPages", is(1)))287.andExpect(jsonPath("$.totalElements", is(10)));288}289
290@Test291public void readArticlesWithPageSizeLimit() throws Exception {292mockMvc.perform(get("/api/v1/articles")293.param("size", "5")294.contentType(APPLICATION_JSON)295)296.andExpect(status().isOk())297.andExpect(jsonPath("$.content", hasSize(5)))298.andExpect(jsonPath("$.number", is(0)))299.andExpect(jsonPath("$.size", is(5)))300.andExpect(jsonPath("$.totalPages", is(2)))301.andExpect(jsonPath("$.totalElements", is(9)));302}303
304@Test305public void updateArticleTitle() throws Exception {306ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()307.title("Some tittle value")308.build();309
310mockMvc.perform(patch("/api/v1/articles/2")311.contentType(APPLICATION_JSON)312.content(json(articleUpdateDto))313)314.andExpect(status().isOk());315}316
317@Test318public void updateArticleSummary() throws Exception {319ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()320.summary("Some summary value")321.build();322
323mockMvc.perform(patch("/api/v1/articles/2")324.contentType(APPLICATION_JSON)325.content(json(articleUpdateDto))326)327.andExpect(status().isOk());328}329
330@Test331public void updateArticleText() throws Exception {332ArticleUpdateDto articleUpdateDto = new ArticleUpdateDto();333articleUpdateDto.setText("Some text value");334
335mockMvc.perform(patch("/api/v1/articles/2")336.contentType(APPLICATION_JSON)337.content(json(articleUpdateDto))338)339.andExpect(status().isOk());340}341
342@Test343public void updateArticleMultipleFields() throws Exception {344ArticleUpdateDto articleUpdateDto = new ArticleUpdateDto();345articleUpdateDto.setSummary("Some summary value");346articleUpdateDto.setText("Some text value");347
348String message = mockMvc.perform(patch("/api/v1/articles/2")349.contentType(APPLICATION_JSON)350.content(json(articleUpdateDto))351)352.andExpect(status().isBadRequest())353.andReturn().getResolvedException().getMessage();354assertThat(message, containsString("Only one field should be modified at once"));355}356
357@Test358public void updateAbsentArticle() throws Exception {359ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()360.title("q")361.build();362
363mockMvc.perform(patch("/api/v1/articles/123")364.contentType(APPLICATION_JSON)365.content(json(articleUpdateDto)))366.andExpect(status().isNotFound());367}368
369@Test370public void updateArticleWithEmptyTitle() throws Exception {371ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()372.title("")373.build();374
375String message = mockMvc.perform(patch("/api/v1/articles/2")376.contentType(APPLICATION_JSON)377.content(json(articleUpdateDto))378)379.andExpect(status().isBadRequest())380.andReturn().getResolvedException().getMessage();381assertThat(message, containsString("Title length must be between 1 and 100"));382}383
384@Test385public void updateArticleWithTooLongTitle() throws Exception {386ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()387.title(createStringWithLength(101))388.build();389
390String message = mockMvc.perform(patch("/api/v1/articles/2")391.contentType(APPLICATION_JSON)392.content(json(articleUpdateDto))393)394.andExpect(status().isBadRequest())395.andReturn().getResolvedException().getMessage();396assertThat(message, containsString("Title length must be between 1 and 100"));397}398
399@Test400public void updateArticleWithTooLongSummary() throws Exception {401ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()402.summary(createStringWithLength(260))403.build();404
405String message = mockMvc.perform(patch("/api/v1/articles/2")406.contentType(APPLICATION_JSON)407.content(json(articleUpdateDto))408)409.andExpect(status().isBadRequest())410.andReturn().getResolvedException().getMessage();411assertThat(message, containsString("Summary length shouldn't be greater than 255"));412}413
414@Test415public void updateArticleWithEmptyText() throws Exception {416ArticleUpdateDto articleUpdateDto = ArticleUpdateDto.builder()417.text("")418.build();419
420String message = mockMvc.perform(patch("/api/v1/articles/2")421.contentType(APPLICATION_JSON)422.content(json(articleUpdateDto))423)424.andExpect(status().isBadRequest())425.andReturn().getResolvedException().getMessage();426assertThat(message, containsString("Text length should be 1 at least"));427}428
429private String json(Object o) throws IOException {430return objectMapper.writeValueAsString(o);431}432
433private String createStringWithLength(int length) {434StringBuilder builder = new StringBuilder();435for (int index = 0; index < length; index++) {436builder.append("a");437}438return builder.toString();439}440}
441