|
1 | 1 | package exercise; |
2 | 2 |
|
3 | | -import org.junit.jupiter.api.Test; |
4 | | -import org.junit.jupiter.api.BeforeEach; |
5 | | -import org.junit.jupiter.api.TestMethodOrder; |
6 | | -import org.junit.jupiter.api.MethodOrderer; |
7 | | -import org.junit.jupiter.api.Order; |
8 | | - |
9 | | -import org.springframework.beans.factory.annotation.Autowired; |
10 | | -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
11 | | -import org.springframework.boot.test.context.SpringBootTest; |
12 | | -import org.springframework.test.web.servlet.MockMvc; |
13 | | -import org.springframework.http.MediaType; |
14 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; |
15 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; |
16 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
17 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
18 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
19 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; |
20 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; |
21 | | - |
22 | | -import java.nio.charset.StandardCharsets; |
23 | | -import java.util.ArrayList; |
24 | 3 | import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | +import java.util.stream.Collectors; |
| 6 | + |
| 7 | +import org.springframework.boot.SpringApplication; |
| 8 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 9 | +import org.springframework.http.HttpStatus; |
| 10 | +import org.springframework.http.ResponseEntity; |
| 11 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 12 | +import org.springframework.web.bind.annotation.GetMapping; |
| 13 | +import org.springframework.web.bind.annotation.PathVariable; |
| 14 | +import org.springframework.web.bind.annotation.PostMapping; |
| 15 | +import org.springframework.web.bind.annotation.PutMapping; |
| 16 | +import org.springframework.web.bind.annotation.RequestBody; |
| 17 | +import org.springframework.web.bind.annotation.RestController; |
| 18 | +import org.springframework.http.MediaType; |
| 19 | +import org.springframework.web.util.UriComponentsBuilder; |
25 | 20 |
|
26 | | -import org.springframework.web.context.WebApplicationContext; |
27 | | -import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
28 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
29 | 21 | import exercise.model.Post; |
| 22 | +import exercise.Data; |
30 | 23 |
|
31 | | -@SpringBootTest |
32 | | -@AutoConfigureMockMvc |
33 | | -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
34 | | -class ApplicationTest { |
35 | | - |
36 | | - @Autowired |
37 | | - private WebApplicationContext wac; |
| 24 | +@SpringBootApplication |
| 25 | +@RestController |
| 26 | +class Application { |
| 27 | + // Хранилище добавленных постов |
| 28 | + private List<Post> posts = Data.getPosts(); |
38 | 29 |
|
39 | | - @Autowired |
40 | | - private MockMvc mockMvc; |
41 | | - |
42 | | - @Autowired |
43 | | - private ObjectMapper om; |
44 | | - |
45 | | - @BeforeEach |
46 | | - public void setUp() throws Exception { |
47 | | - mockMvc = MockMvcBuilders.webAppContextSetup(wac) |
48 | | - .defaultResponseCharacterEncoding(StandardCharsets.UTF_8) |
49 | | - .build(); |
50 | | - Post testPost = new Post("test-post", "Test post", "Test body"); |
51 | | - Application.setPosts(new ArrayList<>(List.of(testPost))); |
| 30 | + public static void main(String[] args) { |
| 31 | + SpringApplication.run(Application.class, args); |
52 | 32 | } |
53 | 33 |
|
54 | | - @Order(1) |
55 | | - @Test |
56 | | - public void testShow() throws Exception { |
57 | | - mockMvc.perform(get("/posts/test-post")) |
58 | | - .andExpect(status().isOk()); |
| 34 | + // BEGIN |
| 35 | + @GetMapping("/posts") |
| 36 | + public ResponseEntity<List<Post>> getAllPosts() { |
| 37 | + int totalCount = posts.size(); |
| 38 | + return ResponseEntity.ok() |
| 39 | + .header("X-Total-Count", String.valueOf(totalCount)) |
| 40 | + .body(posts); |
59 | 41 | } |
60 | 42 |
|
61 | | - @Test |
62 | | - public void testCreatePost() throws Exception { |
63 | | - var post = new Post("another-post", "another post", "body"); |
64 | | - |
65 | | - var request = post("/posts") |
66 | | - .contentType(MediaType.APPLICATION_JSON) |
67 | | - .content(om.writeValueAsString(post)); |
68 | | - |
69 | | - mockMvc.perform(request) |
70 | | - .andExpect(status().isCreated()) |
71 | | - .andExpect(content().json(om.writeValueAsString(post))); |
| 43 | + @GetMapping("/posts/{id}") |
| 44 | + public ResponseEntity<Post> getPostById(@PathVariable String id) { |
| 45 | + Optional<Post> postOptional = posts.stream() |
| 46 | + .filter(post -> post.getId().equals(id)) |
| 47 | + .findFirst(); |
| 48 | + |
| 49 | + if (postOptional.isPresent()) { |
| 50 | + return ResponseEntity.ok(postOptional.get()); |
| 51 | + } else { |
| 52 | + return ResponseEntity.notFound().build(); |
| 53 | + } |
72 | 54 | } |
73 | 55 |
|
74 | | - @Test |
75 | | - public void testIndex() throws Exception { |
76 | | - mockMvc.perform(get("/posts")) |
77 | | - .andExpect(status().isOk()) |
78 | | - .andExpect(header().string("X-Total-Count", "1")); |
| 56 | + @PostMapping("/posts") |
| 57 | + public ResponseEntity<Post> createPost(@RequestBody Post post) { |
| 58 | + posts.add(post); |
| 59 | + return ResponseEntity.status(HttpStatus.CREATED) |
| 60 | + .location(UriComponentsBuilder.fromPath("/posts/{id}").buildAndExpand(post.getId()).toUri()) |
| 61 | + .body(post); |
79 | 62 | } |
80 | 63 |
|
81 | | - @Test |
82 | | - public void testShowUnknown() throws Exception { |
83 | | - mockMvc.perform(get("/posts/unknown-post")) |
84 | | - .andExpect(status().isNotFound()); |
| 64 | + @PutMapping("/posts/{id}") |
| 65 | + public ResponseEntity<Post> updatePost(@PathVariable String id, @RequestBody Post updatedPost) { |
| 66 | + for (int i = 0; i < posts.size(); i++) { |
| 67 | + if (posts.get(i).getId().equals(id)) { |
| 68 | + posts.set(i, updatedPost); |
| 69 | + return ResponseEntity.ok(updatedPost); |
| 70 | + } |
| 71 | + } |
| 72 | + return ResponseEntity.notFound().build(); |
85 | 73 | } |
86 | 74 |
|
87 | | - @Test |
88 | | - public void testUpdatePost() throws Exception { |
89 | | - var post = new Post("test-post", "new title", "new body"); |
90 | | - |
91 | | - var request = put("/posts/test-post") |
92 | | - .contentType(MediaType.APPLICATION_JSON) |
93 | | - .content(om.writeValueAsString(post)); |
94 | | - |
95 | | - mockMvc.perform(request) |
96 | | - .andExpect(status().isOk()) |
97 | | - .andExpect(content().json(om.writeValueAsString(post))); |
98 | | - |
99 | | - mockMvc.perform(get("/posts/test-post")) |
100 | | - .andExpect(status().isOk()) |
101 | | - .andExpect(content().json(om.writeValueAsString(post))); |
102 | | - } |
103 | | - |
104 | | - @Test |
105 | | - public void testUpdateUnknownPost() throws Exception { |
106 | | - var post = new Post("unknown-post", "new title", "new body"); |
107 | | - |
108 | | - var request = put("/posts/unknown-post") |
109 | | - .contentType(MediaType.APPLICATION_JSON) |
110 | | - .content(om.writeValueAsString(post)); |
111 | | - |
112 | | - mockMvc.perform(request) |
113 | | - .andExpect(status().isNotFound()); |
114 | | - } |
115 | | - |
116 | | - @Test |
117 | | - public void testDelete() throws Exception { |
118 | | - mockMvc.perform(delete("/posts/test-post")) |
119 | | - .andExpect(status().isOk()); |
120 | | - |
| 75 | + @DeleteMapping("/posts/{id}") |
| 76 | + public ResponseEntity<Void> deletePost(@PathVariable String id) { |
| 77 | + posts.removeIf(post -> post.getId().equals(id)); |
| 78 | + return ResponseEntity.ok().build(); |
121 | 79 | } |
| 80 | + // END |
122 | 81 | } |
0 commit comments