Hendrik Jungnitsch
2022-09-19 1226b4f51f9440c82dc80516b360ddd72c78a437
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
package de.gedoplan.seminar.sbt.sbtrestexercise;
 
import com.jayway.jsonpath.JsonPath;
import org.junit.jupiter.api.Test;
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.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
 
@SpringBootTest
@AutoConfigureMockMvc
public class Exercise02Test {
 
    @Autowired
    private MockMvc mockMvc;
 
    @Test
    public void testPut() throws Exception {
        mockMvc.perform(put("/personen/{id}",1)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(MUSTERMANN_JSON))
                .andExpect(status().is(HttpStatus.NO_CONTENT.value()));
        mockMvc.perform(get("/personen/{id}",1))
                .andExpect(jsonPath("$.firstname").value("Maximilian"));
    }
 
    @Test
    public void testPutNonMatchingId() throws Exception {
        mockMvc.perform(put("/personen/{id}",2)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(MUSTERMANN_JSON))
                .andExpect(status().is(HttpStatus.BAD_REQUEST.value()));
    }
 
    @Test
    public void testPutNotFound() throws Exception {
        mockMvc.perform(put("/personen/{id}",10)
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(MUSTERMANN_ID10_JSON))
                .andExpect(status().is(HttpStatus.NOT_FOUND.value()));
    }
 
    @Test
    public void testDelete() throws Exception {
        String responseBody = mockMvc.perform(get("/personen"))
                .andExpect(status().is(HttpStatus.OK.value())).andReturn().getResponse().getContentAsString();
        Integer count = JsonPath.read(responseBody,"$.size()");
 
        mockMvc.perform(delete("/personen/{id}",2))
                .andExpect(status().is(HttpStatus.OK.value()));
        mockMvc.perform(get("/personen"))
                .andExpect(jsonPath("$.size()").value(count-1));
    }
 
    @Test
    public void testPost() throws Exception {
        String location = mockMvc.perform(post("/personen")
                        .contentType(MediaType.APPLICATION_JSON).content(
                                """
                                        {
                                          "name": "Test",
                                          "firstname": "Test"
                                        }"""))
                .andExpect(status().is(HttpStatus.CREATED.value()))
                .andReturn().getResponse().getHeader("location");
 
        mockMvc.perform(get(location))
                .andExpect(jsonPath("$.name").value("Test"));
    }
 
    private static String MUSTERMANN_JSON =
            """
            {
              "id": "1",
              "name": "Mustermann",
              "firstname": "Maximilian"
            }
            """;
 
    private static String MUSTERMANN_ID10_JSON =
            """
            {
              "id": "10",
              "name": "Mustermann",
              "firstname": "Maximilian"
            }
            """;
}